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]; 1152856Snd150628 (void) sprintf(name, "zio_buf_%lu", (ulong_t)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; 2612856Snd150628 mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL); 262789Sahrens zio_push_transform(zio, data, size, size); 263789Sahrens 264789Sahrens if (pio == NULL) { 265789Sahrens if (!(flags & ZIO_FLAG_CONFIG_HELD)) 2661544Seschrock spa_config_enter(zio->io_spa, RW_READER, zio); 267789Sahrens zio->io_root = zio; 268789Sahrens } else { 269789Sahrens zio->io_root = pio->io_root; 2701544Seschrock if (!(flags & ZIO_FLAG_NOBOOKMARK)) 2711544Seschrock zio->io_logical = pio->io_logical; 272789Sahrens mutex_enter(&pio->io_lock); 273789Sahrens if (stage < ZIO_STAGE_READY) 274789Sahrens pio->io_children_notready++; 275789Sahrens pio->io_children_notdone++; 276789Sahrens zio->io_sibling_next = pio->io_child; 277789Sahrens zio->io_sibling_prev = NULL; 278789Sahrens if (pio->io_child != NULL) 279789Sahrens pio->io_child->io_sibling_prev = zio; 280789Sahrens pio->io_child = zio; 2811775Sbillm zio->io_ndvas = pio->io_ndvas; 282789Sahrens mutex_exit(&pio->io_lock); 283789Sahrens } 284789Sahrens 285789Sahrens return (zio); 286789Sahrens } 287789Sahrens 288789Sahrens zio_t * 289789Sahrens zio_null(zio_t *pio, spa_t *spa, zio_done_func_t *done, void *private, 290789Sahrens int flags) 291789Sahrens { 292789Sahrens zio_t *zio; 293789Sahrens 294789Sahrens zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private, 295789Sahrens ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, ZIO_STAGE_OPEN, 296789Sahrens ZIO_WAIT_FOR_CHILDREN_PIPELINE); 297789Sahrens 298789Sahrens return (zio); 299789Sahrens } 300789Sahrens 301789Sahrens zio_t * 302789Sahrens zio_root(spa_t *spa, zio_done_func_t *done, void *private, int flags) 303789Sahrens { 304789Sahrens return (zio_null(NULL, spa, done, private, flags)); 305789Sahrens } 306789Sahrens 307789Sahrens zio_t * 308789Sahrens zio_read(zio_t *pio, spa_t *spa, blkptr_t *bp, void *data, 309789Sahrens uint64_t size, zio_done_func_t *done, void *private, 3101544Seschrock int priority, int flags, zbookmark_t *zb) 311789Sahrens { 312789Sahrens zio_t *zio; 313789Sahrens 314789Sahrens ASSERT3U(size, ==, BP_GET_LSIZE(bp)); 315789Sahrens 316789Sahrens zio = zio_create(pio, spa, bp->blk_birth, bp, data, size, done, private, 317789Sahrens ZIO_TYPE_READ, priority, flags, ZIO_STAGE_OPEN, ZIO_READ_PIPELINE); 3181544Seschrock zio->io_bookmark = *zb; 3191544Seschrock 3201544Seschrock zio->io_logical = zio; 321789Sahrens 322789Sahrens /* 323789Sahrens * Work off our copy of the bp so the caller can free it. 324789Sahrens */ 325789Sahrens zio->io_bp = &zio->io_bp_copy; 326789Sahrens 327789Sahrens if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) { 328789Sahrens uint64_t csize = BP_GET_PSIZE(bp); 329789Sahrens void *cbuf = zio_buf_alloc(csize); 330789Sahrens 331789Sahrens zio_push_transform(zio, cbuf, csize, csize); 332789Sahrens zio->io_pipeline |= 1U << ZIO_STAGE_READ_DECOMPRESS; 333789Sahrens } 334789Sahrens 3351775Sbillm if (BP_IS_GANG(bp)) { 336789Sahrens uint64_t gsize = SPA_GANGBLOCKSIZE; 337789Sahrens void *gbuf = zio_buf_alloc(gsize); 338789Sahrens 339789Sahrens zio_push_transform(zio, gbuf, gsize, gsize); 340789Sahrens zio->io_pipeline |= 1U << ZIO_STAGE_READ_GANG_MEMBERS; 341789Sahrens } 342789Sahrens 343789Sahrens return (zio); 344789Sahrens } 345789Sahrens 346789Sahrens zio_t * 3471775Sbillm zio_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies, 348789Sahrens uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 3491544Seschrock zio_done_func_t *done, void *private, int priority, int flags, 3501544Seschrock zbookmark_t *zb) 351789Sahrens { 352789Sahrens zio_t *zio; 353789Sahrens 354789Sahrens ASSERT(checksum >= ZIO_CHECKSUM_OFF && 355789Sahrens checksum < ZIO_CHECKSUM_FUNCTIONS); 356789Sahrens 357789Sahrens ASSERT(compress >= ZIO_COMPRESS_OFF && 358789Sahrens compress < ZIO_COMPRESS_FUNCTIONS); 359789Sahrens 360789Sahrens zio = zio_create(pio, spa, txg, bp, data, size, done, private, 361789Sahrens ZIO_TYPE_WRITE, priority, flags, 362789Sahrens ZIO_STAGE_OPEN, ZIO_WRITE_PIPELINE); 363789Sahrens 3641544Seschrock zio->io_bookmark = *zb; 3651544Seschrock 3661544Seschrock zio->io_logical = zio; 3671544Seschrock 368789Sahrens zio->io_checksum = checksum; 369789Sahrens zio->io_compress = compress; 3701775Sbillm zio->io_ndvas = ncopies; 371789Sahrens 372789Sahrens if (compress != ZIO_COMPRESS_OFF) 373789Sahrens zio->io_async_stages |= 1U << ZIO_STAGE_WRITE_COMPRESS; 374789Sahrens 375789Sahrens if (bp->blk_birth != txg) { 376789Sahrens /* XXX the bp usually (always?) gets re-zeroed later */ 377789Sahrens BP_ZERO(bp); 378789Sahrens BP_SET_LSIZE(bp, size); 379789Sahrens BP_SET_PSIZE(bp, size); 3801775Sbillm } else { 3811775Sbillm /* Make sure someone doesn't change their mind on overwrites */ 3821775Sbillm ASSERT(MIN(zio->io_ndvas + BP_IS_GANG(bp), 3831775Sbillm spa_max_replication(spa)) == BP_GET_NDVAS(bp)); 384789Sahrens } 385789Sahrens 386789Sahrens return (zio); 387789Sahrens } 388789Sahrens 389789Sahrens zio_t * 390789Sahrens zio_rewrite(zio_t *pio, spa_t *spa, int checksum, 391789Sahrens uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 3921544Seschrock zio_done_func_t *done, void *private, int priority, int flags, 3931544Seschrock zbookmark_t *zb) 394789Sahrens { 395789Sahrens zio_t *zio; 396789Sahrens 397789Sahrens zio = zio_create(pio, spa, txg, bp, data, size, done, private, 398789Sahrens ZIO_TYPE_WRITE, priority, flags, 399789Sahrens ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE); 400789Sahrens 4011544Seschrock zio->io_bookmark = *zb; 402789Sahrens zio->io_checksum = checksum; 403789Sahrens zio->io_compress = ZIO_COMPRESS_OFF; 404789Sahrens 4051775Sbillm if (pio != NULL) 4061775Sbillm ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp)); 4071775Sbillm 408789Sahrens return (zio); 409789Sahrens } 410789Sahrens 411789Sahrens static zio_t * 412789Sahrens zio_write_allocate(zio_t *pio, spa_t *spa, int checksum, 413789Sahrens uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 414789Sahrens zio_done_func_t *done, void *private, int priority, int flags) 415789Sahrens { 416789Sahrens zio_t *zio; 417789Sahrens 418789Sahrens BP_ZERO(bp); 419789Sahrens BP_SET_LSIZE(bp, size); 420789Sahrens BP_SET_PSIZE(bp, size); 421789Sahrens BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 422789Sahrens 423789Sahrens zio = zio_create(pio, spa, txg, bp, data, size, done, private, 424789Sahrens ZIO_TYPE_WRITE, priority, flags, 425789Sahrens ZIO_STAGE_OPEN, ZIO_WRITE_ALLOCATE_PIPELINE); 426789Sahrens 427789Sahrens zio->io_checksum = checksum; 428789Sahrens zio->io_compress = ZIO_COMPRESS_OFF; 429789Sahrens 430789Sahrens return (zio); 431789Sahrens } 432789Sahrens 433789Sahrens zio_t * 434789Sahrens zio_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 435789Sahrens zio_done_func_t *done, void *private) 436789Sahrens { 437789Sahrens zio_t *zio; 438789Sahrens 439789Sahrens ASSERT(!BP_IS_HOLE(bp)); 440789Sahrens 441789Sahrens if (txg == spa->spa_syncing_txg && 442789Sahrens spa->spa_sync_pass > zio_sync_pass.zp_defer_free) { 443789Sahrens bplist_enqueue_deferred(&spa->spa_sync_bplist, bp); 444789Sahrens return (zio_null(pio, spa, NULL, NULL, 0)); 445789Sahrens } 446789Sahrens 447789Sahrens zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private, 448789Sahrens ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, 0, 449789Sahrens ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE); 450789Sahrens 451789Sahrens zio->io_bp = &zio->io_bp_copy; 452789Sahrens 453789Sahrens return (zio); 454789Sahrens } 455789Sahrens 456789Sahrens zio_t * 457789Sahrens zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 458789Sahrens zio_done_func_t *done, void *private) 459789Sahrens { 460789Sahrens zio_t *zio; 461789Sahrens 462789Sahrens /* 463789Sahrens * A claim is an allocation of a specific block. Claims are needed 464789Sahrens * to support immediate writes in the intent log. The issue is that 465789Sahrens * immediate writes contain committed data, but in a txg that was 466789Sahrens * *not* committed. Upon opening the pool after an unclean shutdown, 467789Sahrens * the intent log claims all blocks that contain immediate write data 468789Sahrens * so that the SPA knows they're in use. 469789Sahrens * 470789Sahrens * All claims *must* be resolved in the first txg -- before the SPA 471789Sahrens * starts allocating blocks -- so that nothing is allocated twice. 472789Sahrens */ 473789Sahrens ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa)); 474789Sahrens ASSERT3U(spa_first_txg(spa), <=, txg); 475789Sahrens 476789Sahrens zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private, 477789Sahrens ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, 0, 478789Sahrens ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE); 479789Sahrens 480789Sahrens zio->io_bp = &zio->io_bp_copy; 481789Sahrens 482789Sahrens return (zio); 483789Sahrens } 484789Sahrens 485789Sahrens zio_t * 486789Sahrens zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd, 487789Sahrens zio_done_func_t *done, void *private, int priority, int flags) 488789Sahrens { 489789Sahrens zio_t *zio; 490789Sahrens int c; 491789Sahrens 492789Sahrens if (vd->vdev_children == 0) { 493789Sahrens zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private, 494789Sahrens ZIO_TYPE_IOCTL, priority, flags, 495789Sahrens ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE); 496789Sahrens 497789Sahrens zio->io_vd = vd; 498789Sahrens zio->io_cmd = cmd; 499789Sahrens } else { 500789Sahrens zio = zio_null(pio, spa, NULL, NULL, flags); 501789Sahrens 502789Sahrens for (c = 0; c < vd->vdev_children; c++) 503789Sahrens zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd, 504789Sahrens done, private, priority, flags)); 505789Sahrens } 506789Sahrens 507789Sahrens return (zio); 508789Sahrens } 509789Sahrens 510789Sahrens static void 511789Sahrens zio_phys_bp_init(vdev_t *vd, blkptr_t *bp, uint64_t offset, uint64_t size, 512789Sahrens int checksum) 513789Sahrens { 514789Sahrens ASSERT(vd->vdev_children == 0); 515789Sahrens 516789Sahrens ASSERT(size <= SPA_MAXBLOCKSIZE); 517789Sahrens ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0); 518789Sahrens ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0); 519789Sahrens 520789Sahrens ASSERT(offset + size <= VDEV_LABEL_START_SIZE || 521789Sahrens offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 522789Sahrens ASSERT3U(offset + size, <=, vd->vdev_psize); 523789Sahrens 524789Sahrens BP_ZERO(bp); 525789Sahrens 526789Sahrens BP_SET_LSIZE(bp, size); 527789Sahrens BP_SET_PSIZE(bp, size); 528789Sahrens 529789Sahrens BP_SET_CHECKSUM(bp, checksum); 530789Sahrens BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 531789Sahrens BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 532789Sahrens 533789Sahrens if (checksum != ZIO_CHECKSUM_OFF) 534789Sahrens ZIO_SET_CHECKSUM(&bp->blk_cksum, offset, 0, 0, 0); 535789Sahrens } 536789Sahrens 537789Sahrens zio_t * 538789Sahrens zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 539789Sahrens void *data, int checksum, zio_done_func_t *done, void *private, 540789Sahrens int priority, int flags) 541789Sahrens { 542789Sahrens zio_t *zio; 543789Sahrens blkptr_t blk; 544789Sahrens 545789Sahrens zio_phys_bp_init(vd, &blk, offset, size, checksum); 546789Sahrens 547789Sahrens zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private, 548789Sahrens ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, 549789Sahrens ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE); 550789Sahrens 551789Sahrens zio->io_vd = vd; 552789Sahrens zio->io_offset = offset; 553789Sahrens 554789Sahrens /* 555789Sahrens * Work off our copy of the bp so the caller can free it. 556789Sahrens */ 557789Sahrens zio->io_bp = &zio->io_bp_copy; 558789Sahrens 559789Sahrens return (zio); 560789Sahrens } 561789Sahrens 562789Sahrens zio_t * 563789Sahrens zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 564789Sahrens void *data, int checksum, zio_done_func_t *done, void *private, 565789Sahrens int priority, int flags) 566789Sahrens { 567789Sahrens zio_block_tail_t *zbt; 568789Sahrens void *wbuf; 569789Sahrens zio_t *zio; 570789Sahrens blkptr_t blk; 571789Sahrens 572789Sahrens zio_phys_bp_init(vd, &blk, offset, size, checksum); 573789Sahrens 574789Sahrens zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private, 575789Sahrens ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, 576789Sahrens ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE); 577789Sahrens 578789Sahrens zio->io_vd = vd; 579789Sahrens zio->io_offset = offset; 580789Sahrens 581789Sahrens zio->io_bp = &zio->io_bp_copy; 582789Sahrens zio->io_checksum = checksum; 583789Sahrens 584789Sahrens if (zio_checksum_table[checksum].ci_zbt) { 585789Sahrens /* 586789Sahrens * zbt checksums are necessarily destructive -- they modify 587789Sahrens * one word of the write buffer to hold the verifier/checksum. 588789Sahrens * Therefore, we must make a local copy in case the data is 589789Sahrens * being written to multiple places. 590789Sahrens */ 591789Sahrens wbuf = zio_buf_alloc(size); 592789Sahrens bcopy(data, wbuf, size); 593789Sahrens zio_push_transform(zio, wbuf, size, size); 594789Sahrens 595789Sahrens zbt = (zio_block_tail_t *)((char *)wbuf + size) - 1; 596789Sahrens zbt->zbt_cksum = blk.blk_cksum; 597789Sahrens } 598789Sahrens 599789Sahrens return (zio); 600789Sahrens } 601789Sahrens 602789Sahrens /* 603789Sahrens * Create a child I/O to do some work for us. It has no associated bp. 604789Sahrens */ 605789Sahrens zio_t * 606789Sahrens zio_vdev_child_io(zio_t *zio, blkptr_t *bp, vdev_t *vd, uint64_t offset, 607789Sahrens void *data, uint64_t size, int type, int priority, int flags, 608789Sahrens zio_done_func_t *done, void *private) 609789Sahrens { 610789Sahrens uint32_t pipeline = ZIO_VDEV_CHILD_PIPELINE; 611789Sahrens zio_t *cio; 612789Sahrens 613789Sahrens if (type == ZIO_TYPE_READ && bp != NULL) { 614789Sahrens /* 615789Sahrens * If we have the bp, then the child should perform the 616789Sahrens * checksum and the parent need not. This pushes error 617789Sahrens * detection as close to the leaves as possible and 618789Sahrens * eliminates redundant checksums in the interior nodes. 619789Sahrens */ 620789Sahrens pipeline |= 1U << ZIO_STAGE_CHECKSUM_VERIFY; 621789Sahrens zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY); 622789Sahrens } 623789Sahrens 624789Sahrens cio = zio_create(zio, zio->io_spa, zio->io_txg, bp, data, size, 625789Sahrens done, private, type, priority, 626789Sahrens (zio->io_flags & ZIO_FLAG_VDEV_INHERIT) | ZIO_FLAG_CANFAIL | flags, 6271775Sbillm ZIO_STAGE_VDEV_IO_START - 1, pipeline); 628789Sahrens 629789Sahrens cio->io_vd = vd; 630789Sahrens cio->io_offset = offset; 631789Sahrens 632789Sahrens return (cio); 633789Sahrens } 634789Sahrens 635789Sahrens /* 636789Sahrens * ========================================================================== 637789Sahrens * Initiate I/O, either sync or async 638789Sahrens * ========================================================================== 639789Sahrens */ 640789Sahrens int 641789Sahrens zio_wait(zio_t *zio) 642789Sahrens { 643789Sahrens int error; 644789Sahrens 645789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 646789Sahrens 647789Sahrens zio->io_waiter = curthread; 648789Sahrens 649789Sahrens zio_next_stage_async(zio); 650789Sahrens 651789Sahrens mutex_enter(&zio->io_lock); 652789Sahrens while (zio->io_stalled != ZIO_STAGE_DONE) 653789Sahrens cv_wait(&zio->io_cv, &zio->io_lock); 654789Sahrens mutex_exit(&zio->io_lock); 655789Sahrens 656789Sahrens error = zio->io_error; 6572856Snd150628 mutex_destroy(&zio->io_lock); 658789Sahrens kmem_free(zio, sizeof (zio_t)); 659789Sahrens 660789Sahrens return (error); 661789Sahrens } 662789Sahrens 663789Sahrens void 664789Sahrens zio_nowait(zio_t *zio) 665789Sahrens { 666789Sahrens zio_next_stage_async(zio); 667789Sahrens } 668789Sahrens 669789Sahrens /* 670789Sahrens * ========================================================================== 671789Sahrens * I/O pipeline interlocks: parent/child dependency scoreboarding 672789Sahrens * ========================================================================== 673789Sahrens */ 674789Sahrens static void 675789Sahrens zio_wait_for_children(zio_t *zio, uint32_t stage, uint64_t *countp) 676789Sahrens { 677789Sahrens mutex_enter(&zio->io_lock); 678789Sahrens if (*countp == 0) { 679789Sahrens ASSERT(zio->io_stalled == 0); 680789Sahrens mutex_exit(&zio->io_lock); 681789Sahrens zio_next_stage(zio); 682789Sahrens } else { 683789Sahrens zio->io_stalled = stage; 684789Sahrens mutex_exit(&zio->io_lock); 685789Sahrens } 686789Sahrens } 687789Sahrens 688789Sahrens static void 689789Sahrens zio_notify_parent(zio_t *zio, uint32_t stage, uint64_t *countp) 690789Sahrens { 691789Sahrens zio_t *pio = zio->io_parent; 692789Sahrens 693789Sahrens mutex_enter(&pio->io_lock); 694789Sahrens if (pio->io_error == 0 && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 695789Sahrens pio->io_error = zio->io_error; 696789Sahrens if (--*countp == 0 && pio->io_stalled == stage) { 697789Sahrens pio->io_stalled = 0; 698789Sahrens mutex_exit(&pio->io_lock); 699789Sahrens zio_next_stage_async(pio); 700789Sahrens } else { 701789Sahrens mutex_exit(&pio->io_lock); 702789Sahrens } 703789Sahrens } 704789Sahrens 705789Sahrens static void 706789Sahrens zio_wait_children_ready(zio_t *zio) 707789Sahrens { 708789Sahrens zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_READY, 709789Sahrens &zio->io_children_notready); 710789Sahrens } 711789Sahrens 712789Sahrens void 713789Sahrens zio_wait_children_done(zio_t *zio) 714789Sahrens { 715789Sahrens zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_DONE, 716789Sahrens &zio->io_children_notdone); 717789Sahrens } 718789Sahrens 719789Sahrens static void 720789Sahrens zio_ready(zio_t *zio) 721789Sahrens { 722789Sahrens zio_t *pio = zio->io_parent; 723789Sahrens 724789Sahrens if (pio != NULL) 725789Sahrens zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_READY, 726789Sahrens &pio->io_children_notready); 727789Sahrens 728789Sahrens if (zio->io_bp) 729789Sahrens zio->io_bp_copy = *zio->io_bp; 730789Sahrens 731789Sahrens zio_next_stage(zio); 732789Sahrens } 733789Sahrens 734789Sahrens static void 735789Sahrens zio_done(zio_t *zio) 736789Sahrens { 737789Sahrens zio_t *pio = zio->io_parent; 738789Sahrens spa_t *spa = zio->io_spa; 739789Sahrens blkptr_t *bp = zio->io_bp; 740789Sahrens vdev_t *vd = zio->io_vd; 741896Smaybee char blkbuf[BP_SPRINTF_LEN]; 742789Sahrens 743789Sahrens ASSERT(zio->io_children_notready == 0); 744789Sahrens ASSERT(zio->io_children_notdone == 0); 745789Sahrens 746789Sahrens if (bp != NULL) { 747789Sahrens ASSERT(bp->blk_pad[0] == 0); 748789Sahrens ASSERT(bp->blk_pad[1] == 0); 749789Sahrens ASSERT(bp->blk_pad[2] == 0); 750789Sahrens ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0); 751789Sahrens if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) && 7521775Sbillm !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) { 753789Sahrens ASSERT(!BP_SHOULD_BYTESWAP(bp)); 7541775Sbillm if (zio->io_ndvas != 0) 7551775Sbillm ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp)); 7561775Sbillm ASSERT(BP_COUNT_GANG(bp) == 0 || 7571775Sbillm (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp))); 7581775Sbillm } 759789Sahrens } 760789Sahrens 761789Sahrens if (vd != NULL) 762789Sahrens vdev_stat_update(zio); 763789Sahrens 764789Sahrens if (zio->io_error) { 7651544Seschrock /* 7661544Seschrock * If this I/O is attached to a particular vdev, 7671544Seschrock * generate an error message describing the I/O failure 7681544Seschrock * at the block level. We ignore these errors if the 7691544Seschrock * device is currently unavailable. 7701544Seschrock */ 7711732Sbonwick if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd)) 7721544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_IO, 7731732Sbonwick zio->io_spa, vd, zio, 0, 0); 774789Sahrens 7751544Seschrock if ((zio->io_error == EIO || 7761544Seschrock !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) && 7771544Seschrock zio->io_logical == zio) { 7781544Seschrock /* 7791544Seschrock * For root I/O requests, tell the SPA to log the error 7801544Seschrock * appropriately. Also, generate a logical data 7811544Seschrock * ereport. 7821544Seschrock */ 7831544Seschrock spa_log_error(zio->io_spa, zio); 7841544Seschrock 7851544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_DATA, 7861544Seschrock zio->io_spa, NULL, zio, 0, 0); 7871544Seschrock } 788789Sahrens 7891544Seschrock /* 7901544Seschrock * For I/O requests that cannot fail, panic appropriately. 7911544Seschrock */ 7921544Seschrock if (!(zio->io_flags & ZIO_FLAG_CANFAIL)) { 7931544Seschrock sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, 7941544Seschrock bp ? bp : &zio->io_bp_copy); 7951544Seschrock panic("ZFS: %s (%s on %s off %llx: zio %p %s): error " 7961544Seschrock "%d", zio->io_error == ECKSUM ? 7971544Seschrock "bad checksum" : "I/O failure", 7981544Seschrock zio_type_name[zio->io_type], 7991544Seschrock vdev_description(vd), 8001544Seschrock (u_longlong_t)zio->io_offset, 8011544Seschrock zio, blkbuf, zio->io_error); 8021544Seschrock } 803789Sahrens } 804789Sahrens 805789Sahrens zio_clear_transform_stack(zio); 806789Sahrens 807789Sahrens if (zio->io_done) 808789Sahrens zio->io_done(zio); 809789Sahrens 810789Sahrens ASSERT(zio->io_delegate_list == NULL); 811789Sahrens ASSERT(zio->io_delegate_next == NULL); 812789Sahrens 813789Sahrens if (pio != NULL) { 814789Sahrens zio_t *next, *prev; 815789Sahrens 816789Sahrens mutex_enter(&pio->io_lock); 817789Sahrens next = zio->io_sibling_next; 818789Sahrens prev = zio->io_sibling_prev; 819789Sahrens if (next != NULL) 820789Sahrens next->io_sibling_prev = prev; 821789Sahrens if (prev != NULL) 822789Sahrens prev->io_sibling_next = next; 823789Sahrens if (pio->io_child == zio) 824789Sahrens pio->io_child = next; 825789Sahrens mutex_exit(&pio->io_lock); 826789Sahrens 827789Sahrens zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_DONE, 828789Sahrens &pio->io_children_notdone); 829789Sahrens } 830789Sahrens 831789Sahrens if (pio == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_HELD)) 8321544Seschrock spa_config_exit(spa, zio); 833789Sahrens 834789Sahrens if (zio->io_waiter != NULL) { 835789Sahrens mutex_enter(&zio->io_lock); 836789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_DONE); 837789Sahrens zio->io_stalled = zio->io_stage; 838789Sahrens cv_broadcast(&zio->io_cv); 839789Sahrens mutex_exit(&zio->io_lock); 840789Sahrens } else { 841789Sahrens kmem_free(zio, sizeof (zio_t)); 842789Sahrens } 843789Sahrens } 844789Sahrens 845789Sahrens /* 846789Sahrens * ========================================================================== 847789Sahrens * Compression support 848789Sahrens * ========================================================================== 849789Sahrens */ 850789Sahrens static void 851789Sahrens zio_write_compress(zio_t *zio) 852789Sahrens { 853789Sahrens int compress = zio->io_compress; 854789Sahrens blkptr_t *bp = zio->io_bp; 855789Sahrens void *cbuf; 856789Sahrens uint64_t lsize = zio->io_size; 857789Sahrens uint64_t csize = lsize; 858789Sahrens uint64_t cbufsize = 0; 859789Sahrens int pass; 860789Sahrens 861789Sahrens if (bp->blk_birth == zio->io_txg) { 862789Sahrens /* 863789Sahrens * We're rewriting an existing block, which means we're 864789Sahrens * working on behalf of spa_sync(). For spa_sync() to 865789Sahrens * converge, it must eventually be the case that we don't 866789Sahrens * have to allocate new blocks. But compression changes 867789Sahrens * the blocksize, which forces a reallocate, and makes 868789Sahrens * convergence take longer. Therefore, after the first 869789Sahrens * few passes, stop compressing to ensure convergence. 870789Sahrens */ 871789Sahrens pass = spa_sync_pass(zio->io_spa); 872789Sahrens if (pass > zio_sync_pass.zp_dontcompress) 873789Sahrens compress = ZIO_COMPRESS_OFF; 874789Sahrens } else { 875789Sahrens ASSERT(BP_IS_HOLE(bp)); 876789Sahrens pass = 1; 877789Sahrens } 878789Sahrens 879789Sahrens if (compress != ZIO_COMPRESS_OFF) 880789Sahrens if (!zio_compress_data(compress, zio->io_data, zio->io_size, 881789Sahrens &cbuf, &csize, &cbufsize)) 882789Sahrens compress = ZIO_COMPRESS_OFF; 883789Sahrens 884789Sahrens if (compress != ZIO_COMPRESS_OFF && csize != 0) 885789Sahrens zio_push_transform(zio, cbuf, csize, cbufsize); 886789Sahrens 887789Sahrens /* 888789Sahrens * The final pass of spa_sync() must be all rewrites, but the first 889789Sahrens * few passes offer a trade-off: allocating blocks defers convergence, 890789Sahrens * but newly allocated blocks are sequential, so they can be written 891789Sahrens * to disk faster. Therefore, we allow the first few passes of 892789Sahrens * spa_sync() to reallocate new blocks, but force rewrites after that. 893789Sahrens * There should only be a handful of blocks after pass 1 in any case. 894789Sahrens */ 895789Sahrens if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == csize && 896789Sahrens pass > zio_sync_pass.zp_rewrite) { 897789Sahrens ASSERT(csize != 0); 898*2885Sahrens BP_SET_LSIZE(bp, lsize); 899*2885Sahrens BP_SET_COMPRESS(bp, compress); 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 12671807Sbonwick 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 { 12771807Sbonwick 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 1396789Sahrens zio->io_retries++; 1397789Sahrens zio->io_error = 0; 1398789Sahrens zio->io_flags &= ZIO_FLAG_VDEV_INHERIT; 1399789Sahrens /* XXPOLICY */ 1400789Sahrens zio->io_flags &= ~ZIO_FLAG_FAILFAST; 1401789Sahrens zio->io_flags |= ZIO_FLAG_DONT_CACHE; 14021775Sbillm zio->io_stage = ZIO_STAGE_VDEV_IO_START - 1; 1403789Sahrens 1404789Sahrens dprintf("retry #%d for %s to %s offset %llx\n", 1405789Sahrens zio->io_retries, zio_type_name[zio->io_type], 1406789Sahrens vdev_description(vd), zio->io_offset); 1407789Sahrens 14081544Seschrock zio_next_stage_async(zio); 14091544Seschrock return; 14101544Seschrock } 1411789Sahrens 14121775Sbillm if (zio->io_error != 0 && zio->io_error != ECKSUM && 14131775Sbillm !(zio->io_flags & ZIO_FLAG_SPECULATIVE) && vd) { 1414789Sahrens /* 14151544Seschrock * Poor man's hotplug support. Even if we're done retrying this 14161544Seschrock * I/O, try to reopen the vdev to see if it's still attached. 14171544Seschrock * To avoid excessive thrashing, we only try it once a minute. 14181544Seschrock * This also has the effect of detecting when missing devices 14191544Seschrock * have come back, by polling the device once a minute. 14201544Seschrock * 14211544Seschrock * We need to do this asynchronously because we can't grab 14221544Seschrock * all the necessary locks way down here. 1423789Sahrens */ 14241544Seschrock if (gethrtime() - vd->vdev_last_try > 60ULL * NANOSEC) { 14251544Seschrock vd->vdev_last_try = gethrtime(); 14261544Seschrock tvd->vdev_reopen_wanted = 1; 14271544Seschrock spa_async_request(vd->vdev_spa, SPA_ASYNC_REOPEN); 14281544Seschrock } 1429789Sahrens } 1430789Sahrens 1431789Sahrens zio_next_stage(zio); 1432789Sahrens } 1433789Sahrens 1434789Sahrens void 1435789Sahrens zio_vdev_io_reissue(zio_t *zio) 1436789Sahrens { 1437789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 1438789Sahrens ASSERT(zio->io_error == 0); 1439789Sahrens 1440789Sahrens zio->io_stage--; 1441789Sahrens } 1442789Sahrens 1443789Sahrens void 1444789Sahrens zio_vdev_io_redone(zio_t *zio) 1445789Sahrens { 1446789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE); 1447789Sahrens 1448789Sahrens zio->io_stage--; 1449789Sahrens } 1450789Sahrens 1451789Sahrens void 1452789Sahrens zio_vdev_io_bypass(zio_t *zio) 1453789Sahrens { 1454789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START); 1455789Sahrens ASSERT(zio->io_error == 0); 1456789Sahrens 1457789Sahrens zio->io_flags |= ZIO_FLAG_IO_BYPASS; 1458789Sahrens zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS - 1; 1459789Sahrens } 1460789Sahrens 1461789Sahrens /* 1462789Sahrens * ========================================================================== 1463789Sahrens * Generate and verify checksums 1464789Sahrens * ========================================================================== 1465789Sahrens */ 1466789Sahrens static void 1467789Sahrens zio_checksum_generate(zio_t *zio) 1468789Sahrens { 1469789Sahrens int checksum = zio->io_checksum; 1470789Sahrens blkptr_t *bp = zio->io_bp; 1471789Sahrens 1472789Sahrens ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 1473789Sahrens 1474789Sahrens BP_SET_CHECKSUM(bp, checksum); 1475789Sahrens BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 1476789Sahrens 1477789Sahrens zio_checksum(checksum, &bp->blk_cksum, zio->io_data, zio->io_size); 1478789Sahrens 1479789Sahrens zio_next_stage(zio); 1480789Sahrens } 1481789Sahrens 1482789Sahrens static void 1483789Sahrens zio_gang_checksum_generate(zio_t *zio) 1484789Sahrens { 1485789Sahrens zio_cksum_t zc; 1486789Sahrens zio_gbh_phys_t *gbh = zio->io_data; 1487789Sahrens 14881775Sbillm ASSERT(BP_IS_GANG(zio->io_bp)); 1489789Sahrens ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE); 1490789Sahrens 1491789Sahrens zio_set_gang_verifier(zio, &gbh->zg_tail.zbt_cksum); 1492789Sahrens 1493789Sahrens zio_checksum(ZIO_CHECKSUM_GANG_HEADER, &zc, zio->io_data, zio->io_size); 1494789Sahrens 1495789Sahrens zio_next_stage(zio); 1496789Sahrens } 1497789Sahrens 1498789Sahrens static void 1499789Sahrens zio_checksum_verify(zio_t *zio) 1500789Sahrens { 1501789Sahrens if (zio->io_bp != NULL) { 1502789Sahrens zio->io_error = zio_checksum_error(zio); 15031544Seschrock if (zio->io_error && !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) 15041544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM, 15051544Seschrock zio->io_spa, zio->io_vd, zio, 0, 0); 1506789Sahrens } 1507789Sahrens 1508789Sahrens zio_next_stage(zio); 1509789Sahrens } 1510789Sahrens 1511789Sahrens /* 1512789Sahrens * Called by RAID-Z to ensure we don't compute the checksum twice. 1513789Sahrens */ 1514789Sahrens void 1515789Sahrens zio_checksum_verified(zio_t *zio) 1516789Sahrens { 1517789Sahrens zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY); 1518789Sahrens } 1519789Sahrens 1520789Sahrens /* 1521789Sahrens * Set the external verifier for a gang block based on stuff in the bp 1522789Sahrens */ 1523789Sahrens void 1524789Sahrens zio_set_gang_verifier(zio_t *zio, zio_cksum_t *zcp) 1525789Sahrens { 15261775Sbillm blkptr_t *bp = zio->io_bp; 15271775Sbillm 15281775Sbillm zcp->zc_word[0] = DVA_GET_VDEV(BP_IDENTITY(bp)); 15291775Sbillm zcp->zc_word[1] = DVA_GET_OFFSET(BP_IDENTITY(bp)); 15301775Sbillm zcp->zc_word[2] = bp->blk_birth; 1531789Sahrens zcp->zc_word[3] = 0; 1532789Sahrens } 1533789Sahrens 1534789Sahrens /* 1535789Sahrens * ========================================================================== 1536789Sahrens * Define the pipeline 1537789Sahrens * ========================================================================== 1538789Sahrens */ 1539789Sahrens typedef void zio_pipe_stage_t(zio_t *zio); 1540789Sahrens 1541789Sahrens static void 1542789Sahrens zio_badop(zio_t *zio) 1543789Sahrens { 1544789Sahrens panic("Invalid I/O pipeline stage %u for zio %p", zio->io_stage, zio); 1545789Sahrens } 1546789Sahrens 1547789Sahrens zio_pipe_stage_t *zio_pipeline[ZIO_STAGE_DONE + 2] = { 1548789Sahrens zio_badop, 1549789Sahrens zio_wait_children_ready, 1550789Sahrens zio_write_compress, 1551789Sahrens zio_checksum_generate, 1552789Sahrens zio_gang_pipeline, 1553789Sahrens zio_get_gang_header, 1554789Sahrens zio_rewrite_gang_members, 1555789Sahrens zio_free_gang_members, 1556789Sahrens zio_claim_gang_members, 1557789Sahrens zio_dva_allocate, 1558789Sahrens zio_dva_free, 1559789Sahrens zio_dva_claim, 1560789Sahrens zio_gang_checksum_generate, 1561789Sahrens zio_ready, 1562789Sahrens zio_vdev_io_start, 1563789Sahrens zio_vdev_io_done, 1564789Sahrens zio_vdev_io_assess, 1565789Sahrens zio_wait_children_done, 1566789Sahrens zio_checksum_verify, 1567789Sahrens zio_read_gang_members, 1568789Sahrens zio_read_decompress, 1569789Sahrens zio_done, 1570789Sahrens zio_badop 1571789Sahrens }; 1572789Sahrens 1573789Sahrens /* 1574789Sahrens * Move an I/O to the next stage of the pipeline and execute that stage. 1575789Sahrens * There's no locking on io_stage because there's no legitimate way for 1576789Sahrens * multiple threads to be attempting to process the same I/O. 1577789Sahrens */ 1578789Sahrens void 1579789Sahrens zio_next_stage(zio_t *zio) 1580789Sahrens { 1581789Sahrens uint32_t pipeline = zio->io_pipeline; 1582789Sahrens 1583789Sahrens ASSERT(!MUTEX_HELD(&zio->io_lock)); 1584789Sahrens 1585789Sahrens if (zio->io_error) { 1586789Sahrens dprintf("zio %p vdev %s offset %llx stage %d error %d\n", 1587789Sahrens zio, vdev_description(zio->io_vd), 1588789Sahrens zio->io_offset, zio->io_stage, zio->io_error); 1589789Sahrens if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0) 1590789Sahrens pipeline &= ZIO_ERROR_PIPELINE_MASK; 1591789Sahrens } 1592789Sahrens 1593789Sahrens while (((1U << ++zio->io_stage) & pipeline) == 0) 1594789Sahrens continue; 1595789Sahrens 1596789Sahrens ASSERT(zio->io_stage <= ZIO_STAGE_DONE); 1597789Sahrens ASSERT(zio->io_stalled == 0); 1598789Sahrens 1599789Sahrens zio_pipeline[zio->io_stage](zio); 1600789Sahrens } 1601789Sahrens 1602789Sahrens void 1603789Sahrens zio_next_stage_async(zio_t *zio) 1604789Sahrens { 1605789Sahrens taskq_t *tq; 1606789Sahrens uint32_t pipeline = zio->io_pipeline; 1607789Sahrens 1608789Sahrens ASSERT(!MUTEX_HELD(&zio->io_lock)); 1609789Sahrens 1610789Sahrens if (zio->io_error) { 1611789Sahrens dprintf("zio %p vdev %s offset %llx stage %d error %d\n", 1612789Sahrens zio, vdev_description(zio->io_vd), 1613789Sahrens zio->io_offset, zio->io_stage, zio->io_error); 1614789Sahrens if (((1U << zio->io_stage) & ZIO_VDEV_IO_PIPELINE) == 0) 1615789Sahrens pipeline &= ZIO_ERROR_PIPELINE_MASK; 1616789Sahrens } 1617789Sahrens 1618789Sahrens while (((1U << ++zio->io_stage) & pipeline) == 0) 1619789Sahrens continue; 1620789Sahrens 1621789Sahrens ASSERT(zio->io_stage <= ZIO_STAGE_DONE); 1622789Sahrens ASSERT(zio->io_stalled == 0); 1623789Sahrens 1624789Sahrens /* 1625789Sahrens * For performance, we'll probably want two sets of task queues: 1626789Sahrens * per-CPU issue taskqs and per-CPU completion taskqs. The per-CPU 1627789Sahrens * part is for read performance: since we have to make a pass over 1628789Sahrens * the data to checksum it anyway, we want to do this on the same CPU 1629789Sahrens * that issued the read, because (assuming CPU scheduling affinity) 1630789Sahrens * that thread is probably still there. Getting this optimization 1631789Sahrens * right avoids performance-hostile cache-to-cache transfers. 1632789Sahrens * 1633789Sahrens * Note that having two sets of task queues is also necessary for 1634789Sahrens * correctness: if all of the issue threads get bogged down waiting 1635789Sahrens * for dependent reads (e.g. metaslab freelist) to complete, then 1636789Sahrens * there won't be any threads available to service I/O completion 1637789Sahrens * interrupts. 1638789Sahrens */ 1639789Sahrens if ((1U << zio->io_stage) & zio->io_async_stages) { 1640789Sahrens if (zio->io_stage < ZIO_STAGE_VDEV_IO_DONE) 1641789Sahrens tq = zio->io_spa->spa_zio_issue_taskq[zio->io_type]; 1642789Sahrens else 1643789Sahrens tq = zio->io_spa->spa_zio_intr_taskq[zio->io_type]; 1644789Sahrens (void) taskq_dispatch(tq, 1645789Sahrens (task_func_t *)zio_pipeline[zio->io_stage], zio, TQ_SLEEP); 1646789Sahrens } else { 1647789Sahrens zio_pipeline[zio->io_stage](zio); 1648789Sahrens } 1649789Sahrens } 1650789Sahrens 1651789Sahrens /* 1652789Sahrens * Try to allocate an intent log block. Return 0 on success, errno on failure. 1653789Sahrens */ 1654789Sahrens int 16551807Sbonwick zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *bp, uint64_t txg) 1656789Sahrens { 1657789Sahrens int error; 1658789Sahrens 16591544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1660789Sahrens 1661789Sahrens BP_ZERO(bp); 1662789Sahrens 16631775Sbillm error = metaslab_alloc(spa, size, bp, 1, txg, NULL); 1664789Sahrens 1665789Sahrens if (error == 0) { 1666789Sahrens BP_SET_LSIZE(bp, size); 1667789Sahrens BP_SET_PSIZE(bp, size); 1668789Sahrens BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 16691807Sbonwick BP_SET_CHECKSUM(bp, ZIO_CHECKSUM_ZILOG); 1670789Sahrens BP_SET_TYPE(bp, DMU_OT_INTENT_LOG); 1671789Sahrens BP_SET_LEVEL(bp, 0); 1672789Sahrens BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 1673789Sahrens bp->blk_birth = txg; 1674789Sahrens } 1675789Sahrens 16761544Seschrock spa_config_exit(spa, FTAG); 1677789Sahrens 1678789Sahrens return (error); 1679789Sahrens } 1680789Sahrens 1681789Sahrens /* 1682789Sahrens * Free an intent log block. We know it can't be a gang block, so there's 1683789Sahrens * nothing to do except metaslab_free() it. 1684789Sahrens */ 1685789Sahrens void 1686789Sahrens zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg) 1687789Sahrens { 16881775Sbillm ASSERT(!BP_IS_GANG(bp)); 1689789Sahrens 16901544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1691789Sahrens 16921807Sbonwick metaslab_free(spa, bp, txg, B_FALSE); 1693789Sahrens 16941544Seschrock spa_config_exit(spa, FTAG); 1695789Sahrens } 1696