Print this page
NEX-20218 Backport Illumos #9474 txg_kick() fails to see that we are quiescing, forcing transactions to their next stages without leaving them accumulate changes
MFV illumos-gate@fa41d87de9ec9000964c605eb01d6dc19e4a1abe
    9464 txg_kick() fails to see that we are quiescing, forcing transactions to their next stages without leaving them accumulate changes
    Reviewed by: Matt Ahrens <matt@delphix.com>
    Reviewed by: Brad Lewis <brad.lewis@delphix.com>
    Reviewed by: Andriy Gapon <avg@FreeBSD.org>
    Approved by: Dan McDonald <danmcd@joyent.com>
NEX-20208 Backport Illumos #9993 zil writes can get delayed in zio pipeline
MFV illumos-gate@2258ad0b755b24a55c6173b1e6bb6188389f72dd
    9993 zil writes can get delayed in zio pipeline
    Reviewed by: Prakash Surya <prakash.surya@delphix.com>
    Reviewed by: Brad Lewis <brad.lewis@delphix.com>
    Reviewed by: Matt Ahrens <matt@delphix.com>
    Approved by: Dan McDonald <danmcd@joyent.com>
NEX-9552 zfs_scan_idle throttling harms performance and needs to be removed
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
NEX-15067 KRRP: system panics during ZFS-receive: assertion failed: arc_can_share(hdr, buf)
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
NEX-15067 KRRP: system panics during ZFS-receive: assertion failed: arc_can_share(hdr, buf)
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
NEX-14571 remove isal support remnants
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
NEX-13140 DVA-throttle support for special-class
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
NEX-9752 backport illumos 6950 ARC should cache compressed data
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
6950 ARC should cache compressed data
Reviewed by: Prakash Surya <prakash.surya@delphix.com>
Reviewed by: Dan Kimmel <dan.kimmel@delphix.com>
Reviewed by: Matt Ahrens <mahrens@delphix.com>
Reviewed by: Paul Dagnelie <pcd@delphix.com>
Reviewed by: Don Brady <don.brady@intel.com>
Reviewed by: Richard Elling <Richard.Elling@RichardElling.com>
Approved by: Richard Lowe <richlowe@richlowe.net>
NEX-6088 ZFS scrub/resilver take excessively long due to issuing lots of random IO
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
NEX-8065 ZFS doesn't notice when disk vdevs have no write cache
Reviewed by: Dan Fields <dan.fields@nexenta.com>
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
NEX-5856 ddt_capped isn't reset when deduped dataset is destroyed
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
NEX-5795 Rename 'wrc' as 'wbc' in the source and in the tech docs
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
NEX-5367 special vdev: sync-write options (NEW)
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
NEX-5318 Cleanup specialclass property (obsolete, not used) and fix related meta-to-special case
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
NEX-5188 Removed special-vdev causes panic on read or on get size of special-bp
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
NEX-5058 WBC: Race between the purging of window and opening new one
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
NEX-2830 ZFS smart compression
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
NEX-4794 Write Back Cache sync and async writes: adjust routing according to watermark limits
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
NEX-4619 Want kstats to monitor TRIM and UNMAP operation
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Hans Rosenfeld <hans.rosenfeld@nexenta.com>
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
6328 Fix cstyle errors in zfs codebase (fix studio)
6328 Fix cstyle errors in zfs codebase
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: Alex Reece <alex@delphix.com>
Reviewed by: Richard Elling <Richard.Elling@RichardElling.com>
Reviewed by: Jorgen Lundman <lundman@lundman.net>
Approved by: Robert Mustacchi <rm@joyent.com>
4185 add new cryptographic checksums to ZFS: SHA-512, Skein, Edon-R (fix studio build)
4185 add new cryptographic checksums to ZFS: SHA-512, Skein, Edon-R
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Prakash Surya <prakash.surya@delphix.com>
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
Reviewed by: Richard Lowe <richlowe@richlowe.net>
Approved by: Garrett D'Amore <garrett@damore.org>
NEX-4582 update wrc test cases for allow to use write back cache per tree of datasets
Reviewed by: Steve Peng <steve.peng@nexenta.com>
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
5960 zfs recv should prefetch indirect blocks
5925 zfs receive -o origin=
Reviewed by: Prakash Surya <prakash.surya@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
5438 zfs_blkptr_verify should continue after zfs_panic_recover
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: George Wilson <george@delphix.com>
Reviewed by: Xin LI <delphij@freebsd.org>
Approved by: Dan McDonald <danmcd@omniti.com>
5818 zfs {ref}compressratio is incorrect with 4k sector size
Reviewed by: Alex Reece <alex@delphix.com>
Reviewed by: George Wilson <george@delphix.com>
Reviewed by: Richard Elling <richard.elling@richardelling.com>
Reviewed by: Steven Hartland <killing@multiplay.co.uk>
Reviewed by: Don Brady <dev.fs.zfs@gmail.com>
Approved by: Albert Lee <trisk@omniti.com>
NEX-3502 dedup ceiling should set a pool prop when cap is in effect
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
NEX-3984 On-demand TRIM
Reviewed by: Alek Pinchuk <alek@nexenta.com>
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
Conflicts:
        usr/src/common/zfs/zpool_prop.c
        usr/src/uts/common/sys/fs/zfs.h
NEX-4003 WRC: System panics on debug build
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
NEX-3558 KRRP Integration
NEX-3508 CLONE - Port NEX-2946 Add UNMAP/TRIM functionality to ZFS and illumos
Reviewed by: Josef Sipek <josef.sipek@nexenta.com>
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Conflicts:
    usr/src/uts/common/io/scsi/targets/sd.c
    usr/src/uts/common/sys/scsi/targets/sddef.h
NEX-3411 Removal of small l2arc ddt vdev disables dedup despite enough RAM
Reviewed by: Kirill Davydychev <kirill.davydychev@nexenta.com>
Reviewed by: Tony Nguyen <tony.nguyen@nexenta.com>
NEX-3300 ddt byte count ceiling tunables should not depend on zfs_ddt_limit_type being set
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
NEX-3165 need some dedup improvements
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
4370 avoid transmitting holes during zfs send
4371 DMU code clean up
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Approved by: Garrett D'Amore <garrett@damore.org>
NEX-1110 Odd zpool Latency Output
OS-70 remove zio timer code
Moved closed ZFS files to open repo, changed Makefiles accordingly
Removed unneeded weak symbols
Support for secondarycache=data option
Align mutex tables in arc.c and dbuf.c to 64 bytes (cache line), place each kmutex_t on cache line by itself to avoid false sharing
Fixup merge results
re #13989 port of illumos-3805
3805 arc shouldn't cache freed blocks
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Reviewed by: Richard Elling <richard.elling@dey-sys.com>
Reviewed by: Will Andrews <will@firepipe.net>
Approved by: Dan McDonald <danmcd@nexenta.com>
SUP-504 Multiple disks being falsely failed/retired by new zio_timeout handling code
re #12770 rb4121 zio latency reports can produce false positives
re #12645 rb4073 Make vdev delay simulator independent of DEBUG
re #12643 rb4064 ZFS meta refactoring - vdev utilization tracking, auto-dedup
re #12616 rb4051 zfs_log_write()/dmu_sync() write once to special refactoring
re #8279 rb3915 need a mechanism to notify NMS about ZFS config changes (fix lint -courtesy of Yuri Pankov)
re #12584 rb4049 zfsxx latest code merge (fix lint - courtesy of Yuri Pankov)
re #12585 rb4049 ZFS++ work port - refactoring to improve separation of open/closed code, bug fixes, performance improvements - open code
re #12393 rb3935 Kerberos and smbd disagree about who is our AD server (fix elf runtime attributes check)
re #11612 rb3907 Failing vdev of a mirrored pool should not take zfs operations out of action for extended periods of time.
re #8346 rb2639 KT disk failures
Bug 11205: add missing libzfs_closed_stubs.c to fix opensource-only build.
ZFS plus work: special vdevs, cos, cos/vdev properties
   1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */

  21 /*
  22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
  24  * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
  25  * Copyright (c) 2014 Integros [integros.com]

  26  */
  27 
  28 #include <sys/sysmacros.h>
  29 #include <sys/zfs_context.h>
  30 #include <sys/fm/fs/zfs.h>
  31 #include <sys/spa.h>
  32 #include <sys/txg.h>
  33 #include <sys/spa_impl.h>
  34 #include <sys/vdev_impl.h>
  35 #include <sys/zio_impl.h>
  36 #include <sys/zio_compress.h>
  37 #include <sys/zio_checksum.h>
  38 #include <sys/dmu_objset.h>
  39 #include <sys/arc.h>
  40 #include <sys/ddt.h>
  41 #include <sys/blkptr.h>


  42 #include <sys/zfeature.h>



  43 #include <sys/metaslab_impl.h>
  44 #include <sys/abd.h>
  45 


  46 /*
  47  * ==========================================================================
  48  * I/O type descriptions
  49  * ==========================================================================
  50  */
  51 const char *zio_type_name[ZIO_TYPES] = {
  52         "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
  53         "zio_ioctl"
  54 };
  55 
  56 boolean_t zio_dva_throttle_enabled = B_TRUE;
  57 
  58 /*
  59  * ==========================================================================
  60  * I/O kmem caches
  61  * ==========================================================================
  62  */
  63 kmem_cache_t *zio_cache;
  64 kmem_cache_t *zio_link_cache;
  65 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
  66 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
  67 
  68 #ifdef _KERNEL
  69 extern vmem_t *zio_alloc_arena;
  70 #endif
  71 
  72 #define ZIO_PIPELINE_CONTINUE           0x100
  73 #define ZIO_PIPELINE_STOP               0x101
  74 
  75 #define BP_SPANB(indblkshift, level) \
  76         (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
  77 #define COMPARE_META_LEVEL      0x80000000ul

  78 /*
  79  * The following actions directly effect the spa's sync-to-convergence logic.
  80  * The values below define the sync pass when we start performing the action.
  81  * Care should be taken when changing these values as they directly impact
  82  * spa_sync() performance. Tuning these values may introduce subtle performance
  83  * pathologies and should only be done in the context of performance analysis.
  84  * These tunables will eventually be removed and replaced with #defines once
  85  * enough analysis has been done to determine optimal values.
  86  *
  87  * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
  88  * regular blocks are not deferred.
  89  */
  90 int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
  91 int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
  92 int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
  93 
  94 /*
  95  * An allocating zio is one that either currently has the DVA allocate
  96  * stage set or will have it later in its lifetime.
  97  */
  98 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
  99 
 100 boolean_t       zio_requeue_io_start_cut_in_line = B_TRUE;
 101 
 102 #ifdef ZFS_DEBUG
 103 int zio_buf_debug_limit = 16384;
 104 #else
 105 int zio_buf_debug_limit = 0;
 106 #endif
 107 















 108 static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
 109 
 110 void
 111 zio_init(void)
 112 {
 113         size_t c;
 114         vmem_t *data_alloc_arena = NULL;
 115 
 116 #ifdef _KERNEL
 117         data_alloc_arena = zio_alloc_arena;
 118 #endif
 119         zio_cache = kmem_cache_create("zio_cache",
 120             sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
 121         zio_link_cache = kmem_cache_create("zio_link_cache",
 122             sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
 123 
 124         /*
 125          * For small buffers, we want a cache for each multiple of
 126          * SPA_MINBLOCKSIZE.  For larger buffers, we want a cache
 127          * for each quarter-power of 2.


 163                          * stored with the buffers.
 164                          */
 165                         (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
 166                         zio_data_buf_cache[c] = kmem_cache_create(name, size,
 167                             align, NULL, NULL, NULL, NULL, data_alloc_arena,
 168                             cflags | KMC_NOTOUCH);
 169                 }
 170         }
 171 
 172         while (--c != 0) {
 173                 ASSERT(zio_buf_cache[c] != NULL);
 174                 if (zio_buf_cache[c - 1] == NULL)
 175                         zio_buf_cache[c - 1] = zio_buf_cache[c];
 176 
 177                 ASSERT(zio_data_buf_cache[c] != NULL);
 178                 if (zio_data_buf_cache[c - 1] == NULL)
 179                         zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
 180         }
 181 
 182         zio_inject_init();

 183 }
 184 
 185 void
 186 zio_fini(void)
 187 {
 188         size_t c;
 189         kmem_cache_t *last_cache = NULL;
 190         kmem_cache_t *last_data_cache = NULL;
 191 
 192         for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
 193                 if (zio_buf_cache[c] != last_cache) {
 194                         last_cache = zio_buf_cache[c];
 195                         kmem_cache_destroy(zio_buf_cache[c]);
 196                 }
 197                 zio_buf_cache[c] = NULL;
 198 
 199                 if (zio_data_buf_cache[c] != last_data_cache) {
 200                         last_data_cache = zio_data_buf_cache[c];
 201                         kmem_cache_destroy(zio_data_buf_cache[c]);
 202                 }


 425 {
 426         ASSERT(zl->zl_parent == pio);
 427         ASSERT(zl->zl_child == cio);
 428 
 429         mutex_enter(&cio->io_lock);
 430         mutex_enter(&pio->io_lock);
 431 
 432         list_remove(&pio->io_child_list, zl);
 433         list_remove(&cio->io_parent_list, zl);
 434 
 435         pio->io_child_count--;
 436         cio->io_parent_count--;
 437 
 438         mutex_exit(&pio->io_lock);
 439         mutex_exit(&cio->io_lock);
 440 
 441         kmem_cache_free(zio_link_cache, zl);
 442 }
 443 
 444 static boolean_t
 445 zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait)
 446 {

 447         boolean_t waiting = B_FALSE;
 448 
 449         mutex_enter(&zio->io_lock);
 450         ASSERT(zio->io_stall == NULL);
 451         for (int c = 0; c < ZIO_CHILD_TYPES; c++) {
 452                 if (!(ZIO_CHILD_BIT_IS_SET(childbits, c)))
 453                         continue;
 454 
 455                 uint64_t *countp = &zio->io_children[c][wait];
 456                 if (*countp != 0) {
 457                         zio->io_stage >>= 1;
 458                         ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
 459                         zio->io_stall = countp;
 460                         waiting = B_TRUE;
 461                         break;
 462                 }
 463         }
 464         mutex_exit(&zio->io_lock);

 465         return (waiting);
 466 }
 467 
 468 static void
 469 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
 470 {
 471         uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
 472         int *errorp = &pio->io_child_error[zio->io_child_type];
 473 
 474         mutex_enter(&pio->io_lock);
 475         if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
 476                 *errorp = zio_worst_error(*errorp, zio->io_error);
 477         pio->io_reexecute |= zio->io_reexecute;
 478         ASSERT3U(*countp, >, 0);
 479 
 480         (*countp)--;
 481 
 482         if (*countp == 0 && pio->io_stall == countp) {
 483                 zio_taskq_type_t type =
 484                     pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :


 602         zio->io_private = private;
 603         zio->io_type = type;
 604         zio->io_priority = priority;
 605         zio->io_vd = vd;
 606         zio->io_offset = offset;
 607         zio->io_orig_abd = zio->io_abd = data;
 608         zio->io_orig_size = zio->io_size = psize;
 609         zio->io_lsize = lsize;
 610         zio->io_orig_flags = zio->io_flags = flags;
 611         zio->io_orig_stage = zio->io_stage = stage;
 612         zio->io_orig_pipeline = zio->io_pipeline = pipeline;
 613         zio->io_pipeline_trace = ZIO_STAGE_OPEN;
 614 
 615         zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
 616         zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
 617 
 618         if (zb != NULL)
 619                 zio->io_bookmark = *zb;
 620 
 621         if (pio != NULL) {

 622                 if (zio->io_logical == NULL)
 623                         zio->io_logical = pio->io_logical;
 624                 if (zio->io_child_type == ZIO_CHILD_GANG)
 625                         zio->io_gang_leader = pio->io_gang_leader;
 626                 zio_add_child(pio, zio);




 627         }
 628 
 629         return (zio);
 630 }
 631 
 632 static void
 633 zio_destroy(zio_t *zio)
 634 {
 635         metaslab_trace_fini(&zio->io_alloc_list);
 636         list_destroy(&zio->io_parent_list);
 637         list_destroy(&zio->io_child_list);
 638         mutex_destroy(&zio->io_lock);
 639         cv_destroy(&zio->io_cv);
 640         kmem_cache_free(zio_cache, zio);
 641 }
 642 
 643 zio_t *
 644 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
 645     void *private, enum zio_flag flags)
 646 {
 647         zio_t *zio;
 648 
 649         zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
 650             ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
 651             ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
 652 
 653         return (zio);
 654 }
 655 
 656 zio_t *
 657 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
 658 {
 659         return (zio_null(NULL, spa, NULL, done, private, flags));
 660 }
 661 
 662 void
 663 zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
 664 {








 665         if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
 666                 zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
 667                     bp, (longlong_t)BP_GET_TYPE(bp));
 668         }
 669         if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
 670             BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
 671                 zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
 672                     bp, (longlong_t)BP_GET_CHECKSUM(bp));
 673         }
 674         if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
 675             BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
 676                 zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
 677                     bp, (longlong_t)BP_GET_COMPRESS(bp));
 678         }
 679         if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
 680                 zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
 681                     bp, (longlong_t)BP_GET_LSIZE(bp));
 682         }
 683         if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
 684                 zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
 685                     bp, (longlong_t)BP_GET_PSIZE(bp));
 686         }
 687 
 688         if (BP_IS_EMBEDDED(bp)) {
 689                 if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
 690                         zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
 691                             bp, (longlong_t)BPE_GET_ETYPE(bp));
 692                 }
 693         }
 694 
 695         /*
 696          * Do not verify individual DVAs if the config is not trusted. This
 697          * will be done once the zio is executed in vdev_mirror_map_alloc.
 698          */
 699         if (!spa->spa_trust_config)
 700                 return;
 701 
 702         /*
 703          * Pool-specific checks.
 704          *
 705          * Note: it would be nice to verify that the blk_birth and
 706          * BP_PHYSICAL_BIRTH() are not too large.  However, spa_freeze()
 707          * allows the birth time of log blocks (and dmu_sync()-ed blocks
 708          * that are in the log) to be arbitrarily large.
 709          */
 710         for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
 711                 uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
 712                 if (vdevid >= spa->spa_root_vdev->vdev_children) {
 713                         zfs_panic_recover("blkptr at %p DVA %u has invalid "
 714                             "VDEV %llu",
 715                             bp, i, (longlong_t)vdevid);
 716                         continue;
 717                 }
 718                 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
 719                 if (vd == NULL) {
 720                         zfs_panic_recover("blkptr at %p DVA %u has invalid "
 721                             "VDEV %llu",
 722                             bp, i, (longlong_t)vdevid);
 723                         continue;
 724                 }
 725                 if (vd->vdev_ops == &vdev_hole_ops) {
 726                         zfs_panic_recover("blkptr at %p DVA %u has hole "
 727                             "VDEV %llu",
 728                             bp, i, (longlong_t)vdevid);
 729                         continue;
 730                 }
 731                 if (vd->vdev_ops == &vdev_missing_ops) {
 732                         /*
 733                          * "missing" vdevs are valid during import, but we
 734                          * don't have their detailed info (e.g. asize), so
 735                          * we can't perform any more checks on them.
 736                          */
 737                         continue;
 738                 }
 739                 uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
 740                 uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
 741                 if (BP_IS_GANG(bp))
 742                         asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
 743                 if (offset + asize > vd->vdev_asize) {
 744                         zfs_panic_recover("blkptr at %p DVA %u has invalid "
 745                             "OFFSET %llu",
 746                             bp, i, (longlong_t)offset);
 747                 }
 748         }
 749 }
 750 
 751 boolean_t
 752 zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp)
 753 {
 754         uint64_t vdevid = DVA_GET_VDEV(dva);
 755 
 756         if (vdevid >= spa->spa_root_vdev->vdev_children)
 757                 return (B_FALSE);
 758 
 759         vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
 760         if (vd == NULL)
 761                 return (B_FALSE);
 762 
 763         if (vd->vdev_ops == &vdev_hole_ops)
 764                 return (B_FALSE);
 765 
 766         if (vd->vdev_ops == &vdev_missing_ops) {
 767                 return (B_FALSE);
 768         }
 769 
 770         uint64_t offset = DVA_GET_OFFSET(dva);
 771         uint64_t asize = DVA_GET_ASIZE(dva);
 772 
 773         if (BP_IS_GANG(bp))
 774                 asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
 775         if (offset + asize > vd->vdev_asize)
 776                 return (B_FALSE);
 777 
 778         return (B_TRUE);
 779 }
 780 
 781 zio_t *
 782 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
 783     abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
 784     zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
 785 {
 786         zio_t *zio;
 787 
 788         zfs_blkptr_verify(spa, bp);
 789 
 790         zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
 791             data, size, size, done, private,
 792             ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
 793             ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
 794             ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
 795 
 796         return (zio);
 797 }
 798 
 799 zio_t *
 800 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
 801     abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
 802     zio_done_func_t *ready, zio_done_func_t *children_ready,
 803     zio_done_func_t *physdone, zio_done_func_t *done,
 804     void *private, zio_priority_t priority, enum zio_flag flags,
 805     const zbookmark_phys_t *zb)

 806 {
 807         zio_t *zio;
 808 
 809         ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
 810             zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
 811             zp->zp_compress >= ZIO_COMPRESS_OFF &&
 812             zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
 813             DMU_OT_IS_VALID(zp->zp_type) &&
 814             zp->zp_level < 32 &&
 815             zp->zp_copies > 0 &&
 816             zp->zp_copies <= spa_max_replication(spa));
 817 
 818         zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
 819             ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
 820             ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
 821             ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
 822 
 823         zio->io_ready = ready;
 824         zio->io_children_ready = children_ready;
 825         zio->io_physdone = physdone;
 826         zio->io_prop = *zp;


 827 
 828         /*
 829          * Data can be NULL if we are going to call zio_write_override() to
 830          * provide the already-allocated BP.  But we may need the data to
 831          * verify a dedup hit (if requested).  In this case, don't try to
 832          * dedup (just take the already-allocated BP verbatim).
 833          */
 834         if (data == NULL && zio->io_prop.zp_dedup_verify) {
 835                 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
 836         }
 837 
 838         return (zio);
 839 }
 840 
 841 zio_t *
 842 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
 843     uint64_t size, zio_done_func_t *done, void *private,
 844     zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
 845 {
 846         zio_t *zio;


 858         ASSERT(zio->io_type == ZIO_TYPE_WRITE);
 859         ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
 860         ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
 861         ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
 862 
 863         /*
 864          * We must reset the io_prop to match the values that existed
 865          * when the bp was first written by dmu_sync() keeping in mind
 866          * that nopwrite and dedup are mutually exclusive.
 867          */
 868         zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
 869         zio->io_prop.zp_nopwrite = nopwrite;
 870         zio->io_prop.zp_copies = copies;
 871         zio->io_bp_override = bp;
 872 }
 873 
 874 void
 875 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
 876 {
 877 
 878         zfs_blkptr_verify(spa, bp);
 879 
 880         /*
 881          * The check for EMBEDDED is a performance optimization.  We
 882          * process the free here (by ignoring it) rather than
 883          * putting it on the list and then processing it in zio_free_sync().
 884          */
 885         if (BP_IS_EMBEDDED(bp))
 886                 return;
 887         metaslab_check_free(spa, bp);
 888 
 889         /*
 890          * Frees that are for the currently-syncing txg, are not going to be
 891          * deferred, and which will not need to do a read (i.e. not GANG or
 892          * DEDUP), can be processed immediately.  Otherwise, put them on the
 893          * in-memory list for later processing.
 894          */
 895         if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
 896             txg != spa->spa_syncing_txg ||
 897             spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
 898                 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
 899         } else {
 900                 VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0)));
 901         }
 902 }
 903 
 904 zio_t *
 905 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
 906     enum zio_flag flags)
 907 {
 908         zio_t *zio;
 909         enum zio_stage stage = ZIO_FREE_PIPELINE;
 910 
 911         ASSERT(!BP_IS_HOLE(bp));
 912         ASSERT(spa_syncing_txg(spa) == txg);
 913         ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
 914 
 915         if (BP_IS_EMBEDDED(bp))
 916                 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
 917 
 918         metaslab_check_free(spa, bp);
 919         arc_freed(spa, bp);

 920 
 921         /*
 922          * GANG and DEDUP blocks can induce a read (for the gang block header,
 923          * or the DDT), so issue them asynchronously so that this thread is
 924          * not tied up.
 925          */
 926         if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
 927                 stage |= ZIO_STAGE_ISSUE_ASYNC;
 928 
 929         zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
 930             BP_GET_PSIZE(bp), NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
 931             flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
 932 
 933         return (zio);
 934 }
 935 
 936 zio_t *
 937 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
 938     zio_done_func_t *done, void *private, enum zio_flag flags)
 939 {
 940         zio_t *zio;
 941 
 942         zfs_blkptr_verify(spa, bp);
 943 
 944         if (BP_IS_EMBEDDED(bp))
 945                 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
 946 
 947         /*
 948          * A claim is an allocation of a specific block.  Claims are needed
 949          * to support immediate writes in the intent log.  The issue is that
 950          * immediate writes contain committed data, but in a txg that was
 951          * *not* committed.  Upon opening the pool after an unclean shutdown,
 952          * the intent log claims all blocks that contain immediate write data
 953          * so that the SPA knows they're in use.
 954          *
 955          * All claims *must* be resolved in the first txg -- before the SPA
 956          * starts allocating blocks -- so that nothing is allocated twice.
 957          * If txg == 0 we just verify that the block is claimable.
 958          */
 959         ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
 960         ASSERT(txg == spa_first_txg(spa) || txg == 0);
 961         ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa));       /* zdb(1M) */
 962 
 963         zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
 964             BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
 965             flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
 966         ASSERT0(zio->io_queued_timestamp);
 967 
 968         return (zio);
 969 }
 970 
 971 zio_t *
 972 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
 973     zio_done_func_t *done, void *private, enum zio_flag flags)

 974 {
 975         zio_t *zio;
 976         int c;
 977 
 978         if (vd->vdev_children == 0) {
 979                 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
 980                     ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
 981                     ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
 982 
 983                 zio->io_cmd = cmd;
 984         } else {
 985                 zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
 986 









 987                 for (c = 0; c < vd->vdev_children; c++)
 988                         zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
 989                             done, private, flags));

 990         }

 991 
 992         return (zio);
 993 }
 994 
 995 zio_t *
















































































































 996 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
 997     abd_t *data, int checksum, zio_done_func_t *done, void *private,
 998     zio_priority_t priority, enum zio_flag flags, boolean_t labels)
 999 {
1000         zio_t *zio;
1001 
1002         ASSERT(vd->vdev_children == 0);
1003         ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1004             offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1005         ASSERT3U(offset + size, <=, vd->vdev_psize);
1006 
1007         zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1008             private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1009             offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1010 
1011         zio->io_prop.zp_checksum = checksum;
1012 
1013         return (zio);
1014 }
1015 


1041                 abd_t *wbuf = abd_alloc_sametype(data, size);
1042                 abd_copy(wbuf, data, size);
1043 
1044                 zio_push_transform(zio, wbuf, size, size, NULL);
1045         }
1046 
1047         return (zio);
1048 }
1049 
1050 /*
1051  * Create a child I/O to do some work for us.
1052  */
1053 zio_t *
1054 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1055     abd_t *data, uint64_t size, int type, zio_priority_t priority,
1056     enum zio_flag flags, zio_done_func_t *done, void *private)
1057 {
1058         enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1059         zio_t *zio;
1060 
1061         /*
1062          * vdev child I/Os do not propagate their error to the parent.
1063          * Therefore, for correct operation the caller *must* check for
1064          * and handle the error in the child i/o's done callback.
1065          * The only exceptions are i/os that we don't care about
1066          * (OPTIONAL or REPAIR).
1067          */
1068         ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) ||
1069             done != NULL);
1070 
1071         /*
1072          * In the common case, where the parent zio was to a normal vdev,
1073          * the child zio must be to a child vdev of that vdev.  Otherwise,
1074          * the child zio must be to a top-level vdev.
1075          */
1076         if (pio->io_vd != NULL && pio->io_vd->vdev_ops != &vdev_indirect_ops) {
1077                 ASSERT3P(vd->vdev_parent, ==, pio->io_vd);
1078         } else {
1079                 ASSERT3P(vd, ==, vd->vdev_top);
1080         }
1081 
1082         if (type == ZIO_TYPE_READ && bp != NULL) {
1083                 /*
1084                  * If we have the bp, then the child should perform the
1085                  * checksum and the parent need not.  This pushes error
1086                  * detection as close to the leaves as possible and
1087                  * eliminates redundant checksums in the interior nodes.
1088                  */
1089                 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1090                 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1091         }
1092 
1093         if (vd->vdev_ops->vdev_op_leaf) {
1094                 ASSERT0(vd->vdev_children);
1095                 offset += VDEV_LABEL_START_SIZE;
1096         }
1097 
1098         flags |= ZIO_VDEV_CHILD_FLAGS(pio);
1099 
1100         /*
1101          * If we've decided to do a repair, the write is not speculative --
1102          * even if the original read was.
1103          */
1104         if (flags & ZIO_FLAG_IO_REPAIR)
1105                 flags &= ~ZIO_FLAG_SPECULATIVE;
1106 
1107         /*
1108          * If we're creating a child I/O that is not associated with a
1109          * top-level vdev, then the child zio is not an allocating I/O.
1110          * If this is a retried I/O then we ignore it since we will
1111          * have already processed the original allocating I/O.
1112          */
1113         if (flags & ZIO_FLAG_IO_ALLOCATING &&
1114             (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1115                 metaslab_class_t *mc = spa_normal_class(pio->io_spa);
1116 
1117                 ASSERT(mc->mc_alloc_throttle_enabled);
1118                 ASSERT(type == ZIO_TYPE_WRITE);
1119                 ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1120                 ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1121                 ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1122                     pio->io_child_type == ZIO_CHILD_GANG);
1123 
1124                 flags &= ~ZIO_FLAG_IO_ALLOCATING;
1125         }
1126 
1127         zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1128             done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1129             ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1130         ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1131 
1132         zio->io_physdone = pio->io_physdone;
1133         if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
1134                 zio->io_logical->io_phys_children++;
1135 


1176          */
1177         ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1178         if (!BP_IS_RAIDZ(zio->io_bp)) {
1179                 /* we are not doing a raw write */
1180                 ASSERT3U(zio->io_size, ==, zio->io_lsize);
1181                 zio->io_orig_size = zio->io_size = zio->io_lsize = size;
1182         }
1183 }
1184 
1185 /*
1186  * ==========================================================================
1187  * Prepare to read and write logical blocks
1188  * ==========================================================================
1189  */
1190 
1191 static int
1192 zio_read_bp_init(zio_t *zio)
1193 {
1194         blkptr_t *bp = zio->io_bp;
1195 
1196         ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1197 
1198         if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1199             zio->io_child_type == ZIO_CHILD_LOGICAL &&
1200             !(zio->io_flags & ZIO_FLAG_RAW)) {
1201                 uint64_t psize =
1202                     BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1203                 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1204                     psize, psize, zio_decompress);
1205         }
1206 
1207         if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1208                 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1209 
1210                 int psize = BPE_GET_PSIZE(bp);
1211                 void *data = abd_borrow_buf(zio->io_abd, psize);
1212                 decode_embedded_bp_compressed(bp, data);
1213                 abd_return_buf_copy(zio->io_abd, data, psize);
1214         } else {
1215                 ASSERT(!BP_IS_EMBEDDED(bp));
1216                 ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1217         }
1218 
1219         if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
1220                 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1221 
1222         if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1223                 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1224 
1225         if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1226                 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1227 
1228         return (ZIO_PIPELINE_CONTINUE);
1229 }
1230 
1231 static int
1232 zio_write_bp_init(zio_t *zio)
1233 {
1234         if (!IO_IS_ALLOCATING(zio))
1235                 return (ZIO_PIPELINE_CONTINUE);
1236 
1237         ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1238 
1239         if (zio->io_bp_override) {


1287         return (ZIO_PIPELINE_CONTINUE);
1288 }
1289 
1290 static int
1291 zio_write_compress(zio_t *zio)
1292 {
1293         spa_t *spa = zio->io_spa;
1294         zio_prop_t *zp = &zio->io_prop;
1295         enum zio_compress compress = zp->zp_compress;
1296         blkptr_t *bp = zio->io_bp;
1297         uint64_t lsize = zio->io_lsize;
1298         uint64_t psize = zio->io_size;
1299         int pass = 1;
1300 
1301         EQUIV(lsize != psize, (zio->io_flags & ZIO_FLAG_RAW) != 0);
1302 
1303         /*
1304          * If our children haven't all reached the ready stage,
1305          * wait for them and then repeat this pipeline stage.
1306          */
1307         if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
1308             ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) {
1309                 return (ZIO_PIPELINE_STOP);
1310         }
1311 
1312         if (!IO_IS_ALLOCATING(zio))
1313                 return (ZIO_PIPELINE_CONTINUE);
1314 
1315         if (zio->io_children_ready != NULL) {
1316                 /*
1317                  * Now that all our children are ready, run the callback
1318                  * associated with this zio in case it wants to modify the
1319                  * data to be written.
1320                  */
1321                 ASSERT3U(zp->zp_level, >, 0);
1322                 zio->io_children_ready(zio);
1323         }
1324 
1325         ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1326         ASSERT(zio->io_bp_override == NULL);
1327 
1328         if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1329                 /*
1330                  * We're rewriting an existing block, which means we're


1332                  * converge, it must eventually be the case that we don't
1333                  * have to allocate new blocks.  But compression changes
1334                  * the blocksize, which forces a reallocate, and makes
1335                  * convergence take longer.  Therefore, after the first
1336                  * few passes, stop compressing to ensure convergence.
1337                  */
1338                 pass = spa_sync_pass(spa);
1339 
1340                 ASSERT(zio->io_txg == spa_syncing_txg(spa));
1341                 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1342                 ASSERT(!BP_GET_DEDUP(bp));
1343 
1344                 if (pass >= zfs_sync_pass_dont_compress)
1345                         compress = ZIO_COMPRESS_OFF;
1346 
1347                 /* Make sure someone doesn't change their mind on overwrites */
1348                 ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1349                     spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1350         }
1351 

1352         /* If it's a compressed write that is not raw, compress the buffer. */
1353         if (compress != ZIO_COMPRESS_OFF && psize == lsize) {

1354                 void *cbuf = zio_buf_alloc(lsize);
1355                 psize = zio_compress_data(compress, zio->io_abd, cbuf, lsize);
1356                 if (psize == 0 || psize == lsize) {
1357                         compress = ZIO_COMPRESS_OFF;
1358                         zio_buf_free(cbuf, lsize);
1359                 } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE &&
1360                     zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1361                     spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1362                         encode_embedded_bp_compressed(bp,
1363                             cbuf, compress, lsize, psize);
1364                         BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1365                         BP_SET_TYPE(bp, zio->io_prop.zp_type);
1366                         BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1367                         zio_buf_free(cbuf, lsize);
1368                         bp->blk_birth = zio->io_txg;
1369                         zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1370                         ASSERT(spa_feature_is_active(spa,
1371                             SPA_FEATURE_EMBEDDED_DATA));






1372                         return (ZIO_PIPELINE_CONTINUE);
1373                 } else {
1374                         /*
1375                          * Round up compressed size up to the ashift
1376                          * of the smallest-ashift device, and zero the tail.
1377                          * This ensures that the compressed size of the BP
1378                          * (and thus compressratio property) are correct,
1379                          * in that we charge for the padding used to fill out
1380                          * the last sector.
1381                          */
1382                         ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
1383                         size_t rounded = (size_t)P2ROUNDUP(psize,
1384                             1ULL << spa->spa_min_ashift);
1385                         if (rounded >= lsize) {
1386                                 compress = ZIO_COMPRESS_OFF;
1387                                 zio_buf_free(cbuf, lsize);
1388                                 psize = lsize;
1389                         } else {
1390                                 abd_t *cdata = abd_get_from_buf(cbuf, lsize);
1391                                 abd_take_ownership_of_buf(cdata, B_TRUE);
1392                                 abd_zero_off(cdata, psize, rounded - psize);
1393                                 psize = rounded;
1394                                 zio_push_transform(zio, cdata,
1395                                     psize, lsize, NULL);
1396                         }
1397                 }
1398 







1399                 /*
1400                  * We were unable to handle this as an override bp, treat
1401                  * it as a regular write I/O.
1402                  */
1403                 zio->io_bp_override = NULL;
1404                 *bp = zio->io_bp_orig;
1405                 zio->io_pipeline = zio->io_orig_pipeline;
1406         } else {
1407                 ASSERT3U(psize, !=, 0);











1408         }
1409 
1410         /*
1411          * The final pass of spa_sync() must be all rewrites, but the first
1412          * few passes offer a trade-off: allocating blocks defers convergence,
1413          * but newly allocated blocks are sequential, so they can be written
1414          * to disk faster.  Therefore, we allow the first few passes of
1415          * spa_sync() to allocate new blocks, but force rewrites after that.
1416          * There should only be a handful of blocks after pass 1 in any case.
1417          */
1418         if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
1419             BP_GET_PSIZE(bp) == psize &&
1420             pass >= zfs_sync_pass_rewrite) {
1421                 ASSERT(psize != 0);
1422                 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1423                 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1424                 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1425         } else {
1426                 BP_ZERO(bp);
1427                 zio->io_pipeline = ZIO_WRITE_PIPELINE;
1428         }
1429 
1430         if (psize == 0) {
1431                 if (zio->io_bp_orig.blk_birth != 0 &&
1432                     spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
1433                         BP_SET_LSIZE(bp, lsize);
1434                         BP_SET_TYPE(bp, zp->zp_type);
1435                         BP_SET_LEVEL(bp, zp->zp_level);
1436                         BP_SET_BIRTH(bp, zio->io_txg, 0);
1437                 }
1438                 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1439         } else {




1440                 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1441                 BP_SET_LSIZE(bp, lsize);
1442                 BP_SET_TYPE(bp, zp->zp_type);
1443                 BP_SET_LEVEL(bp, zp->zp_level);
1444                 BP_SET_PSIZE(bp, psize);
1445                 BP_SET_COMPRESS(bp, compress);
1446                 BP_SET_CHECKSUM(bp, zp->zp_checksum);
1447                 BP_SET_DEDUP(bp, zp->zp_dedup);
1448                 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1449                 if (zp->zp_dedup) {
1450                         ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1451                         ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1452                         zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1453                 }
1454                 if (zp->zp_nopwrite) {
1455                         ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1456                         ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1457                         zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1458                 }
1459         }
1460         return (ZIO_PIPELINE_CONTINUE);
1461 }
1462 
1463 static int
1464 zio_free_bp_init(zio_t *zio)
1465 {
1466         blkptr_t *bp = zio->io_bp;
1467 
1468         if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1469                 if (BP_GET_DEDUP(bp))
1470                         zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1471         }
1472 
1473         ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1474 
1475         return (ZIO_PIPELINE_CONTINUE);
1476 }
1477 
1478 /*
1479  * ==========================================================================
1480  * Execute the I/O pipeline
1481  * ==========================================================================
1482  */
1483 
1484 static void
1485 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1486 {
1487         spa_t *spa = zio->io_spa;
1488         zio_type_t t = zio->io_type;
1489         int flags = (cutinline ? TQ_FRONT : 0);
1490 
1491         /*
1492          * If we're a config writer or a probe, the normal issue and
1493          * interrupt threads may all be blocked waiting for the config lock.
1494          * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1495          */
1496         if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1497                 t = ZIO_TYPE_NULL;
1498 
1499         /*
1500          * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1501          */
1502         if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1503                 t = ZIO_TYPE_NULL;
1504 
1505         /*
1506          * If this is a high priority I/O, then use the high priority taskq if
1507          * available.
1508          */
1509         if (zio->io_priority == ZIO_PRIORITY_NOW &&

1510             spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
1511                 q++;
1512 
1513         ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1514 
1515         /*
1516          * NB: We are assuming that the zio can only be dispatched
1517          * to a single taskq at a time.  It would be a grievous error
1518          * to dispatch the zio to another taskq at the same time.
1519          */
1520         ASSERT(zio->io_tqent.tqent_next == NULL);
1521         spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1522             flags, &zio->io_tqent);
1523 }
1524 
1525 static boolean_t
1526 zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1527 {
1528         kthread_t *executor = zio->io_executor;
1529         spa_t *spa = zio->io_spa;


1616  *      (5) the I/O is deferred due to vdev-level queueing
1617  *      (6) the I/O is handed off to another thread.
1618  *
1619  * In all cases, the pipeline stops whenever there's no CPU work; it never
1620  * burns a thread in cv_wait().
1621  *
1622  * There's no locking on io_stage because there's no legitimate way
1623  * for multiple threads to be attempting to process the same I/O.
1624  */
1625 static zio_pipe_stage_t *zio_pipeline[];
1626 
1627 void
1628 zio_execute(zio_t *zio)
1629 {
1630         zio->io_executor = curthread;
1631 
1632         ASSERT3U(zio->io_queued_timestamp, >, 0);
1633 
1634         while (zio->io_stage < ZIO_STAGE_DONE) {
1635                 enum zio_stage pipeline = zio->io_pipeline;

1636                 enum zio_stage stage = zio->io_stage;
1637                 int rv;
1638 
1639                 ASSERT(!MUTEX_HELD(&zio->io_lock));
1640                 ASSERT(ISP2(stage));
1641                 ASSERT(zio->io_stall == NULL);
1642 
1643                 do {
1644                         stage <<= 1;
1645                 } while ((stage & pipeline) == 0);
1646 
1647                 ASSERT(stage <= ZIO_STAGE_DONE);
1648 
1649                 /*
1650                  * If we are in interrupt context and this pipeline stage
1651                  * will grab a config lock that is held across I/O,
1652                  * or may wait for an I/O that needs an interrupt thread
1653                  * to complete, issue async to avoid deadlock.
1654                  *
1655                  * For VDEV_IO_START, we cut in line so that the io will
1656                  * be sent to disk promptly.
1657                  */
1658                 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1659                     zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1660                         boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1661                             zio_requeue_io_start_cut_in_line : B_FALSE;
1662                         zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1663                         return;
1664                 }
1665 
1666                 zio->io_stage = stage;
1667                 zio->io_pipeline_trace |= zio->io_stage;
1668                 rv = zio_pipeline[highbit64(stage) - 1](zio);
1669 
1670                 if (rv == ZIO_PIPELINE_STOP)
1671                         return;
1672 






1673                 ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1674         }
1675 }
1676 
1677 /*
1678  * ==========================================================================
1679  * Initiate I/O, either sync or async
1680  * ==========================================================================
1681  */
1682 int
1683 zio_wait(zio_t *zio)
1684 {
1685         int error;
1686 
1687         ASSERT3P(zio->io_stage, ==, ZIO_STAGE_OPEN);
1688         ASSERT3P(zio->io_executor, ==, NULL);
1689 
1690         zio->io_waiter = curthread;
1691         ASSERT0(zio->io_queued_timestamp);
1692         zio->io_queued_timestamp = gethrtime();


2133 static int
2134 zio_gang_assemble(zio_t *zio)
2135 {
2136         blkptr_t *bp = zio->io_bp;
2137 
2138         ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2139         ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2140 
2141         zio->io_gang_leader = zio;
2142 
2143         zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2144 
2145         return (ZIO_PIPELINE_CONTINUE);
2146 }
2147 
2148 static int
2149 zio_gang_issue(zio_t *zio)
2150 {
2151         blkptr_t *bp = zio->io_bp;
2152 
2153         if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) {
2154                 return (ZIO_PIPELINE_STOP);
2155         }
2156 
2157         ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2158         ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2159 
2160         if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2161                 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
2162                     0);
2163         else
2164                 zio_gang_tree_free(&zio->io_gang_tree);
2165 
2166         zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2167 
2168         return (ZIO_PIPELINE_CONTINUE);
2169 }
2170 
2171 static void
2172 zio_write_gang_member_ready(zio_t *zio)
2173 {
2174         zio_t *pio = zio_unique_parent(zio);
2175         zio_t *gio = zio->io_gang_leader;


2191         mutex_enter(&pio->io_lock);
2192         for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
2193                 ASSERT(DVA_GET_GANG(&pdva[d]));
2194                 asize = DVA_GET_ASIZE(&pdva[d]);
2195                 asize += DVA_GET_ASIZE(&cdva[d]);
2196                 DVA_SET_ASIZE(&pdva[d], asize);
2197         }
2198         mutex_exit(&pio->io_lock);
2199 }
2200 
2201 static void
2202 zio_write_gang_done(zio_t *zio)
2203 {
2204         abd_put(zio->io_abd);
2205 }
2206 
2207 static int
2208 zio_write_gang_block(zio_t *pio)
2209 {
2210         spa_t *spa = pio->io_spa;
2211         metaslab_class_t *mc = spa_normal_class(spa);
2212         blkptr_t *bp = pio->io_bp;
2213         zio_t *gio = pio->io_gang_leader;
2214         zio_t *zio;
2215         zio_gang_node_t *gn, **gnpp;
2216         zio_gbh_phys_t *gbh;
2217         abd_t *gbh_abd;
2218         uint64_t txg = pio->io_txg;
2219         uint64_t resid = pio->io_size;
2220         uint64_t lsize;
2221         int copies = gio->io_prop.zp_copies;
2222         int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2223         zio_prop_t zp;
2224         int error;
2225 
2226         int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
2227         if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2228                 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2229                 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2230 
2231                 flags |= METASLAB_ASYNC_ALLOC;


2288          * Create and nowait the gang children.
2289          */
2290         for (int g = 0; resid != 0; resid -= lsize, g++) {
2291                 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2292                     SPA_MINBLOCKSIZE);
2293                 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2294 
2295                 zp.zp_checksum = gio->io_prop.zp_checksum;
2296                 zp.zp_compress = ZIO_COMPRESS_OFF;
2297                 zp.zp_type = DMU_OT_NONE;
2298                 zp.zp_level = 0;
2299                 zp.zp_copies = gio->io_prop.zp_copies;
2300                 zp.zp_dedup = B_FALSE;
2301                 zp.zp_dedup_verify = B_FALSE;
2302                 zp.zp_nopwrite = B_FALSE;
2303 
2304                 zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
2305                     abd_get_offset(pio->io_abd, pio->io_size - resid), lsize,
2306                     lsize, &zp, zio_write_gang_member_ready, NULL, NULL,
2307                     zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
2308                     ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);

2309 


2310                 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2311                         ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2312                         ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2313 
2314                         /*
2315                          * Gang children won't throttle but we should
2316                          * account for their work, so reserve an allocation
2317                          * slot for them here.
2318                          */
2319                         VERIFY(metaslab_class_throttle_reserve(mc,
2320                             zp.zp_copies, cio, flags));
2321                 }
2322                 zio_nowait(cio);
2323         }
2324 
2325         /*
2326          * Set pio's pipeline to just wait for zio to finish.
2327          */
2328         pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2329 


2456                             abd_alloc_for_io(zio->io_size, B_TRUE),
2457                             zio->io_size, zio_ddt_child_read_done, dde,
2458                             zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
2459                             ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
2460                 }
2461                 return (ZIO_PIPELINE_CONTINUE);
2462         }
2463 
2464         zio_nowait(zio_read(zio, zio->io_spa, bp,
2465             zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
2466             ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2467 
2468         return (ZIO_PIPELINE_CONTINUE);
2469 }
2470 
2471 static int
2472 zio_ddt_read_done(zio_t *zio)
2473 {
2474         blkptr_t *bp = zio->io_bp;
2475 
2476         if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) {
2477                 return (ZIO_PIPELINE_STOP);
2478         }
2479 
2480         ASSERT(BP_GET_DEDUP(bp));
2481         ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2482         ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2483 
2484         if (zio->io_child_error[ZIO_CHILD_DDT]) {
2485                 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2486                 ddt_entry_t *dde = zio->io_vsd;
2487                 if (ddt == NULL) {
2488                         ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2489                         return (ZIO_PIPELINE_CONTINUE);
2490                 }
2491                 if (dde == NULL) {
2492                         zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
2493                         zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2494                         return (ZIO_PIPELINE_STOP);
2495                 }
2496                 if (dde->dde_repair_abd != NULL) {
2497                         abd_copy(zio->io_abd, dde->dde_repair_abd,
2498                             zio->io_size);
2499                         zio->io_child_error[ZIO_CHILD_DDT] = 0;
2500                 }
2501                 ddt_repair_done(ddt, dde);
2502                 zio->io_vsd = NULL;
2503         }
2504 
2505         ASSERT(zio->io_vsd == NULL);
2506 
2507         return (ZIO_PIPELINE_CONTINUE);
2508 }
2509 

2510 static boolean_t
2511 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2512 {
2513         spa_t *spa = zio->io_spa;
2514         boolean_t do_raw = (zio->io_flags & ZIO_FLAG_RAW);
2515 
2516         /* We should never get a raw, override zio */
2517         ASSERT(!(zio->io_bp_override && do_raw));
2518 
2519         /*
2520          * Note: we compare the original data, not the transformed data,
2521          * because when zio->io_bp is an override bp, we will not have
2522          * pushed the I/O transforms.  That's an important optimization
2523          * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2524          */
2525         for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2526                 zio_t *lio = dde->dde_lead_zio[p];
2527 
2528                 if (lio != NULL) {
2529                         return (lio->io_orig_size != zio->io_orig_size ||
2530                             abd_cmp(zio->io_orig_abd, lio->io_orig_abd,
2531                             zio->io_orig_size) != 0);
2532                 }
2533         }
2534 
2535         for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2536                 ddt_phys_t *ddp = &dde->dde_phys[p];
2537 
2538                 if (ddp->ddp_phys_birth != 0) {
2539                         arc_buf_t *abuf = NULL;
2540                         arc_flags_t aflags = ARC_FLAG_WAIT;
2541                         int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
2542                         blkptr_t blk = *zio->io_bp;
2543                         int error;
2544 
2545                         ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2546 
2547                         ddt_exit(ddt);
2548 
2549                         /*
2550                          * Intuitively, it would make more sense to compare
2551                          * io_abd than io_orig_abd in the raw case since you
2552                          * don't want to look at any transformations that have
2553                          * happened to the data. However, for raw I/Os the
2554                          * data will actually be the same in io_abd and
2555                          * io_orig_abd, so all we have to do is issue this as
2556                          * a raw ARC read.
2557                          */
2558                         if (do_raw) {
2559                                 zio_flags |= ZIO_FLAG_RAW;
2560                                 ASSERT3U(zio->io_size, ==, zio->io_orig_size);
2561                                 ASSERT0(abd_cmp(zio->io_abd, zio->io_orig_abd,
2562                                     zio->io_size));
2563                                 ASSERT3P(zio->io_transform_stack, ==, NULL);
2564                         }
2565 
2566                         error = arc_read(NULL, spa, &blk,
2567                             arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
2568                             zio_flags, &aflags, &zio->io_bookmark);
2569 
2570                         if (error == 0) {
2571                                 if (arc_buf_size(abuf) != zio->io_orig_size ||
2572                                     abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
2573                                     zio->io_orig_size) != 0)
2574                                         error = SET_ERROR(EEXIST);
2575                                 arc_buf_destroy(abuf, &abuf);
2576                         }
2577 
2578                         ddt_enter(ddt);
2579                         return (error != 0);
2580                 }
2581         }
2582 
2583         return (B_FALSE);
2584 }
2585 
2586 static void
2587 zio_ddt_child_write_ready(zio_t *zio)
2588 {
2589         int p = zio->io_prop.zp_copies;
2590         ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2591         ddt_entry_t *dde = zio->io_private;
2592         ddt_phys_t *ddp = &dde->dde_phys[p];
2593         zio_t *pio;
2594 
2595         if (zio->io_error)
2596                 return;
2597 
2598         ddt_enter(ddt);
2599 
2600         ASSERT(dde->dde_lead_zio[p] == zio);
2601 
2602         ddt_phys_fill(ddp, zio->io_bp);
2603 
2604         zio_link_t *zl = NULL;
2605         while ((pio = zio_walk_parents(zio, &zl)) != NULL)
2606                 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2607 
2608         ddt_exit(ddt);
2609 }
2610 
2611 static void
2612 zio_ddt_child_write_done(zio_t *zio)
2613 {
2614         int p = zio->io_prop.zp_copies;
2615         ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2616         ddt_entry_t *dde = zio->io_private;
2617         ddt_phys_t *ddp = &dde->dde_phys[p];
2618 
2619         ddt_enter(ddt);
2620 
2621         ASSERT(ddp->ddp_refcnt == 0);
2622         ASSERT(dde->dde_lead_zio[p] == zio);
2623         dde->dde_lead_zio[p] = NULL;
2624 
2625         if (zio->io_error == 0) {
2626                 zio_link_t *zl = NULL;
2627                 while (zio_walk_parents(zio, &zl) != NULL)
2628                         ddt_phys_addref(ddp);
2629         } else {
2630                 ddt_phys_clear(ddp);
2631         }
2632 
2633         ddt_exit(ddt);
2634 }
2635 
2636 static void
2637 zio_ddt_ditto_write_done(zio_t *zio)
2638 {
2639         int p = DDT_PHYS_DITTO;
2640         zio_prop_t *zp = &zio->io_prop;
2641         blkptr_t *bp = zio->io_bp;
2642         ddt_t *ddt = ddt_select(zio->io_spa, bp);
2643         ddt_entry_t *dde = zio->io_private;
2644         ddt_phys_t *ddp = &dde->dde_phys[p];
2645         ddt_key_t *ddk = &dde->dde_key;
2646 
2647         ddt_enter(ddt);
2648 
2649         ASSERT(ddp->ddp_refcnt == 0);
2650         ASSERT(dde->dde_lead_zio[p] == zio);
2651         dde->dde_lead_zio[p] = NULL;
2652 
2653         if (zio->io_error == 0) {
2654                 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2655                 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2656                 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2657                 if (ddp->ddp_phys_birth != 0)
2658                         ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2659                 ddt_phys_fill(ddp, bp);
2660         }
2661 
2662         ddt_exit(ddt);
2663 }
2664 
2665 static int
2666 zio_ddt_write(zio_t *zio)
2667 {
2668         spa_t *spa = zio->io_spa;
2669         blkptr_t *bp = zio->io_bp;
2670         uint64_t txg = zio->io_txg;
2671         zio_prop_t *zp = &zio->io_prop;
2672         int p = zp->zp_copies;
2673         int ditto_copies;
2674         zio_t *cio = NULL;
2675         zio_t *dio = NULL;
2676         ddt_t *ddt = ddt_select(spa, bp);
2677         ddt_entry_t *dde;
2678         ddt_phys_t *ddp;
2679 
2680         ASSERT(BP_GET_DEDUP(bp));
2681         ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2682         ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
2683         ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
2684 
2685         ddt_enter(ddt);
2686         dde = ddt_lookup(ddt, bp, B_TRUE);
2687         ddp = &dde->dde_phys[p];
2688 
























2689         if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2690                 /*
2691                  * If we're using a weak checksum, upgrade to a strong checksum
2692                  * and try again.  If we're already using a strong checksum,
2693                  * we can't resolve it, so just convert to an ordinary write.
2694                  * (And automatically e-mail a paper to Nature?)
2695                  */
2696                 if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
2697                     ZCHECKSUM_FLAG_DEDUP)) {
2698                         zp->zp_checksum = spa_dedup_checksum(spa);
2699                         zio_pop_transforms(zio);
2700                         zio->io_stage = ZIO_STAGE_OPEN;
2701                         BP_ZERO(bp);
2702                 } else {
2703                         zp->zp_dedup = B_FALSE;
2704                         BP_SET_DEDUP(bp, B_FALSE);
2705                 }
2706                 ASSERT(!BP_GET_DEDUP(bp));
2707                 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2708                 ddt_exit(ddt);
2709                 return (ZIO_PIPELINE_CONTINUE);
2710         }
2711 

2712         ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2713         ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2714 
2715         if (ditto_copies > ddt_ditto_copies_present(dde) &&
2716             dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2717                 zio_prop_t czp = *zp;
2718 
2719                 czp.zp_copies = ditto_copies;
2720 
2721                 /*
2722                  * If we arrived here with an override bp, we won't have run
2723                  * the transform stack, so we won't have the data we need to
2724                  * generate a child i/o.  So, toss the override bp and restart.
2725                  * This is safe, because using the override bp is just an
2726                  * optimization; and it's rare, so the cost doesn't matter.
2727                  */
2728                 if (zio->io_bp_override) {
2729                         zio_pop_transforms(zio);
2730                         zio->io_stage = ZIO_STAGE_OPEN;
2731                         zio->io_pipeline = ZIO_WRITE_PIPELINE;
2732                         zio->io_bp_override = NULL;
2733                         BP_ZERO(bp);
2734                         ddt_exit(ddt);
2735                         return (ZIO_PIPELINE_CONTINUE);
2736                 }
2737 
2738                 dio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
2739                     zio->io_orig_size, zio->io_orig_size, &czp, NULL, NULL,
2740                     NULL, zio_ddt_ditto_write_done, dde, zio->io_priority,
2741                     ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2742 
2743                 zio_push_transform(dio, zio->io_abd, zio->io_size, 0, NULL);
2744                 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2745         }
2746 
2747         if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2748                 if (ddp->ddp_phys_birth != 0)
2749                         ddt_bp_fill(ddp, bp, txg);
2750                 if (dde->dde_lead_zio[p] != NULL)
2751                         zio_add_child(zio, dde->dde_lead_zio[p]);
2752                 else
2753                         ddt_phys_addref(ddp);
2754         } else if (zio->io_bp_override) {
2755                 ASSERT(bp->blk_birth == txg);
2756                 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
2757                 ddt_phys_fill(ddp, bp);
2758                 ddt_phys_addref(ddp);
2759         } else {
2760                 cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
2761                     zio->io_orig_size, zio->io_orig_size, zp,
2762                     zio_ddt_child_write_ready, NULL, NULL,
2763                     zio_ddt_child_write_done, dde, zio->io_priority,
2764                     ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2765 
2766                 zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
2767                 dde->dde_lead_zio[p] = cio;
2768         }
2769 
2770         ddt_exit(ddt);
2771 
2772         if (cio)
2773                 zio_nowait(cio);
2774         if (dio)
2775                 zio_nowait(dio);
2776 
2777         return (ZIO_PIPELINE_CONTINUE);
2778 }
2779 
2780 ddt_entry_t *freedde; /* for debugging */
2781 
2782 static int
2783 zio_ddt_free(zio_t *zio)
2784 {
2785         spa_t *spa = zio->io_spa;
2786         blkptr_t *bp = zio->io_bp;
2787         ddt_t *ddt = ddt_select(spa, bp);
2788         ddt_entry_t *dde;
2789         ddt_phys_t *ddp;
2790 
2791         ASSERT(BP_GET_DEDUP(bp));
2792         ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2793 
2794         ddt_enter(ddt);
2795         freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
2796         ddp = ddt_phys_select(dde, bp);

2797         ddt_phys_decref(ddp);
2798         ddt_exit(ddt);
2799 
2800         return (ZIO_PIPELINE_CONTINUE);
2801 }
2802 
2803 /*
2804  * ==========================================================================
2805  * Allocate and free blocks
2806  * ==========================================================================
2807  */
2808 
2809 static zio_t *
2810 zio_io_to_allocate(spa_t *spa)
2811 {
2812         zio_t *zio;
2813 
2814         ASSERT(MUTEX_HELD(&spa->spa_alloc_lock));
2815 
2816         zio = avl_first(&spa->spa_alloc_tree);
2817         if (zio == NULL)
2818                 return (NULL);
2819 
2820         ASSERT(IO_IS_ALLOCATING(zio));
2821 
2822         /*
2823          * Try to place a reservation for this zio. If we're unable to
2824          * reserve then we throttle.
2825          */
2826         if (!metaslab_class_throttle_reserve(spa_normal_class(spa),
2827             zio->io_prop.zp_copies, zio, 0)) {
2828                 return (NULL);
2829         }
2830 
2831         avl_remove(&spa->spa_alloc_tree, zio);
2832         ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
2833 
2834         return (zio);
2835 }
2836 
2837 static int
2838 zio_dva_throttle(zio_t *zio)
2839 {
2840         spa_t *spa = zio->io_spa;
2841         zio_t *nio;
2842 







2843         if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
2844             !spa_normal_class(zio->io_spa)->mc_alloc_throttle_enabled ||
2845             zio->io_child_type == ZIO_CHILD_GANG ||
2846             zio->io_flags & ZIO_FLAG_NODATA) {
2847                 return (ZIO_PIPELINE_CONTINUE);
2848         }
2849 
2850         ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2851 
2852         ASSERT3U(zio->io_queued_timestamp, >, 0);
2853         ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
2854 
2855         mutex_enter(&spa->spa_alloc_lock);
2856 
2857         ASSERT(zio->io_type == ZIO_TYPE_WRITE);
2858         avl_add(&spa->spa_alloc_tree, zio);
2859 
2860         nio = zio_io_to_allocate(zio->io_spa);
2861         mutex_exit(&spa->spa_alloc_lock);
2862 
2863         if (nio == zio)
2864                 return (ZIO_PIPELINE_CONTINUE);
2865 
2866         if (nio != NULL) {
2867                 ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE);
2868                 /*
2869                  * We are passing control to a new zio so make sure that
2870                  * it is processed by a different thread. We do this to
2871                  * avoid stack overflows that can occur when parents are
2872                  * throttled and children are making progress. We allow
2873                  * it to go to the head of the taskq since it's already
2874                  * been waiting.
2875                  */
2876                 zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE);
2877         }
2878         return (ZIO_PIPELINE_STOP);
2879 }
2880 
2881 void
2882 zio_allocate_dispatch(spa_t *spa)
2883 {
2884         zio_t *zio;
2885 
2886         mutex_enter(&spa->spa_alloc_lock);
2887         zio = zio_io_to_allocate(spa);
2888         mutex_exit(&spa->spa_alloc_lock);
2889         if (zio == NULL)
2890                 return;
2891 
2892         ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
2893         ASSERT0(zio->io_error);
2894         zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
2895 }
2896 
2897 static int
2898 zio_dva_allocate(zio_t *zio)
2899 {
2900         spa_t *spa = zio->io_spa;
2901         metaslab_class_t *mc = spa_normal_class(spa);

2902         blkptr_t *bp = zio->io_bp;
2903         int error;
2904         int flags = 0;
2905 
2906         if (zio->io_gang_leader == NULL) {
2907                 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2908                 zio->io_gang_leader = zio;
2909         }
2910 
2911         ASSERT(BP_IS_HOLE(bp));
2912         ASSERT0(BP_GET_NDVAS(bp));
2913         ASSERT3U(zio->io_prop.zp_copies, >, 0);
2914         ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
2915         ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
2916 
2917         if (zio->io_flags & ZIO_FLAG_NODATA) {
2918                 flags |= METASLAB_DONT_THROTTLE;
2919         }
2920         if (zio->io_flags & ZIO_FLAG_GANG_CHILD) {
2921                 flags |= METASLAB_GANG_CHILD;
2922         }
2923         if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE) {

2924                 flags |= METASLAB_ASYNC_ALLOC;
2925         }
2926 
2927         error = metaslab_alloc(spa, mc, zio->io_size, bp,
2928             zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
2929             &zio->io_alloc_list, zio);
2930 










2931         if (error != 0) {
2932                 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, "
2933                     "size %llu, error %d", spa_name(spa), zio, zio->io_size,
2934                     error);
2935                 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)






2936                         return (zio_write_gang_block(zio));


2937                 zio->io_error = error;
2938         }
2939 
2940         return (ZIO_PIPELINE_CONTINUE);
2941 }
2942 
2943 static int
2944 zio_dva_free(zio_t *zio)
2945 {
2946         metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
2947 
2948         return (ZIO_PIPELINE_CONTINUE);
2949 }
2950 
2951 static int
2952 zio_dva_claim(zio_t *zio)
2953 {
2954         int error;
2955 
2956         error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);


2974         if (!BP_IS_HOLE(bp))
2975                 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
2976 
2977         if (gn != NULL) {
2978                 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2979                         zio_dva_unallocate(zio, gn->gn_child[g],
2980                             &gn->gn_gbh->zg_blkptr[g]);
2981                 }
2982         }
2983 }
2984 
2985 /*
2986  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
2987  */
2988 int
2989 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp,
2990     uint64_t size, boolean_t *slog)
2991 {
2992         int error = 1;
2993         zio_alloc_list_t io_alloc_list;

2994 
2995         ASSERT(txg > spa_syncing_txg(spa));
2996 
2997         metaslab_trace_init(&io_alloc_list);
2998         error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
2999             txg, old_bp, METASLAB_HINTBP_AVOID, &io_alloc_list, NULL);
3000         if (error == 0) {

















3001                 *slog = TRUE;
3002         } else {




















3003                 error = metaslab_alloc(spa, spa_normal_class(spa), size,
3004                     new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID,
3005                     &io_alloc_list, NULL);




3006                 if (error == 0)
3007                         *slog = FALSE;
3008         }

3009         metaslab_trace_fini(&io_alloc_list);
3010 
3011         if (error == 0) {
3012                 BP_SET_LSIZE(new_bp, size);
3013                 BP_SET_PSIZE(new_bp, size);
3014                 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
3015                 BP_SET_CHECKSUM(new_bp,
3016                     spa_version(spa) >= SPA_VERSION_SLIM_ZIL
3017                     ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
3018                 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3019                 BP_SET_LEVEL(new_bp, 0);
3020                 BP_SET_DEDUP(new_bp, 0);
3021                 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
3022         } else {
3023                 zfs_dbgmsg("%s: zil block allocation failure: "
3024                     "size %llu, error %d", spa_name(spa), size, error);
3025         }
3026 
3027         return (error);
3028 }


3045  * ==========================================================================
3046  */
3047 
3048 
3049 /*
3050  * Issue an I/O to the underlying vdev. Typically the issue pipeline
3051  * stops after this stage and will resume upon I/O completion.
3052  * However, there are instances where the vdev layer may need to
3053  * continue the pipeline when an I/O was not issued. Since the I/O
3054  * that was sent to the vdev layer might be different than the one
3055  * currently active in the pipeline (see vdev_queue_io()), we explicitly
3056  * force the underlying vdev layers to call either zio_execute() or
3057  * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3058  */
3059 static int
3060 zio_vdev_io_start(zio_t *zio)
3061 {
3062         vdev_t *vd = zio->io_vd;
3063         uint64_t align;
3064         spa_t *spa = zio->io_spa;


3065 
3066         ASSERT(zio->io_error == 0);
3067         ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
3068 
3069         if (vd == NULL) {
3070                 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3071                         spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
3072 
3073                 /*
3074                  * The mirror_ops handle multiple DVAs in a single BP.
3075                  */
3076                 vdev_mirror_ops.vdev_op_io_start(zio);
3077                 return (ZIO_PIPELINE_STOP);
3078         }
3079 
3080         ASSERT3P(zio->io_logical, !=, zio);
3081         if (zio->io_type == ZIO_TYPE_WRITE) {
3082                 ASSERT(spa->spa_trust_config);
3083 
3084                 if (zio->io_vd->vdev_removing) {
3085                         ASSERT(zio->io_flags &
3086                             (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL |
3087                             ZIO_FLAG_INDUCE_DAMAGE));
3088                 }
3089         }
3090 
3091         /*
3092          * We keep track of time-sensitive I/Os so that the scan thread
3093          * can quickly react to certain workloads.  In particular, we care
3094          * about non-scrubbing, top-level reads and writes with the following
3095          * characteristics:
3096          *      - synchronous writes of user data to non-slog devices
3097          *      - any reads of user data
3098          * When these conditions are met, adjust the timestamp of spa_last_io
3099          * which allows the scan thread to adjust its workload accordingly.
3100          */
3101         if (!(zio->io_flags & ZIO_FLAG_SCAN_THREAD) && zio->io_bp != NULL &&
3102             vd == vd->vdev_top && !vd->vdev_islog &&
3103             zio->io_bookmark.zb_objset != DMU_META_OBJSET &&
3104             zio->io_txg != spa_syncing_txg(spa)) {
3105                 uint64_t old = spa->spa_last_io;
3106                 uint64_t new = ddi_get_lbolt64();
3107                 if (old != new)
3108                         (void) atomic_cas_64(&spa->spa_last_io, old, new);
3109         }
3110 
3111         align = 1ULL << vd->vdev_top->vdev_ashift;
3112 
3113         if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
3114             P2PHASE(zio->io_size, align) != 0) {
3115                 /* Transform logical writes to be a full physical block size. */
3116                 uint64_t asize = P2ROUNDUP(zio->io_size, align);
3117                 abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
3118                 ASSERT(vd == vd->vdev_top);
3119                 if (zio->io_type == ZIO_TYPE_WRITE) {
3120                         abd_copy(abuf, zio->io_abd, zio->io_size);
3121                         abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
3122                 }
3123                 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
3124         }
3125 
3126         /*
3127          * If this is not a physical io, make sure that it is properly aligned
3128          * before proceeding.
3129          */
3130         if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
3131                 ASSERT0(P2PHASE(zio->io_offset, align));
3132                 ASSERT0(P2PHASE(zio->io_size, align));
3133         } else {
3134                 /*
3135                  * For physical writes, we allow 512b aligned writes and assume
3136                  * the device will perform a read-modify-write as necessary.
3137                  */
3138                 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
3139                 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
3140         }
3141 
3142         VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
3143 
3144         /*
3145          * If this is a repair I/O, and there's no self-healing involved --
3146          * that is, we're just resilvering what we expect to resilver --
3147          * then don't do the I/O unless zio's txg is actually in vd's DTL.
3148          * This prevents spurious resilvering with nested replication.
3149          * For example, given a mirror of mirrors, (A+B)+(C+D), if only
3150          * A is out of date, we'll read from C+D, then use the data to
3151          * resilver A+B -- but we don't actually want to resilver B, just A.
3152          * The top-level mirror has no way to know this, so instead we just
3153          * discard unnecessary repairs as we work our way down the vdev tree.
3154          * The same logic applies to any form of nested replication:
3155          * ditto + mirror, RAID-Z + replacing, etc.  This covers them all.
3156          */
3157         if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
3158             !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
3159             zio->io_txg != 0 &&      /* not a delegated i/o */
3160             !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
3161                 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3162                 zio_vdev_io_bypass(zio);
3163                 return (ZIO_PIPELINE_CONTINUE);
3164         }
3165 
3166         if (vd->vdev_ops->vdev_op_leaf &&
3167             (zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE)) {
3168 
3169                 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio))
3170                         return (ZIO_PIPELINE_CONTINUE);
3171 
3172                 if ((zio = vdev_queue_io(zio)) == NULL)
3173                         return (ZIO_PIPELINE_STOP);
3174 
3175                 if (!vdev_accessible(vd, zio)) {
3176                         zio->io_error = SET_ERROR(ENXIO);
3177                         zio_interrupt(zio);
3178                         return (ZIO_PIPELINE_STOP);
3179                 }








3180         }

3181 
3182         vd->vdev_ops->vdev_op_io_start(zio);
3183         return (ZIO_PIPELINE_STOP);
3184 }
3185 
3186 static int
3187 zio_vdev_io_done(zio_t *zio)
3188 {
3189         vdev_t *vd = zio->io_vd;
3190         vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
3191         boolean_t unexpected_error = B_FALSE;
3192 
3193         if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
3194                 return (ZIO_PIPELINE_STOP);
3195         }
3196 
3197         ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
3198 
3199         if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
3200 
3201                 vdev_queue_io_done(zio);
3202 
3203                 if (zio->io_type == ZIO_TYPE_WRITE)
3204                         vdev_cache_write(zio);
3205 
3206                 if (zio_injection_enabled && zio->io_error == 0)
3207                         zio->io_error = zio_handle_device_injection(vd,
3208                             zio, EIO);
3209 
3210                 if (zio_injection_enabled && zio->io_error == 0)
3211                         zio->io_error = zio_handle_label_injection(zio, EIO);
3212 
3213                 if (zio->io_error) {
3214                         if (!vdev_accessible(vd, zio)) {
3215                                 zio->io_error = SET_ERROR(ENXIO);
3216                         } else {
3217                                 unexpected_error = B_TRUE;
3218                         }
3219                 }
3220         }
3221 
3222         ops->vdev_op_io_done(zio);
3223 
3224         if (unexpected_error)
3225                 VERIFY(vdev_probe(vd, zio) == NULL);
3226 






3227         return (ZIO_PIPELINE_CONTINUE);
3228 }
3229 
3230 /*
3231  * For non-raidz ZIOs, we can just copy aside the bad data read from the
3232  * disk, and use that to finish the checksum ereport later.
3233  */
3234 static void
3235 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
3236     const void *good_buf)
3237 {
3238         /* no processing needed */
3239         zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
3240 }
3241 
3242 /*ARGSUSED*/
3243 void
3244 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
3245 {
3246         void *buf = zio_buf_alloc(zio->io_size);
3247 
3248         abd_copy_to_buf(buf, zio->io_abd, zio->io_size);
3249 
3250         zcr->zcr_cbinfo = zio->io_size;
3251         zcr->zcr_cbdata = buf;
3252         zcr->zcr_finish = zio_vsd_default_cksum_finish;
3253         zcr->zcr_free = zio_buf_free;
3254 }
3255 
3256 static int
3257 zio_vdev_io_assess(zio_t *zio)
3258 {
3259         vdev_t *vd = zio->io_vd;
3260 
3261         if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
3262                 return (ZIO_PIPELINE_STOP);
3263         }
3264 
3265         if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3266                 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
3267 
3268         if (zio->io_vsd != NULL) {
3269                 zio->io_vsd_ops->vsd_free(zio);
3270                 zio->io_vsd = NULL;
3271         }
3272 
3273         if (zio_injection_enabled && zio->io_error == 0)
3274                 zio->io_error = zio_handle_fault_injection(zio, EIO);
3275 
3276         /*
3277          * If the I/O failed, determine whether we should attempt to retry it.
3278          *
3279          * On retry, we cut in line in the issue queue, since we don't want
3280          * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3281          */
3282         if (zio->io_error && vd == NULL &&
3283             !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {


3458 
3459         for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
3460                 if (e2 == zio_error_rank[r2])
3461                         break;
3462 
3463         return (r1 > r2 ? e1 : e2);
3464 }
3465 
3466 /*
3467  * ==========================================================================
3468  * I/O completion
3469  * ==========================================================================
3470  */
3471 static int
3472 zio_ready(zio_t *zio)
3473 {
3474         blkptr_t *bp = zio->io_bp;
3475         zio_t *pio, *pio_next;
3476         zio_link_t *zl = NULL;
3477 
3478         if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT,
3479             ZIO_WAIT_READY)) {
3480                 return (ZIO_PIPELINE_STOP);
3481         }
3482 
3483         if (zio->io_ready) {
3484                 ASSERT(IO_IS_ALLOCATING(zio));
3485                 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
3486                     (zio->io_flags & ZIO_FLAG_NOPWRITE));
3487                 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
3488 
3489                 zio->io_ready(zio);
3490         }
3491 
3492         if (bp != NULL && bp != &zio->io_bp_copy)
3493                 zio->io_bp_copy = *bp;
3494 
3495         if (zio->io_error != 0) {
3496                 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3497 
3498                 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3499                         ASSERT(IO_IS_ALLOCATING(zio));
3500                         ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3501                         /*
3502                          * We were unable to allocate anything, unreserve and
3503                          * issue the next I/O to allocate.
3504                          */
3505                         metaslab_class_throttle_unreserve(
3506                             spa_normal_class(zio->io_spa),
3507                             zio->io_prop.zp_copies, zio);
3508                         zio_allocate_dispatch(zio->io_spa);
3509                 }
3510         }
3511 
3512         mutex_enter(&zio->io_lock);
3513         zio->io_state[ZIO_WAIT_READY] = 1;
3514         pio = zio_walk_parents(zio, &zl);
3515         mutex_exit(&zio->io_lock);
3516 
3517         /*
3518          * As we notify zio's parents, new parents could be added.
3519          * New parents go to the head of zio's io_parent_list, however,
3520          * so we will (correctly) not notify them.  The remainder of zio's
3521          * io_parent_list, from 'pio_next' onward, cannot change because
3522          * all parents must wait for us to be done before they can be done.
3523          */
3524         for (; pio != NULL; pio = pio_next) {
3525                 pio_next = zio_walk_parents(zio, &zl);
3526                 zio_notify_parent(pio, zio, ZIO_WAIT_READY);
3527         }
3528 


3574         if (pio->io_child_type == ZIO_CHILD_GANG) {
3575                 /*
3576                  * If our parent is a rewrite gang child then our grandparent
3577                  * would have been the one that performed the allocation.
3578                  */
3579                 if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
3580                         pio = zio_unique_parent(pio);
3581                 flags |= METASLAB_GANG_CHILD;
3582         }
3583 
3584         ASSERT(IO_IS_ALLOCATING(pio));
3585         ASSERT3P(zio, !=, zio->io_logical);
3586         ASSERT(zio->io_logical != NULL);
3587         ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
3588         ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
3589 
3590         mutex_enter(&pio->io_lock);
3591         metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags);
3592         mutex_exit(&pio->io_lock);
3593 
3594         metaslab_class_throttle_unreserve(spa_normal_class(zio->io_spa),
3595             1, pio);
3596 
3597         /*
3598          * Call into the pipeline to see if there is more work that
3599          * needs to be done. If there is work to be done it will be
3600          * dispatched to another taskq thread.
3601          */
3602         zio_allocate_dispatch(zio->io_spa);
3603 }
3604 
3605 static int
3606 zio_done(zio_t *zio)
3607 {
3608         spa_t *spa = zio->io_spa;
3609         zio_t *lio = zio->io_logical;
3610         blkptr_t *bp = zio->io_bp;
3611         vdev_t *vd = zio->io_vd;
3612         uint64_t psize = zio->io_size;
3613         zio_t *pio, *pio_next;
3614         metaslab_class_t *mc = spa_normal_class(spa);
3615         zio_link_t *zl = NULL;
3616 
3617         /*
3618          * If our children haven't all completed,
3619          * wait for them and then repeat this pipeline stage.
3620          */
3621         if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) {



3622                 return (ZIO_PIPELINE_STOP);
3623         }
3624 
3625         /*
3626          * If the allocation throttle is enabled, then update the accounting.
3627          * We only track child I/Os that are part of an allocating async
3628          * write. We must do this since the allocation is performed
3629          * by the logical I/O but the actual write is done by child I/Os.
3630          */
3631         if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
3632             zio->io_child_type == ZIO_CHILD_VDEV) {
3633                 ASSERT(mc->mc_alloc_throttle_enabled);
3634                 zio_dva_throttle_done(zio);
3635         }
3636 
3637         /*
3638          * If the allocation throttle is enabled, verify that
3639          * we have decremented the refcounts for every I/O that was throttled.
3640          */
3641         if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3642                 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3643                 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);


3893         zl = NULL;
3894         for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
3895                 zio_link_t *remove_zl = zl;
3896                 pio_next = zio_walk_parents(zio, &zl);
3897                 zio_remove_child(pio, zio, remove_zl);
3898                 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
3899         }
3900 
3901         if (zio->io_waiter != NULL) {
3902                 mutex_enter(&zio->io_lock);
3903                 zio->io_executor = NULL;
3904                 cv_broadcast(&zio->io_cv);
3905                 mutex_exit(&zio->io_lock);
3906         } else {
3907                 zio_destroy(zio);
3908         }
3909 
3910         return (ZIO_PIPELINE_STOP);
3911 }
3912 




























3913 /*
3914  * ==========================================================================
3915  * I/O pipeline definition
3916  * ==========================================================================
3917  */
3918 static zio_pipe_stage_t *zio_pipeline[] = {
3919         NULL,
3920         zio_read_bp_init,
3921         zio_write_bp_init,
3922         zio_free_bp_init,
3923         zio_issue_async,
3924         zio_write_compress,
3925         zio_checksum_generate,
3926         zio_nop_write,
3927         zio_ddt_read_start,
3928         zio_ddt_read_done,
3929         zio_ddt_write,
3930         zio_ddt_free,
3931         zio_gang_assemble,
3932         zio_gang_issue,


   1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.

  25  * Copyright (c) 2014 Integros [integros.com]
  26  * Copyright 2017 Nexenta Systems, Inc. All rights reserved.
  27  */
  28 
  29 #include <sys/sysmacros.h>
  30 #include <sys/zfs_context.h>
  31 #include <sys/fm/fs/zfs.h>
  32 #include <sys/spa.h>
  33 #include <sys/txg.h>
  34 #include <sys/spa_impl.h>
  35 #include <sys/vdev_impl.h>
  36 #include <sys/zio_impl.h>
  37 #include <sys/zio_compress.h>
  38 #include <sys/zio_checksum.h>
  39 #include <sys/dmu_objset.h>
  40 #include <sys/arc.h>
  41 #include <sys/ddt.h>
  42 #include <sys/blkptr.h>
  43 #include <sys/special.h>
  44 #include <sys/blkptr.h>
  45 #include <sys/zfeature.h>
  46 #include <sys/dkioc_free_util.h>
  47 #include <sys/dsl_scan.h>
  48 
  49 #include <sys/metaslab_impl.h>
  50 #include <sys/abd.h>
  51 
  52 extern int zfs_txg_timeout;
  53 
  54 /*
  55  * ==========================================================================
  56  * I/O type descriptions
  57  * ==========================================================================
  58  */
  59 const char *zio_type_name[ZIO_TYPES] = {
  60         "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
  61         "zio_ioctl"
  62 };
  63 
  64 boolean_t zio_dva_throttle_enabled = B_TRUE;
  65 
  66 /*
  67  * ==========================================================================
  68  * I/O kmem caches
  69  * ==========================================================================
  70  */
  71 kmem_cache_t *zio_cache;
  72 kmem_cache_t *zio_link_cache;
  73 kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
  74 kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
  75 
  76 #ifdef _KERNEL
  77 extern vmem_t *zio_alloc_arena;
  78 #endif
  79 



  80 #define BP_SPANB(indblkshift, level) \
  81         (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
  82 #define COMPARE_META_LEVEL      0x80000000ul
  83 
  84 /*
  85  * The following actions directly effect the spa's sync-to-convergence logic.
  86  * The values below define the sync pass when we start performing the action.
  87  * Care should be taken when changing these values as they directly impact
  88  * spa_sync() performance. Tuning these values may introduce subtle performance
  89  * pathologies and should only be done in the context of performance analysis.
  90  * These tunables will eventually be removed and replaced with #defines once
  91  * enough analysis has been done to determine optimal values.
  92  *
  93  * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
  94  * regular blocks are not deferred.
  95  */
  96 int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
  97 int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
  98 int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
  99 
 100 /*
 101  * An allocating zio is one that either currently has the DVA allocate
 102  * stage set or will have it later in its lifetime.
 103  */
 104 #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
 105 
 106 boolean_t       zio_requeue_io_start_cut_in_line = B_TRUE;
 107 
 108 #ifdef ZFS_DEBUG
 109 int zio_buf_debug_limit = 16384;
 110 #else
 111 int zio_buf_debug_limit = 0;
 112 #endif
 113 
 114 /*
 115  * Fault insertion for stress testing
 116  */
 117 int zio_faulty_vdev_enabled = 0;
 118 uint64_t zio_faulty_vdev_guid;
 119 uint64_t zio_faulty_vdev_delay_us = 1000000;    /* 1 second */
 120 
 121 /*
 122  * Tunable to allow for debugging SCSI UNMAP/SATA TRIM calls. Disabling
 123  * it will prevent ZFS from attempting to issue DKIOCFREE ioctls to the
 124  * underlying storage.
 125  */
 126 boolean_t zfs_trim = B_TRUE;
 127 uint64_t zfs_trim_min_ext_sz = 1 << 20;   /* 1 MB */
 128 
 129 static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
 130 
 131 void
 132 zio_init(void)
 133 {
 134         size_t c;
 135         vmem_t *data_alloc_arena = NULL;
 136 
 137 #ifdef _KERNEL
 138         data_alloc_arena = zio_alloc_arena;
 139 #endif
 140         zio_cache = kmem_cache_create("zio_cache",
 141             sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
 142         zio_link_cache = kmem_cache_create("zio_link_cache",
 143             sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
 144 
 145         /*
 146          * For small buffers, we want a cache for each multiple of
 147          * SPA_MINBLOCKSIZE.  For larger buffers, we want a cache
 148          * for each quarter-power of 2.


 184                          * stored with the buffers.
 185                          */
 186                         (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
 187                         zio_data_buf_cache[c] = kmem_cache_create(name, size,
 188                             align, NULL, NULL, NULL, NULL, data_alloc_arena,
 189                             cflags | KMC_NOTOUCH);
 190                 }
 191         }
 192 
 193         while (--c != 0) {
 194                 ASSERT(zio_buf_cache[c] != NULL);
 195                 if (zio_buf_cache[c - 1] == NULL)
 196                         zio_buf_cache[c - 1] = zio_buf_cache[c];
 197 
 198                 ASSERT(zio_data_buf_cache[c] != NULL);
 199                 if (zio_data_buf_cache[c - 1] == NULL)
 200                         zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
 201         }
 202 
 203         zio_inject_init();
 204 
 205 }
 206 
 207 void
 208 zio_fini(void)
 209 {
 210         size_t c;
 211         kmem_cache_t *last_cache = NULL;
 212         kmem_cache_t *last_data_cache = NULL;
 213 
 214         for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
 215                 if (zio_buf_cache[c] != last_cache) {
 216                         last_cache = zio_buf_cache[c];
 217                         kmem_cache_destroy(zio_buf_cache[c]);
 218                 }
 219                 zio_buf_cache[c] = NULL;
 220 
 221                 if (zio_data_buf_cache[c] != last_data_cache) {
 222                         last_data_cache = zio_data_buf_cache[c];
 223                         kmem_cache_destroy(zio_data_buf_cache[c]);
 224                 }


 447 {
 448         ASSERT(zl->zl_parent == pio);
 449         ASSERT(zl->zl_child == cio);
 450 
 451         mutex_enter(&cio->io_lock);
 452         mutex_enter(&pio->io_lock);
 453 
 454         list_remove(&pio->io_child_list, zl);
 455         list_remove(&cio->io_parent_list, zl);
 456 
 457         pio->io_child_count--;
 458         cio->io_parent_count--;
 459 
 460         mutex_exit(&pio->io_lock);
 461         mutex_exit(&cio->io_lock);
 462 
 463         kmem_cache_free(zio_link_cache, zl);
 464 }
 465 
 466 static boolean_t
 467 zio_wait_for_children(zio_t *zio, enum zio_child child, enum zio_wait_type wait)
 468 {
 469         uint64_t *countp = &zio->io_children[child][wait];
 470         boolean_t waiting = B_FALSE;
 471 
 472         mutex_enter(&zio->io_lock);
 473         ASSERT(zio->io_stall == NULL);





 474         if (*countp != 0) {
 475                 zio->io_stage >>= 1;
 476                 ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
 477                 zio->io_stall = countp;
 478                 waiting = B_TRUE;

 479         }

 480         mutex_exit(&zio->io_lock);
 481 
 482         return (waiting);
 483 }
 484 
 485 static void
 486 zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
 487 {
 488         uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
 489         int *errorp = &pio->io_child_error[zio->io_child_type];
 490 
 491         mutex_enter(&pio->io_lock);
 492         if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
 493                 *errorp = zio_worst_error(*errorp, zio->io_error);
 494         pio->io_reexecute |= zio->io_reexecute;
 495         ASSERT3U(*countp, >, 0);
 496 
 497         (*countp)--;
 498 
 499         if (*countp == 0 && pio->io_stall == countp) {
 500                 zio_taskq_type_t type =
 501                     pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :


 619         zio->io_private = private;
 620         zio->io_type = type;
 621         zio->io_priority = priority;
 622         zio->io_vd = vd;
 623         zio->io_offset = offset;
 624         zio->io_orig_abd = zio->io_abd = data;
 625         zio->io_orig_size = zio->io_size = psize;
 626         zio->io_lsize = lsize;
 627         zio->io_orig_flags = zio->io_flags = flags;
 628         zio->io_orig_stage = zio->io_stage = stage;
 629         zio->io_orig_pipeline = zio->io_pipeline = pipeline;
 630         zio->io_pipeline_trace = ZIO_STAGE_OPEN;
 631 
 632         zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
 633         zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
 634 
 635         if (zb != NULL)
 636                 zio->io_bookmark = *zb;
 637 
 638         if (pio != NULL) {
 639                 zio->io_mc = pio->io_mc;
 640                 if (zio->io_logical == NULL)
 641                         zio->io_logical = pio->io_logical;
 642                 if (zio->io_child_type == ZIO_CHILD_GANG)
 643                         zio->io_gang_leader = pio->io_gang_leader;
 644                 zio_add_child(pio, zio);
 645 
 646                 /* copy the smartcomp setting when creating child zio's */
 647                 bcopy(&pio->io_smartcomp, &zio->io_smartcomp,
 648                     sizeof (zio->io_smartcomp));
 649         }
 650 
 651         return (zio);
 652 }
 653 
 654 static void
 655 zio_destroy(zio_t *zio)
 656 {
 657         metaslab_trace_fini(&zio->io_alloc_list);
 658         list_destroy(&zio->io_parent_list);
 659         list_destroy(&zio->io_child_list);
 660         mutex_destroy(&zio->io_lock);
 661         cv_destroy(&zio->io_cv);
 662         kmem_cache_free(zio_cache, zio);
 663 }
 664 
 665 zio_t *
 666 zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
 667     void *private, enum zio_flag flags)
 668 {
 669         zio_t *zio;
 670 
 671         zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
 672             ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
 673             ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
 674 
 675         return (zio);
 676 }
 677 
 678 zio_t *
 679 zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
 680 {
 681         return (zio_null(NULL, spa, NULL, done, private, flags));
 682 }
 683 
 684 void
 685 zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
 686 {
 687         /*
 688          * SPECIAL-BP has two DVAs, but DVA[0] in this case is a
 689          * temporary DVA, and after migration only the DVA[1]
 690          * contains valid data. Therefore, we start walking for
 691          * these BPs from DVA[1].
 692          */
 693         int start_dva = BP_IS_SPECIAL(bp) ? 1 : 0;
 694 
 695         if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
 696                 zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
 697                     bp, (longlong_t)BP_GET_TYPE(bp));
 698         }
 699         if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
 700             BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
 701                 zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
 702                     bp, (longlong_t)BP_GET_CHECKSUM(bp));
 703         }
 704         if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
 705             BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
 706                 zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
 707                     bp, (longlong_t)BP_GET_COMPRESS(bp));
 708         }
 709         if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
 710                 zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
 711                     bp, (longlong_t)BP_GET_LSIZE(bp));
 712         }
 713         if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
 714                 zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
 715                     bp, (longlong_t)BP_GET_PSIZE(bp));
 716         }
 717 
 718         if (BP_IS_EMBEDDED(bp)) {
 719                 if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
 720                         zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
 721                             bp, (longlong_t)BPE_GET_ETYPE(bp));
 722                 }
 723         }
 724 
 725         /*







 726          * Pool-specific checks.
 727          *
 728          * Note: it would be nice to verify that the blk_birth and
 729          * BP_PHYSICAL_BIRTH() are not too large.  However, spa_freeze()
 730          * allows the birth time of log blocks (and dmu_sync()-ed blocks
 731          * that are in the log) to be arbitrarily large.
 732          */
 733         for (int i = start_dva; i < BP_GET_NDVAS(bp); i++) {
 734                 uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
 735                 if (vdevid >= spa->spa_root_vdev->vdev_children) {
 736                         zfs_panic_recover("blkptr at %p DVA %u has invalid "
 737                             "VDEV %llu",
 738                             bp, i, (longlong_t)vdevid);
 739                         continue;
 740                 }
 741                 vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
 742                 if (vd == NULL) {
 743                         zfs_panic_recover("blkptr at %p DVA %u has invalid "
 744                             "VDEV %llu",
 745                             bp, i, (longlong_t)vdevid);
 746                         continue;
 747                 }
 748                 if (vd->vdev_ops == &vdev_hole_ops) {
 749                         zfs_panic_recover("blkptr at %p DVA %u has hole "
 750                             "VDEV %llu",
 751                             bp, i, (longlong_t)vdevid);
 752                         continue;
 753                 }
 754                 if (vd->vdev_ops == &vdev_missing_ops) {
 755                         /*
 756                          * "missing" vdevs are valid during import, but we
 757                          * don't have their detailed info (e.g. asize), so
 758                          * we can't perform any more checks on them.
 759                          */
 760                         continue;
 761                 }
 762                 uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
 763                 uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
 764                 if (BP_IS_GANG(bp))
 765                         asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
 766                 if (offset + asize > vd->vdev_asize) {
 767                         zfs_panic_recover("blkptr at %p DVA %u has invalid "
 768                             "OFFSET %llu",
 769                             bp, i, (longlong_t)offset);
 770                 }
 771         }
 772 }
 773 






























 774 zio_t *
 775 zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
 776     abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
 777     zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
 778 {
 779         zio_t *zio;
 780 
 781         zfs_blkptr_verify(spa, bp);
 782 
 783         zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
 784             data, size, size, done, private,
 785             ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
 786             ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
 787             ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
 788 
 789         return (zio);
 790 }
 791 
 792 zio_t *
 793 zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
 794     abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
 795     zio_done_func_t *ready, zio_done_func_t *children_ready,
 796     zio_done_func_t *physdone, zio_done_func_t *done,
 797     void *private, zio_priority_t priority, enum zio_flag flags,
 798     const zbookmark_phys_t *zb,
 799     const zio_smartcomp_info_t *smartcomp)
 800 {
 801         zio_t *zio;
 802 
 803         ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
 804             zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
 805             zp->zp_compress >= ZIO_COMPRESS_OFF &&
 806             zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
 807             DMU_OT_IS_VALID(zp->zp_type) &&
 808             zp->zp_level < 32 &&
 809             zp->zp_copies > 0 &&
 810             zp->zp_copies <= spa_max_replication(spa));
 811 
 812         zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
 813             ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
 814             ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
 815             ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
 816 
 817         zio->io_ready = ready;
 818         zio->io_children_ready = children_ready;
 819         zio->io_physdone = physdone;
 820         zio->io_prop = *zp;
 821         if (smartcomp != NULL)
 822                 bcopy(smartcomp, &zio->io_smartcomp, sizeof (*smartcomp));
 823 
 824         /*
 825          * Data can be NULL if we are going to call zio_write_override() to
 826          * provide the already-allocated BP.  But we may need the data to
 827          * verify a dedup hit (if requested).  In this case, don't try to
 828          * dedup (just take the already-allocated BP verbatim).
 829          */
 830         if (data == NULL && zio->io_prop.zp_dedup_verify) {
 831                 zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
 832         }
 833 
 834         return (zio);
 835 }
 836 
 837 zio_t *
 838 zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
 839     uint64_t size, zio_done_func_t *done, void *private,
 840     zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
 841 {
 842         zio_t *zio;


 854         ASSERT(zio->io_type == ZIO_TYPE_WRITE);
 855         ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
 856         ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
 857         ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
 858 
 859         /*
 860          * We must reset the io_prop to match the values that existed
 861          * when the bp was first written by dmu_sync() keeping in mind
 862          * that nopwrite and dedup are mutually exclusive.
 863          */
 864         zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
 865         zio->io_prop.zp_nopwrite = nopwrite;
 866         zio->io_prop.zp_copies = copies;
 867         zio->io_bp_override = bp;
 868 }
 869 
 870 void
 871 zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
 872 {
 873 


 874         /*
 875          * The check for EMBEDDED is a performance optimization.  We
 876          * process the free here (by ignoring it) rather than
 877          * putting it on the list and then processing it in zio_free_sync().
 878          */
 879         if (BP_IS_EMBEDDED(bp))
 880                 return;
 881         metaslab_check_free(spa, bp);
 882 
 883         /*
 884          * Frees that are for the currently-syncing txg, are not going to be
 885          * deferred, and which will not need to do a read (i.e. not GANG or
 886          * DEDUP), can be processed immediately.  Otherwise, put them on the
 887          * in-memory list for later processing.
 888          */
 889         if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp) ||
 890             txg != spa->spa_syncing_txg ||
 891             spa_sync_pass(spa) >= zfs_sync_pass_deferred_free) {
 892                 bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
 893         } else {
 894                 VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0)));
 895         }
 896 }
 897 
 898 zio_t *
 899 zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
 900     enum zio_flag flags)
 901 {
 902         zio_t *zio;
 903         enum zio_stage stage = ZIO_FREE_PIPELINE;
 904 
 905         ASSERT(!BP_IS_HOLE(bp));
 906         ASSERT(spa_syncing_txg(spa) == txg);
 907         ASSERT(spa_sync_pass(spa) < zfs_sync_pass_deferred_free);
 908 
 909         if (BP_IS_EMBEDDED(bp))
 910                 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
 911 
 912         metaslab_check_free(spa, bp);
 913         arc_freed(spa, bp);
 914         dsl_scan_freed(spa, bp);
 915 
 916         /*
 917          * GANG and DEDUP blocks can induce a read (for the gang block header,
 918          * or the DDT), so issue them asynchronously so that this thread is
 919          * not tied up.
 920          */
 921         if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
 922                 stage |= ZIO_STAGE_ISSUE_ASYNC;
 923 
 924         zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
 925             BP_GET_PSIZE(bp), NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
 926             flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
 927 
 928         return (zio);
 929 }
 930 
 931 zio_t *
 932 zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
 933     zio_done_func_t *done, void *private, enum zio_flag flags)
 934 {
 935         zio_t *zio;
 936 
 937         dprintf_bp(bp, "claiming in txg %llu", txg);
 938 
 939         if (BP_IS_EMBEDDED(bp))
 940                 return (zio_null(pio, spa, NULL, NULL, NULL, 0));
 941 
 942         /*
 943          * A claim is an allocation of a specific block.  Claims are needed
 944          * to support immediate writes in the intent log.  The issue is that
 945          * immediate writes contain committed data, but in a txg that was
 946          * *not* committed.  Upon opening the pool after an unclean shutdown,
 947          * the intent log claims all blocks that contain immediate write data
 948          * so that the SPA knows they're in use.
 949          *
 950          * All claims *must* be resolved in the first txg -- before the SPA
 951          * starts allocating blocks -- so that nothing is allocated twice.
 952          * If txg == 0 we just verify that the block is claimable.
 953          */
 954         ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <, spa_first_txg(spa));
 955         ASSERT(txg == spa_first_txg(spa) || txg == 0);
 956         ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa));       /* zdb(1M) */
 957 
 958         zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
 959             BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
 960             flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
 961         ASSERT0(zio->io_queued_timestamp);
 962 
 963         return (zio);
 964 }
 965 
 966 static zio_t *
 967 zio_ioctl_with_pipeline(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
 968     zio_done_func_t *done, void *private, enum zio_flag flags,
 969     enum zio_stage pipeline)
 970 {
 971         zio_t *zio;
 972         int c;
 973 
 974         if (vd->vdev_children == 0) {
 975                 zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
 976                     ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
 977                     ZIO_STAGE_OPEN, pipeline);
 978 
 979                 zio->io_cmd = cmd;
 980         } else {
 981                 zio = zio_null(pio, spa, vd, done, private, flags);
 982                 /*
 983                  * DKIOCFREE ioctl's need some special handling on interior
 984                  * vdevs. If the device provides an ops function to handle
 985                  * recomputing dkioc_free extents, then we call it.
 986                  * Otherwise the default behavior applies, which simply fans
 987                  * out the ioctl to all component vdevs.
 988                  */
 989                 if (cmd == DKIOCFREE && vd->vdev_ops->vdev_op_trim != NULL) {
 990                         vd->vdev_ops->vdev_op_trim(vd, zio, private);
 991                 } else {
 992                         for (c = 0; c < vd->vdev_children; c++)
 993                                 zio_nowait(zio_ioctl_with_pipeline(zio,
 994                                     spa, vd->vdev_child[c], cmd, NULL,
 995                                     private, flags, pipeline));
 996                 }
 997         }
 998 
 999         return (zio);
1000 }
1001 
1002 zio_t *
1003 zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
1004     zio_done_func_t *done, void *private, enum zio_flag flags)
1005 {
1006         return (zio_ioctl_with_pipeline(pio, spa, vd, cmd, done,
1007             private, flags, ZIO_IOCTL_PIPELINE));
1008 }
1009 
1010 /*
1011  * Callback for when a trim zio has completed. This simply frees the
1012  * dkioc_free_list_t extent list of the DKIOCFREE ioctl.
1013  */
1014 static void
1015 zio_trim_done(zio_t *zio)
1016 {
1017         VERIFY(zio->io_private != NULL);
1018         dfl_free(zio->io_private);
1019 }
1020 
1021 static void
1022 zio_trim_check(uint64_t start, uint64_t len, void *msp)
1023 {
1024         metaslab_t *ms = msp;
1025         boolean_t held = MUTEX_HELD(&ms->ms_lock);
1026         if (!held)
1027                 mutex_enter(&ms->ms_lock);
1028         ASSERT(ms->ms_trimming_ts != NULL);
1029         ASSERT(range_tree_contains(ms->ms_trimming_ts->ts_tree,
1030             start - VDEV_LABEL_START_SIZE, len));
1031         if (!held)
1032                 mutex_exit(&ms->ms_lock);
1033 }
1034 
1035 /*
1036  * Takes a bunch of freed extents and tells the underlying vdevs that the
1037  * space associated with these extents can be released.
1038  * This is used by flash storage to pre-erase blocks for rapid reuse later
1039  * and thin-provisioned block storage to reclaim unused blocks.
1040  */
1041 zio_t *
1042 zio_trim(spa_t *spa, vdev_t *vd, struct range_tree *tree,
1043     zio_done_func_t *done, void *private, enum zio_flag flags,
1044     int trim_flags, metaslab_t *msp)
1045 {
1046         dkioc_free_list_t *dfl = NULL;
1047         range_seg_t *rs;
1048         uint64_t rs_idx;
1049         uint64_t num_exts;
1050         uint64_t bytes_issued = 0, bytes_skipped = 0, exts_skipped = 0;
1051         /*
1052          * We need this to invoke the caller's `done' callback with the
1053          * correct io_private (not the dkioc_free_list_t, which is needed
1054          * by the underlying DKIOCFREE ioctl).
1055          */
1056         zio_t *sub_pio = zio_root(spa, done, private, flags);
1057 
1058         ASSERT(range_tree_space(tree) != 0);
1059 
1060         if (!zfs_trim)
1061                 return (sub_pio);
1062 
1063         num_exts = avl_numnodes(&tree->rt_root);
1064         dfl = kmem_zalloc(DFL_SZ(num_exts), KM_SLEEP);
1065         dfl->dfl_flags = trim_flags;
1066         dfl->dfl_num_exts = num_exts;
1067         dfl->dfl_offset = VDEV_LABEL_START_SIZE;
1068         if (msp) {
1069                 dfl->dfl_ck_func = zio_trim_check;
1070                 dfl->dfl_ck_arg = msp;
1071         }
1072 
1073         for (rs = avl_first(&tree->rt_root), rs_idx = 0; rs != NULL;
1074             rs = AVL_NEXT(&tree->rt_root, rs)) {
1075                 uint64_t len = rs->rs_end - rs->rs_start;
1076 
1077                 if (len < zfs_trim_min_ext_sz) {
1078                         bytes_skipped += len;
1079                         exts_skipped++;
1080                         continue;
1081                 }
1082 
1083                 dfl->dfl_exts[rs_idx].dfle_start = rs->rs_start;
1084                 dfl->dfl_exts[rs_idx].dfle_length = len;
1085 
1086                 // check we're a multiple of the vdev ashift
1087                 ASSERT0(dfl->dfl_exts[rs_idx].dfle_start &
1088                     ((1 << vd->vdev_ashift) - 1));
1089                 ASSERT0(dfl->dfl_exts[rs_idx].dfle_length &
1090                     ((1 << vd->vdev_ashift) - 1));
1091 
1092                 rs_idx++;
1093                 bytes_issued += len;
1094         }
1095 
1096         spa_trimstats_update(spa, rs_idx, bytes_issued, exts_skipped,
1097             bytes_skipped);
1098 
1099         /* the zfs_trim_min_ext_sz filter may have shortened the list */
1100         if (dfl->dfl_num_exts != rs_idx) {
1101                 dkioc_free_list_t *dfl2 = kmem_zalloc(DFL_SZ(rs_idx), KM_SLEEP);
1102                 bcopy(dfl, dfl2, DFL_SZ(rs_idx));
1103                 dfl2->dfl_num_exts = rs_idx;
1104                 dfl_free(dfl);
1105                 dfl = dfl2;
1106         }
1107 
1108         zio_nowait(zio_ioctl_with_pipeline(sub_pio, spa, vd, DKIOCFREE,
1109             zio_trim_done, dfl, ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE |
1110             ZIO_FLAG_DONT_RETRY, ZIO_TRIM_PIPELINE));
1111         return (sub_pio);
1112 }
1113 
1114 zio_t *
1115 zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1116     abd_t *data, int checksum, zio_done_func_t *done, void *private,
1117     zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1118 {
1119         zio_t *zio;
1120 
1121         ASSERT(vd->vdev_children == 0);
1122         ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1123             offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1124         ASSERT3U(offset + size, <=, vd->vdev_psize);
1125 
1126         zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1127             private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1128             offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1129 
1130         zio->io_prop.zp_checksum = checksum;
1131 
1132         return (zio);
1133 }
1134 


1160                 abd_t *wbuf = abd_alloc_sametype(data, size);
1161                 abd_copy(wbuf, data, size);
1162 
1163                 zio_push_transform(zio, wbuf, size, size, NULL);
1164         }
1165 
1166         return (zio);
1167 }
1168 
1169 /*
1170  * Create a child I/O to do some work for us.
1171  */
1172 zio_t *
1173 zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1174     abd_t *data, uint64_t size, int type, zio_priority_t priority,
1175     enum zio_flag flags, zio_done_func_t *done, void *private)
1176 {
1177         enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1178         zio_t *zio;
1179 
1180         ASSERT(vd->vdev_parent ==
1181             (pio->io_vd ? pio->io_vd : pio->io_spa->spa_root_vdev));







1182 











1183         if (type == ZIO_TYPE_READ && bp != NULL) {
1184                 /*
1185                  * If we have the bp, then the child should perform the
1186                  * checksum and the parent need not.  This pushes error
1187                  * detection as close to the leaves as possible and
1188                  * eliminates redundant checksums in the interior nodes.
1189                  */
1190                 pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1191                 pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1192         }
1193 
1194         if (vd->vdev_children == 0)

1195                 offset += VDEV_LABEL_START_SIZE;

1196 
1197         flags |= ZIO_VDEV_CHILD_FLAGS(pio) | ZIO_FLAG_DONT_PROPAGATE;
1198 
1199         /*
1200          * If we've decided to do a repair, the write is not speculative --
1201          * even if the original read was.
1202          */
1203         if (flags & ZIO_FLAG_IO_REPAIR)
1204                 flags &= ~ZIO_FLAG_SPECULATIVE;
1205 
1206         /*
1207          * If we're creating a child I/O that is not associated with a
1208          * top-level vdev, then the child zio is not an allocating I/O.
1209          * If this is a retried I/O then we ignore it since we will
1210          * have already processed the original allocating I/O.
1211          */
1212         if (flags & ZIO_FLAG_IO_ALLOCATING &&
1213             (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1214                 metaslab_class_t *mc = pio->io_mc;
1215 
1216                 ASSERT(mc->mc_alloc_throttle_enabled);
1217                 ASSERT(type == ZIO_TYPE_WRITE);
1218                 ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1219                 ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1220                 ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1221                     pio->io_child_type == ZIO_CHILD_GANG);
1222 
1223                 flags &= ~ZIO_FLAG_IO_ALLOCATING;
1224         }
1225 
1226         zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1227             done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1228             ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1229         ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1230 
1231         zio->io_physdone = pio->io_physdone;
1232         if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
1233                 zio->io_logical->io_phys_children++;
1234 


1275          */
1276         ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1277         if (!BP_IS_RAIDZ(zio->io_bp)) {
1278                 /* we are not doing a raw write */
1279                 ASSERT3U(zio->io_size, ==, zio->io_lsize);
1280                 zio->io_orig_size = zio->io_size = zio->io_lsize = size;
1281         }
1282 }
1283 
1284 /*
1285  * ==========================================================================
1286  * Prepare to read and write logical blocks
1287  * ==========================================================================
1288  */
1289 
1290 static int
1291 zio_read_bp_init(zio_t *zio)
1292 {
1293         blkptr_t *bp = zio->io_bp;
1294 


1295         if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1296             zio->io_child_type == ZIO_CHILD_LOGICAL &&
1297             !(zio->io_flags & ZIO_FLAG_RAW)) {
1298                 uint64_t psize =
1299                     BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1300                 zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1301                     psize, psize, zio_decompress);
1302         }
1303 
1304         if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1305                 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1306 
1307                 int psize = BPE_GET_PSIZE(bp);
1308                 void *data = abd_borrow_buf(zio->io_abd, psize);
1309                 decode_embedded_bp_compressed(bp, data);
1310                 abd_return_buf_copy(zio->io_abd, data, psize);
1311         } else {
1312                 ASSERT(!BP_IS_EMBEDDED(bp));

1313         }
1314 
1315         if (!BP_IS_METADATA(bp))
1316                 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1317 
1318         if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1319                 zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1320 
1321         if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1322                 zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1323 
1324         return (ZIO_PIPELINE_CONTINUE);
1325 }
1326 
1327 static int
1328 zio_write_bp_init(zio_t *zio)
1329 {
1330         if (!IO_IS_ALLOCATING(zio))
1331                 return (ZIO_PIPELINE_CONTINUE);
1332 
1333         ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1334 
1335         if (zio->io_bp_override) {


1383         return (ZIO_PIPELINE_CONTINUE);
1384 }
1385 
1386 static int
1387 zio_write_compress(zio_t *zio)
1388 {
1389         spa_t *spa = zio->io_spa;
1390         zio_prop_t *zp = &zio->io_prop;
1391         enum zio_compress compress = zp->zp_compress;
1392         blkptr_t *bp = zio->io_bp;
1393         uint64_t lsize = zio->io_lsize;
1394         uint64_t psize = zio->io_size;
1395         int pass = 1;
1396 
1397         EQUIV(lsize != psize, (zio->io_flags & ZIO_FLAG_RAW) != 0);
1398 
1399         /*
1400          * If our children haven't all reached the ready stage,
1401          * wait for them and then repeat this pipeline stage.
1402          */
1403         if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
1404             zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_READY))
1405                 return (ZIO_PIPELINE_STOP);

1406 
1407         if (!IO_IS_ALLOCATING(zio))
1408                 return (ZIO_PIPELINE_CONTINUE);
1409 
1410         if (zio->io_children_ready != NULL) {
1411                 /*
1412                  * Now that all our children are ready, run the callback
1413                  * associated with this zio in case it wants to modify the
1414                  * data to be written.
1415                  */
1416                 ASSERT3U(zp->zp_level, >, 0);
1417                 zio->io_children_ready(zio);
1418         }
1419 
1420         ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1421         ASSERT(zio->io_bp_override == NULL);
1422 
1423         if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1424                 /*
1425                  * We're rewriting an existing block, which means we're


1427                  * converge, it must eventually be the case that we don't
1428                  * have to allocate new blocks.  But compression changes
1429                  * the blocksize, which forces a reallocate, and makes
1430                  * convergence take longer.  Therefore, after the first
1431                  * few passes, stop compressing to ensure convergence.
1432                  */
1433                 pass = spa_sync_pass(spa);
1434 
1435                 ASSERT(zio->io_txg == spa_syncing_txg(spa));
1436                 ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1437                 ASSERT(!BP_GET_DEDUP(bp));
1438 
1439                 if (pass >= zfs_sync_pass_dont_compress)
1440                         compress = ZIO_COMPRESS_OFF;
1441 
1442                 /* Make sure someone doesn't change their mind on overwrites */
1443                 ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1444                     spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1445         }
1446 
1447         DTRACE_PROBE1(zio_compress_ready, zio_t *, zio);
1448         /* If it's a compressed write that is not raw, compress the buffer. */
1449         if (compress != ZIO_COMPRESS_OFF && psize == lsize &&
1450             ZIO_SHOULD_COMPRESS(zio)) {
1451                 void *cbuf = zio_buf_alloc(lsize);
1452                 psize = zio_compress_data(compress, zio->io_abd, cbuf, lsize);
1453                 if (psize == 0 || psize == lsize) {
1454                         compress = ZIO_COMPRESS_OFF;
1455                         zio_buf_free(cbuf, lsize);
1456                 } else if (!zp->zp_dedup && psize <= BPE_PAYLOAD_SIZE &&
1457                     zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1458                     spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1459                         encode_embedded_bp_compressed(bp,
1460                             cbuf, compress, lsize, psize);
1461                         BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1462                         BP_SET_TYPE(bp, zio->io_prop.zp_type);
1463                         BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1464                         zio_buf_free(cbuf, lsize);
1465                         bp->blk_birth = zio->io_txg;
1466                         zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1467                         ASSERT(spa_feature_is_active(spa,
1468                             SPA_FEATURE_EMBEDDED_DATA));
1469                         if (zio->io_smartcomp.sc_result != NULL) {
1470                                 zio->io_smartcomp.sc_result(
1471                                     zio->io_smartcomp.sc_userinfo, zio);
1472                         } else {
1473                                 ASSERT(zio->io_smartcomp.sc_ask == NULL);
1474                         }
1475                         return (ZIO_PIPELINE_CONTINUE);
1476                 } else {
1477                         /*
1478                          * Round up compressed size up to the ashift
1479                          * of the smallest-ashift device, and zero the tail.
1480                          * This ensures that the compressed size of the BP
1481                          * (and thus compressratio property) are correct,
1482                          * in that we charge for the padding used to fill out
1483                          * the last sector.
1484                          */
1485                         ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
1486                         size_t rounded = (size_t)P2ROUNDUP(psize,
1487                             1ULL << spa->spa_min_ashift);
1488                         if (rounded >= lsize) {
1489                                 compress = ZIO_COMPRESS_OFF;
1490                                 zio_buf_free(cbuf, lsize);
1491                                 psize = lsize;
1492                         } else {
1493                                 abd_t *cdata = abd_get_from_buf(cbuf, lsize);
1494                                 abd_take_ownership_of_buf(cdata, B_TRUE);
1495                                 abd_zero_off(cdata, psize, rounded - psize);
1496                                 psize = rounded;
1497                                 zio_push_transform(zio, cdata,
1498                                     psize, lsize, NULL);
1499                         }
1500                 }
1501 
1502                 if (zio->io_smartcomp.sc_result != NULL) {
1503                         zio->io_smartcomp.sc_result(
1504                             zio->io_smartcomp.sc_userinfo, zio);
1505                 } else {
1506                         ASSERT(zio->io_smartcomp.sc_ask == NULL);
1507                 }
1508 
1509                 /*
1510                  * We were unable to handle this as an override bp, treat
1511                  * it as a regular write I/O.
1512                  */
1513                 zio->io_bp_override = NULL;
1514                 *bp = zio->io_bp_orig;
1515                 zio->io_pipeline = zio->io_orig_pipeline;
1516         } else {
1517                 ASSERT3U(psize, !=, 0);
1518 
1519                 /*
1520                  * We are here because of:
1521                  *      - compress == ZIO_COMPRESS_OFF
1522                  *      - SmartCompression decides don't compress this data
1523                  *      - this is a RAW-write
1524                  *
1525                  *      In case of RAW-write we should not override "compress"
1526                  */
1527                 if ((zio->io_flags & ZIO_FLAG_RAW) == 0)
1528                         compress = ZIO_COMPRESS_OFF;
1529         }
1530 
1531         /*
1532          * The final pass of spa_sync() must be all rewrites, but the first
1533          * few passes offer a trade-off: allocating blocks defers convergence,
1534          * but newly allocated blocks are sequential, so they can be written
1535          * to disk faster.  Therefore, we allow the first few passes of
1536          * spa_sync() to allocate new blocks, but force rewrites after that.
1537          * There should only be a handful of blocks after pass 1 in any case.
1538          */
1539         if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
1540             BP_GET_PSIZE(bp) == psize &&
1541             pass >= zfs_sync_pass_rewrite) {
1542                 ASSERT(psize != 0);
1543                 enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1544                 zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1545                 zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1546         } else {
1547                 BP_ZERO(bp);
1548                 zio->io_pipeline = ZIO_WRITE_PIPELINE;
1549         }
1550 
1551         if (psize == 0) {
1552                 if (zio->io_bp_orig.blk_birth != 0 &&
1553                     spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
1554                         BP_SET_LSIZE(bp, lsize);
1555                         BP_SET_TYPE(bp, zp->zp_type);
1556                         BP_SET_LEVEL(bp, zp->zp_level);
1557                         BP_SET_BIRTH(bp, zio->io_txg, 0);
1558                 }
1559                 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1560         } else {
1561                 if (zp->zp_dedup) {
1562                         /* check the best-effort dedup setting */
1563                         zio_best_effort_dedup(zio);
1564                 }
1565                 ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1566                 BP_SET_LSIZE(bp, lsize);
1567                 BP_SET_TYPE(bp, zp->zp_type);
1568                 BP_SET_LEVEL(bp, zp->zp_level);
1569                 BP_SET_PSIZE(bp, psize);
1570                 BP_SET_COMPRESS(bp, compress);
1571                 BP_SET_CHECKSUM(bp, zp->zp_checksum);
1572                 BP_SET_DEDUP(bp, zp->zp_dedup);
1573                 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1574                 if (zp->zp_dedup) {
1575                         ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1576                         ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1577                         zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1578                 }
1579                 if (zp->zp_nopwrite) {
1580                         ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1581                         ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1582                         zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1583                 }
1584         }
1585         return (ZIO_PIPELINE_CONTINUE);
1586 }
1587 
1588 static int
1589 zio_free_bp_init(zio_t *zio)
1590 {
1591         blkptr_t *bp = zio->io_bp;
1592 
1593         if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1594                 if (BP_GET_DEDUP(bp))
1595                         zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1596         }
1597 


1598         return (ZIO_PIPELINE_CONTINUE);
1599 }
1600 
1601 /*
1602  * ==========================================================================
1603  * Execute the I/O pipeline
1604  * ==========================================================================
1605  */
1606 
1607 static void
1608 zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1609 {
1610         spa_t *spa = zio->io_spa;
1611         zio_type_t t = zio->io_type;
1612         int flags = (cutinline ? TQ_FRONT : 0);
1613 
1614         /*
1615          * If we're a config writer or a probe, the normal issue and
1616          * interrupt threads may all be blocked waiting for the config lock.
1617          * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1618          */
1619         if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1620                 t = ZIO_TYPE_NULL;
1621 
1622         /*
1623          * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1624          */
1625         if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1626                 t = ZIO_TYPE_NULL;
1627 
1628         /*
1629          * If this is a high priority I/O, then use the high priority taskq if
1630          * available.
1631          */
1632         if ((zio->io_priority == ZIO_PRIORITY_NOW ||
1633             zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) &&
1634             spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
1635                 q++;
1636 
1637         ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1638 
1639         /*
1640          * NB: We are assuming that the zio can only be dispatched
1641          * to a single taskq at a time.  It would be a grievous error
1642          * to dispatch the zio to another taskq at the same time.
1643          */
1644         ASSERT(zio->io_tqent.tqent_next == NULL);
1645         spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1646             flags, &zio->io_tqent);
1647 }
1648 
1649 static boolean_t
1650 zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1651 {
1652         kthread_t *executor = zio->io_executor;
1653         spa_t *spa = zio->io_spa;


1740  *      (5) the I/O is deferred due to vdev-level queueing
1741  *      (6) the I/O is handed off to another thread.
1742  *
1743  * In all cases, the pipeline stops whenever there's no CPU work; it never
1744  * burns a thread in cv_wait().
1745  *
1746  * There's no locking on io_stage because there's no legitimate way
1747  * for multiple threads to be attempting to process the same I/O.
1748  */
1749 static zio_pipe_stage_t *zio_pipeline[];
1750 
1751 void
1752 zio_execute(zio_t *zio)
1753 {
1754         zio->io_executor = curthread;
1755 
1756         ASSERT3U(zio->io_queued_timestamp, >, 0);
1757 
1758         while (zio->io_stage < ZIO_STAGE_DONE) {
1759                 enum zio_stage pipeline = zio->io_pipeline;
1760                 enum zio_stage old_stage = zio->io_stage;
1761                 enum zio_stage stage = zio->io_stage;
1762                 int rv;
1763 
1764                 ASSERT(!MUTEX_HELD(&zio->io_lock));
1765                 ASSERT(ISP2(stage));
1766                 ASSERT(zio->io_stall == NULL);
1767 
1768                 do {
1769                         stage <<= 1;
1770                 } while ((stage & pipeline) == 0);
1771 
1772                 ASSERT(stage <= ZIO_STAGE_DONE);
1773 
1774                 /*
1775                  * If we are in interrupt context and this pipeline stage
1776                  * will grab a config lock that is held across I/O,
1777                  * or may wait for an I/O that needs an interrupt thread
1778                  * to complete, issue async to avoid deadlock.
1779                  *
1780                  * For VDEV_IO_START, we cut in line so that the io will
1781                  * be sent to disk promptly.
1782                  */
1783                 if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1784                     zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1785                         boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1786                             zio_requeue_io_start_cut_in_line : B_FALSE;
1787                         zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1788                         return;
1789                 }
1790 
1791                 zio->io_stage = stage;
1792                 zio->io_pipeline_trace |= zio->io_stage;
1793                 rv = zio_pipeline[highbit64(stage) - 1](zio);
1794 
1795                 if (rv == ZIO_PIPELINE_STOP)
1796                         return;
1797 
1798                 if (rv == ZIO_PIPELINE_RESTART_STAGE) {
1799                         zio->io_stage = old_stage;
1800                         (void) zio_issue_async(zio);
1801                         return;
1802                 }
1803 
1804                 ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1805         }
1806 }
1807 
1808 /*
1809  * ==========================================================================
1810  * Initiate I/O, either sync or async
1811  * ==========================================================================
1812  */
1813 int
1814 zio_wait(zio_t *zio)
1815 {
1816         int error;
1817 
1818         ASSERT3P(zio->io_stage, ==, ZIO_STAGE_OPEN);
1819         ASSERT3P(zio->io_executor, ==, NULL);
1820 
1821         zio->io_waiter = curthread;
1822         ASSERT0(zio->io_queued_timestamp);
1823         zio->io_queued_timestamp = gethrtime();


2264 static int
2265 zio_gang_assemble(zio_t *zio)
2266 {
2267         blkptr_t *bp = zio->io_bp;
2268 
2269         ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2270         ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2271 
2272         zio->io_gang_leader = zio;
2273 
2274         zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2275 
2276         return (ZIO_PIPELINE_CONTINUE);
2277 }
2278 
2279 static int
2280 zio_gang_issue(zio_t *zio)
2281 {
2282         blkptr_t *bp = zio->io_bp;
2283 
2284         if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE))
2285                 return (ZIO_PIPELINE_STOP);

2286 
2287         ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2288         ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2289 
2290         if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2291                 zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
2292                     0);
2293         else
2294                 zio_gang_tree_free(&zio->io_gang_tree);
2295 
2296         zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2297 
2298         return (ZIO_PIPELINE_CONTINUE);
2299 }
2300 
2301 static void
2302 zio_write_gang_member_ready(zio_t *zio)
2303 {
2304         zio_t *pio = zio_unique_parent(zio);
2305         zio_t *gio = zio->io_gang_leader;


2321         mutex_enter(&pio->io_lock);
2322         for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
2323                 ASSERT(DVA_GET_GANG(&pdva[d]));
2324                 asize = DVA_GET_ASIZE(&pdva[d]);
2325                 asize += DVA_GET_ASIZE(&cdva[d]);
2326                 DVA_SET_ASIZE(&pdva[d], asize);
2327         }
2328         mutex_exit(&pio->io_lock);
2329 }
2330 
2331 static void
2332 zio_write_gang_done(zio_t *zio)
2333 {
2334         abd_put(zio->io_abd);
2335 }
2336 
2337 static int
2338 zio_write_gang_block(zio_t *pio)
2339 {
2340         spa_t *spa = pio->io_spa;
2341         metaslab_class_t *mc = pio->io_mc;
2342         blkptr_t *bp = pio->io_bp;
2343         zio_t *gio = pio->io_gang_leader;
2344         zio_t *zio;
2345         zio_gang_node_t *gn, **gnpp;
2346         zio_gbh_phys_t *gbh;
2347         abd_t *gbh_abd;
2348         uint64_t txg = pio->io_txg;
2349         uint64_t resid = pio->io_size;
2350         uint64_t lsize;
2351         int copies = gio->io_prop.zp_copies;
2352         int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2353         zio_prop_t zp;
2354         int error;
2355 
2356         int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
2357         if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2358                 ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2359                 ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2360 
2361                 flags |= METASLAB_ASYNC_ALLOC;


2418          * Create and nowait the gang children.
2419          */
2420         for (int g = 0; resid != 0; resid -= lsize, g++) {
2421                 lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2422                     SPA_MINBLOCKSIZE);
2423                 ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2424 
2425                 zp.zp_checksum = gio->io_prop.zp_checksum;
2426                 zp.zp_compress = ZIO_COMPRESS_OFF;
2427                 zp.zp_type = DMU_OT_NONE;
2428                 zp.zp_level = 0;
2429                 zp.zp_copies = gio->io_prop.zp_copies;
2430                 zp.zp_dedup = B_FALSE;
2431                 zp.zp_dedup_verify = B_FALSE;
2432                 zp.zp_nopwrite = B_FALSE;
2433 
2434                 zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
2435                     abd_get_offset(pio->io_abd, pio->io_size - resid), lsize,
2436                     lsize, &zp, zio_write_gang_member_ready, NULL, NULL,
2437                     zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
2438                     ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark,
2439                     &pio->io_smartcomp);
2440 
2441                 cio->io_mc = mc;
2442 
2443                 if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2444                         ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2445                         ASSERT(!(pio->io_flags & ZIO_FLAG_NODATA));
2446 
2447                         /*
2448                          * Gang children won't throttle but we should
2449                          * account for their work, so reserve an allocation
2450                          * slot for them here.
2451                          */
2452                         VERIFY(metaslab_class_throttle_reserve(mc,
2453                             zp.zp_copies, cio, flags));
2454                 }
2455                 zio_nowait(cio);
2456         }
2457 
2458         /*
2459          * Set pio's pipeline to just wait for zio to finish.
2460          */
2461         pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2462 


2589                             abd_alloc_for_io(zio->io_size, B_TRUE),
2590                             zio->io_size, zio_ddt_child_read_done, dde,
2591                             zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
2592                             ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
2593                 }
2594                 return (ZIO_PIPELINE_CONTINUE);
2595         }
2596 
2597         zio_nowait(zio_read(zio, zio->io_spa, bp,
2598             zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
2599             ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2600 
2601         return (ZIO_PIPELINE_CONTINUE);
2602 }
2603 
2604 static int
2605 zio_ddt_read_done(zio_t *zio)
2606 {
2607         blkptr_t *bp = zio->io_bp;
2608 
2609         if (zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE))
2610                 return (ZIO_PIPELINE_STOP);

2611 
2612         ASSERT(BP_GET_DEDUP(bp));
2613         ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2614         ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2615 
2616         if (zio->io_child_error[ZIO_CHILD_DDT]) {
2617                 ddt_t *ddt = ddt_select(zio->io_spa, bp);
2618                 ddt_entry_t *dde = zio->io_vsd;
2619                 if (ddt == NULL) {
2620                         ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2621                         return (ZIO_PIPELINE_CONTINUE);
2622                 }
2623                 if (dde == NULL) {
2624                         zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
2625                         zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2626                         return (ZIO_PIPELINE_STOP);
2627                 }
2628                 if (dde->dde_repair_abd != NULL) {
2629                         abd_copy(zio->io_abd, dde->dde_repair_abd,
2630                             zio->io_size);
2631                         zio->io_child_error[ZIO_CHILD_DDT] = 0;
2632                 }
2633                 ddt_repair_done(ddt, dde);
2634                 zio->io_vsd = NULL;
2635         }
2636 
2637         ASSERT(zio->io_vsd == NULL);
2638 
2639         return (ZIO_PIPELINE_CONTINUE);
2640 }
2641 
2642 /* ARGSUSED */
2643 static boolean_t
2644 zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2645 {
2646         spa_t *spa = zio->io_spa;
2647         boolean_t do_raw = (zio->io_flags & ZIO_FLAG_RAW);
2648 
2649         /* We should never get a raw, override zio */
2650         ASSERT(!(zio->io_bp_override && do_raw));
2651 
2652         /*
2653          * Note: we compare the original data, not the transformed data,
2654          * because when zio->io_bp is an override bp, we will not have
2655          * pushed the I/O transforms.  That's an important optimization
2656          * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2657          */
2658         for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2659                 zio_t *lio = dde->dde_lead_zio[p];
2660 
2661                 if (lio != NULL) {
2662                         return (lio->io_orig_size != zio->io_orig_size ||
2663                             abd_cmp(zio->io_orig_abd, lio->io_orig_abd,
2664                             zio->io_orig_size) != 0);
2665                 }
2666         }
2667 
2668         for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2669                 ddt_phys_t *ddp = &dde->dde_phys[p];
2670 
2671                 if (ddp->ddp_phys_birth != 0) {
2672                         arc_buf_t *abuf = NULL;
2673                         arc_flags_t aflags = ARC_FLAG_WAIT;
2674                         int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
2675                         blkptr_t blk = *zio->io_bp;
2676                         int error;
2677 
2678                         ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2679 
2680                         dde_exit(dde);
2681 
2682                         /*
2683                          * Intuitively, it would make more sense to compare
2684                          * io_abd than io_orig_abd in the raw case since you
2685                          * don't want to look at any transformations that have
2686                          * happened to the data. However, for raw I/Os the
2687                          * data will actually be the same in io_abd and
2688                          * io_orig_abd, so all we have to do is issue this as
2689                          * a raw ARC read.
2690                          */
2691                         if (do_raw) {
2692                                 zio_flags |= ZIO_FLAG_RAW;
2693                                 ASSERT3U(zio->io_size, ==, zio->io_orig_size);
2694                                 ASSERT0(abd_cmp(zio->io_abd, zio->io_orig_abd,
2695                                     zio->io_size));
2696                                 ASSERT3P(zio->io_transform_stack, ==, NULL);
2697                         }
2698 
2699                         error = arc_read(NULL, spa, &blk,
2700                             arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
2701                             zio_flags, &aflags, &zio->io_bookmark);
2702 
2703                         if (error == 0) {
2704                                 if (arc_buf_size(abuf) != zio->io_orig_size ||
2705                                     abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
2706                                     zio->io_orig_size) != 0)
2707                                         error = SET_ERROR(EEXIST);
2708                                 arc_buf_destroy(abuf, &abuf);
2709                         }
2710 
2711                         dde_enter(dde);
2712                         return (error != 0);
2713                 }
2714         }
2715 
2716         return (B_FALSE);
2717 }
2718 
2719 static void
2720 zio_ddt_child_write_ready(zio_t *zio)
2721 {
2722         int p = zio->io_prop.zp_copies;

2723         ddt_entry_t *dde = zio->io_private;
2724         ddt_phys_t *ddp = &dde->dde_phys[p];
2725         zio_t *pio;
2726 
2727         if (zio->io_error)
2728                 return;
2729 
2730         dde_enter(dde);
2731 
2732         ASSERT(dde->dde_lead_zio[p] == zio);
2733 
2734         ddt_phys_fill(ddp, zio->io_bp);
2735 
2736         zio_link_t *zl = NULL;
2737         while ((pio = zio_walk_parents(zio, &zl)) != NULL)
2738                 ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2739 
2740         dde_exit(dde);
2741 }
2742 
2743 static void
2744 zio_ddt_child_write_done(zio_t *zio)
2745 {
2746         int p = zio->io_prop.zp_copies;

2747         ddt_entry_t *dde = zio->io_private;
2748         ddt_phys_t *ddp = &dde->dde_phys[p];
2749 
2750         dde_enter(dde);
2751 
2752         ASSERT(ddp->ddp_refcnt == 0);
2753         ASSERT(dde->dde_lead_zio[p] == zio);
2754         dde->dde_lead_zio[p] = NULL;
2755 
2756         if (zio->io_error == 0) {
2757                 zio_link_t *zl = NULL;
2758                 while (zio_walk_parents(zio, &zl) != NULL)
2759                         ddt_phys_addref(ddp);
2760         } else {
2761                 ddt_phys_clear(ddp);
2762         }
2763 
2764         dde_exit(dde);
2765 }
2766 
2767 static void
2768 zio_ddt_ditto_write_done(zio_t *zio)
2769 {
2770         int p = DDT_PHYS_DITTO;
2771         zio_prop_t *zp = &zio->io_prop;
2772         blkptr_t *bp = zio->io_bp;
2773         ddt_t *ddt = ddt_select(zio->io_spa, bp);
2774         ddt_entry_t *dde = zio->io_private;
2775         ddt_phys_t *ddp = &dde->dde_phys[p];
2776         ddt_key_t *ddk = &dde->dde_key;
2777 
2778         dde_enter(dde);
2779 
2780         ASSERT(ddp->ddp_refcnt == 0);
2781         ASSERT(dde->dde_lead_zio[p] == zio);
2782         dde->dde_lead_zio[p] = NULL;
2783 
2784         if (zio->io_error == 0) {
2785                 ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2786                 ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2787                 ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2788                 if (ddp->ddp_phys_birth != 0)
2789                         ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2790                 ddt_phys_fill(ddp, bp);
2791         }
2792 
2793         dde_exit(dde);
2794 }
2795 
2796 static int
2797 zio_ddt_write(zio_t *zio)
2798 {
2799         spa_t *spa = zio->io_spa;
2800         blkptr_t *bp = zio->io_bp;
2801         uint64_t txg = zio->io_txg;
2802         zio_prop_t *zp = &zio->io_prop;
2803         int p = zp->zp_copies;
2804         int ditto_copies;
2805         zio_t *cio = NULL;
2806         zio_t *dio = NULL;
2807         ddt_t *ddt = ddt_select(spa, bp);
2808         ddt_entry_t *dde;
2809         ddt_phys_t *ddp;
2810 
2811         ASSERT(BP_GET_DEDUP(bp));
2812         ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2813         ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
2814         ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
2815 

2816         dde = ddt_lookup(ddt, bp, B_TRUE);

2817 
2818         /*
2819          * If we're not using special tier, for each new DDE that's not on disk:
2820          * disable dedup if we have exhausted "allowed" DDT L2/ARC space
2821          */
2822         if ((dde->dde_state & DDE_NEW) && !spa->spa_usesc &&
2823             (zfs_ddt_limit_type != DDT_NO_LIMIT || zfs_ddt_byte_ceiling != 0)) {
2824                 /* turn off dedup if we need to stop DDT growth */
2825                 if (spa_enable_dedup_cap(spa)) {
2826                         dde->dde_state |= DDE_DONT_SYNC;
2827 
2828                         /* disable dedup and use the ordinary write pipeline */
2829                         zio_pop_transforms(zio);
2830                         zp->zp_dedup = zp->zp_dedup_verify = B_FALSE;
2831                         zio->io_stage = ZIO_STAGE_OPEN;
2832                         zio->io_pipeline = ZIO_WRITE_PIPELINE;
2833                         zio->io_bp_override = NULL;
2834                         BP_ZERO(bp);
2835                         dde_exit(dde);
2836 
2837                         return (ZIO_PIPELINE_CONTINUE);
2838                 }
2839         }
2840         ASSERT(!(dde->dde_state & DDE_DONT_SYNC));
2841 
2842         if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2843                 /*
2844                  * If we're using a weak checksum, upgrade to a strong checksum
2845                  * and try again.  If we're already using a strong checksum,
2846                  * we can't resolve it, so just convert to an ordinary write.
2847                  * (And automatically e-mail a paper to Nature?)
2848                  */
2849                 if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
2850                     ZCHECKSUM_FLAG_DEDUP)) {
2851                         zp->zp_checksum = spa_dedup_checksum(spa);
2852                         zio_pop_transforms(zio);
2853                         zio->io_stage = ZIO_STAGE_OPEN;
2854                         BP_ZERO(bp);
2855                 } else {
2856                         zp->zp_dedup = B_FALSE;
2857                         BP_SET_DEDUP(bp, B_FALSE);
2858                 }
2859                 ASSERT(!BP_GET_DEDUP(bp));
2860                 zio->io_pipeline = ZIO_WRITE_PIPELINE;
2861                 dde_exit(dde);
2862                 return (ZIO_PIPELINE_CONTINUE);
2863         }
2864 
2865         ddp = &dde->dde_phys[p];
2866         ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2867         ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2868 
2869         if (ditto_copies > ddt_ditto_copies_present(dde) &&
2870             dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2871                 zio_prop_t czp = *zp;
2872 
2873                 czp.zp_copies = ditto_copies;
2874 
2875                 /*
2876                  * If we arrived here with an override bp, we won't have run
2877                  * the transform stack, so we won't have the data we need to
2878                  * generate a child i/o.  So, toss the override bp and restart.
2879                  * This is safe, because using the override bp is just an
2880                  * optimization; and it's rare, so the cost doesn't matter.
2881                  */
2882                 if (zio->io_bp_override) {
2883                         zio_pop_transforms(zio);
2884                         zio->io_stage = ZIO_STAGE_OPEN;
2885                         zio->io_pipeline = ZIO_WRITE_PIPELINE;
2886                         zio->io_bp_override = NULL;
2887                         BP_ZERO(bp);
2888                         dde_exit(dde);
2889                         return (ZIO_PIPELINE_CONTINUE);
2890                 }
2891 
2892                 dio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
2893                     zio->io_orig_size, zio->io_orig_size, &czp, NULL, NULL,
2894                     NULL, zio_ddt_ditto_write_done, dde, zio->io_priority,
2895                     ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark, NULL);
2896 
2897                 zio_push_transform(dio, zio->io_abd, zio->io_size, 0, NULL);
2898                 dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
2899         }
2900 
2901         if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
2902                 if (ddp->ddp_phys_birth != 0)
2903                         ddt_bp_fill(ddp, bp, txg);
2904                 if (dde->dde_lead_zio[p] != NULL)
2905                         zio_add_child(zio, dde->dde_lead_zio[p]);
2906                 else
2907                         ddt_phys_addref(ddp);
2908         } else if (zio->io_bp_override) {
2909                 ASSERT(bp->blk_birth == txg);
2910                 ASSERT(BP_EQUAL(bp, zio->io_bp_override));
2911                 ddt_phys_fill(ddp, bp);
2912                 ddt_phys_addref(ddp);
2913         } else {
2914                 cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
2915                     zio->io_orig_size, zio->io_orig_size, zp,
2916                     zio_ddt_child_write_ready, NULL, NULL,
2917                     zio_ddt_child_write_done, dde, zio->io_priority,
2918                     ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark, NULL);
2919 
2920                 zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
2921                 dde->dde_lead_zio[p] = cio;
2922         }
2923 
2924         dde_exit(dde);
2925 
2926         if (cio)
2927                 zio_nowait(cio);
2928         if (dio)
2929                 zio_nowait(dio);
2930 
2931         return (ZIO_PIPELINE_CONTINUE);
2932 }
2933 
2934 ddt_entry_t *freedde; /* for debugging */
2935 
2936 static int
2937 zio_ddt_free(zio_t *zio)
2938 {
2939         spa_t *spa = zio->io_spa;
2940         blkptr_t *bp = zio->io_bp;
2941         ddt_t *ddt = ddt_select(spa, bp);
2942         ddt_entry_t *dde;
2943         ddt_phys_t *ddp;
2944 
2945         ASSERT(BP_GET_DEDUP(bp));
2946         ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2947 

2948         freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
2949         ddp = ddt_phys_select(dde, bp);
2950         if (ddp)
2951                 ddt_phys_decref(ddp);
2952         dde_exit(dde);
2953 
2954         return (ZIO_PIPELINE_CONTINUE);
2955 }
2956 
2957 /*
2958  * ==========================================================================
2959  * Allocate and free blocks
2960  * ==========================================================================
2961  */
2962 
2963 static zio_t *
2964 zio_io_to_allocate(metaslab_class_t *mc)
2965 {
2966         zio_t *zio;
2967 
2968         ASSERT(MUTEX_HELD(&mc->mc_alloc_lock));
2969 
2970         zio = avl_first(&mc->mc_alloc_tree);
2971         if (zio == NULL)
2972                 return (NULL);
2973 
2974         ASSERT(IO_IS_ALLOCATING(zio));
2975 
2976         /*
2977          * Try to place a reservation for this zio. If we're unable to
2978          * reserve then we throttle.
2979          */
2980         if (!metaslab_class_throttle_reserve(mc,
2981             zio->io_prop.zp_copies, zio, 0)) {
2982                 return (NULL);
2983         }
2984 
2985         avl_remove(&mc->mc_alloc_tree, zio);
2986         ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
2987 
2988         return (zio);
2989 }
2990 
2991 static int
2992 zio_dva_throttle(zio_t *zio)
2993 {
2994         spa_t *spa = zio->io_spa;
2995         zio_t *nio;
2996 
2997         /* We need to use parent's MetaslabClass */
2998         if (zio->io_mc == NULL) {
2999                 zio->io_mc = spa_select_class(spa, zio);
3000                 if (zio->io_prop.zp_usewbc)
3001                         return (ZIO_PIPELINE_CONTINUE);
3002         }
3003 
3004         if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
3005             !zio->io_mc->mc_alloc_throttle_enabled ||
3006             zio->io_child_type == ZIO_CHILD_GANG ||
3007             zio->io_flags & ZIO_FLAG_NODATA) {
3008                 return (ZIO_PIPELINE_CONTINUE);
3009         }
3010 
3011         ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3012 
3013         ASSERT3U(zio->io_queued_timestamp, >, 0);
3014         ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
3015 
3016         mutex_enter(&zio->io_mc->mc_alloc_lock);
3017 
3018         ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3019         avl_add(&zio->io_mc->mc_alloc_tree, zio);
3020 
3021         nio = zio_io_to_allocate(zio->io_mc);
3022         mutex_exit(&zio->io_mc->mc_alloc_lock);
3023 
3024         if (nio == zio)
3025                 return (ZIO_PIPELINE_CONTINUE);
3026 
3027         if (nio != NULL) {
3028                 ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE);
3029                 /*
3030                  * We are passing control to a new zio so make sure that
3031                  * it is processed by a different thread. We do this to
3032                  * avoid stack overflows that can occur when parents are
3033                  * throttled and children are making progress. We allow
3034                  * it to go to the head of the taskq since it's already
3035                  * been waiting.
3036                  */
3037                 zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE);
3038         }
3039         return (ZIO_PIPELINE_STOP);
3040 }
3041 
3042 void
3043 zio_allocate_dispatch(metaslab_class_t *mc)
3044 {
3045         zio_t *zio;
3046 
3047         mutex_enter(&mc->mc_alloc_lock);
3048         zio = zio_io_to_allocate(mc);
3049         mutex_exit(&mc->mc_alloc_lock);
3050         if (zio == NULL)
3051                 return;
3052 
3053         ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
3054         ASSERT0(zio->io_error);
3055         zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
3056 }
3057 
3058 static int
3059 zio_dva_allocate(zio_t *zio)
3060 {
3061         spa_t *spa = zio->io_spa;
3062         metaslab_class_t *mc = zio->io_mc;
3063 
3064         blkptr_t *bp = zio->io_bp;
3065         int error;
3066         int flags = 0;
3067 
3068         if (zio->io_gang_leader == NULL) {
3069                 ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3070                 zio->io_gang_leader = zio;
3071         }
3072 
3073         ASSERT(BP_IS_HOLE(bp));
3074         ASSERT0(BP_GET_NDVAS(bp));
3075         ASSERT3U(zio->io_prop.zp_copies, >, 0);
3076         ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
3077         ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
3078 
3079         if (zio->io_flags & ZIO_FLAG_NODATA || zio->io_prop.zp_usewbc) {
3080                 flags |= METASLAB_DONT_THROTTLE;
3081         }
3082         if (zio->io_flags & ZIO_FLAG_GANG_CHILD) {
3083                 flags |= METASLAB_GANG_CHILD;
3084         }
3085         if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE &&
3086             zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3087                 flags |= METASLAB_ASYNC_ALLOC;
3088         }
3089 
3090         error = metaslab_alloc(spa, mc, zio->io_size, bp,
3091             zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
3092             &zio->io_alloc_list, zio);
3093 
3094 #ifdef _KERNEL
3095         DTRACE_PROBE6(zio_dva_allocate,
3096             uint64_t, DVA_GET_VDEV(&bp->blk_dva[0]),
3097             uint64_t, DVA_GET_VDEV(&bp->blk_dva[1]),
3098             uint64_t, BP_GET_LEVEL(bp),
3099             boolean_t, BP_IS_SPECIAL(bp),
3100             boolean_t, BP_IS_METADATA(bp),
3101             int, error);
3102 #endif
3103 
3104         if (error != 0) {
3105                 spa_dbgmsg(spa, "%s: metaslab allocation failure: zio %p, "
3106                     "size %llu, error %d", spa_name(spa), zio, zio->io_size,
3107                     error);
3108                 if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE) {
3109                         if (zio->io_prop.zp_usewbc) {
3110                                 zio->io_prop.zp_usewbc = B_FALSE;
3111                                 zio->io_prop.zp_usesc = B_FALSE;
3112                                 zio->io_mc = spa_normal_class(spa);
3113                         }
3114 
3115                         return (zio_write_gang_block(zio));
3116                 }
3117 
3118                 zio->io_error = error;
3119         }
3120 
3121         return (ZIO_PIPELINE_CONTINUE);
3122 }
3123 
3124 static int
3125 zio_dva_free(zio_t *zio)
3126 {
3127         metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
3128 
3129         return (ZIO_PIPELINE_CONTINUE);
3130 }
3131 
3132 static int
3133 zio_dva_claim(zio_t *zio)
3134 {
3135         int error;
3136 
3137         error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);


3155         if (!BP_IS_HOLE(bp))
3156                 metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
3157 
3158         if (gn != NULL) {
3159                 for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
3160                         zio_dva_unallocate(zio, gn->gn_child[g],
3161                             &gn->gn_gbh->zg_blkptr[g]);
3162                 }
3163         }
3164 }
3165 
3166 /*
3167  * Try to allocate an intent log block.  Return 0 on success, errno on failure.
3168  */
3169 int
3170 zio_alloc_zil(spa_t *spa, uint64_t txg, blkptr_t *new_bp, blkptr_t *old_bp,
3171     uint64_t size, boolean_t *slog)
3172 {
3173         int error = 1;
3174         zio_alloc_list_t io_alloc_list;
3175         spa_meta_placement_t *mp = &spa->spa_meta_policy;
3176 
3177         ASSERT(txg > spa_syncing_txg(spa));
3178 
3179         metaslab_trace_init(&io_alloc_list);
3180 
3181         /*
3182          * ZIL blocks are always contiguous (i.e. not gang blocks)
3183          * so we set the METASLAB_HINTBP_AVOID flag so that they
3184          * don't "fast gang" when allocating them.
3185          * If the caller indicates that slog is not to be used
3186          * (via use_slog)
3187          * separate allocation class will not indeed be used,
3188          * independently of whether this is log or special
3189          */
3190 
3191         if (spa_has_slogs(spa)) {
3192                 error = metaslab_alloc(spa, spa_log_class(spa),
3193                     size, new_bp, 1, txg, old_bp,
3194                     METASLAB_HINTBP_AVOID, &io_alloc_list, NULL);
3195 
3196                 DTRACE_PROBE2(zio_alloc_zil_log,
3197                     spa_t *, spa, int, error);
3198 
3199                 if (error == 0)
3200                         *slog = TRUE;
3201         }
3202 
3203         /*
3204          * use special when failed to allocate from the regular
3205          * slog, but only if allowed and if the special used
3206          * space is  below watermarks
3207          */
3208         if (error != 0 && spa_can_special_be_used(spa) &&
3209             mp->spa_sync_to_special != SYNC_TO_SPECIAL_DISABLED) {
3210                 error = metaslab_alloc(spa, spa_special_class(spa),
3211                     size, new_bp, 1, txg, old_bp,
3212                     METASLAB_HINTBP_AVOID, &io_alloc_list, NULL);
3213 
3214                 DTRACE_PROBE2(zio_alloc_zil_special,
3215                     spa_t *, spa, int, error);
3216 
3217                 if (error == 0)
3218                         *slog = FALSE;
3219         }
3220 
3221         if (error != 0) {
3222                 error = metaslab_alloc(spa, spa_normal_class(spa), size,
3223                     new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID,
3224                     &io_alloc_list, NULL);
3225 
3226                 DTRACE_PROBE2(zio_alloc_zil_normal,
3227                     spa_t *, spa, int, error);
3228 
3229                 if (error == 0)
3230                         *slog = FALSE;
3231         }
3232 
3233         metaslab_trace_fini(&io_alloc_list);
3234 
3235         if (error == 0) {
3236                 BP_SET_LSIZE(new_bp, size);
3237                 BP_SET_PSIZE(new_bp, size);
3238                 BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
3239                 BP_SET_CHECKSUM(new_bp,
3240                     spa_version(spa) >= SPA_VERSION_SLIM_ZIL
3241                     ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
3242                 BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3243                 BP_SET_LEVEL(new_bp, 0);
3244                 BP_SET_DEDUP(new_bp, 0);
3245                 BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
3246         } else {
3247                 zfs_dbgmsg("%s: zil block allocation failure: "
3248                     "size %llu, error %d", spa_name(spa), size, error);
3249         }
3250 
3251         return (error);
3252 }


3269  * ==========================================================================
3270  */
3271 
3272 
3273 /*
3274  * Issue an I/O to the underlying vdev. Typically the issue pipeline
3275  * stops after this stage and will resume upon I/O completion.
3276  * However, there are instances where the vdev layer may need to
3277  * continue the pipeline when an I/O was not issued. Since the I/O
3278  * that was sent to the vdev layer might be different than the one
3279  * currently active in the pipeline (see vdev_queue_io()), we explicitly
3280  * force the underlying vdev layers to call either zio_execute() or
3281  * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3282  */
3283 static int
3284 zio_vdev_io_start(zio_t *zio)
3285 {
3286         vdev_t *vd = zio->io_vd;
3287         uint64_t align;
3288         spa_t *spa = zio->io_spa;
3289         zio_type_t type = zio->io_type;
3290         zio->io_vd_timestamp = gethrtime();
3291 
3292         ASSERT(zio->io_error == 0);
3293         ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
3294 
3295         if (vd == NULL) {
3296                 if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3297                         spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
3298 
3299                 /*
3300                  * The mirror_ops handle multiple DVAs in a single BP.
3301                  */
3302                 vdev_mirror_ops.vdev_op_io_start(zio);
3303                 return (ZIO_PIPELINE_STOP);
3304         }
3305 
3306         ASSERT3P(zio->io_logical, !=, zio);


3307 



























3308         align = 1ULL << vd->vdev_top->vdev_ashift;
3309 
3310         if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
3311             P2PHASE(zio->io_size, align) != 0) {
3312                 /* Transform logical writes to be a full physical block size. */
3313                 uint64_t asize = P2ROUNDUP(zio->io_size, align);
3314                 abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
3315                 ASSERT(vd == vd->vdev_top);
3316                 if (type == ZIO_TYPE_WRITE) {
3317                         abd_copy(abuf, zio->io_abd, zio->io_size);
3318                         abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
3319                 }
3320                 zio_push_transform(zio, abuf, asize, asize, zio_subblock);
3321         }
3322 
3323         /*
3324          * If this is not a physical io, make sure that it is properly aligned
3325          * before proceeding.
3326          */
3327         if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
3328                 ASSERT0(P2PHASE(zio->io_offset, align));
3329                 ASSERT0(P2PHASE(zio->io_size, align));
3330         } else {
3331                 /*
3332                  * For physical writes, we allow 512b aligned writes and assume
3333                  * the device will perform a read-modify-write as necessary.
3334                  */
3335                 ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
3336                 ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
3337         }
3338 
3339         VERIFY(type != ZIO_TYPE_WRITE || spa_writeable(spa));
3340 
3341         /*
3342          * If this is a repair I/O, and there's no self-healing involved --
3343          * that is, we're just resilvering what we expect to resilver --
3344          * then don't do the I/O unless zio's txg is actually in vd's DTL.
3345          * This prevents spurious resilvering with nested replication.
3346          * For example, given a mirror of mirrors, (A+B)+(C+D), if only
3347          * A is out of date, we'll read from C+D, then use the data to
3348          * resilver A+B -- but we don't actually want to resilver B, just A.
3349          * The top-level mirror has no way to know this, so instead we just
3350          * discard unnecessary repairs as we work our way down the vdev tree.
3351          * The same logic applies to any form of nested replication:
3352          * ditto + mirror, RAID-Z + replacing, etc.  This covers them all.
3353          */
3354         if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
3355             !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
3356             zio->io_txg != 0 &&      /* not a delegated i/o */
3357             !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
3358                 ASSERT(type == ZIO_TYPE_WRITE);
3359                 zio_vdev_io_bypass(zio);
3360                 return (ZIO_PIPELINE_CONTINUE);
3361         }
3362 
3363         if (vd->vdev_ops->vdev_op_leaf &&
3364             (type == ZIO_TYPE_READ || type == ZIO_TYPE_WRITE)) {
3365                 if (type == ZIO_TYPE_READ && vdev_cache_read(zio))

3366                         return (ZIO_PIPELINE_CONTINUE);
3367 
3368                 if ((zio = vdev_queue_io(zio)) == NULL)
3369                         return (ZIO_PIPELINE_STOP);
3370 
3371                 if (!vdev_accessible(vd, zio)) {
3372                         zio->io_error = SET_ERROR(ENXIO);
3373                         zio_interrupt(zio);
3374                         return (ZIO_PIPELINE_STOP);
3375                 }
3376 
3377                 /*
3378                  * Insert a fault simulation delay for a particular vdev.
3379                  */
3380                 if (zio_faulty_vdev_enabled &&
3381                     (zio->io_vd->vdev_guid == zio_faulty_vdev_guid)) {
3382                         delay(NSEC_TO_TICK(zio_faulty_vdev_delay_us *
3383                             (NANOSEC / MICROSEC)));
3384                 }
3385         }
3386 
3387         vd->vdev_ops->vdev_op_io_start(zio);
3388         return (ZIO_PIPELINE_STOP);
3389 }
3390 
3391 static int
3392 zio_vdev_io_done(zio_t *zio)
3393 {
3394         vdev_t *vd = zio->io_vd;
3395         vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
3396         boolean_t unexpected_error = B_FALSE;
3397 
3398         if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
3399                 return (ZIO_PIPELINE_STOP);

3400 
3401         ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
3402 
3403         if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {

3404                 vdev_queue_io_done(zio);
3405 
3406                 if (zio->io_type == ZIO_TYPE_WRITE)
3407                         vdev_cache_write(zio);
3408 
3409                 if (zio_injection_enabled && zio->io_error == 0)
3410                         zio->io_error = zio_handle_device_injection(vd,
3411                             zio, EIO);
3412 
3413                 if (zio_injection_enabled && zio->io_error == 0)
3414                         zio->io_error = zio_handle_label_injection(zio, EIO);
3415 
3416                 if (zio->io_error) {
3417                         if (!vdev_accessible(vd, zio)) {
3418                                 zio->io_error = SET_ERROR(ENXIO);
3419                         } else {
3420                                 unexpected_error = B_TRUE;
3421                         }
3422                 }
3423         }
3424 
3425         ops->vdev_op_io_done(zio);
3426 
3427         if (unexpected_error)
3428                 VERIFY(vdev_probe(vd, zio) == NULL);
3429 
3430         /*
3431          * Measure delta between start and end of the I/O in nanoseconds.
3432          * XXX: Handle overflow.
3433          */
3434         zio->io_vd_timestamp = gethrtime() - zio->io_vd_timestamp;
3435 
3436         return (ZIO_PIPELINE_CONTINUE);
3437 }
3438 
3439 /*
3440  * For non-raidz ZIOs, we can just copy aside the bad data read from the
3441  * disk, and use that to finish the checksum ereport later.
3442  */
3443 static void
3444 zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
3445     const void *good_buf)
3446 {
3447         /* no processing needed */
3448         zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
3449 }
3450 
3451 /*ARGSUSED*/
3452 void
3453 zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
3454 {
3455         void *buf = zio_buf_alloc(zio->io_size);
3456 
3457         abd_copy_to_buf(buf, zio->io_abd, zio->io_size);
3458 
3459         zcr->zcr_cbinfo = zio->io_size;
3460         zcr->zcr_cbdata = buf;
3461         zcr->zcr_finish = zio_vsd_default_cksum_finish;
3462         zcr->zcr_free = zio_buf_free;
3463 }
3464 
3465 static int
3466 zio_vdev_io_assess(zio_t *zio)
3467 {
3468         vdev_t *vd = zio->io_vd;
3469 
3470         if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE))
3471                 return (ZIO_PIPELINE_STOP);

3472 
3473         if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3474                 spa_config_exit(zio->io_spa, SCL_ZIO, zio);
3475 
3476         if (zio->io_vsd != NULL) {
3477                 zio->io_vsd_ops->vsd_free(zio);
3478                 zio->io_vsd = NULL;
3479         }
3480 
3481         if (zio_injection_enabled && zio->io_error == 0)
3482                 zio->io_error = zio_handle_fault_injection(zio, EIO);
3483 
3484         /*
3485          * If the I/O failed, determine whether we should attempt to retry it.
3486          *
3487          * On retry, we cut in line in the issue queue, since we don't want
3488          * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3489          */
3490         if (zio->io_error && vd == NULL &&
3491             !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {


3666 
3667         for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
3668                 if (e2 == zio_error_rank[r2])
3669                         break;
3670 
3671         return (r1 > r2 ? e1 : e2);
3672 }
3673 
3674 /*
3675  * ==========================================================================
3676  * I/O completion
3677  * ==========================================================================
3678  */
3679 static int
3680 zio_ready(zio_t *zio)
3681 {
3682         blkptr_t *bp = zio->io_bp;
3683         zio_t *pio, *pio_next;
3684         zio_link_t *zl = NULL;
3685 
3686         if (zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_READY) ||
3687             zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_READY))
3688                 return (ZIO_PIPELINE_STOP);

3689 
3690         if (zio->io_ready) {
3691                 ASSERT(IO_IS_ALLOCATING(zio));
3692                 ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
3693                     (zio->io_flags & ZIO_FLAG_NOPWRITE));
3694                 ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
3695 
3696                 zio->io_ready(zio);
3697         }
3698 
3699         if (bp != NULL && bp != &zio->io_bp_copy)
3700                 zio->io_bp_copy = *bp;
3701 
3702         if (zio->io_error != 0) {
3703                 zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3704 
3705                 if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3706                         ASSERT(IO_IS_ALLOCATING(zio));
3707                         ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
3708                         /*
3709                          * We were unable to allocate anything, unreserve and
3710                          * issue the next I/O to allocate.
3711                          */
3712                         metaslab_class_throttle_unreserve(zio->io_mc,

3713                             zio->io_prop.zp_copies, zio);
3714                         zio_allocate_dispatch(zio->io_mc);
3715                 }
3716         }
3717 
3718         mutex_enter(&zio->io_lock);
3719         zio->io_state[ZIO_WAIT_READY] = 1;
3720         pio = zio_walk_parents(zio, &zl);
3721         mutex_exit(&zio->io_lock);
3722 
3723         /*
3724          * As we notify zio's parents, new parents could be added.
3725          * New parents go to the head of zio's io_parent_list, however,
3726          * so we will (correctly) not notify them.  The remainder of zio's
3727          * io_parent_list, from 'pio_next' onward, cannot change because
3728          * all parents must wait for us to be done before they can be done.
3729          */
3730         for (; pio != NULL; pio = pio_next) {
3731                 pio_next = zio_walk_parents(zio, &zl);
3732                 zio_notify_parent(pio, zio, ZIO_WAIT_READY);
3733         }
3734 


3780         if (pio->io_child_type == ZIO_CHILD_GANG) {
3781                 /*
3782                  * If our parent is a rewrite gang child then our grandparent
3783                  * would have been the one that performed the allocation.
3784                  */
3785                 if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
3786                         pio = zio_unique_parent(pio);
3787                 flags |= METASLAB_GANG_CHILD;
3788         }
3789 
3790         ASSERT(IO_IS_ALLOCATING(pio));
3791         ASSERT3P(zio, !=, zio->io_logical);
3792         ASSERT(zio->io_logical != NULL);
3793         ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
3794         ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
3795 
3796         mutex_enter(&pio->io_lock);
3797         metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags);
3798         mutex_exit(&pio->io_lock);
3799 
3800         metaslab_class_throttle_unreserve(pio->io_mc, 1, pio);

3801 
3802         /*
3803          * Call into the pipeline to see if there is more work that
3804          * needs to be done. If there is work to be done it will be
3805          * dispatched to another taskq thread.
3806          */
3807         zio_allocate_dispatch(pio->io_mc);
3808 }
3809 
3810 static int
3811 zio_done(zio_t *zio)
3812 {
3813         spa_t *spa = zio->io_spa;
3814         zio_t *lio = zio->io_logical;
3815         blkptr_t *bp = zio->io_bp;
3816         vdev_t *vd = zio->io_vd;
3817         uint64_t psize = zio->io_size;
3818         zio_t *pio, *pio_next;
3819         metaslab_class_t *mc = zio->io_mc;
3820         zio_link_t *zl = NULL;
3821 
3822         /*
3823          * If our children haven't all completed,
3824          * wait for them and then repeat this pipeline stage.
3825          */
3826         if (zio_wait_for_children(zio, ZIO_CHILD_VDEV, ZIO_WAIT_DONE) ||
3827             zio_wait_for_children(zio, ZIO_CHILD_GANG, ZIO_WAIT_DONE) ||
3828             zio_wait_for_children(zio, ZIO_CHILD_DDT, ZIO_WAIT_DONE) ||
3829             zio_wait_for_children(zio, ZIO_CHILD_LOGICAL, ZIO_WAIT_DONE))
3830                 return (ZIO_PIPELINE_STOP);

3831 
3832         /*
3833          * If the allocation throttle is enabled, then update the accounting.
3834          * We only track child I/Os that are part of an allocating async
3835          * write. We must do this since the allocation is performed
3836          * by the logical I/O but the actual write is done by child I/Os.
3837          */
3838         if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
3839             zio->io_child_type == ZIO_CHILD_VDEV) {
3840                 ASSERT(mc->mc_alloc_throttle_enabled);
3841                 zio_dva_throttle_done(zio);
3842         }
3843 
3844         /*
3845          * If the allocation throttle is enabled, verify that
3846          * we have decremented the refcounts for every I/O that was throttled.
3847          */
3848         if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
3849                 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3850                 ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);


4100         zl = NULL;
4101         for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
4102                 zio_link_t *remove_zl = zl;
4103                 pio_next = zio_walk_parents(zio, &zl);
4104                 zio_remove_child(pio, zio, remove_zl);
4105                 zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
4106         }
4107 
4108         if (zio->io_waiter != NULL) {
4109                 mutex_enter(&zio->io_lock);
4110                 zio->io_executor = NULL;
4111                 cv_broadcast(&zio->io_cv);
4112                 mutex_exit(&zio->io_lock);
4113         } else {
4114                 zio_destroy(zio);
4115         }
4116 
4117         return (ZIO_PIPELINE_STOP);
4118 }
4119 
4120 zio_t *
4121 zio_wbc(zio_type_t type, vdev_t *vd, abd_t *data,
4122     uint64_t size, uint64_t offset)
4123 {
4124         zio_t *zio = NULL;
4125 
4126         switch (type) {
4127         case ZIO_TYPE_WRITE:
4128                 zio = zio_create(NULL, vd->vdev_spa, 0, NULL, data, size,
4129                     size, NULL, NULL, ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
4130                     ZIO_FLAG_PHYSICAL, vd, offset,
4131                     NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
4132                 break;
4133         case ZIO_TYPE_READ:
4134                 zio = zio_create(NULL, vd->vdev_spa, 0, NULL, data, size,
4135                     size, NULL, NULL, ZIO_TYPE_READ, ZIO_PRIORITY_ASYNC_READ,
4136                     ZIO_FLAG_DONT_CACHE | ZIO_FLAG_PHYSICAL, vd, offset,
4137                     NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
4138                 break;
4139         default:
4140                 ASSERT(0);
4141         }
4142 
4143         zio->io_prop.zp_checksum = ZIO_CHECKSUM_OFF;
4144 
4145         return (zio);
4146 }
4147 
4148 /*
4149  * ==========================================================================
4150  * I/O pipeline definition
4151  * ==========================================================================
4152  */
4153 static zio_pipe_stage_t *zio_pipeline[] = {
4154         NULL,
4155         zio_read_bp_init,
4156         zio_write_bp_init,
4157         zio_free_bp_init,
4158         zio_issue_async,
4159         zio_write_compress,
4160         zio_checksum_generate,
4161         zio_nop_write,
4162         zio_ddt_read_start,
4163         zio_ddt_read_done,
4164         zio_ddt_write,
4165         zio_ddt_free,
4166         zio_gang_assemble,
4167         zio_gang_issue,