1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51544Seschrock * Common Development and Distribution License (the "License"). 61544Seschrock * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 221544Seschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/zfs_context.h> 291544Seschrock #include <sys/fm/fs/zfs.h> 30789Sahrens #include <sys/spa.h> 31789Sahrens #include <sys/txg.h> 32789Sahrens #include <sys/spa_impl.h> 33789Sahrens #include <sys/vdev_impl.h> 34789Sahrens #include <sys/zio_impl.h> 35789Sahrens #include <sys/zio_compress.h> 36789Sahrens #include <sys/zio_checksum.h> 37789Sahrens 38789Sahrens /* 39789Sahrens * ========================================================================== 40789Sahrens * I/O priority table 41789Sahrens * ========================================================================== 42789Sahrens */ 43789Sahrens uint8_t zio_priority_table[ZIO_PRIORITY_TABLE_SIZE] = { 44789Sahrens 0, /* ZIO_PRIORITY_NOW */ 45789Sahrens 0, /* ZIO_PRIORITY_SYNC_READ */ 46789Sahrens 0, /* ZIO_PRIORITY_SYNC_WRITE */ 47789Sahrens 6, /* ZIO_PRIORITY_ASYNC_READ */ 48789Sahrens 4, /* ZIO_PRIORITY_ASYNC_WRITE */ 49789Sahrens 4, /* ZIO_PRIORITY_FREE */ 50789Sahrens 0, /* ZIO_PRIORITY_CACHE_FILL */ 51789Sahrens 0, /* ZIO_PRIORITY_LOG_WRITE */ 52789Sahrens 10, /* ZIO_PRIORITY_RESILVER */ 53789Sahrens 20, /* ZIO_PRIORITY_SCRUB */ 54789Sahrens }; 55789Sahrens 56789Sahrens /* 57789Sahrens * ========================================================================== 58789Sahrens * I/O type descriptions 59789Sahrens * ========================================================================== 60789Sahrens */ 61789Sahrens char *zio_type_name[ZIO_TYPES] = { 62789Sahrens "null", "read", "write", "free", "claim", "ioctl" }; 63789Sahrens 64789Sahrens /* At or above this size, force gang blocking - for testing */ 65789Sahrens uint64_t zio_gang_bang = SPA_MAXBLOCKSIZE + 1; 66789Sahrens 67789Sahrens typedef struct zio_sync_pass { 68789Sahrens int zp_defer_free; /* defer frees after this pass */ 69789Sahrens int zp_dontcompress; /* don't compress after this pass */ 70789Sahrens int zp_rewrite; /* rewrite new bps after this pass */ 71789Sahrens } zio_sync_pass_t; 72789Sahrens 73789Sahrens zio_sync_pass_t zio_sync_pass = { 74789Sahrens 1, /* zp_defer_free */ 75789Sahrens 4, /* zp_dontcompress */ 76789Sahrens 1, /* zp_rewrite */ 77789Sahrens }; 78789Sahrens 79789Sahrens /* 80789Sahrens * ========================================================================== 81789Sahrens * I/O kmem caches 82789Sahrens * ========================================================================== 83789Sahrens */ 84789Sahrens kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT]; 85789Sahrens 86789Sahrens void 87789Sahrens zio_init(void) 88789Sahrens { 89789Sahrens size_t c; 90789Sahrens 91789Sahrens /* 92789Sahrens * For small buffers, we want a cache for each multiple of 93789Sahrens * SPA_MINBLOCKSIZE. For medium-size buffers, we want a cache 94789Sahrens * for each quarter-power of 2. For large buffers, we want 95789Sahrens * a cache for each multiple of PAGESIZE. 96789Sahrens */ 97789Sahrens for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) { 98789Sahrens size_t size = (c + 1) << SPA_MINBLOCKSHIFT; 99789Sahrens size_t p2 = size; 100789Sahrens size_t align = 0; 101789Sahrens 102789Sahrens while (p2 & (p2 - 1)) 103789Sahrens p2 &= p2 - 1; 104789Sahrens 105789Sahrens if (size <= 4 * SPA_MINBLOCKSIZE) { 106789Sahrens align = SPA_MINBLOCKSIZE; 107789Sahrens } else if (P2PHASE(size, PAGESIZE) == 0) { 108789Sahrens align = PAGESIZE; 109789Sahrens } else if (P2PHASE(size, p2 >> 2) == 0) { 110789Sahrens align = p2 >> 2; 111789Sahrens } 112789Sahrens 113789Sahrens if (align != 0) { 114789Sahrens char name[30]; 115789Sahrens (void) sprintf(name, "zio_buf_%lu", size); 116789Sahrens zio_buf_cache[c] = kmem_cache_create(name, size, 117849Sbonwick align, NULL, NULL, NULL, NULL, NULL, KMC_NODEBUG); 118789Sahrens dprintf("creating cache for size %5lx align %5lx\n", 119789Sahrens size, align); 120789Sahrens } 121789Sahrens } 122789Sahrens 123789Sahrens while (--c != 0) { 124789Sahrens ASSERT(zio_buf_cache[c] != NULL); 125789Sahrens if (zio_buf_cache[c - 1] == NULL) 126789Sahrens zio_buf_cache[c - 1] = zio_buf_cache[c]; 127789Sahrens } 1281544Seschrock 1291544Seschrock zio_inject_init(); 130789Sahrens } 131789Sahrens 132789Sahrens void 133789Sahrens zio_fini(void) 134789Sahrens { 135789Sahrens size_t c; 136789Sahrens kmem_cache_t *last_cache = NULL; 137789Sahrens 138789Sahrens for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) { 139789Sahrens if (zio_buf_cache[c] != last_cache) { 140789Sahrens last_cache = zio_buf_cache[c]; 141789Sahrens kmem_cache_destroy(zio_buf_cache[c]); 142789Sahrens } 143789Sahrens zio_buf_cache[c] = NULL; 144789Sahrens } 1451544Seschrock 1461544Seschrock zio_inject_fini(); 147789Sahrens } 148789Sahrens 149789Sahrens /* 150789Sahrens * ========================================================================== 151789Sahrens * Allocate and free I/O buffers 152789Sahrens * ========================================================================== 153789Sahrens */ 154789Sahrens void * 155789Sahrens zio_buf_alloc(size_t size) 156789Sahrens { 157789Sahrens size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 158789Sahrens 159789Sahrens ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 160789Sahrens 161789Sahrens return (kmem_cache_alloc(zio_buf_cache[c], KM_SLEEP)); 162789Sahrens } 163789Sahrens 164789Sahrens void 165789Sahrens zio_buf_free(void *buf, size_t size) 166789Sahrens { 167789Sahrens size_t c = (size - 1) >> SPA_MINBLOCKSHIFT; 168789Sahrens 169789Sahrens ASSERT(c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT); 170789Sahrens 171789Sahrens kmem_cache_free(zio_buf_cache[c], buf); 172789Sahrens } 173789Sahrens 174789Sahrens /* 175789Sahrens * ========================================================================== 176789Sahrens * Push and pop I/O transform buffers 177789Sahrens * ========================================================================== 178789Sahrens */ 179789Sahrens static void 180789Sahrens zio_push_transform(zio_t *zio, void *data, uint64_t size, uint64_t bufsize) 181789Sahrens { 182789Sahrens zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP); 183789Sahrens 184789Sahrens zt->zt_data = data; 185789Sahrens zt->zt_size = size; 186789Sahrens zt->zt_bufsize = bufsize; 187789Sahrens 188789Sahrens zt->zt_next = zio->io_transform_stack; 189789Sahrens zio->io_transform_stack = zt; 190789Sahrens 191789Sahrens zio->io_data = data; 192789Sahrens zio->io_size = size; 193789Sahrens } 194789Sahrens 195789Sahrens static void 196789Sahrens zio_pop_transform(zio_t *zio, void **data, uint64_t *size, uint64_t *bufsize) 197789Sahrens { 198789Sahrens zio_transform_t *zt = zio->io_transform_stack; 199789Sahrens 200789Sahrens *data = zt->zt_data; 201789Sahrens *size = zt->zt_size; 202789Sahrens *bufsize = zt->zt_bufsize; 203789Sahrens 204789Sahrens zio->io_transform_stack = zt->zt_next; 205789Sahrens kmem_free(zt, sizeof (zio_transform_t)); 206789Sahrens 207789Sahrens if ((zt = zio->io_transform_stack) != NULL) { 208789Sahrens zio->io_data = zt->zt_data; 209789Sahrens zio->io_size = zt->zt_size; 210789Sahrens } 211789Sahrens } 212789Sahrens 213789Sahrens static void 214789Sahrens zio_clear_transform_stack(zio_t *zio) 215789Sahrens { 216789Sahrens void *data; 217789Sahrens uint64_t size, bufsize; 218789Sahrens 219789Sahrens ASSERT(zio->io_transform_stack != NULL); 220789Sahrens 221789Sahrens zio_pop_transform(zio, &data, &size, &bufsize); 222789Sahrens while (zio->io_transform_stack != NULL) { 223789Sahrens zio_buf_free(data, bufsize); 224789Sahrens zio_pop_transform(zio, &data, &size, &bufsize); 225789Sahrens } 226789Sahrens } 227789Sahrens 228789Sahrens /* 229789Sahrens * ========================================================================== 230789Sahrens * Create the various types of I/O (read, write, free) 231789Sahrens * ========================================================================== 232789Sahrens */ 233789Sahrens static zio_t * 234789Sahrens zio_create(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 235789Sahrens void *data, uint64_t size, zio_done_func_t *done, void *private, 236789Sahrens zio_type_t type, int priority, int flags, uint8_t stage, uint32_t pipeline) 237789Sahrens { 238789Sahrens zio_t *zio; 239789Sahrens 240789Sahrens ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 241789Sahrens ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0); 242789Sahrens 243789Sahrens zio = kmem_zalloc(sizeof (zio_t), KM_SLEEP); 244789Sahrens zio->io_parent = pio; 245789Sahrens zio->io_spa = spa; 246789Sahrens zio->io_txg = txg; 247789Sahrens if (bp != NULL) { 248789Sahrens zio->io_bp = bp; 249789Sahrens zio->io_bp_copy = *bp; 250789Sahrens zio->io_bp_orig = *bp; 251789Sahrens } 252789Sahrens zio->io_done = done; 253789Sahrens zio->io_private = private; 254789Sahrens zio->io_type = type; 255789Sahrens zio->io_priority = priority; 256789Sahrens zio->io_stage = stage; 257789Sahrens zio->io_pipeline = pipeline; 258789Sahrens zio->io_async_stages = ZIO_ASYNC_PIPELINE_STAGES; 259789Sahrens zio->io_timestamp = lbolt64; 260789Sahrens zio->io_flags = flags; 261789Sahrens zio_push_transform(zio, data, size, size); 262789Sahrens 263789Sahrens if (pio == NULL) { 264789Sahrens if (!(flags & ZIO_FLAG_CONFIG_HELD)) 2651544Seschrock spa_config_enter(zio->io_spa, RW_READER, zio); 266789Sahrens zio->io_root = zio; 267789Sahrens } else { 268789Sahrens zio->io_root = pio->io_root; 2691544Seschrock if (!(flags & ZIO_FLAG_NOBOOKMARK)) 2701544Seschrock zio->io_logical = pio->io_logical; 271789Sahrens mutex_enter(&pio->io_lock); 272789Sahrens if (stage < ZIO_STAGE_READY) 273789Sahrens pio->io_children_notready++; 274789Sahrens pio->io_children_notdone++; 275789Sahrens zio->io_sibling_next = pio->io_child; 276789Sahrens zio->io_sibling_prev = NULL; 277789Sahrens if (pio->io_child != NULL) 278789Sahrens pio->io_child->io_sibling_prev = zio; 279789Sahrens pio->io_child = zio; 2801775Sbillm zio->io_ndvas = pio->io_ndvas; 281789Sahrens mutex_exit(&pio->io_lock); 282789Sahrens } 283789Sahrens 284789Sahrens return (zio); 285789Sahrens } 286789Sahrens 287789Sahrens zio_t * 288789Sahrens zio_null(zio_t *pio, spa_t *spa, zio_done_func_t *done, void *private, 289789Sahrens int flags) 290789Sahrens { 291789Sahrens zio_t *zio; 292789Sahrens 293789Sahrens zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private, 294789Sahrens ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, ZIO_STAGE_OPEN, 295789Sahrens ZIO_WAIT_FOR_CHILDREN_PIPELINE); 296789Sahrens 297789Sahrens return (zio); 298789Sahrens } 299789Sahrens 300789Sahrens zio_t * 301789Sahrens zio_root(spa_t *spa, zio_done_func_t *done, void *private, int flags) 302789Sahrens { 303789Sahrens return (zio_null(NULL, spa, done, private, flags)); 304789Sahrens } 305789Sahrens 306789Sahrens zio_t * 307789Sahrens zio_read(zio_t *pio, spa_t *spa, blkptr_t *bp, void *data, 308789Sahrens uint64_t size, zio_done_func_t *done, void *private, 3091544Seschrock int priority, int flags, zbookmark_t *zb) 310789Sahrens { 311789Sahrens zio_t *zio; 312789Sahrens 313789Sahrens ASSERT3U(size, ==, BP_GET_LSIZE(bp)); 314789Sahrens 315789Sahrens zio = zio_create(pio, spa, bp->blk_birth, bp, data, size, done, private, 316789Sahrens ZIO_TYPE_READ, priority, flags, ZIO_STAGE_OPEN, ZIO_READ_PIPELINE); 3171544Seschrock zio->io_bookmark = *zb; 3181544Seschrock 3191544Seschrock zio->io_logical = zio; 320789Sahrens 321789Sahrens /* 322789Sahrens * Work off our copy of the bp so the caller can free it. 323789Sahrens */ 324789Sahrens zio->io_bp = &zio->io_bp_copy; 325789Sahrens 326789Sahrens if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) { 327789Sahrens uint64_t csize = BP_GET_PSIZE(bp); 328789Sahrens void *cbuf = zio_buf_alloc(csize); 329789Sahrens 330789Sahrens zio_push_transform(zio, cbuf, csize, csize); 331789Sahrens zio->io_pipeline |= 1U << ZIO_STAGE_READ_DECOMPRESS; 332789Sahrens } 333789Sahrens 3341775Sbillm if (BP_IS_GANG(bp)) { 335789Sahrens uint64_t gsize = SPA_GANGBLOCKSIZE; 336789Sahrens void *gbuf = zio_buf_alloc(gsize); 337789Sahrens 338789Sahrens zio_push_transform(zio, gbuf, gsize, gsize); 339789Sahrens zio->io_pipeline |= 1U << ZIO_STAGE_READ_GANG_MEMBERS; 340789Sahrens } 341789Sahrens 342789Sahrens return (zio); 343789Sahrens } 344789Sahrens 345789Sahrens zio_t * 3461775Sbillm zio_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies, 347789Sahrens uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 3481544Seschrock zio_done_func_t *done, void *private, int priority, int flags, 3491544Seschrock zbookmark_t *zb) 350789Sahrens { 351789Sahrens zio_t *zio; 352789Sahrens 353789Sahrens ASSERT(checksum >= ZIO_CHECKSUM_OFF && 354789Sahrens checksum < ZIO_CHECKSUM_FUNCTIONS); 355789Sahrens 356789Sahrens ASSERT(compress >= ZIO_COMPRESS_OFF && 357789Sahrens compress < ZIO_COMPRESS_FUNCTIONS); 358789Sahrens 359789Sahrens zio = zio_create(pio, spa, txg, bp, data, size, done, private, 360789Sahrens ZIO_TYPE_WRITE, priority, flags, 361789Sahrens ZIO_STAGE_OPEN, ZIO_WRITE_PIPELINE); 362789Sahrens 3631544Seschrock zio->io_bookmark = *zb; 3641544Seschrock 3651544Seschrock zio->io_logical = zio; 3661544Seschrock 367789Sahrens zio->io_checksum = checksum; 368789Sahrens zio->io_compress = compress; 3691775Sbillm zio->io_ndvas = ncopies; 370789Sahrens 371789Sahrens if (compress != ZIO_COMPRESS_OFF) 372789Sahrens zio->io_async_stages |= 1U << ZIO_STAGE_WRITE_COMPRESS; 373789Sahrens 374789Sahrens if (bp->blk_birth != txg) { 375789Sahrens /* XXX the bp usually (always?) gets re-zeroed later */ 376789Sahrens BP_ZERO(bp); 377789Sahrens BP_SET_LSIZE(bp, size); 378789Sahrens BP_SET_PSIZE(bp, size); 3791775Sbillm } else { 3801775Sbillm /* Make sure someone doesn't change their mind on overwrites */ 3811775Sbillm ASSERT(MIN(zio->io_ndvas + BP_IS_GANG(bp), 3821775Sbillm spa_max_replication(spa)) == BP_GET_NDVAS(bp)); 383789Sahrens } 384789Sahrens 385789Sahrens return (zio); 386789Sahrens } 387789Sahrens 388789Sahrens zio_t * 389789Sahrens zio_rewrite(zio_t *pio, spa_t *spa, int checksum, 390789Sahrens uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 3911544Seschrock zio_done_func_t *done, void *private, int priority, int flags, 3921544Seschrock zbookmark_t *zb) 393789Sahrens { 394789Sahrens zio_t *zio; 395789Sahrens 396789Sahrens zio = zio_create(pio, spa, txg, bp, data, size, done, private, 397789Sahrens ZIO_TYPE_WRITE, priority, flags, 398789Sahrens ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE); 399789Sahrens 4001544Seschrock zio->io_bookmark = *zb; 401789Sahrens zio->io_checksum = checksum; 402789Sahrens zio->io_compress = ZIO_COMPRESS_OFF; 403789Sahrens 4041775Sbillm if (pio != NULL) 4051775Sbillm ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp)); 4061775Sbillm 407789Sahrens return (zio); 408789Sahrens } 409789Sahrens 410789Sahrens static zio_t * 411789Sahrens zio_write_allocate(zio_t *pio, spa_t *spa, int checksum, 412789Sahrens uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 413789Sahrens zio_done_func_t *done, void *private, int priority, int flags) 414789Sahrens { 415789Sahrens zio_t *zio; 416789Sahrens 417789Sahrens BP_ZERO(bp); 418789Sahrens BP_SET_LSIZE(bp, size); 419789Sahrens BP_SET_PSIZE(bp, size); 420789Sahrens BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 421789Sahrens 422789Sahrens zio = zio_create(pio, spa, txg, bp, data, size, done, private, 423789Sahrens ZIO_TYPE_WRITE, priority, flags, 424789Sahrens ZIO_STAGE_OPEN, ZIO_WRITE_ALLOCATE_PIPELINE); 425789Sahrens 426789Sahrens zio->io_checksum = checksum; 427789Sahrens zio->io_compress = ZIO_COMPRESS_OFF; 428789Sahrens 429789Sahrens return (zio); 430789Sahrens } 431789Sahrens 432789Sahrens zio_t * 433789Sahrens zio_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 434789Sahrens zio_done_func_t *done, void *private) 435789Sahrens { 436789Sahrens zio_t *zio; 437789Sahrens 438789Sahrens ASSERT(!BP_IS_HOLE(bp)); 439789Sahrens 440789Sahrens if (txg == spa->spa_syncing_txg && 441789Sahrens spa->spa_sync_pass > zio_sync_pass.zp_defer_free) { 442789Sahrens bplist_enqueue_deferred(&spa->spa_sync_bplist, bp); 443789Sahrens return (zio_null(pio, spa, NULL, NULL, 0)); 444789Sahrens } 445789Sahrens 446789Sahrens zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private, 447789Sahrens ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, 0, 448789Sahrens ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE); 449789Sahrens 450789Sahrens zio->io_bp = &zio->io_bp_copy; 451789Sahrens 452789Sahrens return (zio); 453789Sahrens } 454789Sahrens 455789Sahrens zio_t * 456789Sahrens zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 457789Sahrens zio_done_func_t *done, void *private) 458789Sahrens { 459789Sahrens zio_t *zio; 460789Sahrens 461789Sahrens /* 462789Sahrens * A claim is an allocation of a specific block. Claims are needed 463789Sahrens * to support immediate writes in the intent log. The issue is that 464789Sahrens * immediate writes contain committed data, but in a txg that was 465789Sahrens * *not* committed. Upon opening the pool after an unclean shutdown, 466789Sahrens * the intent log claims all blocks that contain immediate write data 467789Sahrens * so that the SPA knows they're in use. 468789Sahrens * 469789Sahrens * All claims *must* be resolved in the first txg -- before the SPA 470789Sahrens * starts allocating blocks -- so that nothing is allocated twice. 471789Sahrens */ 472789Sahrens ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa)); 473789Sahrens ASSERT3U(spa_first_txg(spa), <=, txg); 474789Sahrens 475789Sahrens zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private, 476789Sahrens ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, 0, 477789Sahrens ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE); 478789Sahrens 479789Sahrens zio->io_bp = &zio->io_bp_copy; 480789Sahrens 481789Sahrens return (zio); 482789Sahrens } 483789Sahrens 484789Sahrens zio_t * 485789Sahrens zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd, 486789Sahrens zio_done_func_t *done, void *private, int priority, int flags) 487789Sahrens { 488789Sahrens zio_t *zio; 489789Sahrens int c; 490789Sahrens 491789Sahrens if (vd->vdev_children == 0) { 492789Sahrens zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private, 493789Sahrens ZIO_TYPE_IOCTL, priority, flags, 494789Sahrens ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE); 495789Sahrens 496789Sahrens zio->io_vd = vd; 497789Sahrens zio->io_cmd = cmd; 498789Sahrens } else { 499789Sahrens zio = zio_null(pio, spa, NULL, NULL, flags); 500789Sahrens 501789Sahrens for (c = 0; c < vd->vdev_children; c++) 502789Sahrens zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd, 503789Sahrens done, private, priority, flags)); 504789Sahrens } 505789Sahrens 506789Sahrens return (zio); 507789Sahrens } 508789Sahrens 509789Sahrens static void 510789Sahrens zio_phys_bp_init(vdev_t *vd, blkptr_t *bp, uint64_t offset, uint64_t size, 511789Sahrens int checksum) 512789Sahrens { 513789Sahrens ASSERT(vd->vdev_children == 0); 514789Sahrens 515789Sahrens ASSERT(size <= SPA_MAXBLOCKSIZE); 516789Sahrens ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0); 517789Sahrens ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0); 518789Sahrens 519789Sahrens ASSERT(offset + size <= VDEV_LABEL_START_SIZE || 520789Sahrens offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 521789Sahrens ASSERT3U(offset + size, <=, vd->vdev_psize); 522789Sahrens 523789Sahrens BP_ZERO(bp); 524789Sahrens 525789Sahrens BP_SET_LSIZE(bp, size); 526789Sahrens BP_SET_PSIZE(bp, size); 527789Sahrens 528789Sahrens BP_SET_CHECKSUM(bp, checksum); 529789Sahrens BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 530789Sahrens BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 531789Sahrens 532789Sahrens if (checksum != ZIO_CHECKSUM_OFF) 533789Sahrens ZIO_SET_CHECKSUM(&bp->blk_cksum, offset, 0, 0, 0); 534789Sahrens } 535789Sahrens 536789Sahrens zio_t * 537789Sahrens zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 538789Sahrens void *data, int checksum, zio_done_func_t *done, void *private, 539789Sahrens int priority, int flags) 540789Sahrens { 541789Sahrens zio_t *zio; 542789Sahrens blkptr_t blk; 543789Sahrens 544789Sahrens zio_phys_bp_init(vd, &blk, offset, size, checksum); 545789Sahrens 546789Sahrens zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private, 547789Sahrens ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, 548789Sahrens ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE); 549789Sahrens 550789Sahrens zio->io_vd = vd; 551789Sahrens zio->io_offset = offset; 552789Sahrens 553789Sahrens /* 554789Sahrens * Work off our copy of the bp so the caller can free it. 555789Sahrens */ 556789Sahrens zio->io_bp = &zio->io_bp_copy; 557789Sahrens 558789Sahrens return (zio); 559789Sahrens } 560789Sahrens 561789Sahrens zio_t * 562789Sahrens zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 563789Sahrens void *data, int checksum, zio_done_func_t *done, void *private, 564789Sahrens int priority, int flags) 565789Sahrens { 566789Sahrens zio_block_tail_t *zbt; 567789Sahrens void *wbuf; 568789Sahrens zio_t *zio; 569789Sahrens blkptr_t blk; 570789Sahrens 571789Sahrens zio_phys_bp_init(vd, &blk, offset, size, checksum); 572789Sahrens 573789Sahrens zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private, 574789Sahrens ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, 575789Sahrens ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE); 576789Sahrens 577789Sahrens zio->io_vd = vd; 578789Sahrens zio->io_offset = offset; 579789Sahrens 580789Sahrens zio->io_bp = &zio->io_bp_copy; 581789Sahrens zio->io_checksum = checksum; 582789Sahrens 583789Sahrens if (zio_checksum_table[checksum].ci_zbt) { 584789Sahrens /* 585789Sahrens * zbt checksums are necessarily destructive -- they modify 586789Sahrens * one word of the write buffer to hold the verifier/checksum. 587789Sahrens * Therefore, we must make a local copy in case the data is 588789Sahrens * being written to multiple places. 589789Sahrens */ 590789Sahrens wbuf = zio_buf_alloc(size); 591789Sahrens bcopy(data, wbuf, size); 592789Sahrens zio_push_transform(zio, wbuf, size, size); 593789Sahrens 594789Sahrens zbt = (zio_block_tail_t *)((char *)wbuf + size) - 1; 595789Sahrens zbt->zbt_cksum = blk.blk_cksum; 596789Sahrens } 597789Sahrens 598789Sahrens return (zio); 599789Sahrens } 600789Sahrens 601789Sahrens /* 602789Sahrens * Create a child I/O to do some work for us. It has no associated bp. 603789Sahrens */ 604789Sahrens zio_t * 605789Sahrens zio_vdev_child_io(zio_t *zio, blkptr_t *bp, vdev_t *vd, uint64_t offset, 606789Sahrens void *data, uint64_t size, int type, int priority, int flags, 607789Sahrens zio_done_func_t *done, void *private) 608789Sahrens { 609789Sahrens uint32_t pipeline = ZIO_VDEV_CHILD_PIPELINE; 610789Sahrens zio_t *cio; 611789Sahrens 612789Sahrens if (type == ZIO_TYPE_READ && bp != NULL) { 613789Sahrens /* 614789Sahrens * If we have the bp, then the child should perform the 615789Sahrens * checksum and the parent need not. This pushes error 616789Sahrens * detection as close to the leaves as possible and 617789Sahrens * eliminates redundant checksums in the interior nodes. 618789Sahrens */ 619789Sahrens pipeline |= 1U << ZIO_STAGE_CHECKSUM_VERIFY; 620789Sahrens zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY); 621789Sahrens } 622789Sahrens 623789Sahrens cio = zio_create(zio, zio->io_spa, zio->io_txg, bp, data, size, 624789Sahrens done, private, type, priority, 625789Sahrens (zio->io_flags & ZIO_FLAG_VDEV_INHERIT) | ZIO_FLAG_CANFAIL | flags, 6261775Sbillm ZIO_STAGE_VDEV_IO_START - 1, pipeline); 627789Sahrens 628789Sahrens cio->io_vd = vd; 629789Sahrens cio->io_offset = offset; 630789Sahrens 631789Sahrens return (cio); 632789Sahrens } 633789Sahrens 634789Sahrens /* 635789Sahrens * ========================================================================== 636789Sahrens * Initiate I/O, either sync or async 637789Sahrens * ========================================================================== 638789Sahrens */ 639789Sahrens int 640789Sahrens zio_wait(zio_t *zio) 641789Sahrens { 642789Sahrens int error; 643789Sahrens 644789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 645789Sahrens 646789Sahrens zio->io_waiter = curthread; 647789Sahrens 648789Sahrens zio_next_stage_async(zio); 649789Sahrens 650789Sahrens mutex_enter(&zio->io_lock); 651789Sahrens while (zio->io_stalled != ZIO_STAGE_DONE) 652789Sahrens cv_wait(&zio->io_cv, &zio->io_lock); 653789Sahrens mutex_exit(&zio->io_lock); 654789Sahrens 655789Sahrens error = zio->io_error; 656789Sahrens 657789Sahrens kmem_free(zio, sizeof (zio_t)); 658789Sahrens 659789Sahrens return (error); 660789Sahrens } 661789Sahrens 662789Sahrens void 663789Sahrens zio_nowait(zio_t *zio) 664789Sahrens { 665789Sahrens zio_next_stage_async(zio); 666789Sahrens } 667789Sahrens 668789Sahrens /* 669789Sahrens * ========================================================================== 670789Sahrens * I/O pipeline interlocks: parent/child dependency scoreboarding 671789Sahrens * ========================================================================== 672789Sahrens */ 673789Sahrens static void 674789Sahrens zio_wait_for_children(zio_t *zio, uint32_t stage, uint64_t *countp) 675789Sahrens { 676789Sahrens mutex_enter(&zio->io_lock); 677789Sahrens if (*countp == 0) { 678789Sahrens ASSERT(zio->io_stalled == 0); 679789Sahrens mutex_exit(&zio->io_lock); 680789Sahrens zio_next_stage(zio); 681789Sahrens } else { 682789Sahrens zio->io_stalled = stage; 683789Sahrens mutex_exit(&zio->io_lock); 684789Sahrens } 685789Sahrens } 686789Sahrens 687789Sahrens static void 688789Sahrens zio_notify_parent(zio_t *zio, uint32_t stage, uint64_t *countp) 689789Sahrens { 690789Sahrens zio_t *pio = zio->io_parent; 691789Sahrens 692789Sahrens mutex_enter(&pio->io_lock); 693789Sahrens if (pio->io_error == 0 && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 694789Sahrens pio->io_error = zio->io_error; 695789Sahrens if (--*countp == 0 && pio->io_stalled == stage) { 696789Sahrens pio->io_stalled = 0; 697789Sahrens mutex_exit(&pio->io_lock); 698789Sahrens zio_next_stage_async(pio); 699789Sahrens } else { 700789Sahrens mutex_exit(&pio->io_lock); 701789Sahrens } 702789Sahrens } 703789Sahrens 704789Sahrens static void 705789Sahrens zio_wait_children_ready(zio_t *zio) 706789Sahrens { 707789Sahrens zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_READY, 708789Sahrens &zio->io_children_notready); 709789Sahrens } 710789Sahrens 711789Sahrens void 712789Sahrens zio_wait_children_done(zio_t *zio) 713789Sahrens { 714789Sahrens zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_DONE, 715789Sahrens &zio->io_children_notdone); 716789Sahrens } 717789Sahrens 718789Sahrens static void 719789Sahrens zio_ready(zio_t *zio) 720789Sahrens { 721789Sahrens zio_t *pio = zio->io_parent; 722789Sahrens 723789Sahrens if (pio != NULL) 724789Sahrens zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_READY, 725789Sahrens &pio->io_children_notready); 726789Sahrens 727789Sahrens if (zio->io_bp) 728789Sahrens zio->io_bp_copy = *zio->io_bp; 729789Sahrens 730789Sahrens zio_next_stage(zio); 731789Sahrens } 732789Sahrens 733789Sahrens static void 734789Sahrens zio_done(zio_t *zio) 735789Sahrens { 736789Sahrens zio_t *pio = zio->io_parent; 737789Sahrens spa_t *spa = zio->io_spa; 738789Sahrens blkptr_t *bp = zio->io_bp; 739789Sahrens vdev_t *vd = zio->io_vd; 740896Smaybee char blkbuf[BP_SPRINTF_LEN]; 741789Sahrens 742789Sahrens ASSERT(zio->io_children_notready == 0); 743789Sahrens ASSERT(zio->io_children_notdone == 0); 744789Sahrens 745789Sahrens if (bp != NULL) { 746789Sahrens ASSERT(bp->blk_pad[0] == 0); 747789Sahrens ASSERT(bp->blk_pad[1] == 0); 748789Sahrens ASSERT(bp->blk_pad[2] == 0); 749789Sahrens ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0); 750789Sahrens if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) && 7511775Sbillm !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) { 752789Sahrens ASSERT(!BP_SHOULD_BYTESWAP(bp)); 7531775Sbillm if (zio->io_ndvas != 0) 7541775Sbillm ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp)); 7551775Sbillm ASSERT(BP_COUNT_GANG(bp) == 0 || 7561775Sbillm (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp))); 7571775Sbillm } 758789Sahrens } 759789Sahrens 760789Sahrens if (vd != NULL) 761789Sahrens vdev_stat_update(zio); 762789Sahrens 763789Sahrens if (zio->io_error) { 7641544Seschrock /* 7651544Seschrock * If this I/O is attached to a particular vdev, 7661544Seschrock * generate an error message describing the I/O failure 7671544Seschrock * at the block level. We ignore these errors if the 7681544Seschrock * device is currently unavailable. 7691544Seschrock */ 7701732Sbonwick if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd)) 7711544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_IO, 7721732Sbonwick zio->io_spa, vd, zio, 0, 0); 773789Sahrens 7741544Seschrock if ((zio->io_error == EIO || 7751544Seschrock !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) && 7761544Seschrock zio->io_logical == zio) { 7771544Seschrock /* 7781544Seschrock * For root I/O requests, tell the SPA to log the error 7791544Seschrock * appropriately. Also, generate a logical data 7801544Seschrock * ereport. 7811544Seschrock */ 7821544Seschrock spa_log_error(zio->io_spa, zio); 7831544Seschrock 7841544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_DATA, 7851544Seschrock zio->io_spa, NULL, zio, 0, 0); 7861544Seschrock } 787789Sahrens 7881544Seschrock /* 7891544Seschrock * For I/O requests that cannot fail, panic appropriately. 7901544Seschrock */ 7911544Seschrock if (!(zio->io_flags & ZIO_FLAG_CANFAIL)) { 7921544Seschrock sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, 7931544Seschrock bp ? bp : &zio->io_bp_copy); 7941544Seschrock panic("ZFS: %s (%s on %s off %llx: zio %p %s): error " 7951544Seschrock "%d", zio->io_error == ECKSUM ? 7961544Seschrock "bad checksum" : "I/O failure", 7971544Seschrock zio_type_name[zio->io_type], 7981544Seschrock vdev_description(vd), 7991544Seschrock (u_longlong_t)zio->io_offset, 8001544Seschrock zio, blkbuf, zio->io_error); 8011544Seschrock } 802789Sahrens } 803789Sahrens 804789Sahrens zio_clear_transform_stack(zio); 805789Sahrens 806789Sahrens if (zio->io_done) 807789Sahrens zio->io_done(zio); 808789Sahrens 809789Sahrens ASSERT(zio->io_delegate_list == NULL); 810789Sahrens ASSERT(zio->io_delegate_next == NULL); 811789Sahrens 812789Sahrens if (pio != NULL) { 813789Sahrens zio_t *next, *prev; 814789Sahrens 815789Sahrens mutex_enter(&pio->io_lock); 816789Sahrens next = zio->io_sibling_next; 817789Sahrens prev = zio->io_sibling_prev; 818789Sahrens if (next != NULL) 819789Sahrens next->io_sibling_prev = prev; 820789Sahrens if (prev != NULL) 821789Sahrens prev->io_sibling_next = next; 822789Sahrens if (pio->io_child == zio) 823789Sahrens pio->io_child = next; 824789Sahrens mutex_exit(&pio->io_lock); 825789Sahrens 826789Sahrens zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_DONE, 827789Sahrens &pio->io_children_notdone); 828789Sahrens } 829789Sahrens 830789Sahrens if (pio == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_HELD)) 8311544Seschrock spa_config_exit(spa, zio); 832789Sahrens 833789Sahrens if (zio->io_waiter != NULL) { 834789Sahrens mutex_enter(&zio->io_lock); 835789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_DONE); 836789Sahrens zio->io_stalled = zio->io_stage; 837789Sahrens cv_broadcast(&zio->io_cv); 838789Sahrens mutex_exit(&zio->io_lock); 839789Sahrens } else { 840789Sahrens kmem_free(zio, sizeof (zio_t)); 841789Sahrens } 842789Sahrens } 843789Sahrens 844789Sahrens /* 845789Sahrens * ========================================================================== 846789Sahrens * Compression support 847789Sahrens * ========================================================================== 848789Sahrens */ 849789Sahrens static void 850789Sahrens zio_write_compress(zio_t *zio) 851789Sahrens { 852789Sahrens int compress = zio->io_compress; 853789Sahrens blkptr_t *bp = zio->io_bp; 854789Sahrens void *cbuf; 855789Sahrens uint64_t lsize = zio->io_size; 856789Sahrens uint64_t csize = lsize; 857789Sahrens uint64_t cbufsize = 0; 858789Sahrens int pass; 859789Sahrens 860789Sahrens if (bp->blk_birth == zio->io_txg) { 861789Sahrens /* 862789Sahrens * We're rewriting an existing block, which means we're 863789Sahrens * working on behalf of spa_sync(). For spa_sync() to 864789Sahrens * converge, it must eventually be the case that we don't 865789Sahrens * have to allocate new blocks. But compression changes 866789Sahrens * the blocksize, which forces a reallocate, and makes 867789Sahrens * convergence take longer. Therefore, after the first 868789Sahrens * few passes, stop compressing to ensure convergence. 869789Sahrens */ 870789Sahrens pass = spa_sync_pass(zio->io_spa); 871789Sahrens if (pass > zio_sync_pass.zp_dontcompress) 872789Sahrens compress = ZIO_COMPRESS_OFF; 873789Sahrens } else { 874789Sahrens ASSERT(BP_IS_HOLE(bp)); 875789Sahrens pass = 1; 876789Sahrens } 877789Sahrens 878789Sahrens if (compress != ZIO_COMPRESS_OFF) 879789Sahrens if (!zio_compress_data(compress, zio->io_data, zio->io_size, 880789Sahrens &cbuf, &csize, &cbufsize)) 881789Sahrens compress = ZIO_COMPRESS_OFF; 882789Sahrens 883789Sahrens if (compress != ZIO_COMPRESS_OFF && csize != 0) 884789Sahrens zio_push_transform(zio, cbuf, csize, cbufsize); 885789Sahrens 886789Sahrens /* 887789Sahrens * The final pass of spa_sync() must be all rewrites, but the first 888789Sahrens * few passes offer a trade-off: allocating blocks defers convergence, 889789Sahrens * but newly allocated blocks are sequential, so they can be written 890789Sahrens * to disk faster. Therefore, we allow the first few passes of 891789Sahrens * spa_sync() to reallocate new blocks, but force rewrites after that. 892789Sahrens * There should only be a handful of blocks after pass 1 in any case. 893789Sahrens */ 894789Sahrens if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == csize && 895789Sahrens pass > zio_sync_pass.zp_rewrite) { 896789Sahrens ASSERT(csize != 0); 897789Sahrens ASSERT3U(BP_GET_COMPRESS(bp), ==, compress); 898789Sahrens ASSERT3U(BP_GET_LSIZE(bp), ==, lsize); 899789Sahrens 900789Sahrens zio->io_pipeline = ZIO_REWRITE_PIPELINE; 901789Sahrens } else { 902789Sahrens if (bp->blk_birth == zio->io_txg) { 903789Sahrens ASSERT3U(BP_GET_LSIZE(bp), ==, lsize); 904789Sahrens bzero(bp, sizeof (blkptr_t)); 905789Sahrens } 906789Sahrens if (csize == 0) { 907789Sahrens BP_ZERO(bp); 908789Sahrens zio->io_pipeline = ZIO_WAIT_FOR_CHILDREN_PIPELINE; 909789Sahrens } else { 9101775Sbillm ASSERT3U(BP_GET_NDVAS(bp), ==, 0); 911789Sahrens BP_SET_LSIZE(bp, lsize); 912789Sahrens BP_SET_PSIZE(bp, csize); 913789Sahrens BP_SET_COMPRESS(bp, compress); 914789Sahrens zio->io_pipeline = ZIO_WRITE_ALLOCATE_PIPELINE; 915789Sahrens } 916789Sahrens } 917789Sahrens 918789Sahrens zio_next_stage(zio); 919789Sahrens } 920789Sahrens 921789Sahrens static void 922789Sahrens zio_read_decompress(zio_t *zio) 923789Sahrens { 924789Sahrens blkptr_t *bp = zio->io_bp; 925789Sahrens void *data; 926789Sahrens uint64_t size; 927789Sahrens uint64_t bufsize; 928789Sahrens int compress = BP_GET_COMPRESS(bp); 929789Sahrens 930789Sahrens ASSERT(compress != ZIO_COMPRESS_OFF); 931789Sahrens 932789Sahrens zio_pop_transform(zio, &data, &size, &bufsize); 933789Sahrens 934789Sahrens if (zio_decompress_data(compress, data, size, 935789Sahrens zio->io_data, zio->io_size)) 936789Sahrens zio->io_error = EIO; 937789Sahrens 938789Sahrens zio_buf_free(data, bufsize); 939789Sahrens 940789Sahrens zio_next_stage(zio); 941789Sahrens } 942789Sahrens 943789Sahrens /* 944789Sahrens * ========================================================================== 945789Sahrens * Gang block support 946789Sahrens * ========================================================================== 947789Sahrens */ 948789Sahrens static void 949789Sahrens zio_gang_pipeline(zio_t *zio) 950789Sahrens { 951789Sahrens /* 952789Sahrens * By default, the pipeline assumes that we're dealing with a gang 953789Sahrens * block. If we're not, strip out any gang-specific stages. 954789Sahrens */ 9551775Sbillm if (!BP_IS_GANG(zio->io_bp)) 956789Sahrens zio->io_pipeline &= ~ZIO_GANG_STAGES; 957789Sahrens 958789Sahrens zio_next_stage(zio); 959789Sahrens } 960789Sahrens 961789Sahrens static void 962789Sahrens zio_gang_byteswap(zio_t *zio) 963789Sahrens { 964789Sahrens ASSERT(zio->io_size == SPA_GANGBLOCKSIZE); 965789Sahrens 966789Sahrens if (BP_SHOULD_BYTESWAP(zio->io_bp)) 967789Sahrens byteswap_uint64_array(zio->io_data, zio->io_size); 968789Sahrens } 969789Sahrens 970789Sahrens static void 971789Sahrens zio_get_gang_header(zio_t *zio) 972789Sahrens { 973789Sahrens blkptr_t *bp = zio->io_bp; 974789Sahrens uint64_t gsize = SPA_GANGBLOCKSIZE; 975789Sahrens void *gbuf = zio_buf_alloc(gsize); 976789Sahrens 9771775Sbillm ASSERT(BP_IS_GANG(bp)); 978789Sahrens 979789Sahrens zio_push_transform(zio, gbuf, gsize, gsize); 980789Sahrens 981789Sahrens zio_nowait(zio_create(zio, zio->io_spa, bp->blk_birth, bp, gbuf, gsize, 982789Sahrens NULL, NULL, ZIO_TYPE_READ, zio->io_priority, 983789Sahrens zio->io_flags & ZIO_FLAG_GANG_INHERIT, 984789Sahrens ZIO_STAGE_OPEN, ZIO_READ_PIPELINE)); 985789Sahrens 986789Sahrens zio_wait_children_done(zio); 987789Sahrens } 988789Sahrens 989789Sahrens static void 990789Sahrens zio_read_gang_members(zio_t *zio) 991789Sahrens { 992789Sahrens zio_gbh_phys_t *gbh; 993789Sahrens uint64_t gsize, gbufsize, loff, lsize; 994789Sahrens int i; 995789Sahrens 9961775Sbillm ASSERT(BP_IS_GANG(zio->io_bp)); 997789Sahrens 998789Sahrens zio_gang_byteswap(zio); 999789Sahrens zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1000789Sahrens 1001789Sahrens for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) { 1002789Sahrens blkptr_t *gbp = &gbh->zg_blkptr[i]; 1003789Sahrens lsize = BP_GET_PSIZE(gbp); 1004789Sahrens 1005789Sahrens ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF); 1006789Sahrens ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp)); 1007789Sahrens ASSERT3U(loff + lsize, <=, zio->io_size); 1008789Sahrens ASSERT(i < SPA_GBH_NBLKPTRS); 1009789Sahrens ASSERT(!BP_IS_HOLE(gbp)); 1010789Sahrens 1011789Sahrens zio_nowait(zio_read(zio, zio->io_spa, gbp, 1012789Sahrens (char *)zio->io_data + loff, lsize, NULL, NULL, 10131544Seschrock zio->io_priority, zio->io_flags & ZIO_FLAG_GANG_INHERIT, 10141544Seschrock &zio->io_bookmark)); 1015789Sahrens } 1016789Sahrens 1017789Sahrens zio_buf_free(gbh, gbufsize); 1018789Sahrens zio_wait_children_done(zio); 1019789Sahrens } 1020789Sahrens 1021789Sahrens static void 1022789Sahrens zio_rewrite_gang_members(zio_t *zio) 1023789Sahrens { 1024789Sahrens zio_gbh_phys_t *gbh; 1025789Sahrens uint64_t gsize, gbufsize, loff, lsize; 1026789Sahrens int i; 1027789Sahrens 10281775Sbillm ASSERT(BP_IS_GANG(zio->io_bp)); 1029789Sahrens ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE); 1030789Sahrens 1031789Sahrens zio_gang_byteswap(zio); 1032789Sahrens zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1033789Sahrens 1034789Sahrens ASSERT(gsize == gbufsize); 1035789Sahrens 1036789Sahrens for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) { 1037789Sahrens blkptr_t *gbp = &gbh->zg_blkptr[i]; 1038789Sahrens lsize = BP_GET_PSIZE(gbp); 1039789Sahrens 1040789Sahrens ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF); 1041789Sahrens ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp)); 1042789Sahrens ASSERT3U(loff + lsize, <=, zio->io_size); 1043789Sahrens ASSERT(i < SPA_GBH_NBLKPTRS); 1044789Sahrens ASSERT(!BP_IS_HOLE(gbp)); 1045789Sahrens 1046789Sahrens zio_nowait(zio_rewrite(zio, zio->io_spa, zio->io_checksum, 1047789Sahrens zio->io_txg, gbp, (char *)zio->io_data + loff, lsize, 10481544Seschrock NULL, NULL, zio->io_priority, zio->io_flags, 10491544Seschrock &zio->io_bookmark)); 1050789Sahrens } 1051789Sahrens 1052789Sahrens zio_push_transform(zio, gbh, gsize, gbufsize); 1053789Sahrens zio_wait_children_ready(zio); 1054789Sahrens } 1055789Sahrens 1056789Sahrens static void 1057789Sahrens zio_free_gang_members(zio_t *zio) 1058789Sahrens { 1059789Sahrens zio_gbh_phys_t *gbh; 1060789Sahrens uint64_t gsize, gbufsize; 1061789Sahrens int i; 1062789Sahrens 10631775Sbillm ASSERT(BP_IS_GANG(zio->io_bp)); 1064789Sahrens 1065789Sahrens zio_gang_byteswap(zio); 1066789Sahrens zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1067789Sahrens 1068789Sahrens for (i = 0; i < SPA_GBH_NBLKPTRS; i++) { 1069789Sahrens blkptr_t *gbp = &gbh->zg_blkptr[i]; 1070789Sahrens 1071789Sahrens if (BP_IS_HOLE(gbp)) 1072789Sahrens continue; 1073789Sahrens zio_nowait(zio_free(zio, zio->io_spa, zio->io_txg, 1074789Sahrens gbp, NULL, NULL)); 1075789Sahrens } 1076789Sahrens 1077789Sahrens zio_buf_free(gbh, gbufsize); 1078789Sahrens zio_next_stage(zio); 1079789Sahrens } 1080789Sahrens 1081789Sahrens static void 1082789Sahrens zio_claim_gang_members(zio_t *zio) 1083789Sahrens { 1084789Sahrens zio_gbh_phys_t *gbh; 1085789Sahrens uint64_t gsize, gbufsize; 1086789Sahrens int i; 1087789Sahrens 10881775Sbillm ASSERT(BP_IS_GANG(zio->io_bp)); 1089789Sahrens 1090789Sahrens zio_gang_byteswap(zio); 1091789Sahrens zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1092789Sahrens 1093789Sahrens for (i = 0; i < SPA_GBH_NBLKPTRS; i++) { 1094789Sahrens blkptr_t *gbp = &gbh->zg_blkptr[i]; 1095789Sahrens if (BP_IS_HOLE(gbp)) 1096789Sahrens continue; 1097789Sahrens zio_nowait(zio_claim(zio, zio->io_spa, zio->io_txg, 1098789Sahrens gbp, NULL, NULL)); 1099789Sahrens } 1100789Sahrens 1101789Sahrens zio_buf_free(gbh, gbufsize); 1102789Sahrens zio_next_stage(zio); 1103789Sahrens } 1104789Sahrens 1105789Sahrens static void 1106789Sahrens zio_write_allocate_gang_member_done(zio_t *zio) 1107789Sahrens { 1108789Sahrens zio_t *pio = zio->io_parent; 11091775Sbillm dva_t *cdva = zio->io_bp->blk_dva; 11101775Sbillm dva_t *pdva = pio->io_bp->blk_dva; 1111789Sahrens uint64_t asize; 11121775Sbillm int d; 1113789Sahrens 11141775Sbillm ASSERT3U(pio->io_ndvas, ==, zio->io_ndvas); 11151775Sbillm ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp)); 11161775Sbillm ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(zio->io_bp)); 11171775Sbillm ASSERT3U(pio->io_ndvas, <=, BP_GET_NDVAS(pio->io_bp)); 11181775Sbillm 1119789Sahrens mutex_enter(&pio->io_lock); 11201775Sbillm for (d = 0; d < BP_GET_NDVAS(pio->io_bp); d++) { 11211775Sbillm ASSERT(DVA_GET_GANG(&pdva[d])); 11221775Sbillm asize = DVA_GET_ASIZE(&pdva[d]); 11231775Sbillm asize += DVA_GET_ASIZE(&cdva[d]); 11241775Sbillm DVA_SET_ASIZE(&pdva[d], asize); 11251775Sbillm } 1126789Sahrens mutex_exit(&pio->io_lock); 1127789Sahrens } 1128789Sahrens 1129789Sahrens static void 1130789Sahrens zio_write_allocate_gang_members(zio_t *zio) 1131789Sahrens { 1132789Sahrens blkptr_t *bp = zio->io_bp; 11331775Sbillm dva_t *dva = bp->blk_dva; 11341775Sbillm spa_t *spa = zio->io_spa; 1135789Sahrens zio_gbh_phys_t *gbh; 11361775Sbillm uint64_t txg = zio->io_txg; 1137789Sahrens uint64_t resid = zio->io_size; 1138789Sahrens uint64_t maxalloc = P2ROUNDUP(zio->io_size >> 1, SPA_MINBLOCKSIZE); 1139789Sahrens uint64_t gsize, loff, lsize; 1140789Sahrens uint32_t gbps_left; 11411775Sbillm int ndvas = zio->io_ndvas; 11421775Sbillm int gbh_ndvas = MIN(ndvas + 1, spa_max_replication(spa)); 1143789Sahrens int error; 11441775Sbillm int i, d; 1145789Sahrens 1146789Sahrens gsize = SPA_GANGBLOCKSIZE; 1147789Sahrens gbps_left = SPA_GBH_NBLKPTRS; 1148789Sahrens 11491775Sbillm error = metaslab_alloc(spa, gsize, bp, gbh_ndvas, txg, NULL); 1150789Sahrens if (error == ENOSPC) 1151789Sahrens panic("can't allocate gang block header"); 1152789Sahrens ASSERT(error == 0); 1153789Sahrens 11541775Sbillm for (d = 0; d < gbh_ndvas; d++) 11551775Sbillm DVA_SET_GANG(&dva[d], 1); 1156789Sahrens 11571775Sbillm bp->blk_birth = txg; 1158789Sahrens 1159789Sahrens gbh = zio_buf_alloc(gsize); 1160789Sahrens bzero(gbh, gsize); 1161789Sahrens 11621775Sbillm /* We need to test multi-level gang blocks */ 11631775Sbillm if (maxalloc >= zio_gang_bang && (lbolt & 0x1) == 0) 11641775Sbillm maxalloc = MAX(maxalloc >> 2, SPA_MINBLOCKSIZE); 11651775Sbillm 1166789Sahrens for (loff = 0, i = 0; loff != zio->io_size; 1167789Sahrens loff += lsize, resid -= lsize, gbps_left--, i++) { 1168789Sahrens blkptr_t *gbp = &gbh->zg_blkptr[i]; 11691775Sbillm dva = gbp->blk_dva; 1170789Sahrens 1171789Sahrens ASSERT(gbps_left != 0); 1172789Sahrens maxalloc = MIN(maxalloc, resid); 1173789Sahrens 1174789Sahrens while (resid <= maxalloc * gbps_left) { 11751775Sbillm error = metaslab_alloc(spa, maxalloc, gbp, ndvas, 11761775Sbillm txg, bp); 1177789Sahrens if (error == 0) 1178789Sahrens break; 1179789Sahrens ASSERT3U(error, ==, ENOSPC); 1180789Sahrens if (maxalloc == SPA_MINBLOCKSIZE) 1181789Sahrens panic("really out of space"); 1182789Sahrens maxalloc = P2ROUNDUP(maxalloc >> 1, SPA_MINBLOCKSIZE); 1183789Sahrens } 1184789Sahrens 1185789Sahrens if (resid <= maxalloc * gbps_left) { 1186789Sahrens lsize = maxalloc; 1187789Sahrens BP_SET_LSIZE(gbp, lsize); 1188789Sahrens BP_SET_PSIZE(gbp, lsize); 1189789Sahrens BP_SET_COMPRESS(gbp, ZIO_COMPRESS_OFF); 11901775Sbillm gbp->blk_birth = txg; 11911775Sbillm zio_nowait(zio_rewrite(zio, spa, 11921775Sbillm zio->io_checksum, txg, gbp, 1193789Sahrens (char *)zio->io_data + loff, lsize, 1194789Sahrens zio_write_allocate_gang_member_done, NULL, 11951544Seschrock zio->io_priority, zio->io_flags, 11961544Seschrock &zio->io_bookmark)); 1197789Sahrens } else { 1198789Sahrens lsize = P2ROUNDUP(resid / gbps_left, SPA_MINBLOCKSIZE); 1199789Sahrens ASSERT(lsize != SPA_MINBLOCKSIZE); 12001775Sbillm zio_nowait(zio_write_allocate(zio, spa, 12011775Sbillm zio->io_checksum, txg, gbp, 1202789Sahrens (char *)zio->io_data + loff, lsize, 1203789Sahrens zio_write_allocate_gang_member_done, NULL, 1204789Sahrens zio->io_priority, zio->io_flags)); 1205789Sahrens } 1206789Sahrens } 1207789Sahrens 1208789Sahrens ASSERT(resid == 0 && loff == zio->io_size); 1209789Sahrens 1210789Sahrens zio->io_pipeline |= 1U << ZIO_STAGE_GANG_CHECKSUM_GENERATE; 1211789Sahrens 1212789Sahrens zio_push_transform(zio, gbh, gsize, gsize); 12131775Sbillm /* 12141775Sbillm * As much as we'd like this to be zio_wait_children_ready(), 12151775Sbillm * updating our ASIZE doesn't happen until the io_done callback, 12161775Sbillm * so we have to wait for that to finish in order for our BP 12171775Sbillm * to be stable. 12181775Sbillm */ 1219789Sahrens zio_wait_children_done(zio); 1220789Sahrens } 1221789Sahrens 1222789Sahrens /* 1223789Sahrens * ========================================================================== 1224789Sahrens * Allocate and free blocks 1225789Sahrens * ========================================================================== 1226789Sahrens */ 1227789Sahrens static void 1228789Sahrens zio_dva_allocate(zio_t *zio) 1229789Sahrens { 1230789Sahrens blkptr_t *bp = zio->io_bp; 1231789Sahrens int error; 1232789Sahrens 1233789Sahrens ASSERT(BP_IS_HOLE(bp)); 12341775Sbillm ASSERT3U(BP_GET_NDVAS(bp), ==, 0); 12351775Sbillm ASSERT3U(zio->io_ndvas, >, 0); 12361775Sbillm ASSERT3U(zio->io_ndvas, <=, spa_max_replication(zio->io_spa)); 1237789Sahrens 1238789Sahrens /* For testing, make some blocks above a certain size be gang blocks */ 1239789Sahrens if (zio->io_size >= zio_gang_bang && (lbolt & 0x3) == 0) { 1240789Sahrens zio_write_allocate_gang_members(zio); 1241789Sahrens return; 1242789Sahrens } 1243789Sahrens 1244789Sahrens ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 1245789Sahrens 12461775Sbillm error = metaslab_alloc(zio->io_spa, zio->io_size, bp, zio->io_ndvas, 12471775Sbillm zio->io_txg, NULL); 1248789Sahrens 1249789Sahrens if (error == 0) { 1250789Sahrens bp->blk_birth = zio->io_txg; 1251789Sahrens } else if (error == ENOSPC) { 1252789Sahrens if (zio->io_size == SPA_MINBLOCKSIZE) 1253789Sahrens panic("really, truly out of space"); 1254789Sahrens zio_write_allocate_gang_members(zio); 1255789Sahrens return; 1256789Sahrens } else { 1257789Sahrens zio->io_error = error; 1258789Sahrens } 1259789Sahrens zio_next_stage(zio); 1260789Sahrens } 1261789Sahrens 1262789Sahrens static void 1263789Sahrens zio_dva_free(zio_t *zio) 1264789Sahrens { 1265789Sahrens blkptr_t *bp = zio->io_bp; 1266789Sahrens 1267*1807Sbonwick metaslab_free(zio->io_spa, bp, zio->io_txg, B_FALSE); 1268789Sahrens 1269789Sahrens BP_ZERO(bp); 1270789Sahrens 1271789Sahrens zio_next_stage(zio); 1272789Sahrens } 1273789Sahrens 1274789Sahrens static void 1275789Sahrens zio_dva_claim(zio_t *zio) 1276789Sahrens { 1277*1807Sbonwick zio->io_error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg); 1278789Sahrens 1279789Sahrens zio_next_stage(zio); 1280789Sahrens } 1281789Sahrens 1282789Sahrens /* 1283789Sahrens * ========================================================================== 1284789Sahrens * Read and write to physical devices 1285789Sahrens * ========================================================================== 1286789Sahrens */ 1287789Sahrens 1288789Sahrens static void 12891775Sbillm zio_vdev_io_start(zio_t *zio) 1290789Sahrens { 1291789Sahrens vdev_t *vd = zio->io_vd; 12921775Sbillm vdev_t *tvd = vd ? vd->vdev_top : NULL; 12931775Sbillm blkptr_t *bp = zio->io_bp; 12941775Sbillm uint64_t align; 1295789Sahrens 12961775Sbillm if (vd == NULL) { 12971775Sbillm /* The mirror_ops handle multiple DVAs in a single BP */ 12981775Sbillm vdev_mirror_ops.vdev_op_io_start(zio); 12991775Sbillm return; 13001775Sbillm } 13011775Sbillm 13021775Sbillm align = 1ULL << tvd->vdev_ashift; 13031775Sbillm 13041732Sbonwick if (zio->io_retries == 0 && vd == tvd) 1305789Sahrens zio->io_flags |= ZIO_FLAG_FAILFAST; 1306789Sahrens 13071775Sbillm if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) && 13081775Sbillm vd->vdev_children == 0) { 1309789Sahrens zio->io_flags |= ZIO_FLAG_PHYSICAL; 1310789Sahrens zio->io_offset += VDEV_LABEL_START_SIZE; 1311789Sahrens } 1312789Sahrens 13131732Sbonwick if (P2PHASE(zio->io_size, align) != 0) { 13141732Sbonwick uint64_t asize = P2ROUNDUP(zio->io_size, align); 13151732Sbonwick char *abuf = zio_buf_alloc(asize); 13161732Sbonwick ASSERT(vd == tvd); 13171732Sbonwick if (zio->io_type == ZIO_TYPE_WRITE) { 13181732Sbonwick bcopy(zio->io_data, abuf, zio->io_size); 13191732Sbonwick bzero(abuf + zio->io_size, asize - zio->io_size); 13201732Sbonwick } 13211732Sbonwick zio_push_transform(zio, abuf, asize, asize); 13221732Sbonwick ASSERT(!(zio->io_flags & ZIO_FLAG_SUBBLOCK)); 13231732Sbonwick zio->io_flags |= ZIO_FLAG_SUBBLOCK; 13241732Sbonwick } 13251732Sbonwick 13261732Sbonwick ASSERT(P2PHASE(zio->io_offset, align) == 0); 13271732Sbonwick ASSERT(P2PHASE(zio->io_size, align) == 0); 13281732Sbonwick ASSERT(bp == NULL || 13291732Sbonwick P2ROUNDUP(ZIO_GET_IOSIZE(zio), align) == zio->io_size); 1330789Sahrens ASSERT(zio->io_type != ZIO_TYPE_WRITE || (spa_mode & FWRITE)); 1331789Sahrens 1332789Sahrens vdev_io_start(zio); 1333789Sahrens 1334789Sahrens /* zio_next_stage_async() gets called from io completion interrupt */ 1335789Sahrens } 1336789Sahrens 1337789Sahrens static void 1338789Sahrens zio_vdev_io_done(zio_t *zio) 1339789Sahrens { 13401775Sbillm if (zio->io_vd == NULL) 13411775Sbillm /* The mirror_ops handle multiple DVAs in a single BP */ 13421775Sbillm vdev_mirror_ops.vdev_op_io_done(zio); 13431775Sbillm else 13441775Sbillm vdev_io_done(zio); 1345789Sahrens } 1346789Sahrens 1347789Sahrens /* XXPOLICY */ 13481544Seschrock boolean_t 1349789Sahrens zio_should_retry(zio_t *zio) 1350789Sahrens { 1351789Sahrens vdev_t *vd = zio->io_vd; 1352789Sahrens 1353789Sahrens if (zio->io_error == 0) 1354789Sahrens return (B_FALSE); 1355789Sahrens if (zio->io_delegate_list != NULL) 1356789Sahrens return (B_FALSE); 13571775Sbillm if (vd && vd != vd->vdev_top) 1358789Sahrens return (B_FALSE); 1359789Sahrens if (zio->io_flags & ZIO_FLAG_DONT_RETRY) 1360789Sahrens return (B_FALSE); 13611544Seschrock if (zio->io_retries > 0) 1362789Sahrens return (B_FALSE); 1363789Sahrens 1364789Sahrens return (B_TRUE); 1365789Sahrens } 1366789Sahrens 1367789Sahrens static void 1368789Sahrens zio_vdev_io_assess(zio_t *zio) 1369789Sahrens { 1370789Sahrens vdev_t *vd = zio->io_vd; 13711775Sbillm vdev_t *tvd = vd ? vd->vdev_top : NULL; 1372789Sahrens 13731544Seschrock ASSERT(zio->io_vsd == NULL); 1374789Sahrens 13751732Sbonwick if (zio->io_flags & ZIO_FLAG_SUBBLOCK) { 13761732Sbonwick void *abuf; 13771732Sbonwick uint64_t asize; 13781732Sbonwick ASSERT(vd == tvd); 13791732Sbonwick zio_pop_transform(zio, &abuf, &asize, &asize); 13801732Sbonwick if (zio->io_type == ZIO_TYPE_READ) 13811732Sbonwick bcopy(abuf, zio->io_data, zio->io_size); 13821732Sbonwick zio_buf_free(abuf, asize); 13831732Sbonwick zio->io_flags &= ~ZIO_FLAG_SUBBLOCK; 13841732Sbonwick } 13851732Sbonwick 13861544Seschrock if (zio_injection_enabled && !zio->io_error) 13871544Seschrock zio->io_error = zio_handle_fault_injection(zio, EIO); 1388789Sahrens 1389789Sahrens /* 1390789Sahrens * If the I/O failed, determine whether we should attempt to retry it. 1391789Sahrens */ 1392789Sahrens /* XXPOLICY */ 1393789Sahrens if (zio_should_retry(zio)) { 1394789Sahrens ASSERT(tvd == vd); 1395789Sahrens ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)); 1396789Sahrens 1397789Sahrens zio->io_retries++; 1398789Sahrens zio->io_error = 0; 1399789Sahrens zio->io_flags &= ZIO_FLAG_VDEV_INHERIT; 1400789Sahrens /* XXPOLICY */ 1401789Sahrens zio->io_flags &= ~ZIO_FLAG_FAILFAST; 1402789Sahrens zio->io_flags |= ZIO_FLAG_DONT_CACHE; 14031775Sbillm zio->io_stage = ZIO_STAGE_VDEV_IO_START - 1; 1404789Sahrens 1405789Sahrens dprintf("retry #%d for %s to %s offset %llx\n", 1406789Sahrens zio->io_retries, zio_type_name[zio->io_type], 1407789Sahrens vdev_description(vd), zio->io_offset); 1408789Sahrens 14091544Seschrock zio_next_stage_async(zio); 14101544Seschrock return; 14111544Seschrock } 1412789Sahrens 14131775Sbillm if (zio->io_error != 0 && zio->io_error != ECKSUM && 14141775Sbillm !(zio->io_flags & ZIO_FLAG_SPECULATIVE) && vd) { 1415789Sahrens /* 14161544Seschrock * Poor man's hotplug support. Even if we're done retrying this 14171544Seschrock * I/O, try to reopen the vdev to see if it's still attached. 14181544Seschrock * To avoid excessive thrashing, we only try it once a minute. 14191544Seschrock * This also has the effect of detecting when missing devices 14201544Seschrock * have come back, by polling the device once a minute. 14211544Seschrock * 14221544Seschrock * We need to do this asynchronously because we can't grab 14231544Seschrock * all the necessary locks way down here. 1424789Sahrens */ 14251544Seschrock if (gethrtime() - vd->vdev_last_try > 60ULL * NANOSEC) { 14261544Seschrock vd->vdev_last_try = gethrtime(); 14271544Seschrock tvd->vdev_reopen_wanted = 1; 14281544Seschrock spa_async_request(vd->vdev_spa, SPA_ASYNC_REOPEN); 14291544Seschrock } 1430789Sahrens } 1431789Sahrens 1432789Sahrens zio_next_stage(zio); 1433789Sahrens } 1434789Sahrens 1435789Sahrens void 1436789Sahrens zio_vdev_io_reissue(zio_t *zio) 1437789Sahrens { 1438789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 1439789Sahrens ASSERT(zio->io_error == 0); 1440789Sahrens 1441789Sahrens zio->io_stage--; 1442789Sahrens } 1443789Sahrens 1444789Sahrens void 1445789Sahrens zio_vdev_io_redone(zio_t *zio) 1446789Sahrens { 1447789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE); 1448789Sahrens 1449789Sahrens zio->io_stage--; 1450789Sahrens } 1451789Sahrens 1452789Sahrens void 1453789Sahrens zio_vdev_io_bypass(zio_t *zio) 1454789Sahrens { 1455789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 1456789Sahrens ASSERT(zio->io_error == 0); 1457789Sahrens 1458789Sahrens zio->io_flags |= ZIO_FLAG_IO_BYPASS; 1459789Sahrens zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS - 1; 1460789Sahrens } 1461789Sahrens 1462789Sahrens /* 1463789Sahrens * ========================================================================== 1464789Sahrens * Generate and verify checksums 1465789Sahrens * ========================================================================== 1466789Sahrens */ 1467789Sahrens static void 1468789Sahrens zio_checksum_generate(zio_t *zio) 1469789Sahrens { 1470789Sahrens int checksum = zio->io_checksum; 1471789Sahrens blkptr_t *bp = zio->io_bp; 1472789Sahrens 1473789Sahrens ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 1474789Sahrens 1475789Sahrens BP_SET_CHECKSUM(bp, checksum); 1476789Sahrens BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 1477789Sahrens 1478789Sahrens zio_checksum(checksum, &bp->blk_cksum, zio->io_data, zio->io_size); 1479789Sahrens 1480789Sahrens zio_next_stage(zio); 1481789Sahrens } 1482789Sahrens 1483789Sahrens static void 1484789Sahrens zio_gang_checksum_generate(zio_t *zio) 1485789Sahrens { 1486789Sahrens zio_cksum_t zc; 1487789Sahrens zio_gbh_phys_t *gbh = zio->io_data; 1488789Sahrens 14891775Sbillm ASSERT(BP_IS_GANG(zio->io_bp)); 1490789Sahrens ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE); 1491789Sahrens 1492789Sahrens zio_set_gang_verifier(zio, &gbh->zg_tail.zbt_cksum); 1493789Sahrens 1494789Sahrens zio_checksum(ZIO_CHECKSUM_GANG_HEADER, &zc, zio->io_data, zio->io_size); 1495789Sahrens 1496789Sahrens zio_next_stage(zio); 1497789Sahrens } 1498789Sahrens 1499789Sahrens static void 1500789Sahrens zio_checksum_verify(zio_t *zio) 1501789Sahrens { 1502789Sahrens if (zio->io_bp != NULL) { 1503789Sahrens zio->io_error = zio_checksum_error(zio); 15041544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) 15051544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM, 15061544Seschrock zio->io_spa, zio->io_vd, zio, 0, 0); 1507789Sahrens } 1508789Sahrens 1509789Sahrens zio_next_stage(zio); 1510789Sahrens } 1511789Sahrens 1512789Sahrens /* 1513789Sahrens * Called by RAID-Z to ensure we don't compute the checksum twice. 1514789Sahrens */ 1515789Sahrens void 1516789Sahrens zio_checksum_verified(zio_t *zio) 1517789Sahrens { 1518789Sahrens zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY); 1519789Sahrens } 1520789Sahrens 1521789Sahrens /* 1522789Sahrens * Set the external verifier for a gang block based on stuff in the bp 1523789Sahrens */ 1524789Sahrens void 1525789Sahrens zio_set_gang_verifier(zio_t *zio, zio_cksum_t *zcp) 1526789Sahrens { 15271775Sbillm blkptr_t *bp = zio->io_bp; 15281775Sbillm 15291775Sbillm zcp->zc_word[0] = DVA_GET_VDEV(BP_IDENTITY(bp)); 15301775Sbillm zcp->zc_word[1] = DVA_GET_OFFSET(BP_IDENTITY(bp)); 15311775Sbillm zcp->zc_word[2] = bp->blk_birth; 1532789Sahrens zcp->zc_word[3] = 0; 1533789Sahrens } 1534789Sahrens 1535789Sahrens /* 1536789Sahrens * ========================================================================== 1537789Sahrens * Define the pipeline 1538789Sahrens * ========================================================================== 1539789Sahrens */ 1540789Sahrens typedef void zio_pipe_stage_t(zio_t *zio); 1541789Sahrens 1542789Sahrens static void 1543789Sahrens zio_badop(zio_t *zio) 1544789Sahrens { 1545789Sahrens panic("Invalid I/O pipeline stage %u for zio %p", zio->io_stage, zio); 1546789Sahrens } 1547789Sahrens 1548789Sahrens zio_pipe_stage_t *zio_pipeline[ZIO_STAGE_DONE + 2] = { 1549789Sahrens zio_badop, 1550789Sahrens zio_wait_children_ready, 1551789Sahrens zio_write_compress, 1552789Sahrens zio_checksum_generate, 1553789Sahrens zio_gang_pipeline, 1554789Sahrens zio_get_gang_header, 1555789Sahrens zio_rewrite_gang_members, 1556789Sahrens zio_free_gang_members, 1557789Sahrens zio_claim_gang_members, 1558789Sahrens zio_dva_allocate, 1559789Sahrens zio_dva_free, 1560789Sahrens zio_dva_claim, 1561789Sahrens zio_gang_checksum_generate, 1562789Sahrens zio_ready, 1563789Sahrens zio_vdev_io_start, 1564789Sahrens zio_vdev_io_done, 1565789Sahrens zio_vdev_io_assess, 1566789Sahrens zio_wait_children_done, 1567789Sahrens zio_checksum_verify, 1568789Sahrens zio_read_gang_members, 1569789Sahrens zio_read_decompress, 1570789Sahrens zio_done, 1571789Sahrens zio_badop 1572789Sahrens }; 1573789Sahrens 1574789Sahrens /* 1575789Sahrens * Move an I/O to the next stage of the pipeline and execute that stage. 1576789Sahrens * There's no locking on io_stage because there's no legitimate way for 1577789Sahrens * multiple threads to be attempting to process the same I/O. 1578789Sahrens */ 1579789Sahrens void 1580789Sahrens zio_next_stage(zio_t *zio) 1581789Sahrens { 1582789Sahrens uint32_t pipeline = zio->io_pipeline; 1583789Sahrens 1584789Sahrens ASSERT(!MUTEX_HELD(&zio->io_lock)); 1585789Sahrens 1586789Sahrens if (zio->io_error) { 1587789Sahrens dprintf("zio %p vdev %s offset %llx stage %d error %d\n", 1588789Sahrens zio, vdev_description(zio->io_vd), 1589789Sahrens zio->io_offset, zio->io_stage, zio->io_error); 1590789Sahrens if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0) 1591789Sahrens pipeline &= ZIO_ERROR_PIPELINE_MASK; 1592789Sahrens } 1593789Sahrens 1594789Sahrens while (((1U << ++zio->io_stage) & pipeline) == 0) 1595789Sahrens continue; 1596789Sahrens 1597789Sahrens ASSERT(zio->io_stage <= ZIO_STAGE_DONE); 1598789Sahrens ASSERT(zio->io_stalled == 0); 1599789Sahrens 1600789Sahrens zio_pipeline[zio->io_stage](zio); 1601789Sahrens } 1602789Sahrens 1603789Sahrens void 1604789Sahrens zio_next_stage_async(zio_t *zio) 1605789Sahrens { 1606789Sahrens taskq_t *tq; 1607789Sahrens uint32_t pipeline = zio->io_pipeline; 1608789Sahrens 1609789Sahrens ASSERT(!MUTEX_HELD(&zio->io_lock)); 1610789Sahrens 1611789Sahrens if (zio->io_error) { 1612789Sahrens dprintf("zio %p vdev %s offset %llx stage %d error %d\n", 1613789Sahrens zio, vdev_description(zio->io_vd), 1614789Sahrens zio->io_offset, zio->io_stage, zio->io_error); 1615789Sahrens if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0) 1616789Sahrens pipeline &= ZIO_ERROR_PIPELINE_MASK; 1617789Sahrens } 1618789Sahrens 1619789Sahrens while (((1U << ++zio->io_stage) & pipeline) == 0) 1620789Sahrens continue; 1621789Sahrens 1622789Sahrens ASSERT(zio->io_stage <= ZIO_STAGE_DONE); 1623789Sahrens ASSERT(zio->io_stalled == 0); 1624789Sahrens 1625789Sahrens /* 1626789Sahrens * For performance, we'll probably want two sets of task queues: 1627789Sahrens * per-CPU issue taskqs and per-CPU completion taskqs. The per-CPU 1628789Sahrens * part is for read performance: since we have to make a pass over 1629789Sahrens * the data to checksum it anyway, we want to do this on the same CPU 1630789Sahrens * that issued the read, because (assuming CPU scheduling affinity) 1631789Sahrens * that thread is probably still there. Getting this optimization 1632789Sahrens * right avoids performance-hostile cache-to-cache transfers. 1633789Sahrens * 1634789Sahrens * Note that having two sets of task queues is also necessary for 1635789Sahrens * correctness: if all of the issue threads get bogged down waiting 1636789Sahrens * for dependent reads (e.g. metaslab freelist) to complete, then 1637789Sahrens * there won't be any threads available to service I/O completion 1638789Sahrens * interrupts. 1639789Sahrens */ 1640789Sahrens if ((1U << zio->io_stage) & zio->io_async_stages) { 1641789Sahrens if (zio->io_stage < ZIO_STAGE_VDEV_IO_DONE) 1642789Sahrens tq = zio->io_spa->spa_zio_issue_taskq[zio->io_type]; 1643789Sahrens else 1644789Sahrens tq = zio->io_spa->spa_zio_intr_taskq[zio->io_type]; 1645789Sahrens (void) taskq_dispatch(tq, 1646789Sahrens (task_func_t *)zio_pipeline[zio->io_stage], zio, TQ_SLEEP); 1647789Sahrens } else { 1648789Sahrens zio_pipeline[zio->io_stage](zio); 1649789Sahrens } 1650789Sahrens } 1651789Sahrens 1652789Sahrens /* 1653789Sahrens * Try to allocate an intent log block. Return 0 on success, errno on failure. 1654789Sahrens */ 1655789Sahrens int 1656*1807Sbonwick zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *bp, uint64_t txg) 1657789Sahrens { 1658789Sahrens int error; 1659789Sahrens 16601544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1661789Sahrens 1662789Sahrens BP_ZERO(bp); 1663789Sahrens 16641775Sbillm error = metaslab_alloc(spa, size, bp, 1, txg, NULL); 1665789Sahrens 1666789Sahrens if (error == 0) { 1667789Sahrens BP_SET_LSIZE(bp, size); 1668789Sahrens BP_SET_PSIZE(bp, size); 1669789Sahrens BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 1670*1807Sbonwick BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_ZILOG); 1671789Sahrens BP_SET_TYPE(bp, DMU_OT_INTENT_LOG); 1672789Sahrens BP_SET_LEVEL(bp, 0); 1673789Sahrens BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 1674789Sahrens bp->blk_birth = txg; 1675789Sahrens } 1676789Sahrens 16771544Seschrock spa_config_exit(spa, FTAG); 1678789Sahrens 1679789Sahrens return (error); 1680789Sahrens } 1681789Sahrens 1682789Sahrens /* 1683789Sahrens * Free an intent log block. We know it can't be a gang block, so there's 1684789Sahrens * nothing to do except metaslab_free() it. 1685789Sahrens */ 1686789Sahrens void 1687789Sahrens zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg) 1688789Sahrens { 16891775Sbillm ASSERT(!BP_IS_GANG(bp)); 1690789Sahrens 16911544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1692789Sahrens 1693*1807Sbonwick metaslab_free(spa, bp, txg, B_FALSE); 1694789Sahrens 16951544Seschrock spa_config_exit(spa, FTAG); 1696789Sahrens } 1697