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, 3172981Sahrens ZIO_TYPE_READ, priority, flags | ZIO_FLAG_USER, 3182981Sahrens ZIO_STAGE_OPEN, ZIO_READ_PIPELINE); 3191544Seschrock zio->io_bookmark = *zb; 3201544Seschrock 3211544Seschrock zio->io_logical = zio; 322789Sahrens 323789Sahrens /* 324789Sahrens * Work off our copy of the bp so the caller can free it. 325789Sahrens */ 326789Sahrens zio->io_bp = &zio->io_bp_copy; 327789Sahrens 328789Sahrens if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) { 329789Sahrens uint64_t csize = BP_GET_PSIZE(bp); 330789Sahrens void *cbuf = zio_buf_alloc(csize); 331789Sahrens 332789Sahrens zio_push_transform(zio, cbuf, csize, csize); 333789Sahrens zio->io_pipeline |= 1U << ZIO_STAGE_READ_DECOMPRESS; 334789Sahrens } 335789Sahrens 3361775Sbillm if (BP_IS_GANG(bp)) { 337789Sahrens uint64_t gsize = SPA_GANGBLOCKSIZE; 338789Sahrens void *gbuf = zio_buf_alloc(gsize); 339789Sahrens 340789Sahrens zio_push_transform(zio, gbuf, gsize, gsize); 341789Sahrens zio->io_pipeline |= 1U << ZIO_STAGE_READ_GANG_MEMBERS; 342789Sahrens } 343789Sahrens 344789Sahrens return (zio); 345789Sahrens } 346789Sahrens 347789Sahrens zio_t * 3481775Sbillm zio_write(zio_t *pio, spa_t *spa, int checksum, int compress, int ncopies, 349789Sahrens uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 3501544Seschrock zio_done_func_t *done, void *private, int priority, int flags, 3511544Seschrock zbookmark_t *zb) 352789Sahrens { 353789Sahrens zio_t *zio; 354789Sahrens 355789Sahrens ASSERT(checksum >= ZIO_CHECKSUM_OFF && 356789Sahrens checksum < ZIO_CHECKSUM_FUNCTIONS); 357789Sahrens 358789Sahrens ASSERT(compress >= ZIO_COMPRESS_OFF && 359789Sahrens compress < ZIO_COMPRESS_FUNCTIONS); 360789Sahrens 361789Sahrens zio = zio_create(pio, spa, txg, bp, data, size, done, private, 3622981Sahrens ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_USER, 363789Sahrens ZIO_STAGE_OPEN, ZIO_WRITE_PIPELINE); 364789Sahrens 3651544Seschrock zio->io_bookmark = *zb; 3661544Seschrock 3671544Seschrock zio->io_logical = zio; 3681544Seschrock 369789Sahrens zio->io_checksum = checksum; 370789Sahrens zio->io_compress = compress; 3711775Sbillm zio->io_ndvas = ncopies; 372789Sahrens 373789Sahrens if (compress != ZIO_COMPRESS_OFF) 374789Sahrens zio->io_async_stages |= 1U << ZIO_STAGE_WRITE_COMPRESS; 375789Sahrens 376789Sahrens if (bp->blk_birth != txg) { 377789Sahrens /* XXX the bp usually (always?) gets re-zeroed later */ 378789Sahrens BP_ZERO(bp); 379789Sahrens BP_SET_LSIZE(bp, size); 380789Sahrens BP_SET_PSIZE(bp, size); 3811775Sbillm } else { 3821775Sbillm /* Make sure someone doesn't change their mind on overwrites */ 3831775Sbillm ASSERT(MIN(zio->io_ndvas + BP_IS_GANG(bp), 3841775Sbillm spa_max_replication(spa)) == BP_GET_NDVAS(bp)); 385789Sahrens } 386789Sahrens 387789Sahrens return (zio); 388789Sahrens } 389789Sahrens 390789Sahrens zio_t * 391789Sahrens zio_rewrite(zio_t *pio, spa_t *spa, int checksum, 392789Sahrens uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 3931544Seschrock zio_done_func_t *done, void *private, int priority, int flags, 3941544Seschrock zbookmark_t *zb) 395789Sahrens { 396789Sahrens zio_t *zio; 397789Sahrens 398789Sahrens zio = zio_create(pio, spa, txg, bp, data, size, done, private, 3992981Sahrens ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_USER, 400789Sahrens ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE); 401789Sahrens 4021544Seschrock zio->io_bookmark = *zb; 403789Sahrens zio->io_checksum = checksum; 404789Sahrens zio->io_compress = ZIO_COMPRESS_OFF; 405789Sahrens 4061775Sbillm if (pio != NULL) 4071775Sbillm ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp)); 4081775Sbillm 409789Sahrens return (zio); 410789Sahrens } 411789Sahrens 412789Sahrens static zio_t * 413789Sahrens zio_write_allocate(zio_t *pio, spa_t *spa, int checksum, 414789Sahrens uint64_t txg, blkptr_t *bp, void *data, uint64_t size, 415789Sahrens zio_done_func_t *done, void *private, int priority, int flags) 416789Sahrens { 417789Sahrens zio_t *zio; 418789Sahrens 419789Sahrens BP_ZERO(bp); 420789Sahrens BP_SET_LSIZE(bp, size); 421789Sahrens BP_SET_PSIZE(bp, size); 422789Sahrens BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 423789Sahrens 424789Sahrens zio = zio_create(pio, spa, txg, bp, data, size, done, private, 425789Sahrens ZIO_TYPE_WRITE, priority, flags, 426789Sahrens ZIO_STAGE_OPEN, ZIO_WRITE_ALLOCATE_PIPELINE); 427789Sahrens 428789Sahrens zio->io_checksum = checksum; 429789Sahrens zio->io_compress = ZIO_COMPRESS_OFF; 430789Sahrens 431789Sahrens return (zio); 432789Sahrens } 433789Sahrens 434789Sahrens zio_t * 435789Sahrens zio_free(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 436789Sahrens zio_done_func_t *done, void *private) 437789Sahrens { 438789Sahrens zio_t *zio; 439789Sahrens 440789Sahrens ASSERT(!BP_IS_HOLE(bp)); 441789Sahrens 442789Sahrens if (txg == spa->spa_syncing_txg && 443789Sahrens spa->spa_sync_pass > zio_sync_pass.zp_defer_free) { 444789Sahrens bplist_enqueue_deferred(&spa->spa_sync_bplist, bp); 445789Sahrens return (zio_null(pio, spa, NULL, NULL, 0)); 446789Sahrens } 447789Sahrens 448789Sahrens zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private, 4492981Sahrens ZIO_TYPE_FREE, ZIO_PRIORITY_FREE, ZIO_FLAG_USER, 450789Sahrens ZIO_STAGE_OPEN, ZIO_FREE_PIPELINE); 451789Sahrens 452789Sahrens zio->io_bp = &zio->io_bp_copy; 453789Sahrens 454789Sahrens return (zio); 455789Sahrens } 456789Sahrens 457789Sahrens zio_t * 458789Sahrens zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, 459789Sahrens zio_done_func_t *done, void *private) 460789Sahrens { 461789Sahrens zio_t *zio; 462789Sahrens 463789Sahrens /* 464789Sahrens * A claim is an allocation of a specific block. Claims are needed 465789Sahrens * to support immediate writes in the intent log. The issue is that 466789Sahrens * immediate writes contain committed data, but in a txg that was 467789Sahrens * *not* committed. Upon opening the pool after an unclean shutdown, 468789Sahrens * the intent log claims all blocks that contain immediate write data 469789Sahrens * so that the SPA knows they're in use. 470789Sahrens * 471789Sahrens * All claims *must* be resolved in the first txg -- before the SPA 472789Sahrens * starts allocating blocks -- so that nothing is allocated twice. 473789Sahrens */ 474789Sahrens ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa)); 475789Sahrens ASSERT3U(spa_first_txg(spa), <=, txg); 476789Sahrens 477789Sahrens zio = zio_create(pio, spa, txg, bp, NULL, 0, done, private, 478789Sahrens ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW, 0, 479789Sahrens ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE); 480789Sahrens 481789Sahrens zio->io_bp = &zio->io_bp_copy; 482789Sahrens 483789Sahrens return (zio); 484789Sahrens } 485789Sahrens 486789Sahrens zio_t * 487789Sahrens zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd, 488789Sahrens zio_done_func_t *done, void *private, int priority, int flags) 489789Sahrens { 490789Sahrens zio_t *zio; 491789Sahrens int c; 492789Sahrens 493789Sahrens if (vd->vdev_children == 0) { 494789Sahrens zio = zio_create(pio, spa, 0, NULL, NULL, 0, done, private, 495789Sahrens ZIO_TYPE_IOCTL, priority, flags, 496789Sahrens ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE); 497789Sahrens 498789Sahrens zio->io_vd = vd; 499789Sahrens zio->io_cmd = cmd; 500789Sahrens } else { 501789Sahrens zio = zio_null(pio, spa, NULL, NULL, flags); 502789Sahrens 503789Sahrens for (c = 0; c < vd->vdev_children; c++) 504789Sahrens zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd, 505789Sahrens done, private, priority, flags)); 506789Sahrens } 507789Sahrens 508789Sahrens return (zio); 509789Sahrens } 510789Sahrens 511789Sahrens static void 512789Sahrens zio_phys_bp_init(vdev_t *vd, blkptr_t *bp, uint64_t offset, uint64_t size, 513789Sahrens int checksum) 514789Sahrens { 515789Sahrens ASSERT(vd->vdev_children == 0); 516789Sahrens 517789Sahrens ASSERT(size <= SPA_MAXBLOCKSIZE); 518789Sahrens ASSERT(P2PHASE(size, SPA_MINBLOCKSIZE) == 0); 519789Sahrens ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0); 520789Sahrens 521789Sahrens ASSERT(offset + size <= VDEV_LABEL_START_SIZE || 522789Sahrens offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE); 523789Sahrens ASSERT3U(offset + size, <=, vd->vdev_psize); 524789Sahrens 525789Sahrens BP_ZERO(bp); 526789Sahrens 527789Sahrens BP_SET_LSIZE(bp, size); 528789Sahrens BP_SET_PSIZE(bp, size); 529789Sahrens 530789Sahrens BP_SET_CHECKSUM(bp, checksum); 531789Sahrens BP_SET_COMPRESS(bp, ZIO_COMPRESS_OFF); 532789Sahrens BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); 533789Sahrens 534789Sahrens if (checksum != ZIO_CHECKSUM_OFF) 535789Sahrens ZIO_SET_CHECKSUM(&bp->blk_cksum, offset, 0, 0, 0); 536789Sahrens } 537789Sahrens 538789Sahrens zio_t * 539789Sahrens zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 540789Sahrens void *data, int checksum, zio_done_func_t *done, void *private, 541789Sahrens int priority, int flags) 542789Sahrens { 543789Sahrens zio_t *zio; 544789Sahrens blkptr_t blk; 545789Sahrens 546789Sahrens zio_phys_bp_init(vd, &blk, offset, size, checksum); 547789Sahrens 548789Sahrens zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private, 549789Sahrens ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, 550789Sahrens ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE); 551789Sahrens 552789Sahrens zio->io_vd = vd; 553789Sahrens zio->io_offset = offset; 554789Sahrens 555789Sahrens /* 556789Sahrens * Work off our copy of the bp so the caller can free it. 557789Sahrens */ 558789Sahrens zio->io_bp = &zio->io_bp_copy; 559789Sahrens 560789Sahrens return (zio); 561789Sahrens } 562789Sahrens 563789Sahrens zio_t * 564789Sahrens zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size, 565789Sahrens void *data, int checksum, zio_done_func_t *done, void *private, 566789Sahrens int priority, int flags) 567789Sahrens { 568789Sahrens zio_block_tail_t *zbt; 569789Sahrens void *wbuf; 570789Sahrens zio_t *zio; 571789Sahrens blkptr_t blk; 572789Sahrens 573789Sahrens zio_phys_bp_init(vd, &blk, offset, size, checksum); 574789Sahrens 575789Sahrens zio = zio_create(pio, vd->vdev_spa, 0, &blk, data, size, done, private, 576789Sahrens ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, 577789Sahrens ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE); 578789Sahrens 579789Sahrens zio->io_vd = vd; 580789Sahrens zio->io_offset = offset; 581789Sahrens 582789Sahrens zio->io_bp = &zio->io_bp_copy; 583789Sahrens zio->io_checksum = checksum; 584789Sahrens 585789Sahrens if (zio_checksum_table[checksum].ci_zbt) { 586789Sahrens /* 587789Sahrens * zbt checksums are necessarily destructive -- they modify 588789Sahrens * one word of the write buffer to hold the verifier/checksum. 589789Sahrens * Therefore, we must make a local copy in case the data is 590789Sahrens * being written to multiple places. 591789Sahrens */ 592789Sahrens wbuf = zio_buf_alloc(size); 593789Sahrens bcopy(data, wbuf, size); 594789Sahrens zio_push_transform(zio, wbuf, size, size); 595789Sahrens 596789Sahrens zbt = (zio_block_tail_t *)((char *)wbuf + size) - 1; 597789Sahrens zbt->zbt_cksum = blk.blk_cksum; 598789Sahrens } 599789Sahrens 600789Sahrens return (zio); 601789Sahrens } 602789Sahrens 603789Sahrens /* 604789Sahrens * Create a child I/O to do some work for us. It has no associated bp. 605789Sahrens */ 606789Sahrens zio_t * 607789Sahrens zio_vdev_child_io(zio_t *zio, blkptr_t *bp, vdev_t *vd, uint64_t offset, 608789Sahrens void *data, uint64_t size, int type, int priority, int flags, 609789Sahrens zio_done_func_t *done, void *private) 610789Sahrens { 611789Sahrens uint32_t pipeline = ZIO_VDEV_CHILD_PIPELINE; 612789Sahrens zio_t *cio; 613789Sahrens 614789Sahrens if (type == ZIO_TYPE_READ && bp != NULL) { 615789Sahrens /* 616789Sahrens * If we have the bp, then the child should perform the 617789Sahrens * checksum and the parent need not. This pushes error 618789Sahrens * detection as close to the leaves as possible and 619789Sahrens * eliminates redundant checksums in the interior nodes. 620789Sahrens */ 621789Sahrens pipeline |= 1U << ZIO_STAGE_CHECKSUM_VERIFY; 622789Sahrens zio->io_pipeline &= ~(1U << ZIO_STAGE_CHECKSUM_VERIFY); 623789Sahrens } 624789Sahrens 625789Sahrens cio = zio_create(zio, zio->io_spa, zio->io_txg, bp, data, size, 626789Sahrens done, private, type, priority, 627789Sahrens (zio->io_flags & ZIO_FLAG_VDEV_INHERIT) | ZIO_FLAG_CANFAIL | flags, 6281775Sbillm ZIO_STAGE_VDEV_IO_START - 1, pipeline); 629789Sahrens 630789Sahrens cio->io_vd = vd; 631789Sahrens cio->io_offset = offset; 632789Sahrens 633789Sahrens return (cio); 634789Sahrens } 635789Sahrens 636789Sahrens /* 637789Sahrens * ========================================================================== 638789Sahrens * Initiate I/O, either sync or async 639789Sahrens * ========================================================================== 640789Sahrens */ 641789Sahrens int 642789Sahrens zio_wait(zio_t *zio) 643789Sahrens { 644789Sahrens int error; 645789Sahrens 646789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_OPEN); 647789Sahrens 648789Sahrens zio->io_waiter = curthread; 649789Sahrens 650789Sahrens zio_next_stage_async(zio); 651789Sahrens 652789Sahrens mutex_enter(&zio->io_lock); 653789Sahrens while (zio->io_stalled != ZIO_STAGE_DONE) 654789Sahrens cv_wait(&zio->io_cv, &zio->io_lock); 655789Sahrens mutex_exit(&zio->io_lock); 656789Sahrens 657789Sahrens error = zio->io_error; 6582856Snd150628 mutex_destroy(&zio->io_lock); 659789Sahrens kmem_free(zio, sizeof (zio_t)); 660789Sahrens 661789Sahrens return (error); 662789Sahrens } 663789Sahrens 664789Sahrens void 665789Sahrens zio_nowait(zio_t *zio) 666789Sahrens { 667789Sahrens zio_next_stage_async(zio); 668789Sahrens } 669789Sahrens 670789Sahrens /* 671789Sahrens * ========================================================================== 672789Sahrens * I/O pipeline interlocks: parent/child dependency scoreboarding 673789Sahrens * ========================================================================== 674789Sahrens */ 675789Sahrens static void 676789Sahrens zio_wait_for_children(zio_t *zio, uint32_t stage, uint64_t *countp) 677789Sahrens { 678789Sahrens mutex_enter(&zio->io_lock); 679789Sahrens if (*countp == 0) { 680789Sahrens ASSERT(zio->io_stalled == 0); 681789Sahrens mutex_exit(&zio->io_lock); 682789Sahrens zio_next_stage(zio); 683789Sahrens } else { 684789Sahrens zio->io_stalled = stage; 685789Sahrens mutex_exit(&zio->io_lock); 686789Sahrens } 687789Sahrens } 688789Sahrens 689789Sahrens static void 690789Sahrens zio_notify_parent(zio_t *zio, uint32_t stage, uint64_t *countp) 691789Sahrens { 692789Sahrens zio_t *pio = zio->io_parent; 693789Sahrens 694789Sahrens mutex_enter(&pio->io_lock); 695789Sahrens if (pio->io_error == 0 && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE)) 696789Sahrens pio->io_error = zio->io_error; 697789Sahrens if (--*countp == 0 && pio->io_stalled == stage) { 698789Sahrens pio->io_stalled = 0; 699789Sahrens mutex_exit(&pio->io_lock); 700789Sahrens zio_next_stage_async(pio); 701789Sahrens } else { 702789Sahrens mutex_exit(&pio->io_lock); 703789Sahrens } 704789Sahrens } 705789Sahrens 706789Sahrens static void 707789Sahrens zio_wait_children_ready(zio_t *zio) 708789Sahrens { 709789Sahrens zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_READY, 710789Sahrens &zio->io_children_notready); 711789Sahrens } 712789Sahrens 713789Sahrens void 714789Sahrens zio_wait_children_done(zio_t *zio) 715789Sahrens { 716789Sahrens zio_wait_for_children(zio, ZIO_STAGE_WAIT_CHILDREN_DONE, 717789Sahrens &zio->io_children_notdone); 718789Sahrens } 719789Sahrens 720789Sahrens static void 721789Sahrens zio_ready(zio_t *zio) 722789Sahrens { 723789Sahrens zio_t *pio = zio->io_parent; 724789Sahrens 725789Sahrens if (pio != NULL) 726789Sahrens zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_READY, 727789Sahrens &pio->io_children_notready); 728789Sahrens 729789Sahrens if (zio->io_bp) 730789Sahrens zio->io_bp_copy = *zio->io_bp; 731789Sahrens 732789Sahrens zio_next_stage(zio); 733789Sahrens } 734789Sahrens 735789Sahrens static void 736789Sahrens zio_done(zio_t *zio) 737789Sahrens { 738789Sahrens zio_t *pio = zio->io_parent; 739789Sahrens spa_t *spa = zio->io_spa; 740789Sahrens blkptr_t *bp = zio->io_bp; 741789Sahrens vdev_t *vd = zio->io_vd; 742896Smaybee char blkbuf[BP_SPRINTF_LEN]; 743789Sahrens 744789Sahrens ASSERT(zio->io_children_notready == 0); 745789Sahrens ASSERT(zio->io_children_notdone == 0); 746789Sahrens 747789Sahrens if (bp != NULL) { 748789Sahrens ASSERT(bp->blk_pad[0] == 0); 749789Sahrens ASSERT(bp->blk_pad[1] == 0); 750789Sahrens ASSERT(bp->blk_pad[2] == 0); 751789Sahrens ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0); 752789Sahrens if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) && 7531775Sbillm !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) { 754789Sahrens ASSERT(!BP_SHOULD_BYTESWAP(bp)); 7551775Sbillm if (zio->io_ndvas != 0) 7561775Sbillm ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(bp)); 7571775Sbillm ASSERT(BP_COUNT_GANG(bp) == 0 || 7581775Sbillm (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp))); 7591775Sbillm } 760789Sahrens } 761789Sahrens 762789Sahrens if (vd != NULL) 763789Sahrens vdev_stat_update(zio); 764789Sahrens 765789Sahrens if (zio->io_error) { 7661544Seschrock /* 7671544Seschrock * If this I/O is attached to a particular vdev, 7681544Seschrock * generate an error message describing the I/O failure 7691544Seschrock * at the block level. We ignore these errors if the 7701544Seschrock * device is currently unavailable. 7711544Seschrock */ 7721732Sbonwick if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd)) 7731544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_IO, 7741732Sbonwick zio->io_spa, vd, zio, 0, 0); 775789Sahrens 7761544Seschrock if ((zio->io_error == EIO || 7771544Seschrock !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) && 7781544Seschrock zio->io_logical == zio) { 7791544Seschrock /* 7801544Seschrock * For root I/O requests, tell the SPA to log the error 7811544Seschrock * appropriately. Also, generate a logical data 7821544Seschrock * ereport. 7831544Seschrock */ 7841544Seschrock spa_log_error(zio->io_spa, zio); 7851544Seschrock 7861544Seschrock zfs_ereport_post(FM_EREPORT_ZFS_DATA, 7871544Seschrock zio->io_spa, NULL, zio, 0, 0); 7881544Seschrock } 789789Sahrens 7901544Seschrock /* 7911544Seschrock * For I/O requests that cannot fail, panic appropriately. 7921544Seschrock */ 7931544Seschrock if (!(zio->io_flags & ZIO_FLAG_CANFAIL)) { 7941544Seschrock sprintf_blkptr(blkbuf, BP_SPRINTF_LEN, 7951544Seschrock bp ? bp : &zio->io_bp_copy); 7961544Seschrock panic("ZFS: %s (%s on %s off %llx: zio %p %s): error " 7971544Seschrock "%d", zio->io_error == ECKSUM ? 7981544Seschrock "bad checksum" : "I/O failure", 7991544Seschrock zio_type_name[zio->io_type], 8001544Seschrock vdev_description(vd), 8011544Seschrock (u_longlong_t)zio->io_offset, 8021544Seschrock zio, blkbuf, zio->io_error); 8031544Seschrock } 804789Sahrens } 805789Sahrens 806789Sahrens zio_clear_transform_stack(zio); 807789Sahrens 808789Sahrens if (zio->io_done) 809789Sahrens zio->io_done(zio); 810789Sahrens 811789Sahrens ASSERT(zio->io_delegate_list == NULL); 812789Sahrens ASSERT(zio->io_delegate_next == NULL); 813789Sahrens 814789Sahrens if (pio != NULL) { 815789Sahrens zio_t *next, *prev; 816789Sahrens 817789Sahrens mutex_enter(&pio->io_lock); 818789Sahrens next = zio->io_sibling_next; 819789Sahrens prev = zio->io_sibling_prev; 820789Sahrens if (next != NULL) 821789Sahrens next->io_sibling_prev = prev; 822789Sahrens if (prev != NULL) 823789Sahrens prev->io_sibling_next = next; 824789Sahrens if (pio->io_child == zio) 825789Sahrens pio->io_child = next; 826789Sahrens mutex_exit(&pio->io_lock); 827789Sahrens 828789Sahrens zio_notify_parent(zio, ZIO_STAGE_WAIT_CHILDREN_DONE, 829789Sahrens &pio->io_children_notdone); 830789Sahrens } 831789Sahrens 832789Sahrens if (pio == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_HELD)) 8331544Seschrock spa_config_exit(spa, zio); 834789Sahrens 835789Sahrens if (zio->io_waiter != NULL) { 836789Sahrens mutex_enter(&zio->io_lock); 837789Sahrens ASSERT(zio->io_stage == ZIO_STAGE_DONE); 838789Sahrens zio->io_stalled = zio->io_stage; 839789Sahrens cv_broadcast(&zio->io_cv); 840789Sahrens mutex_exit(&zio->io_lock); 841789Sahrens } else { 842789Sahrens kmem_free(zio, sizeof (zio_t)); 843789Sahrens } 844789Sahrens } 845789Sahrens 846789Sahrens /* 847789Sahrens * ========================================================================== 848789Sahrens * Compression support 849789Sahrens * ========================================================================== 850789Sahrens */ 851789Sahrens static void 852789Sahrens zio_write_compress(zio_t *zio) 853789Sahrens { 854789Sahrens int compress = zio->io_compress; 855789Sahrens blkptr_t *bp = zio->io_bp; 856789Sahrens void *cbuf; 857789Sahrens uint64_t lsize = zio->io_size; 858789Sahrens uint64_t csize = lsize; 859789Sahrens uint64_t cbufsize = 0; 860789Sahrens int pass; 861789Sahrens 862789Sahrens if (bp->blk_birth == zio->io_txg) { 863789Sahrens /* 864789Sahrens * We're rewriting an existing block, which means we're 865789Sahrens * working on behalf of spa_sync(). For spa_sync() to 866789Sahrens * converge, it must eventually be the case that we don't 867789Sahrens * have to allocate new blocks. But compression changes 868789Sahrens * the blocksize, which forces a reallocate, and makes 869789Sahrens * convergence take longer. Therefore, after the first 870789Sahrens * few passes, stop compressing to ensure convergence. 871789Sahrens */ 872789Sahrens pass = spa_sync_pass(zio->io_spa); 873789Sahrens if (pass > zio_sync_pass.zp_dontcompress) 874789Sahrens compress = ZIO_COMPRESS_OFF; 875789Sahrens } else { 876789Sahrens ASSERT(BP_IS_HOLE(bp)); 877789Sahrens pass = 1; 878789Sahrens } 879789Sahrens 880789Sahrens if (compress != ZIO_COMPRESS_OFF) 881789Sahrens if (!zio_compress_data(compress, zio->io_data, zio->io_size, 882789Sahrens &cbuf, &csize, &cbufsize)) 883789Sahrens compress = ZIO_COMPRESS_OFF; 884789Sahrens 885789Sahrens if (compress != ZIO_COMPRESS_OFF && csize != 0) 886789Sahrens zio_push_transform(zio, cbuf, csize, cbufsize); 887789Sahrens 888789Sahrens /* 889789Sahrens * The final pass of spa_sync() must be all rewrites, but the first 890789Sahrens * few passes offer a trade-off: allocating blocks defers convergence, 891789Sahrens * but newly allocated blocks are sequential, so they can be written 892789Sahrens * to disk faster. Therefore, we allow the first few passes of 893789Sahrens * spa_sync() to reallocate new blocks, but force rewrites after that. 894789Sahrens * There should only be a handful of blocks after pass 1 in any case. 895789Sahrens */ 896789Sahrens if (bp->blk_birth == zio->io_txg && BP_GET_PSIZE(bp) == csize && 897789Sahrens pass > zio_sync_pass.zp_rewrite) { 898789Sahrens ASSERT(csize != 0); 8992885Sahrens BP_SET_LSIZE(bp, lsize); 9002885Sahrens BP_SET_COMPRESS(bp, compress); 901789Sahrens zio->io_pipeline = ZIO_REWRITE_PIPELINE; 902789Sahrens } else { 903789Sahrens if (bp->blk_birth == zio->io_txg) { 904789Sahrens ASSERT3U(BP_GET_LSIZE(bp), ==, lsize); 905789Sahrens bzero(bp, sizeof (blkptr_t)); 906789Sahrens } 907789Sahrens if (csize == 0) { 908789Sahrens BP_ZERO(bp); 909789Sahrens zio->io_pipeline = ZIO_WAIT_FOR_CHILDREN_PIPELINE; 910789Sahrens } else { 9111775Sbillm ASSERT3U(BP_GET_NDVAS(bp), ==, 0); 912789Sahrens BP_SET_LSIZE(bp, lsize); 913789Sahrens BP_SET_PSIZE(bp, csize); 914789Sahrens BP_SET_COMPRESS(bp, compress); 915789Sahrens zio->io_pipeline = ZIO_WRITE_ALLOCATE_PIPELINE; 916789Sahrens } 917789Sahrens } 918789Sahrens 919789Sahrens zio_next_stage(zio); 920789Sahrens } 921789Sahrens 922789Sahrens static void 923789Sahrens zio_read_decompress(zio_t *zio) 924789Sahrens { 925789Sahrens blkptr_t *bp = zio->io_bp; 926789Sahrens void *data; 927789Sahrens uint64_t size; 928789Sahrens uint64_t bufsize; 929789Sahrens int compress = BP_GET_COMPRESS(bp); 930789Sahrens 931789Sahrens ASSERT(compress != ZIO_COMPRESS_OFF); 932789Sahrens 933789Sahrens zio_pop_transform(zio, &data, &size, &bufsize); 934789Sahrens 935789Sahrens if (zio_decompress_data(compress, data, size, 936789Sahrens zio->io_data, zio->io_size)) 937789Sahrens zio->io_error = EIO; 938789Sahrens 939789Sahrens zio_buf_free(data, bufsize); 940789Sahrens 941789Sahrens zio_next_stage(zio); 942789Sahrens } 943789Sahrens 944789Sahrens /* 945789Sahrens * ========================================================================== 946789Sahrens * Gang block support 947789Sahrens * ========================================================================== 948789Sahrens */ 949789Sahrens static void 950789Sahrens zio_gang_pipeline(zio_t *zio) 951789Sahrens { 952789Sahrens /* 953789Sahrens * By default, the pipeline assumes that we're dealing with a gang 954789Sahrens * block. If we're not, strip out any gang-specific stages. 955789Sahrens */ 9561775Sbillm if (!BP_IS_GANG(zio->io_bp)) 957789Sahrens zio->io_pipeline &= ~ZIO_GANG_STAGES; 958789Sahrens 959789Sahrens zio_next_stage(zio); 960789Sahrens } 961789Sahrens 962789Sahrens static void 963789Sahrens zio_gang_byteswap(zio_t *zio) 964789Sahrens { 965789Sahrens ASSERT(zio->io_size == SPA_GANGBLOCKSIZE); 966789Sahrens 967789Sahrens if (BP_SHOULD_BYTESWAP(zio->io_bp)) 968789Sahrens byteswap_uint64_array(zio->io_data, zio->io_size); 969789Sahrens } 970789Sahrens 971789Sahrens static void 972789Sahrens zio_get_gang_header(zio_t *zio) 973789Sahrens { 974789Sahrens blkptr_t *bp = zio->io_bp; 975789Sahrens uint64_t gsize = SPA_GANGBLOCKSIZE; 976789Sahrens void *gbuf = zio_buf_alloc(gsize); 977789Sahrens 9781775Sbillm ASSERT(BP_IS_GANG(bp)); 979789Sahrens 980789Sahrens zio_push_transform(zio, gbuf, gsize, gsize); 981789Sahrens 982789Sahrens zio_nowait(zio_create(zio, zio->io_spa, bp->blk_birth, bp, gbuf, gsize, 983789Sahrens NULL, NULL, ZIO_TYPE_READ, zio->io_priority, 984789Sahrens zio->io_flags & ZIO_FLAG_GANG_INHERIT, 985789Sahrens ZIO_STAGE_OPEN, ZIO_READ_PIPELINE)); 986789Sahrens 987789Sahrens zio_wait_children_done(zio); 988789Sahrens } 989789Sahrens 990789Sahrens static void 991789Sahrens zio_read_gang_members(zio_t *zio) 992789Sahrens { 993789Sahrens zio_gbh_phys_t *gbh; 994789Sahrens uint64_t gsize, gbufsize, loff, lsize; 995789Sahrens int i; 996789Sahrens 9971775Sbillm ASSERT(BP_IS_GANG(zio->io_bp)); 998789Sahrens 999789Sahrens zio_gang_byteswap(zio); 1000789Sahrens zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1001789Sahrens 1002789Sahrens for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) { 1003789Sahrens blkptr_t *gbp = &gbh->zg_blkptr[i]; 1004789Sahrens lsize = BP_GET_PSIZE(gbp); 1005789Sahrens 1006789Sahrens ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF); 1007789Sahrens ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp)); 1008789Sahrens ASSERT3U(loff + lsize, <=, zio->io_size); 1009789Sahrens ASSERT(i < SPA_GBH_NBLKPTRS); 1010789Sahrens ASSERT(!BP_IS_HOLE(gbp)); 1011789Sahrens 1012789Sahrens zio_nowait(zio_read(zio, zio->io_spa, gbp, 1013789Sahrens (char *)zio->io_data + loff, lsize, NULL, NULL, 10141544Seschrock zio->io_priority, zio->io_flags & ZIO_FLAG_GANG_INHERIT, 10151544Seschrock &zio->io_bookmark)); 1016789Sahrens } 1017789Sahrens 1018789Sahrens zio_buf_free(gbh, gbufsize); 1019789Sahrens zio_wait_children_done(zio); 1020789Sahrens } 1021789Sahrens 1022789Sahrens static void 1023789Sahrens zio_rewrite_gang_members(zio_t *zio) 1024789Sahrens { 1025789Sahrens zio_gbh_phys_t *gbh; 1026789Sahrens uint64_t gsize, gbufsize, loff, lsize; 1027789Sahrens int i; 1028789Sahrens 10291775Sbillm ASSERT(BP_IS_GANG(zio->io_bp)); 1030789Sahrens ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE); 1031789Sahrens 1032789Sahrens zio_gang_byteswap(zio); 1033789Sahrens zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1034789Sahrens 1035789Sahrens ASSERT(gsize == gbufsize); 1036789Sahrens 1037789Sahrens for (loff = 0, i = 0; loff != zio->io_size; loff += lsize, i++) { 1038789Sahrens blkptr_t *gbp = &gbh->zg_blkptr[i]; 1039789Sahrens lsize = BP_GET_PSIZE(gbp); 1040789Sahrens 1041789Sahrens ASSERT(BP_GET_COMPRESS(gbp) == ZIO_COMPRESS_OFF); 1042789Sahrens ASSERT3U(lsize, ==, BP_GET_LSIZE(gbp)); 1043789Sahrens ASSERT3U(loff + lsize, <=, zio->io_size); 1044789Sahrens ASSERT(i < SPA_GBH_NBLKPTRS); 1045789Sahrens ASSERT(!BP_IS_HOLE(gbp)); 1046789Sahrens 1047789Sahrens zio_nowait(zio_rewrite(zio, zio->io_spa, zio->io_checksum, 1048789Sahrens zio->io_txg, gbp, (char *)zio->io_data + loff, lsize, 10491544Seschrock NULL, NULL, zio->io_priority, zio->io_flags, 10501544Seschrock &zio->io_bookmark)); 1051789Sahrens } 1052789Sahrens 1053789Sahrens zio_push_transform(zio, gbh, gsize, gbufsize); 1054789Sahrens zio_wait_children_ready(zio); 1055789Sahrens } 1056789Sahrens 1057789Sahrens static void 1058789Sahrens zio_free_gang_members(zio_t *zio) 1059789Sahrens { 1060789Sahrens zio_gbh_phys_t *gbh; 1061789Sahrens uint64_t gsize, gbufsize; 1062789Sahrens int i; 1063789Sahrens 10641775Sbillm ASSERT(BP_IS_GANG(zio->io_bp)); 1065789Sahrens 1066789Sahrens zio_gang_byteswap(zio); 1067789Sahrens zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1068789Sahrens 1069789Sahrens for (i = 0; i < SPA_GBH_NBLKPTRS; i++) { 1070789Sahrens blkptr_t *gbp = &gbh->zg_blkptr[i]; 1071789Sahrens 1072789Sahrens if (BP_IS_HOLE(gbp)) 1073789Sahrens continue; 1074789Sahrens zio_nowait(zio_free(zio, zio->io_spa, zio->io_txg, 1075789Sahrens gbp, NULL, NULL)); 1076789Sahrens } 1077789Sahrens 1078789Sahrens zio_buf_free(gbh, gbufsize); 1079789Sahrens zio_next_stage(zio); 1080789Sahrens } 1081789Sahrens 1082789Sahrens static void 1083789Sahrens zio_claim_gang_members(zio_t *zio) 1084789Sahrens { 1085789Sahrens zio_gbh_phys_t *gbh; 1086789Sahrens uint64_t gsize, gbufsize; 1087789Sahrens int i; 1088789Sahrens 10891775Sbillm ASSERT(BP_IS_GANG(zio->io_bp)); 1090789Sahrens 1091789Sahrens zio_gang_byteswap(zio); 1092789Sahrens zio_pop_transform(zio, (void **)&gbh, &gsize, &gbufsize); 1093789Sahrens 1094789Sahrens for (i = 0; i < SPA_GBH_NBLKPTRS; i++) { 1095789Sahrens blkptr_t *gbp = &gbh->zg_blkptr[i]; 1096789Sahrens if (BP_IS_HOLE(gbp)) 1097789Sahrens continue; 1098789Sahrens zio_nowait(zio_claim(zio, zio->io_spa, zio->io_txg, 1099789Sahrens gbp, NULL, NULL)); 1100789Sahrens } 1101789Sahrens 1102789Sahrens zio_buf_free(gbh, gbufsize); 1103789Sahrens zio_next_stage(zio); 1104789Sahrens } 1105789Sahrens 1106789Sahrens static void 1107789Sahrens zio_write_allocate_gang_member_done(zio_t *zio) 1108789Sahrens { 1109789Sahrens zio_t *pio = zio->io_parent; 11101775Sbillm dva_t *cdva = zio->io_bp->blk_dva; 11111775Sbillm dva_t *pdva = pio->io_bp->blk_dva; 1112789Sahrens uint64_t asize; 11131775Sbillm int d; 1114789Sahrens 11151775Sbillm ASSERT3U(pio->io_ndvas, ==, zio->io_ndvas); 11161775Sbillm ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp)); 11171775Sbillm ASSERT3U(zio->io_ndvas, <=, BP_GET_NDVAS(zio->io_bp)); 11181775Sbillm ASSERT3U(pio->io_ndvas, <=, BP_GET_NDVAS(pio->io_bp)); 11191775Sbillm 1120789Sahrens mutex_enter(&pio->io_lock); 11211775Sbillm for (d = 0; d < BP_GET_NDVAS(pio->io_bp); d++) { 11221775Sbillm ASSERT(DVA_GET_GANG(&pdva[d])); 11231775Sbillm asize = DVA_GET_ASIZE(&pdva[d]); 11241775Sbillm asize += DVA_GET_ASIZE(&cdva[d]); 11251775Sbillm DVA_SET_ASIZE(&pdva[d], asize); 11261775Sbillm } 1127789Sahrens mutex_exit(&pio->io_lock); 1128789Sahrens } 1129789Sahrens 1130789Sahrens static void 1131789Sahrens zio_write_allocate_gang_members(zio_t *zio) 1132789Sahrens { 1133789Sahrens blkptr_t *bp = zio->io_bp; 11341775Sbillm dva_t *dva = bp->blk_dva; 11351775Sbillm spa_t *spa = zio->io_spa; 1136789Sahrens zio_gbh_phys_t *gbh; 11371775Sbillm uint64_t txg = zio->io_txg; 1138789Sahrens uint64_t resid = zio->io_size; 1139789Sahrens uint64_t maxalloc = P2ROUNDUP(zio->io_size >> 1, SPA_MINBLOCKSIZE); 1140789Sahrens uint64_t gsize, loff, lsize; 1141789Sahrens uint32_t gbps_left; 11421775Sbillm int ndvas = zio->io_ndvas; 11431775Sbillm int gbh_ndvas = MIN(ndvas + 1, spa_max_replication(spa)); 1144789Sahrens int error; 11451775Sbillm int i, d; 1146789Sahrens 1147789Sahrens gsize = SPA_GANGBLOCKSIZE; 1148789Sahrens gbps_left = SPA_GBH_NBLKPTRS; 1149789Sahrens 1150*3063Sperrin error = metaslab_alloc(spa, gsize, bp, gbh_ndvas, txg, NULL, B_FALSE); 1151789Sahrens if (error == ENOSPC) 1152789Sahrens panic("can't allocate gang block header"); 1153789Sahrens ASSERT(error == 0); 1154789Sahrens 11551775Sbillm for (d = 0; d < gbh_ndvas; d++) 11561775Sbillm DVA_SET_GANG(&dva[d], 1); 1157789Sahrens 11581775Sbillm bp->blk_birth = txg; 1159789Sahrens 1160789Sahrens gbh = zio_buf_alloc(gsize); 1161789Sahrens bzero(gbh, gsize); 1162789Sahrens 11631775Sbillm /* We need to test multi-level gang blocks */ 11641775Sbillm if (maxalloc >= zio_gang_bang && (lbolt & 0x1) == 0) 11651775Sbillm maxalloc = MAX(maxalloc >> 2, SPA_MINBLOCKSIZE); 11661775Sbillm 1167789Sahrens for (loff = 0, i = 0; loff != zio->io_size; 1168789Sahrens loff += lsize, resid -= lsize, gbps_left--, i++) { 1169789Sahrens blkptr_t *gbp = &gbh->zg_blkptr[i]; 11701775Sbillm dva = gbp->blk_dva; 1171789Sahrens 1172789Sahrens ASSERT(gbps_left != 0); 1173789Sahrens maxalloc = MIN(maxalloc, resid); 1174789Sahrens 1175789Sahrens while (resid <= maxalloc * gbps_left) { 11761775Sbillm error = metaslab_alloc(spa, maxalloc, gbp, ndvas, 1177*3063Sperrin txg, bp, B_FALSE); 1178789Sahrens if (error == 0) 1179789Sahrens break; 1180789Sahrens ASSERT3U(error, ==, ENOSPC); 1181789Sahrens if (maxalloc == SPA_MINBLOCKSIZE) 1182789Sahrens panic("really out of space"); 1183789Sahrens maxalloc = P2ROUNDUP(maxalloc >> 1, SPA_MINBLOCKSIZE); 1184789Sahrens } 1185789Sahrens 1186789Sahrens if (resid <= maxalloc * gbps_left) { 1187789Sahrens lsize = maxalloc; 1188789Sahrens BP_SET_LSIZE(gbp, lsize); 1189789Sahrens BP_SET_PSIZE(gbp, lsize); 1190789Sahrens BP_SET_COMPRESS(gbp, ZIO_COMPRESS_OFF); 11911775Sbillm gbp->blk_birth = txg; 11921775Sbillm zio_nowait(zio_rewrite(zio, spa, 11931775Sbillm zio->io_checksum, txg, gbp, 1194789Sahrens (char *)zio->io_data + loff, lsize, 1195789Sahrens zio_write_allocate_gang_member_done, NULL, 11961544Seschrock zio->io_priority, zio->io_flags, 11971544Seschrock &zio->io_bookmark)); 1198789Sahrens } else { 1199789Sahrens lsize = P2ROUNDUP(resid / gbps_left, SPA_MINBLOCKSIZE); 1200789Sahrens ASSERT(lsize != SPA_MINBLOCKSIZE); 12011775Sbillm zio_nowait(zio_write_allocate(zio, spa, 12021775Sbillm zio->io_checksum, txg, gbp, 1203789Sahrens (char *)zio->io_data + loff, lsize, 1204789Sahrens zio_write_allocate_gang_member_done, NULL, 1205789Sahrens zio->io_priority, zio->io_flags)); 1206789Sahrens } 1207789Sahrens } 1208789Sahrens 1209789Sahrens ASSERT(resid == 0 && loff == zio->io_size); 1210789Sahrens 1211789Sahrens zio->io_pipeline |= 1U << ZIO_STAGE_GANG_CHECKSUM_GENERATE; 1212789Sahrens 1213789Sahrens zio_push_transform(zio, gbh, gsize, gsize); 12141775Sbillm /* 12151775Sbillm * As much as we'd like this to be zio_wait_children_ready(), 12161775Sbillm * updating our ASIZE doesn't happen until the io_done callback, 12171775Sbillm * so we have to wait for that to finish in order for our BP 12181775Sbillm * to be stable. 12191775Sbillm */ 1220789Sahrens zio_wait_children_done(zio); 1221789Sahrens } 1222789Sahrens 1223789Sahrens /* 1224789Sahrens * ========================================================================== 1225789Sahrens * Allocate and free blocks 1226789Sahrens * ========================================================================== 1227789Sahrens */ 1228789Sahrens static void 1229789Sahrens zio_dva_allocate(zio_t *zio) 1230789Sahrens { 1231789Sahrens blkptr_t *bp = zio->io_bp; 1232789Sahrens int error; 1233789Sahrens 1234789Sahrens ASSERT(BP_IS_HOLE(bp)); 12351775Sbillm ASSERT3U(BP_GET_NDVAS(bp), ==, 0); 12361775Sbillm ASSERT3U(zio->io_ndvas, >, 0); 12371775Sbillm ASSERT3U(zio->io_ndvas, <=, spa_max_replication(zio->io_spa)); 1238789Sahrens 1239789Sahrens /* For testing, make some blocks above a certain size be gang blocks */ 1240789Sahrens if (zio->io_size >= zio_gang_bang && (lbolt & 0x3) == 0) { 1241789Sahrens zio_write_allocate_gang_members(zio); 1242789Sahrens return; 1243789Sahrens } 1244789Sahrens 1245789Sahrens ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp)); 1246789Sahrens 12471775Sbillm error = metaslab_alloc(zio->io_spa, zio->io_size, bp, zio->io_ndvas, 1248*3063Sperrin zio->io_txg, NULL, B_FALSE); 1249789Sahrens 1250789Sahrens if (error == 0) { 1251789Sahrens bp->blk_birth = zio->io_txg; 1252789Sahrens } else if (error == ENOSPC) { 1253789Sahrens if (zio->io_size == SPA_MINBLOCKSIZE) 1254789Sahrens panic("really, truly out of space"); 1255789Sahrens zio_write_allocate_gang_members(zio); 1256789Sahrens return; 1257789Sahrens } else { 1258789Sahrens zio->io_error = error; 1259789Sahrens } 1260789Sahrens zio_next_stage(zio); 1261789Sahrens } 1262789Sahrens 1263789Sahrens static void 1264789Sahrens zio_dva_free(zio_t *zio) 1265789Sahrens { 1266789Sahrens blkptr_t *bp = zio->io_bp; 1267789Sahrens 12681807Sbonwick metaslab_free(zio->io_spa, bp, zio->io_txg, B_FALSE); 1269789Sahrens 1270789Sahrens BP_ZERO(bp); 1271789Sahrens 1272789Sahrens zio_next_stage(zio); 1273789Sahrens } 1274789Sahrens 1275789Sahrens static void 1276789Sahrens zio_dva_claim(zio_t *zio) 1277789Sahrens { 12781807Sbonwick zio->io_error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg); 1279789Sahrens 1280789Sahrens zio_next_stage(zio); 1281789Sahrens } 1282789Sahrens 1283789Sahrens /* 1284789Sahrens * ========================================================================== 1285789Sahrens * Read and write to physical devices 1286789Sahrens * ========================================================================== 1287789Sahrens */ 1288789Sahrens 1289789Sahrens static void 12901775Sbillm zio_vdev_io_start(zio_t *zio) 1291789Sahrens { 1292789Sahrens vdev_t *vd = zio->io_vd; 12931775Sbillm vdev_t *tvd = vd ? vd->vdev_top : NULL; 12941775Sbillm blkptr_t *bp = zio->io_bp; 12951775Sbillm uint64_t align; 1296789Sahrens 12971775Sbillm if (vd == NULL) { 12981775Sbillm /* The mirror_ops handle multiple DVAs in a single BP */ 12991775Sbillm vdev_mirror_ops.vdev_op_io_start(zio); 13001775Sbillm return; 13011775Sbillm } 13021775Sbillm 13031775Sbillm align = 1ULL << tvd->vdev_ashift; 13041775Sbillm 13051732Sbonwick if (zio->io_retries == 0 && vd == tvd) 1306789Sahrens zio->io_flags |= ZIO_FLAG_FAILFAST; 1307789Sahrens 13081775Sbillm if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) && 13091775Sbillm vd->vdev_children == 0) { 1310789Sahrens zio->io_flags |= ZIO_FLAG_PHYSICAL; 1311789Sahrens zio->io_offset += VDEV_LABEL_START_SIZE; 1312789Sahrens } 1313789Sahrens 13141732Sbonwick if (P2PHASE(zio->io_size, align) != 0) { 13151732Sbonwick uint64_t asize = P2ROUNDUP(zio->io_size, align); 13161732Sbonwick char *abuf = zio_buf_alloc(asize); 13171732Sbonwick ASSERT(vd == tvd); 13181732Sbonwick if (zio->io_type == ZIO_TYPE_WRITE) { 13191732Sbonwick bcopy(zio->io_data, abuf, zio->io_size); 13201732Sbonwick bzero(abuf + zio->io_size, asize - zio->io_size); 13211732Sbonwick } 13221732Sbonwick zio_push_transform(zio, abuf, asize, asize); 13231732Sbonwick ASSERT(!(zio->io_flags & ZIO_FLAG_SUBBLOCK)); 13241732Sbonwick zio->io_flags |= ZIO_FLAG_SUBBLOCK; 13251732Sbonwick } 13261732Sbonwick 13271732Sbonwick ASSERT(P2PHASE(zio->io_offset, align) == 0); 13281732Sbonwick ASSERT(P2PHASE(zio->io_size, align) == 0); 13291732Sbonwick ASSERT(bp == NULL || 13301732Sbonwick P2ROUNDUP(ZIO_GET_IOSIZE(zio), align) == zio->io_size); 1331789Sahrens ASSERT(zio->io_type != ZIO_TYPE_WRITE || (spa_mode & FWRITE)); 1332789Sahrens 1333789Sahrens vdev_io_start(zio); 1334789Sahrens 1335789Sahrens /* zio_next_stage_async() gets called from io completion interrupt */ 1336789Sahrens } 1337789Sahrens 1338789Sahrens static void 1339789Sahrens zio_vdev_io_done(zio_t *zio) 1340789Sahrens { 13411775Sbillm if (zio->io_vd == NULL) 13421775Sbillm /* The mirror_ops handle multiple DVAs in a single BP */ 13431775Sbillm vdev_mirror_ops.vdev_op_io_done(zio); 13441775Sbillm else 13451775Sbillm vdev_io_done(zio); 1346789Sahrens } 1347789Sahrens 1348789Sahrens /* XXPOLICY */ 13491544Seschrock boolean_t 1350789Sahrens zio_should_retry(zio_t *zio) 1351789Sahrens { 1352789Sahrens vdev_t *vd = zio->io_vd; 1353789Sahrens 1354789Sahrens if (zio->io_error == 0) 1355789Sahrens return (B_FALSE); 1356789Sahrens if (zio->io_delegate_list != NULL) 1357789Sahrens return (B_FALSE); 13581775Sbillm if (vd && vd != vd->vdev_top) 1359789Sahrens return (B_FALSE); 1360789Sahrens if (zio->io_flags & ZIO_FLAG_DONT_RETRY) 1361789Sahrens return (B_FALSE); 13621544Seschrock if (zio->io_retries > 0) 1363789Sahrens return (B_FALSE); 1364789Sahrens 1365789Sahrens return (B_TRUE); 1366789Sahrens } 1367789Sahrens 1368789Sahrens static void 1369789Sahrens zio_vdev_io_assess(zio_t *zio) 1370789Sahrens { 1371789Sahrens vdev_t *vd = zio->io_vd; 13721775Sbillm vdev_t *tvd = vd ? vd->vdev_top : NULL; 1373789Sahrens 13741544Seschrock ASSERT(zio->io_vsd == NULL); 1375789Sahrens 13761732Sbonwick if (zio->io_flags & ZIO_FLAG_SUBBLOCK) { 13771732Sbonwick void *abuf; 13781732Sbonwick uint64_t asize; 13791732Sbonwick ASSERT(vd == tvd); 13801732Sbonwick zio_pop_transform(zio, &abuf, &asize, &asize); 13811732Sbonwick if (zio->io_type == ZIO_TYPE_READ) 13821732Sbonwick bcopy(abuf, zio->io_data, zio->io_size); 13831732Sbonwick zio_buf_free(abuf, asize); 13841732Sbonwick zio->io_flags &= ~ZIO_FLAG_SUBBLOCK; 13851732Sbonwick } 13861732Sbonwick 13871544Seschrock if (zio_injection_enabled && !zio->io_error) 13881544Seschrock zio->io_error = zio_handle_fault_injection(zio, EIO); 1389789Sahrens 1390789Sahrens /* 1391789Sahrens * If the I/O failed, determine whether we should attempt to retry it. 1392789Sahrens */ 1393789Sahrens /* XXPOLICY */ 1394789Sahrens if (zio_should_retry(zio)) { 1395789Sahrens ASSERT(tvd == vd); 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*3063Sperrin zio_alloc_blk(spa_t *spa, uint64_t size, blkptr_t *new_bp, blkptr_t *old_bp, 1657*3063Sperrin uint64_t txg) 1658789Sahrens { 1659789Sahrens int error; 1660789Sahrens 16611544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1662789Sahrens 1663*3063Sperrin /* 1664*3063Sperrin * We were passed the previous log blocks dva_t in bp->blk_dva[0]. 1665*3063Sperrin */ 1666*3063Sperrin error = metaslab_alloc(spa, size, new_bp, 1, txg, old_bp, B_TRUE); 1667789Sahrens 1668789Sahrens if (error == 0) { 1669*3063Sperrin BP_SET_LSIZE(new_bp, size); 1670*3063Sperrin BP_SET_PSIZE(new_bp, size); 1671*3063Sperrin BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF); 1672*3063Sperrin BP_SET_CHECKSUM(new_bp, ZIO_CHECKSUM_ZILOG); 1673*3063Sperrin BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG); 1674*3063Sperrin BP_SET_LEVEL(new_bp, 0); 1675*3063Sperrin BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER); 1676*3063Sperrin new_bp->blk_birth = txg; 1677789Sahrens } 1678789Sahrens 16791544Seschrock spa_config_exit(spa, FTAG); 1680789Sahrens 1681789Sahrens return (error); 1682789Sahrens } 1683789Sahrens 1684789Sahrens /* 1685789Sahrens * Free an intent log block. We know it can't be a gang block, so there's 1686789Sahrens * nothing to do except metaslab_free() it. 1687789Sahrens */ 1688789Sahrens void 1689789Sahrens zio_free_blk(spa_t *spa, blkptr_t *bp, uint64_t txg) 1690789Sahrens { 16911775Sbillm ASSERT(!BP_IS_GANG(bp)); 1692789Sahrens 16931544Seschrock spa_config_enter(spa, RW_READER, FTAG); 1694789Sahrens 16951807Sbonwick metaslab_free(spa, bp, txg, B_FALSE); 1696789Sahrens 16971544Seschrock spa_config_exit(spa, FTAG); 1698789Sahrens } 1699