Print this page
    
13065 log when suspending a zpool
Reviewed by: Andy Fiddaman <andy@omniosce.org>
Reviewed by: Toomas Soome <tsoome@me.com>
    
      
        | Split | Close | 
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/fs/zfs/zio.c
          +++ new/usr/src/uts/common/fs/zfs/zio.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  
    | ↓ open down ↓ | 16 lines elided | ↑ open up ↑ | 
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23   23   * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
  24   24   * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
  25   25   * Copyright (c) 2014 Integros [integros.com]
  26   26   * Copyright (c) 2017, Intel Corporation.
       27 + * Copyright 2020 Joyent, Inc.
  27   28   */
  28   29  
  29   30  #include <sys/sysmacros.h>
  30   31  #include <sys/zfs_context.h>
  31   32  #include <sys/fm/fs/zfs.h>
  32   33  #include <sys/spa.h>
  33   34  #include <sys/txg.h>
  34   35  #include <sys/spa_impl.h>
  35   36  #include <sys/vdev_impl.h>
  36   37  #include <sys/vdev_trim.h>
  37   38  #include <sys/zio_impl.h>
  38   39  #include <sys/zio_compress.h>
  39   40  #include <sys/zio_checksum.h>
  40   41  #include <sys/dmu_objset.h>
  41   42  #include <sys/arc.h>
  42   43  #include <sys/ddt.h>
  43   44  #include <sys/blkptr.h>
  44   45  #include <sys/zfeature.h>
  45   46  #include <sys/time.h>
  46   47  #include <sys/dsl_scan.h>
  47   48  #include <sys/metaslab_impl.h>
  48   49  #include <sys/abd.h>
  49   50  #include <sys/cityhash.h>
  50   51  #include <sys/dsl_crypt.h>
  51   52  
  52   53  /*
  53   54   * ==========================================================================
  54   55   * I/O type descriptions
  55   56   * ==========================================================================
  56   57   */
  57   58  const char *zio_type_name[ZIO_TYPES] = {
  58   59          "zio_null", "zio_read", "zio_write", "zio_free", "zio_claim",
  59   60          "zio_ioctl", "z_trim"
  60   61  };
  61   62  
  62   63  boolean_t zio_dva_throttle_enabled = B_TRUE;
  63   64  
  64   65  /*
  65   66   * ==========================================================================
  66   67   * I/O kmem caches
  67   68   * ==========================================================================
  68   69   */
  69   70  kmem_cache_t *zio_cache;
  70   71  kmem_cache_t *zio_link_cache;
  71   72  kmem_cache_t *zio_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
  72   73  kmem_cache_t *zio_data_buf_cache[SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT];
  73   74  
  74   75  #ifdef _KERNEL
  75   76  extern vmem_t *zio_alloc_arena;
  76   77  #endif
  77   78  
  78   79  #define ZIO_PIPELINE_CONTINUE           0x100
  79   80  #define ZIO_PIPELINE_STOP               0x101
  80   81  
  81   82  /* Mark IOs as "slow" if they take longer than 30 seconds */
  82   83  int zio_slow_io_ms = (30 * MILLISEC);
  83   84  
  84   85  #define BP_SPANB(indblkshift, level) \
  85   86          (((uint64_t)1) << ((level) * ((indblkshift) - SPA_BLKPTRSHIFT)))
  86   87  #define COMPARE_META_LEVEL      0x80000000ul
  87   88  /*
  88   89   * The following actions directly effect the spa's sync-to-convergence logic.
  89   90   * The values below define the sync pass when we start performing the action.
  90   91   * Care should be taken when changing these values as they directly impact
  91   92   * spa_sync() performance. Tuning these values may introduce subtle performance
  92   93   * pathologies and should only be done in the context of performance analysis.
  93   94   * These tunables will eventually be removed and replaced with #defines once
  94   95   * enough analysis has been done to determine optimal values.
  95   96   *
  96   97   * The 'zfs_sync_pass_deferred_free' pass must be greater than 1 to ensure that
  97   98   * regular blocks are not deferred.
  98   99   */
  99  100  int zfs_sync_pass_deferred_free = 2; /* defer frees starting in this pass */
 100  101  int zfs_sync_pass_dont_compress = 5; /* don't compress starting in this pass */
 101  102  int zfs_sync_pass_rewrite = 2; /* rewrite new bps starting in this pass */
 102  103  
 103  104  /*
 104  105   * An allocating zio is one that either currently has the DVA allocate
 105  106   * stage set or will have it later in its lifetime.
 106  107   */
 107  108  #define IO_IS_ALLOCATING(zio) ((zio)->io_orig_pipeline & ZIO_STAGE_DVA_ALLOCATE)
 108  109  
 109  110  boolean_t       zio_requeue_io_start_cut_in_line = B_TRUE;
 110  111  
 111  112  #ifdef ZFS_DEBUG
 112  113  int zio_buf_debug_limit = 16384;
 113  114  #else
 114  115  int zio_buf_debug_limit = 0;
 115  116  #endif
 116  117  
 117  118  static void zio_taskq_dispatch(zio_t *, zio_taskq_type_t, boolean_t);
 118  119  
 119  120  void
 120  121  zio_init(void)
 121  122  {
 122  123          size_t c;
 123  124          vmem_t *data_alloc_arena = NULL;
 124  125  
 125  126  #ifdef _KERNEL
 126  127          data_alloc_arena = zio_alloc_arena;
 127  128  #endif
 128  129          zio_cache = kmem_cache_create("zio_cache",
 129  130              sizeof (zio_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
 130  131          zio_link_cache = kmem_cache_create("zio_link_cache",
 131  132              sizeof (zio_link_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
 132  133  
 133  134          /*
 134  135           * For small buffers, we want a cache for each multiple of
 135  136           * SPA_MINBLOCKSIZE.  For larger buffers, we want a cache
 136  137           * for each quarter-power of 2.
 137  138           */
 138  139          for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
 139  140                  size_t size = (c + 1) << SPA_MINBLOCKSHIFT;
 140  141                  size_t p2 = size;
 141  142                  size_t align = 0;
 142  143                  size_t cflags = (size > zio_buf_debug_limit) ? KMC_NODEBUG : 0;
 143  144  
 144  145                  while (!ISP2(p2))
 145  146                          p2 &= p2 - 1;
 146  147  
 147  148  #ifndef _KERNEL
 148  149                  /*
 149  150                   * If we are using watchpoints, put each buffer on its own page,
 150  151                   * to eliminate the performance overhead of trapping to the
 151  152                   * kernel when modifying a non-watched buffer that shares the
 152  153                   * page with a watched buffer.
 153  154                   */
 154  155                  if (arc_watch && !IS_P2ALIGNED(size, PAGESIZE))
 155  156                          continue;
 156  157  #endif
 157  158                  if (size <= 4 * SPA_MINBLOCKSIZE) {
 158  159                          align = SPA_MINBLOCKSIZE;
 159  160                  } else if (IS_P2ALIGNED(size, p2 >> 2)) {
 160  161                          align = MIN(p2 >> 2, PAGESIZE);
 161  162                  }
 162  163  
 163  164                  if (align != 0) {
 164  165                          char name[36];
 165  166                          (void) sprintf(name, "zio_buf_%lu", (ulong_t)size);
 166  167                          zio_buf_cache[c] = kmem_cache_create(name, size,
 167  168                              align, NULL, NULL, NULL, NULL, NULL, cflags);
 168  169  
 169  170                          /*
 170  171                           * Since zio_data bufs do not appear in crash dumps, we
 171  172                           * pass KMC_NOTOUCH so that no allocator metadata is
 172  173                           * stored with the buffers.
 173  174                           */
 174  175                          (void) sprintf(name, "zio_data_buf_%lu", (ulong_t)size);
 175  176                          zio_data_buf_cache[c] = kmem_cache_create(name, size,
 176  177                              align, NULL, NULL, NULL, NULL, data_alloc_arena,
 177  178                              cflags | KMC_NOTOUCH);
 178  179                  }
 179  180          }
 180  181  
 181  182          while (--c != 0) {
 182  183                  ASSERT(zio_buf_cache[c] != NULL);
 183  184                  if (zio_buf_cache[c - 1] == NULL)
 184  185                          zio_buf_cache[c - 1] = zio_buf_cache[c];
 185  186  
 186  187                  ASSERT(zio_data_buf_cache[c] != NULL);
 187  188                  if (zio_data_buf_cache[c - 1] == NULL)
 188  189                          zio_data_buf_cache[c - 1] = zio_data_buf_cache[c];
 189  190          }
 190  191  
 191  192          zio_inject_init();
 192  193  }
 193  194  
 194  195  void
 195  196  zio_fini(void)
 196  197  {
 197  198          size_t c;
 198  199          kmem_cache_t *last_cache = NULL;
 199  200          kmem_cache_t *last_data_cache = NULL;
 200  201  
 201  202          for (c = 0; c < SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT; c++) {
 202  203                  if (zio_buf_cache[c] != last_cache) {
 203  204                          last_cache = zio_buf_cache[c];
 204  205                          kmem_cache_destroy(zio_buf_cache[c]);
 205  206                  }
 206  207                  zio_buf_cache[c] = NULL;
 207  208  
 208  209                  if (zio_data_buf_cache[c] != last_data_cache) {
 209  210                          last_data_cache = zio_data_buf_cache[c];
 210  211                          kmem_cache_destroy(zio_data_buf_cache[c]);
 211  212                  }
 212  213                  zio_data_buf_cache[c] = NULL;
 213  214          }
 214  215  
 215  216          kmem_cache_destroy(zio_link_cache);
 216  217          kmem_cache_destroy(zio_cache);
 217  218  
 218  219          zio_inject_fini();
 219  220  }
 220  221  
 221  222  /*
 222  223   * ==========================================================================
 223  224   * Allocate and free I/O buffers
 224  225   * ==========================================================================
 225  226   */
 226  227  
 227  228  /*
 228  229   * Use zio_buf_alloc to allocate ZFS metadata.  This data will appear in a
 229  230   * crashdump if the kernel panics, so use it judiciously.  Obviously, it's
 230  231   * useful to inspect ZFS metadata, but if possible, we should avoid keeping
 231  232   * excess / transient data in-core during a crashdump.
 232  233   */
 233  234  void *
 234  235  zio_buf_alloc(size_t size)
 235  236  {
 236  237          size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
 237  238  
 238  239          VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
 239  240  
 240  241          return (kmem_cache_alloc(zio_buf_cache[c], KM_PUSHPAGE));
 241  242  }
 242  243  
 243  244  /*
 244  245   * Use zio_data_buf_alloc to allocate data.  The data will not appear in a
 245  246   * crashdump if the kernel panics.  This exists so that we will limit the amount
 246  247   * of ZFS data that shows up in a kernel crashdump.  (Thus reducing the amount
 247  248   * of kernel heap dumped to disk when the kernel panics)
 248  249   */
 249  250  void *
 250  251  zio_data_buf_alloc(size_t size)
 251  252  {
 252  253          size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
 253  254  
 254  255          VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
 255  256  
 256  257          return (kmem_cache_alloc(zio_data_buf_cache[c], KM_PUSHPAGE));
 257  258  }
 258  259  
 259  260  void
 260  261  zio_buf_free(void *buf, size_t size)
 261  262  {
 262  263          size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
 263  264  
 264  265          VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
 265  266  
 266  267          kmem_cache_free(zio_buf_cache[c], buf);
 267  268  }
 268  269  
 269  270  void
 270  271  zio_data_buf_free(void *buf, size_t size)
 271  272  {
 272  273          size_t c = (size - 1) >> SPA_MINBLOCKSHIFT;
 273  274  
 274  275          VERIFY3U(c, <, SPA_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
 275  276  
 276  277          kmem_cache_free(zio_data_buf_cache[c], buf);
 277  278  }
 278  279  
 279  280  /* ARGSUSED */
 280  281  static void
 281  282  zio_abd_free(void *abd, size_t size)
 282  283  {
 283  284          abd_free((abd_t *)abd);
 284  285  }
 285  286  
 286  287  /*
 287  288   * ==========================================================================
 288  289   * Push and pop I/O transform buffers
 289  290   * ==========================================================================
 290  291   */
 291  292  void
 292  293  zio_push_transform(zio_t *zio, abd_t *data, uint64_t size, uint64_t bufsize,
 293  294      zio_transform_func_t *transform)
 294  295  {
 295  296          zio_transform_t *zt = kmem_alloc(sizeof (zio_transform_t), KM_SLEEP);
 296  297  
 297  298          /*
 298  299           * Ensure that anyone expecting this zio to contain a linear ABD isn't
 299  300           * going to get a nasty surprise when they try to access the data.
 300  301           */
 301  302          IMPLY(abd_is_linear(zio->io_abd), abd_is_linear(data));
 302  303  
 303  304          zt->zt_orig_abd = zio->io_abd;
 304  305          zt->zt_orig_size = zio->io_size;
 305  306          zt->zt_bufsize = bufsize;
 306  307          zt->zt_transform = transform;
 307  308  
 308  309          zt->zt_next = zio->io_transform_stack;
 309  310          zio->io_transform_stack = zt;
 310  311  
 311  312          zio->io_abd = data;
 312  313          zio->io_size = size;
 313  314  }
 314  315  
 315  316  void
 316  317  zio_pop_transforms(zio_t *zio)
 317  318  {
 318  319          zio_transform_t *zt;
 319  320  
 320  321          while ((zt = zio->io_transform_stack) != NULL) {
 321  322                  if (zt->zt_transform != NULL)
 322  323                          zt->zt_transform(zio,
 323  324                              zt->zt_orig_abd, zt->zt_orig_size);
 324  325  
 325  326                  if (zt->zt_bufsize != 0)
 326  327                          abd_free(zio->io_abd);
 327  328  
 328  329                  zio->io_abd = zt->zt_orig_abd;
 329  330                  zio->io_size = zt->zt_orig_size;
 330  331                  zio->io_transform_stack = zt->zt_next;
 331  332  
 332  333                  kmem_free(zt, sizeof (zio_transform_t));
 333  334          }
 334  335  }
 335  336  
 336  337  /*
 337  338   * ==========================================================================
 338  339   * I/O transform callbacks for subblocks, decompression, and decryption
 339  340   * ==========================================================================
 340  341   */
 341  342  static void
 342  343  zio_subblock(zio_t *zio, abd_t *data, uint64_t size)
 343  344  {
 344  345          ASSERT(zio->io_size > size);
 345  346  
 346  347          if (zio->io_type == ZIO_TYPE_READ)
 347  348                  abd_copy(data, zio->io_abd, size);
 348  349  }
 349  350  
 350  351  static void
 351  352  zio_decompress(zio_t *zio, abd_t *data, uint64_t size)
 352  353  {
 353  354          if (zio->io_error == 0) {
 354  355                  void *tmp = abd_borrow_buf(data, size);
 355  356                  int ret = zio_decompress_data(BP_GET_COMPRESS(zio->io_bp),
 356  357                      zio->io_abd, tmp, zio->io_size, size);
 357  358                  abd_return_buf_copy(data, tmp, size);
 358  359  
 359  360                  if (ret != 0)
 360  361                          zio->io_error = SET_ERROR(EIO);
 361  362          }
 362  363  }
 363  364  
 364  365  static void
 365  366  zio_decrypt(zio_t *zio, abd_t *data, uint64_t size)
 366  367  {
 367  368          int ret;
 368  369          void *tmp;
 369  370          blkptr_t *bp = zio->io_bp;
 370  371          spa_t *spa = zio->io_spa;
 371  372          uint64_t dsobj = zio->io_bookmark.zb_objset;
 372  373          uint64_t lsize = BP_GET_LSIZE(bp);
 373  374          dmu_object_type_t ot = BP_GET_TYPE(bp);
 374  375          uint8_t salt[ZIO_DATA_SALT_LEN];
 375  376          uint8_t iv[ZIO_DATA_IV_LEN];
 376  377          uint8_t mac[ZIO_DATA_MAC_LEN];
 377  378          boolean_t no_crypt = B_FALSE;
 378  379  
 379  380          ASSERT(BP_USES_CRYPT(bp));
 380  381          ASSERT3U(size, !=, 0);
 381  382  
 382  383          if (zio->io_error != 0)
 383  384                  return;
 384  385  
 385  386          /*
 386  387           * Verify the cksum of MACs stored in an indirect bp. It will always
 387  388           * be possible to verify this since it does not require an encryption
 388  389           * key.
 389  390           */
 390  391          if (BP_HAS_INDIRECT_MAC_CKSUM(bp)) {
 391  392                  zio_crypt_decode_mac_bp(bp, mac);
 392  393  
 393  394                  if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF) {
 394  395                          /*
 395  396                           * We haven't decompressed the data yet, but
 396  397                           * zio_crypt_do_indirect_mac_checksum() requires
 397  398                           * decompressed data to be able to parse out the MACs
 398  399                           * from the indirect block. We decompress it now and
 399  400                           * throw away the result after we are finished.
 400  401                           */
 401  402                          tmp = zio_buf_alloc(lsize);
 402  403                          ret = zio_decompress_data(BP_GET_COMPRESS(bp),
 403  404                              zio->io_abd, tmp, zio->io_size, lsize);
 404  405                          if (ret != 0) {
 405  406                                  ret = SET_ERROR(EIO);
 406  407                                  goto error;
 407  408                          }
 408  409                          ret = zio_crypt_do_indirect_mac_checksum(B_FALSE,
 409  410                              tmp, lsize, BP_SHOULD_BYTESWAP(bp), mac);
 410  411                          zio_buf_free(tmp, lsize);
 411  412                  } else {
 412  413                          ret = zio_crypt_do_indirect_mac_checksum_abd(B_FALSE,
 413  414                              zio->io_abd, size, BP_SHOULD_BYTESWAP(bp), mac);
 414  415                  }
 415  416                  abd_copy(data, zio->io_abd, size);
 416  417  
 417  418                  if (ret != 0)
 418  419                          goto error;
 419  420  
 420  421                  return;
 421  422          }
 422  423  
 423  424          /*
 424  425           * If this is an authenticated block, just check the MAC. It would be
 425  426           * nice to separate this out into its own flag, but for the moment
 426  427           * enum zio_flag is out of bits.
 427  428           */
 428  429          if (BP_IS_AUTHENTICATED(bp)) {
 429  430                  if (ot == DMU_OT_OBJSET) {
 430  431                          ret = spa_do_crypt_objset_mac_abd(B_FALSE, spa,
 431  432                              dsobj, zio->io_abd, size, BP_SHOULD_BYTESWAP(bp));
 432  433                  } else {
 433  434                          zio_crypt_decode_mac_bp(bp, mac);
 434  435                          ret = spa_do_crypt_mac_abd(B_FALSE, spa, dsobj,
 435  436                              zio->io_abd, size, mac);
 436  437                  }
 437  438                  abd_copy(data, zio->io_abd, size);
 438  439  
 439  440                  if (zio_injection_enabled && ot != DMU_OT_DNODE && ret == 0) {
 440  441                          ret = zio_handle_decrypt_injection(spa,
 441  442                              &zio->io_bookmark, ot, ECKSUM);
 442  443                  }
 443  444                  if (ret != 0)
 444  445                          goto error;
 445  446  
 446  447                  return;
 447  448          }
 448  449  
 449  450          zio_crypt_decode_params_bp(bp, salt, iv);
 450  451  
 451  452          if (ot == DMU_OT_INTENT_LOG) {
 452  453                  tmp = abd_borrow_buf_copy(zio->io_abd, sizeof (zil_chain_t));
 453  454                  zio_crypt_decode_mac_zil(tmp, mac);
 454  455                  abd_return_buf(zio->io_abd, tmp, sizeof (zil_chain_t));
 455  456          } else {
 456  457                  zio_crypt_decode_mac_bp(bp, mac);
 457  458          }
 458  459  
 459  460          ret = spa_do_crypt_abd(B_FALSE, spa, &zio->io_bookmark, BP_GET_TYPE(bp),
 460  461              BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp), salt, iv, mac, size, data,
 461  462              zio->io_abd, &no_crypt);
 462  463          if (no_crypt)
 463  464                  abd_copy(data, zio->io_abd, size);
 464  465  
 465  466          if (ret != 0)
 466  467                  goto error;
 467  468  
 468  469          return;
 469  470  
 470  471  error:
 471  472          /* assert that the key was found unless this was speculative */
 472  473          ASSERT(ret != EACCES || (zio->io_flags & ZIO_FLAG_SPECULATIVE));
 473  474  
 474  475          /*
 475  476           * If there was a decryption / authentication error return EIO as
 476  477           * the io_error. If this was not a speculative zio, create an ereport.
 477  478           */
 478  479          if (ret == ECKSUM) {
 479  480                  zio->io_error = SET_ERROR(EIO);
 480  481                  if ((zio->io_flags & ZIO_FLAG_SPECULATIVE) == 0) {
 481  482                          spa_log_error(spa, &zio->io_bookmark);
 482  483                          zfs_ereport_post(FM_EREPORT_ZFS_AUTHENTICATION,
 483  484                              spa, NULL, &zio->io_bookmark, zio, 0, 0);
 484  485                  }
 485  486          } else {
 486  487                  zio->io_error = ret;
 487  488          }
 488  489  }
 489  490  
 490  491  /*
 491  492   * ==========================================================================
 492  493   * I/O parent/child relationships and pipeline interlocks
 493  494   * ==========================================================================
 494  495   */
 495  496  zio_t *
 496  497  zio_walk_parents(zio_t *cio, zio_link_t **zl)
 497  498  {
 498  499          list_t *pl = &cio->io_parent_list;
 499  500  
 500  501          *zl = (*zl == NULL) ? list_head(pl) : list_next(pl, *zl);
 501  502          if (*zl == NULL)
 502  503                  return (NULL);
 503  504  
 504  505          ASSERT((*zl)->zl_child == cio);
 505  506          return ((*zl)->zl_parent);
 506  507  }
 507  508  
 508  509  zio_t *
 509  510  zio_walk_children(zio_t *pio, zio_link_t **zl)
 510  511  {
 511  512          list_t *cl = &pio->io_child_list;
 512  513  
 513  514          ASSERT(MUTEX_HELD(&pio->io_lock));
 514  515  
 515  516          *zl = (*zl == NULL) ? list_head(cl) : list_next(cl, *zl);
 516  517          if (*zl == NULL)
 517  518                  return (NULL);
 518  519  
 519  520          ASSERT((*zl)->zl_parent == pio);
 520  521          return ((*zl)->zl_child);
 521  522  }
 522  523  
 523  524  zio_t *
 524  525  zio_unique_parent(zio_t *cio)
 525  526  {
 526  527          zio_link_t *zl = NULL;
 527  528          zio_t *pio = zio_walk_parents(cio, &zl);
 528  529  
 529  530          VERIFY3P(zio_walk_parents(cio, &zl), ==, NULL);
 530  531          return (pio);
 531  532  }
 532  533  
 533  534  void
 534  535  zio_add_child(zio_t *pio, zio_t *cio)
 535  536  {
 536  537          zio_link_t *zl = kmem_cache_alloc(zio_link_cache, KM_SLEEP);
 537  538  
 538  539          /*
 539  540           * Logical I/Os can have logical, gang, or vdev children.
 540  541           * Gang I/Os can have gang or vdev children.
 541  542           * Vdev I/Os can only have vdev children.
 542  543           * The following ASSERT captures all of these constraints.
 543  544           */
 544  545          ASSERT3S(cio->io_child_type, <=, pio->io_child_type);
 545  546  
 546  547          zl->zl_parent = pio;
 547  548          zl->zl_child = cio;
 548  549  
 549  550          mutex_enter(&pio->io_lock);
 550  551          mutex_enter(&cio->io_lock);
 551  552  
 552  553          ASSERT(pio->io_state[ZIO_WAIT_DONE] == 0);
 553  554  
 554  555          for (int w = 0; w < ZIO_WAIT_TYPES; w++)
 555  556                  pio->io_children[cio->io_child_type][w] += !cio->io_state[w];
 556  557  
 557  558          list_insert_head(&pio->io_child_list, zl);
 558  559          list_insert_head(&cio->io_parent_list, zl);
 559  560  
 560  561          pio->io_child_count++;
 561  562          cio->io_parent_count++;
 562  563  
 563  564          mutex_exit(&cio->io_lock);
 564  565          mutex_exit(&pio->io_lock);
 565  566  }
 566  567  
 567  568  static void
 568  569  zio_remove_child(zio_t *pio, zio_t *cio, zio_link_t *zl)
 569  570  {
 570  571          ASSERT(zl->zl_parent == pio);
 571  572          ASSERT(zl->zl_child == cio);
 572  573  
 573  574          mutex_enter(&pio->io_lock);
 574  575          mutex_enter(&cio->io_lock);
 575  576  
 576  577          list_remove(&pio->io_child_list, zl);
 577  578          list_remove(&cio->io_parent_list, zl);
 578  579  
 579  580          pio->io_child_count--;
 580  581          cio->io_parent_count--;
 581  582  
 582  583          mutex_exit(&cio->io_lock);
 583  584          mutex_exit(&pio->io_lock);
 584  585  
 585  586          kmem_cache_free(zio_link_cache, zl);
 586  587  }
 587  588  
 588  589  static boolean_t
 589  590  zio_wait_for_children(zio_t *zio, uint8_t childbits, enum zio_wait_type wait)
 590  591  {
 591  592          boolean_t waiting = B_FALSE;
 592  593  
 593  594          mutex_enter(&zio->io_lock);
 594  595          ASSERT(zio->io_stall == NULL);
 595  596          for (int c = 0; c < ZIO_CHILD_TYPES; c++) {
 596  597                  if (!(ZIO_CHILD_BIT_IS_SET(childbits, c)))
 597  598                          continue;
 598  599  
 599  600                  uint64_t *countp = &zio->io_children[c][wait];
 600  601                  if (*countp != 0) {
 601  602                          zio->io_stage >>= 1;
 602  603                          ASSERT3U(zio->io_stage, !=, ZIO_STAGE_OPEN);
 603  604                          zio->io_stall = countp;
 604  605                          waiting = B_TRUE;
 605  606                          break;
 606  607                  }
 607  608          }
 608  609          mutex_exit(&zio->io_lock);
 609  610          return (waiting);
 610  611  }
 611  612  
 612  613  static void
 613  614  zio_notify_parent(zio_t *pio, zio_t *zio, enum zio_wait_type wait)
 614  615  {
 615  616          uint64_t *countp = &pio->io_children[zio->io_child_type][wait];
 616  617          int *errorp = &pio->io_child_error[zio->io_child_type];
 617  618  
 618  619          mutex_enter(&pio->io_lock);
 619  620          if (zio->io_error && !(zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
 620  621                  *errorp = zio_worst_error(*errorp, zio->io_error);
 621  622          pio->io_reexecute |= zio->io_reexecute;
 622  623          ASSERT3U(*countp, >, 0);
 623  624  
 624  625          (*countp)--;
 625  626  
 626  627          if (*countp == 0 && pio->io_stall == countp) {
 627  628                  zio_taskq_type_t type =
 628  629                      pio->io_stage < ZIO_STAGE_VDEV_IO_START ? ZIO_TASKQ_ISSUE :
 629  630                      ZIO_TASKQ_INTERRUPT;
 630  631                  pio->io_stall = NULL;
 631  632                  mutex_exit(&pio->io_lock);
 632  633                  /*
 633  634                   * Dispatch the parent zio in its own taskq so that
 634  635                   * the child can continue to make progress. This also
 635  636                   * prevents overflowing the stack when we have deeply nested
 636  637                   * parent-child relationships.
 637  638                   */
 638  639                  zio_taskq_dispatch(pio, type, B_FALSE);
 639  640          } else {
 640  641                  mutex_exit(&pio->io_lock);
 641  642          }
 642  643  }
 643  644  
 644  645  static void
 645  646  zio_inherit_child_errors(zio_t *zio, enum zio_child c)
 646  647  {
 647  648          if (zio->io_child_error[c] != 0 && zio->io_error == 0)
 648  649                  zio->io_error = zio->io_child_error[c];
 649  650  }
 650  651  
 651  652  int
 652  653  zio_bookmark_compare(const void *x1, const void *x2)
 653  654  {
 654  655          const zio_t *z1 = x1;
 655  656          const zio_t *z2 = x2;
 656  657  
 657  658          if (z1->io_bookmark.zb_objset < z2->io_bookmark.zb_objset)
 658  659                  return (-1);
 659  660          if (z1->io_bookmark.zb_objset > z2->io_bookmark.zb_objset)
 660  661                  return (1);
 661  662  
 662  663          if (z1->io_bookmark.zb_object < z2->io_bookmark.zb_object)
 663  664                  return (-1);
 664  665          if (z1->io_bookmark.zb_object > z2->io_bookmark.zb_object)
 665  666                  return (1);
 666  667  
 667  668          if (z1->io_bookmark.zb_level < z2->io_bookmark.zb_level)
 668  669                  return (-1);
 669  670          if (z1->io_bookmark.zb_level > z2->io_bookmark.zb_level)
 670  671                  return (1);
 671  672  
 672  673          if (z1->io_bookmark.zb_blkid < z2->io_bookmark.zb_blkid)
 673  674                  return (-1);
 674  675          if (z1->io_bookmark.zb_blkid > z2->io_bookmark.zb_blkid)
 675  676                  return (1);
 676  677  
 677  678          if (z1 < z2)
 678  679                  return (-1);
 679  680          if (z1 > z2)
 680  681                  return (1);
 681  682  
 682  683          return (0);
 683  684  }
 684  685  
 685  686  /*
 686  687   * ==========================================================================
 687  688   * Create the various types of I/O (read, write, free, etc)
 688  689   * ==========================================================================
 689  690   */
 690  691  static zio_t *
 691  692  zio_create(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
 692  693      abd_t *data, uint64_t lsize, uint64_t psize, zio_done_func_t *done,
 693  694      void *private, zio_type_t type, zio_priority_t priority,
 694  695      enum zio_flag flags, vdev_t *vd, uint64_t offset,
 695  696      const zbookmark_phys_t *zb, enum zio_stage stage, enum zio_stage pipeline)
 696  697  {
 697  698          zio_t *zio;
 698  699  
 699  700          IMPLY(type != ZIO_TYPE_TRIM, psize <= SPA_MAXBLOCKSIZE);
 700  701          ASSERT(P2PHASE(psize, SPA_MINBLOCKSIZE) == 0);
 701  702          ASSERT(P2PHASE(offset, SPA_MINBLOCKSIZE) == 0);
 702  703  
 703  704          ASSERT(!vd || spa_config_held(spa, SCL_STATE_ALL, RW_READER));
 704  705          ASSERT(!bp || !(flags & ZIO_FLAG_CONFIG_WRITER));
 705  706          ASSERT(vd || stage == ZIO_STAGE_OPEN);
 706  707  
 707  708          IMPLY(lsize != psize, (flags & ZIO_FLAG_RAW_COMPRESS) != 0);
 708  709  
 709  710          zio = kmem_cache_alloc(zio_cache, KM_SLEEP);
 710  711          bzero(zio, sizeof (zio_t));
 711  712  
 712  713          mutex_init(&zio->io_lock, NULL, MUTEX_DEFAULT, NULL);
 713  714          cv_init(&zio->io_cv, NULL, CV_DEFAULT, NULL);
 714  715  
 715  716          list_create(&zio->io_parent_list, sizeof (zio_link_t),
 716  717              offsetof(zio_link_t, zl_parent_node));
 717  718          list_create(&zio->io_child_list, sizeof (zio_link_t),
 718  719              offsetof(zio_link_t, zl_child_node));
 719  720          metaslab_trace_init(&zio->io_alloc_list);
 720  721  
 721  722          if (vd != NULL)
 722  723                  zio->io_child_type = ZIO_CHILD_VDEV;
 723  724          else if (flags & ZIO_FLAG_GANG_CHILD)
 724  725                  zio->io_child_type = ZIO_CHILD_GANG;
 725  726          else if (flags & ZIO_FLAG_DDT_CHILD)
 726  727                  zio->io_child_type = ZIO_CHILD_DDT;
 727  728          else
 728  729                  zio->io_child_type = ZIO_CHILD_LOGICAL;
 729  730  
 730  731          if (bp != NULL) {
 731  732                  zio->io_bp = (blkptr_t *)bp;
 732  733                  zio->io_bp_copy = *bp;
 733  734                  zio->io_bp_orig = *bp;
 734  735                  if (type != ZIO_TYPE_WRITE ||
 735  736                      zio->io_child_type == ZIO_CHILD_DDT)
 736  737                          zio->io_bp = &zio->io_bp_copy;  /* so caller can free */
 737  738                  if (zio->io_child_type == ZIO_CHILD_LOGICAL)
 738  739                          zio->io_logical = zio;
 739  740                  if (zio->io_child_type > ZIO_CHILD_GANG && BP_IS_GANG(bp))
 740  741                          pipeline |= ZIO_GANG_STAGES;
 741  742          }
 742  743  
 743  744          zio->io_spa = spa;
 744  745          zio->io_txg = txg;
 745  746          zio->io_done = done;
 746  747          zio->io_private = private;
 747  748          zio->io_type = type;
 748  749          zio->io_priority = priority;
 749  750          zio->io_vd = vd;
 750  751          zio->io_offset = offset;
 751  752          zio->io_orig_abd = zio->io_abd = data;
 752  753          zio->io_orig_size = zio->io_size = psize;
 753  754          zio->io_lsize = lsize;
 754  755          zio->io_orig_flags = zio->io_flags = flags;
 755  756          zio->io_orig_stage = zio->io_stage = stage;
 756  757          zio->io_orig_pipeline = zio->io_pipeline = pipeline;
 757  758          zio->io_pipeline_trace = ZIO_STAGE_OPEN;
 758  759  
 759  760          zio->io_state[ZIO_WAIT_READY] = (stage >= ZIO_STAGE_READY);
 760  761          zio->io_state[ZIO_WAIT_DONE] = (stage >= ZIO_STAGE_DONE);
 761  762  
 762  763          if (zb != NULL)
 763  764                  zio->io_bookmark = *zb;
 764  765  
 765  766          if (pio != NULL) {
 766  767                  if (zio->io_metaslab_class == NULL)
 767  768                          zio->io_metaslab_class = pio->io_metaslab_class;
 768  769                  if (zio->io_logical == NULL)
 769  770                          zio->io_logical = pio->io_logical;
 770  771                  if (zio->io_child_type == ZIO_CHILD_GANG)
 771  772                          zio->io_gang_leader = pio->io_gang_leader;
 772  773                  zio_add_child(pio, zio);
 773  774          }
 774  775  
 775  776          return (zio);
 776  777  }
 777  778  
 778  779  static void
 779  780  zio_destroy(zio_t *zio)
 780  781  {
 781  782          metaslab_trace_fini(&zio->io_alloc_list);
 782  783          list_destroy(&zio->io_parent_list);
 783  784          list_destroy(&zio->io_child_list);
 784  785          mutex_destroy(&zio->io_lock);
 785  786          cv_destroy(&zio->io_cv);
 786  787          kmem_cache_free(zio_cache, zio);
 787  788  }
 788  789  
 789  790  zio_t *
 790  791  zio_null(zio_t *pio, spa_t *spa, vdev_t *vd, zio_done_func_t *done,
 791  792      void *private, enum zio_flag flags)
 792  793  {
 793  794          zio_t *zio;
 794  795  
 795  796          zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
 796  797              ZIO_TYPE_NULL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
 797  798              ZIO_STAGE_OPEN, ZIO_INTERLOCK_PIPELINE);
 798  799  
 799  800          return (zio);
 800  801  }
 801  802  
 802  803  zio_t *
 803  804  zio_root(spa_t *spa, zio_done_func_t *done, void *private, enum zio_flag flags)
 804  805  {
 805  806          return (zio_null(NULL, spa, NULL, done, private, flags));
 806  807  }
 807  808  
 808  809  void
 809  810  zfs_blkptr_verify(spa_t *spa, const blkptr_t *bp)
 810  811  {
 811  812          if (!DMU_OT_IS_VALID(BP_GET_TYPE(bp))) {
 812  813                  zfs_panic_recover("blkptr at %p has invalid TYPE %llu",
 813  814                      bp, (longlong_t)BP_GET_TYPE(bp));
 814  815          }
 815  816          if (BP_GET_CHECKSUM(bp) >= ZIO_CHECKSUM_FUNCTIONS ||
 816  817              BP_GET_CHECKSUM(bp) <= ZIO_CHECKSUM_ON) {
 817  818                  zfs_panic_recover("blkptr at %p has invalid CHECKSUM %llu",
 818  819                      bp, (longlong_t)BP_GET_CHECKSUM(bp));
 819  820          }
 820  821          if (BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_FUNCTIONS ||
 821  822              BP_GET_COMPRESS(bp) <= ZIO_COMPRESS_ON) {
 822  823                  zfs_panic_recover("blkptr at %p has invalid COMPRESS %llu",
 823  824                      bp, (longlong_t)BP_GET_COMPRESS(bp));
 824  825          }
 825  826          if (BP_GET_LSIZE(bp) > SPA_MAXBLOCKSIZE) {
 826  827                  zfs_panic_recover("blkptr at %p has invalid LSIZE %llu",
 827  828                      bp, (longlong_t)BP_GET_LSIZE(bp));
 828  829          }
 829  830          if (BP_GET_PSIZE(bp) > SPA_MAXBLOCKSIZE) {
 830  831                  zfs_panic_recover("blkptr at %p has invalid PSIZE %llu",
 831  832                      bp, (longlong_t)BP_GET_PSIZE(bp));
 832  833          }
 833  834  
 834  835          if (BP_IS_EMBEDDED(bp)) {
 835  836                  if (BPE_GET_ETYPE(bp) > NUM_BP_EMBEDDED_TYPES) {
 836  837                          zfs_panic_recover("blkptr at %p has invalid ETYPE %llu",
 837  838                              bp, (longlong_t)BPE_GET_ETYPE(bp));
 838  839                  }
 839  840          }
 840  841  
 841  842          /*
 842  843           * Do not verify individual DVAs if the config is not trusted. This
 843  844           * will be done once the zio is executed in vdev_mirror_map_alloc.
 844  845           */
 845  846          if (!spa->spa_trust_config)
 846  847                  return;
 847  848  
 848  849          /*
 849  850           * Pool-specific checks.
 850  851           *
 851  852           * Note: it would be nice to verify that the blk_birth and
 852  853           * BP_PHYSICAL_BIRTH() are not too large.  However, spa_freeze()
 853  854           * allows the birth time of log blocks (and dmu_sync()-ed blocks
 854  855           * that are in the log) to be arbitrarily large.
 855  856           */
 856  857          for (int i = 0; i < BP_GET_NDVAS(bp); i++) {
 857  858                  uint64_t vdevid = DVA_GET_VDEV(&bp->blk_dva[i]);
 858  859                  if (vdevid >= spa->spa_root_vdev->vdev_children) {
 859  860                          zfs_panic_recover("blkptr at %p DVA %u has invalid "
 860  861                              "VDEV %llu",
 861  862                              bp, i, (longlong_t)vdevid);
 862  863                          continue;
 863  864                  }
 864  865                  vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
 865  866                  if (vd == NULL) {
 866  867                          zfs_panic_recover("blkptr at %p DVA %u has invalid "
 867  868                              "VDEV %llu",
 868  869                              bp, i, (longlong_t)vdevid);
 869  870                          continue;
 870  871                  }
 871  872                  if (vd->vdev_ops == &vdev_hole_ops) {
 872  873                          zfs_panic_recover("blkptr at %p DVA %u has hole "
 873  874                              "VDEV %llu",
 874  875                              bp, i, (longlong_t)vdevid);
 875  876                          continue;
 876  877                  }
 877  878                  if (vd->vdev_ops == &vdev_missing_ops) {
 878  879                          /*
 879  880                           * "missing" vdevs are valid during import, but we
 880  881                           * don't have their detailed info (e.g. asize), so
 881  882                           * we can't perform any more checks on them.
 882  883                           */
 883  884                          continue;
 884  885                  }
 885  886                  uint64_t offset = DVA_GET_OFFSET(&bp->blk_dva[i]);
 886  887                  uint64_t asize = DVA_GET_ASIZE(&bp->blk_dva[i]);
 887  888                  if (BP_IS_GANG(bp))
 888  889                          asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
 889  890                  if (offset + asize > vd->vdev_asize) {
 890  891                          zfs_panic_recover("blkptr at %p DVA %u has invalid "
 891  892                              "OFFSET %llu",
 892  893                              bp, i, (longlong_t)offset);
 893  894                  }
 894  895          }
 895  896  }
 896  897  
 897  898  boolean_t
 898  899  zfs_dva_valid(spa_t *spa, const dva_t *dva, const blkptr_t *bp)
 899  900  {
 900  901          uint64_t vdevid = DVA_GET_VDEV(dva);
 901  902  
 902  903          if (vdevid >= spa->spa_root_vdev->vdev_children)
 903  904                  return (B_FALSE);
 904  905  
 905  906          vdev_t *vd = spa->spa_root_vdev->vdev_child[vdevid];
 906  907          if (vd == NULL)
 907  908                  return (B_FALSE);
 908  909  
 909  910          if (vd->vdev_ops == &vdev_hole_ops)
 910  911                  return (B_FALSE);
 911  912  
 912  913          if (vd->vdev_ops == &vdev_missing_ops) {
 913  914                  return (B_FALSE);
 914  915          }
 915  916  
 916  917          uint64_t offset = DVA_GET_OFFSET(dva);
 917  918          uint64_t asize = DVA_GET_ASIZE(dva);
 918  919  
 919  920          if (BP_IS_GANG(bp))
 920  921                  asize = vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE);
 921  922          if (offset + asize > vd->vdev_asize)
 922  923                  return (B_FALSE);
 923  924  
 924  925          return (B_TRUE);
 925  926  }
 926  927  
 927  928  zio_t *
 928  929  zio_read(zio_t *pio, spa_t *spa, const blkptr_t *bp,
 929  930      abd_t *data, uint64_t size, zio_done_func_t *done, void *private,
 930  931      zio_priority_t priority, enum zio_flag flags, const zbookmark_phys_t *zb)
 931  932  {
 932  933          zio_t *zio;
 933  934  
 934  935          zfs_blkptr_verify(spa, bp);
 935  936  
 936  937          zio = zio_create(pio, spa, BP_PHYSICAL_BIRTH(bp), bp,
 937  938              data, size, size, done, private,
 938  939              ZIO_TYPE_READ, priority, flags, NULL, 0, zb,
 939  940              ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
 940  941              ZIO_DDT_CHILD_READ_PIPELINE : ZIO_READ_PIPELINE);
 941  942  
 942  943          return (zio);
 943  944  }
 944  945  
 945  946  zio_t *
 946  947  zio_write(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp,
 947  948      abd_t *data, uint64_t lsize, uint64_t psize, const zio_prop_t *zp,
 948  949      zio_done_func_t *ready, zio_done_func_t *children_ready,
 949  950      zio_done_func_t *physdone, zio_done_func_t *done,
 950  951      void *private, zio_priority_t priority, enum zio_flag flags,
 951  952      const zbookmark_phys_t *zb)
 952  953  {
 953  954          zio_t *zio;
 954  955  
 955  956          ASSERT(zp->zp_checksum >= ZIO_CHECKSUM_OFF &&
 956  957              zp->zp_checksum < ZIO_CHECKSUM_FUNCTIONS &&
 957  958              zp->zp_compress >= ZIO_COMPRESS_OFF &&
 958  959              zp->zp_compress < ZIO_COMPRESS_FUNCTIONS &&
 959  960              DMU_OT_IS_VALID(zp->zp_type) &&
 960  961              zp->zp_level < 32 &&
 961  962              zp->zp_copies > 0 &&
 962  963              zp->zp_copies <= spa_max_replication(spa));
 963  964  
 964  965          zio = zio_create(pio, spa, txg, bp, data, lsize, psize, done, private,
 965  966              ZIO_TYPE_WRITE, priority, flags, NULL, 0, zb,
 966  967              ZIO_STAGE_OPEN, (flags & ZIO_FLAG_DDT_CHILD) ?
 967  968              ZIO_DDT_CHILD_WRITE_PIPELINE : ZIO_WRITE_PIPELINE);
 968  969  
 969  970          zio->io_ready = ready;
 970  971          zio->io_children_ready = children_ready;
 971  972          zio->io_physdone = physdone;
 972  973          zio->io_prop = *zp;
 973  974  
 974  975          /*
 975  976           * Data can be NULL if we are going to call zio_write_override() to
 976  977           * provide the already-allocated BP.  But we may need the data to
 977  978           * verify a dedup hit (if requested).  In this case, don't try to
 978  979           * dedup (just take the already-allocated BP verbatim). Encrypted
 979  980           * dedup blocks need data as well so we also disable dedup in this
 980  981           * case.
 981  982           */
 982  983          if (data == NULL &&
 983  984              (zio->io_prop.zp_dedup_verify || zio->io_prop.zp_encrypt)) {
 984  985                  zio->io_prop.zp_dedup = zio->io_prop.zp_dedup_verify = B_FALSE;
 985  986          }
 986  987  
 987  988          return (zio);
 988  989  }
 989  990  
 990  991  zio_t *
 991  992  zio_rewrite(zio_t *pio, spa_t *spa, uint64_t txg, blkptr_t *bp, abd_t *data,
 992  993      uint64_t size, zio_done_func_t *done, void *private,
 993  994      zio_priority_t priority, enum zio_flag flags, zbookmark_phys_t *zb)
 994  995  {
 995  996          zio_t *zio;
 996  997  
 997  998          zio = zio_create(pio, spa, txg, bp, data, size, size, done, private,
 998  999              ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_IO_REWRITE, NULL, 0, zb,
 999 1000              ZIO_STAGE_OPEN, ZIO_REWRITE_PIPELINE);
1000 1001  
1001 1002          return (zio);
1002 1003  }
1003 1004  
1004 1005  void
1005 1006  zio_write_override(zio_t *zio, blkptr_t *bp, int copies, boolean_t nopwrite)
1006 1007  {
1007 1008          ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1008 1009          ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1009 1010          ASSERT(zio->io_stage == ZIO_STAGE_OPEN);
1010 1011          ASSERT(zio->io_txg == spa_syncing_txg(zio->io_spa));
1011 1012  
1012 1013          /*
1013 1014           * We must reset the io_prop to match the values that existed
1014 1015           * when the bp was first written by dmu_sync() keeping in mind
1015 1016           * that nopwrite and dedup are mutually exclusive.
1016 1017           */
1017 1018          zio->io_prop.zp_dedup = nopwrite ? B_FALSE : zio->io_prop.zp_dedup;
1018 1019          zio->io_prop.zp_nopwrite = nopwrite;
1019 1020          zio->io_prop.zp_copies = copies;
1020 1021          zio->io_bp_override = bp;
1021 1022  }
1022 1023  
1023 1024  void
1024 1025  zio_free(spa_t *spa, uint64_t txg, const blkptr_t *bp)
1025 1026  {
1026 1027  
1027 1028          zfs_blkptr_verify(spa, bp);
1028 1029  
1029 1030          /*
1030 1031           * The check for EMBEDDED is a performance optimization.  We
1031 1032           * process the free here (by ignoring it) rather than
1032 1033           * putting it on the list and then processing it in zio_free_sync().
1033 1034           */
1034 1035          if (BP_IS_EMBEDDED(bp))
1035 1036                  return;
1036 1037          metaslab_check_free(spa, bp);
1037 1038  
1038 1039          /*
1039 1040           * Frees that are for the currently-syncing txg, are not going to be
1040 1041           * deferred, and which will not need to do a read (i.e. not GANG or
1041 1042           * DEDUP), can be processed immediately.  Otherwise, put them on the
1042 1043           * in-memory list for later processing.
1043 1044           *
1044 1045           * Note that we only defer frees after zfs_sync_pass_deferred_free
1045 1046           * when the log space map feature is disabled. [see relevant comment
1046 1047           * in spa_sync_iterate_to_convergence()]
1047 1048           */
1048 1049          if (BP_IS_GANG(bp) ||
1049 1050              BP_GET_DEDUP(bp) ||
1050 1051              txg != spa->spa_syncing_txg ||
1051 1052              (spa_sync_pass(spa) >= zfs_sync_pass_deferred_free &&
1052 1053              !spa_feature_is_active(spa, SPA_FEATURE_LOG_SPACEMAP))) {
1053 1054                  bplist_append(&spa->spa_free_bplist[txg & TXG_MASK], bp);
1054 1055          } else {
1055 1056                  VERIFY0(zio_wait(zio_free_sync(NULL, spa, txg, bp, 0)));
1056 1057          }
1057 1058  }
1058 1059  
1059 1060  zio_t *
1060 1061  zio_free_sync(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1061 1062      enum zio_flag flags)
1062 1063  {
1063 1064          zio_t *zio;
1064 1065          enum zio_stage stage = ZIO_FREE_PIPELINE;
1065 1066  
1066 1067          ASSERT(!BP_IS_HOLE(bp));
1067 1068          ASSERT(spa_syncing_txg(spa) == txg);
1068 1069  
1069 1070          if (BP_IS_EMBEDDED(bp))
1070 1071                  return (zio_null(pio, spa, NULL, NULL, NULL, 0));
1071 1072  
1072 1073          metaslab_check_free(spa, bp);
1073 1074          arc_freed(spa, bp);
1074 1075          dsl_scan_freed(spa, bp);
1075 1076  
1076 1077          /*
1077 1078           * GANG and DEDUP blocks can induce a read (for the gang block header,
1078 1079           * or the DDT), so issue them asynchronously so that this thread is
1079 1080           * not tied up.
1080 1081           */
1081 1082          if (BP_IS_GANG(bp) || BP_GET_DEDUP(bp))
1082 1083                  stage |= ZIO_STAGE_ISSUE_ASYNC;
1083 1084  
1084 1085          zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1085 1086              BP_GET_PSIZE(bp), NULL, NULL, ZIO_TYPE_FREE, ZIO_PRIORITY_NOW,
1086 1087              flags, NULL, 0, NULL, ZIO_STAGE_OPEN, stage);
1087 1088  
1088 1089          return (zio);
1089 1090  }
1090 1091  
1091 1092  zio_t *
1092 1093  zio_claim(zio_t *pio, spa_t *spa, uint64_t txg, const blkptr_t *bp,
1093 1094      zio_done_func_t *done, void *private, enum zio_flag flags)
1094 1095  {
1095 1096          zio_t *zio;
1096 1097  
1097 1098          zfs_blkptr_verify(spa, bp);
1098 1099  
1099 1100          if (BP_IS_EMBEDDED(bp))
1100 1101                  return (zio_null(pio, spa, NULL, NULL, NULL, 0));
1101 1102  
1102 1103          /*
1103 1104           * A claim is an allocation of a specific block.  Claims are needed
1104 1105           * to support immediate writes in the intent log.  The issue is that
1105 1106           * immediate writes contain committed data, but in a txg that was
1106 1107           * *not* committed.  Upon opening the pool after an unclean shutdown,
1107 1108           * the intent log claims all blocks that contain immediate write data
1108 1109           * so that the SPA knows they're in use.
1109 1110           *
1110 1111           * All claims *must* be resolved in the first txg -- before the SPA
1111 1112           * starts allocating blocks -- so that nothing is allocated twice.
1112 1113           * If txg == 0 we just verify that the block is claimable.
1113 1114           */
1114 1115          ASSERT3U(spa->spa_uberblock.ub_rootbp.blk_birth, <,
1115 1116              spa_min_claim_txg(spa));
1116 1117          ASSERT(txg == spa_min_claim_txg(spa) || txg == 0);
1117 1118          ASSERT(!BP_GET_DEDUP(bp) || !spa_writeable(spa));       /* zdb(1M) */
1118 1119  
1119 1120          zio = zio_create(pio, spa, txg, bp, NULL, BP_GET_PSIZE(bp),
1120 1121              BP_GET_PSIZE(bp), done, private, ZIO_TYPE_CLAIM, ZIO_PRIORITY_NOW,
1121 1122              flags, NULL, 0, NULL, ZIO_STAGE_OPEN, ZIO_CLAIM_PIPELINE);
1122 1123          ASSERT0(zio->io_queued_timestamp);
1123 1124  
1124 1125          return (zio);
1125 1126  }
1126 1127  
1127 1128  zio_t *
1128 1129  zio_ioctl(zio_t *pio, spa_t *spa, vdev_t *vd, int cmd,
1129 1130      zio_done_func_t *done, void *private, enum zio_flag flags)
1130 1131  {
1131 1132          zio_t *zio;
1132 1133          int c;
1133 1134  
1134 1135          if (vd->vdev_children == 0) {
1135 1136                  zio = zio_create(pio, spa, 0, NULL, NULL, 0, 0, done, private,
1136 1137                      ZIO_TYPE_IOCTL, ZIO_PRIORITY_NOW, flags, vd, 0, NULL,
1137 1138                      ZIO_STAGE_OPEN, ZIO_IOCTL_PIPELINE);
1138 1139  
1139 1140                  zio->io_cmd = cmd;
1140 1141          } else {
1141 1142                  zio = zio_null(pio, spa, NULL, NULL, NULL, flags);
1142 1143  
1143 1144                  for (c = 0; c < vd->vdev_children; c++)
1144 1145                          zio_nowait(zio_ioctl(zio, spa, vd->vdev_child[c], cmd,
1145 1146                              done, private, flags));
1146 1147          }
1147 1148  
1148 1149          return (zio);
1149 1150  }
1150 1151  
1151 1152  zio_t *
1152 1153  zio_trim(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1153 1154      zio_done_func_t *done, void *private, zio_priority_t priority,
1154 1155      enum zio_flag flags, enum trim_flag trim_flags)
1155 1156  {
1156 1157          zio_t *zio;
1157 1158  
1158 1159          ASSERT0(vd->vdev_children);
1159 1160          ASSERT0(P2PHASE(offset, 1ULL << vd->vdev_ashift));
1160 1161          ASSERT0(P2PHASE(size, 1ULL << vd->vdev_ashift));
1161 1162          ASSERT3U(size, !=, 0);
1162 1163  
1163 1164          zio = zio_create(pio, vd->vdev_spa, 0, NULL, NULL, size, size, done,
1164 1165              private, ZIO_TYPE_TRIM, priority, flags | ZIO_FLAG_PHYSICAL,
1165 1166              vd, offset, NULL, ZIO_STAGE_OPEN, ZIO_TRIM_PIPELINE);
1166 1167          zio->io_trim_flags = trim_flags;
1167 1168  
1168 1169          return (zio);
1169 1170  }
1170 1171  
1171 1172  zio_t *
1172 1173  zio_read_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1173 1174      abd_t *data, int checksum, zio_done_func_t *done, void *private,
1174 1175      zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1175 1176  {
1176 1177          zio_t *zio;
1177 1178  
1178 1179          ASSERT(vd->vdev_children == 0);
1179 1180          ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1180 1181              offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1181 1182          ASSERT3U(offset + size, <=, vd->vdev_psize);
1182 1183  
1183 1184          zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1184 1185              private, ZIO_TYPE_READ, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1185 1186              offset, NULL, ZIO_STAGE_OPEN, ZIO_READ_PHYS_PIPELINE);
1186 1187  
1187 1188          zio->io_prop.zp_checksum = checksum;
1188 1189  
1189 1190          return (zio);
1190 1191  }
1191 1192  
1192 1193  zio_t *
1193 1194  zio_write_phys(zio_t *pio, vdev_t *vd, uint64_t offset, uint64_t size,
1194 1195      abd_t *data, int checksum, zio_done_func_t *done, void *private,
1195 1196      zio_priority_t priority, enum zio_flag flags, boolean_t labels)
1196 1197  {
1197 1198          zio_t *zio;
1198 1199  
1199 1200          ASSERT(vd->vdev_children == 0);
1200 1201          ASSERT(!labels || offset + size <= VDEV_LABEL_START_SIZE ||
1201 1202              offset >= vd->vdev_psize - VDEV_LABEL_END_SIZE);
1202 1203          ASSERT3U(offset + size, <=, vd->vdev_psize);
1203 1204  
1204 1205          zio = zio_create(pio, vd->vdev_spa, 0, NULL, data, size, size, done,
1205 1206              private, ZIO_TYPE_WRITE, priority, flags | ZIO_FLAG_PHYSICAL, vd,
1206 1207              offset, NULL, ZIO_STAGE_OPEN, ZIO_WRITE_PHYS_PIPELINE);
1207 1208  
1208 1209          zio->io_prop.zp_checksum = checksum;
1209 1210  
1210 1211          if (zio_checksum_table[checksum].ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
1211 1212                  /*
1212 1213                   * zec checksums are necessarily destructive -- they modify
1213 1214                   * the end of the write buffer to hold the verifier/checksum.
1214 1215                   * Therefore, we must make a local copy in case the data is
1215 1216                   * being written to multiple places in parallel.
1216 1217                   */
1217 1218                  abd_t *wbuf = abd_alloc_sametype(data, size);
1218 1219                  abd_copy(wbuf, data, size);
1219 1220  
1220 1221                  zio_push_transform(zio, wbuf, size, size, NULL);
1221 1222          }
1222 1223  
1223 1224          return (zio);
1224 1225  }
1225 1226  
1226 1227  /*
1227 1228   * Create a child I/O to do some work for us.
1228 1229   */
1229 1230  zio_t *
1230 1231  zio_vdev_child_io(zio_t *pio, blkptr_t *bp, vdev_t *vd, uint64_t offset,
1231 1232      abd_t *data, uint64_t size, int type, zio_priority_t priority,
1232 1233      enum zio_flag flags, zio_done_func_t *done, void *private)
1233 1234  {
1234 1235          enum zio_stage pipeline = ZIO_VDEV_CHILD_PIPELINE;
1235 1236          zio_t *zio;
1236 1237  
1237 1238          /*
1238 1239           * vdev child I/Os do not propagate their error to the parent.
1239 1240           * Therefore, for correct operation the caller *must* check for
1240 1241           * and handle the error in the child i/o's done callback.
1241 1242           * The only exceptions are i/os that we don't care about
1242 1243           * (OPTIONAL or REPAIR).
1243 1244           */
1244 1245          ASSERT((flags & ZIO_FLAG_OPTIONAL) || (flags & ZIO_FLAG_IO_REPAIR) ||
1245 1246              done != NULL);
1246 1247  
1247 1248          if (type == ZIO_TYPE_READ && bp != NULL) {
1248 1249                  /*
1249 1250                   * If we have the bp, then the child should perform the
1250 1251                   * checksum and the parent need not.  This pushes error
1251 1252                   * detection as close to the leaves as possible and
1252 1253                   * eliminates redundant checksums in the interior nodes.
1253 1254                   */
1254 1255                  pipeline |= ZIO_STAGE_CHECKSUM_VERIFY;
1255 1256                  pio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
1256 1257          }
1257 1258  
1258 1259          if (vd->vdev_ops->vdev_op_leaf) {
1259 1260                  ASSERT0(vd->vdev_children);
1260 1261                  offset += VDEV_LABEL_START_SIZE;
1261 1262          }
1262 1263  
1263 1264          flags |= ZIO_VDEV_CHILD_FLAGS(pio);
1264 1265  
1265 1266          /*
1266 1267           * If we've decided to do a repair, the write is not speculative --
1267 1268           * even if the original read was.
1268 1269           */
1269 1270          if (flags & ZIO_FLAG_IO_REPAIR)
1270 1271                  flags &= ~ZIO_FLAG_SPECULATIVE;
1271 1272  
1272 1273          /*
1273 1274           * If we're creating a child I/O that is not associated with a
1274 1275           * top-level vdev, then the child zio is not an allocating I/O.
1275 1276           * If this is a retried I/O then we ignore it since we will
1276 1277           * have already processed the original allocating I/O.
1277 1278           */
1278 1279          if (flags & ZIO_FLAG_IO_ALLOCATING &&
1279 1280              (vd != vd->vdev_top || (flags & ZIO_FLAG_IO_RETRY))) {
1280 1281                  ASSERT(pio->io_metaslab_class != NULL);
1281 1282                  ASSERT(pio->io_metaslab_class->mc_alloc_throttle_enabled);
1282 1283                  ASSERT(type == ZIO_TYPE_WRITE);
1283 1284                  ASSERT(priority == ZIO_PRIORITY_ASYNC_WRITE);
1284 1285                  ASSERT(!(flags & ZIO_FLAG_IO_REPAIR));
1285 1286                  ASSERT(!(pio->io_flags & ZIO_FLAG_IO_REWRITE) ||
1286 1287                      pio->io_child_type == ZIO_CHILD_GANG);
1287 1288  
1288 1289                  flags &= ~ZIO_FLAG_IO_ALLOCATING;
1289 1290          }
1290 1291  
1291 1292          zio = zio_create(pio, pio->io_spa, pio->io_txg, bp, data, size, size,
1292 1293              done, private, type, priority, flags, vd, offset, &pio->io_bookmark,
1293 1294              ZIO_STAGE_VDEV_IO_START >> 1, pipeline);
1294 1295          ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
1295 1296  
1296 1297          zio->io_physdone = pio->io_physdone;
1297 1298          if (vd->vdev_ops->vdev_op_leaf && zio->io_logical != NULL)
1298 1299                  zio->io_logical->io_phys_children++;
1299 1300  
1300 1301          return (zio);
1301 1302  }
1302 1303  
1303 1304  zio_t *
1304 1305  zio_vdev_delegated_io(vdev_t *vd, uint64_t offset, abd_t *data, uint64_t size,
1305 1306      zio_type_t type, zio_priority_t priority, enum zio_flag flags,
1306 1307      zio_done_func_t *done, void *private)
1307 1308  {
1308 1309          zio_t *zio;
1309 1310  
1310 1311          ASSERT(vd->vdev_ops->vdev_op_leaf);
1311 1312  
1312 1313          zio = zio_create(NULL, vd->vdev_spa, 0, NULL,
1313 1314              data, size, size, done, private, type, priority,
1314 1315              flags | ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_RETRY | ZIO_FLAG_DELEGATED,
1315 1316              vd, offset, NULL,
1316 1317              ZIO_STAGE_VDEV_IO_START >> 1, ZIO_VDEV_CHILD_PIPELINE);
1317 1318  
1318 1319          return (zio);
1319 1320  }
1320 1321  
1321 1322  void
1322 1323  zio_flush(zio_t *zio, vdev_t *vd)
1323 1324  {
1324 1325          zio_nowait(zio_ioctl(zio, zio->io_spa, vd, DKIOCFLUSHWRITECACHE,
1325 1326              NULL, NULL,
1326 1327              ZIO_FLAG_CANFAIL | ZIO_FLAG_DONT_PROPAGATE | ZIO_FLAG_DONT_RETRY));
1327 1328  }
1328 1329  
1329 1330  void
1330 1331  zio_shrink(zio_t *zio, uint64_t size)
1331 1332  {
1332 1333          ASSERT3P(zio->io_executor, ==, NULL);
1333 1334          ASSERT3P(zio->io_orig_size, ==, zio->io_size);
1334 1335          ASSERT3U(size, <=, zio->io_size);
1335 1336  
1336 1337          /*
1337 1338           * We don't shrink for raidz because of problems with the
1338 1339           * reconstruction when reading back less than the block size.
1339 1340           * Note, BP_IS_RAIDZ() assumes no compression.
1340 1341           */
1341 1342          ASSERT(BP_GET_COMPRESS(zio->io_bp) == ZIO_COMPRESS_OFF);
1342 1343          if (!BP_IS_RAIDZ(zio->io_bp)) {
1343 1344                  /* we are not doing a raw write */
1344 1345                  ASSERT3U(zio->io_size, ==, zio->io_lsize);
1345 1346                  zio->io_orig_size = zio->io_size = zio->io_lsize = size;
1346 1347          }
1347 1348  }
1348 1349  
1349 1350  /*
1350 1351   * ==========================================================================
1351 1352   * Prepare to read and write logical blocks
1352 1353   * ==========================================================================
1353 1354   */
1354 1355  
1355 1356  static int
1356 1357  zio_read_bp_init(zio_t *zio)
1357 1358  {
1358 1359          blkptr_t *bp = zio->io_bp;
1359 1360          uint64_t psize =
1360 1361              BP_IS_EMBEDDED(bp) ? BPE_GET_PSIZE(bp) : BP_GET_PSIZE(bp);
1361 1362  
1362 1363          ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1363 1364  
1364 1365          if (BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
1365 1366              zio->io_child_type == ZIO_CHILD_LOGICAL &&
1366 1367              !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1367 1368                  zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1368 1369                      psize, psize, zio_decompress);
1369 1370          }
1370 1371  
1371 1372          if (((BP_IS_PROTECTED(bp) && !(zio->io_flags & ZIO_FLAG_RAW_ENCRYPT)) ||
1372 1373              BP_HAS_INDIRECT_MAC_CKSUM(bp)) &&
1373 1374              zio->io_child_type == ZIO_CHILD_LOGICAL) {
1374 1375                  zio_push_transform(zio, abd_alloc_sametype(zio->io_abd, psize),
1375 1376                      psize, psize, zio_decrypt);
1376 1377          }
1377 1378  
1378 1379          if (BP_IS_EMBEDDED(bp) && BPE_GET_ETYPE(bp) == BP_EMBEDDED_TYPE_DATA) {
1379 1380                  int psize = BPE_GET_PSIZE(bp);
1380 1381                  void *data = abd_borrow_buf(zio->io_abd, psize);
1381 1382  
1382 1383                  zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1383 1384                  decode_embedded_bp_compressed(bp, data);
1384 1385                  abd_return_buf_copy(zio->io_abd, data, psize);
1385 1386          } else {
1386 1387                  ASSERT(!BP_IS_EMBEDDED(bp));
1387 1388                  ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1388 1389          }
1389 1390  
1390 1391          if (!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)) && BP_GET_LEVEL(bp) == 0)
1391 1392                  zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1392 1393  
1393 1394          if (BP_GET_TYPE(bp) == DMU_OT_DDT_ZAP)
1394 1395                  zio->io_flags |= ZIO_FLAG_DONT_CACHE;
1395 1396  
1396 1397          if (BP_GET_DEDUP(bp) && zio->io_child_type == ZIO_CHILD_LOGICAL)
1397 1398                  zio->io_pipeline = ZIO_DDT_READ_PIPELINE;
1398 1399  
1399 1400          return (ZIO_PIPELINE_CONTINUE);
1400 1401  }
1401 1402  
1402 1403  static int
1403 1404  zio_write_bp_init(zio_t *zio)
1404 1405  {
1405 1406          if (!IO_IS_ALLOCATING(zio))
1406 1407                  return (ZIO_PIPELINE_CONTINUE);
1407 1408  
1408 1409          ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1409 1410  
1410 1411          if (zio->io_bp_override) {
1411 1412                  blkptr_t *bp = zio->io_bp;
1412 1413                  zio_prop_t *zp = &zio->io_prop;
1413 1414  
1414 1415                  ASSERT(bp->blk_birth != zio->io_txg);
1415 1416                  ASSERT(BP_GET_DEDUP(zio->io_bp_override) == 0);
1416 1417  
1417 1418                  *bp = *zio->io_bp_override;
1418 1419                  zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1419 1420  
1420 1421                  if (BP_IS_EMBEDDED(bp))
1421 1422                          return (ZIO_PIPELINE_CONTINUE);
1422 1423  
1423 1424                  /*
1424 1425                   * If we've been overridden and nopwrite is set then
1425 1426                   * set the flag accordingly to indicate that a nopwrite
1426 1427                   * has already occurred.
1427 1428                   */
1428 1429                  if (!BP_IS_HOLE(bp) && zp->zp_nopwrite) {
1429 1430                          ASSERT(!zp->zp_dedup);
1430 1431                          ASSERT3U(BP_GET_CHECKSUM(bp), ==, zp->zp_checksum);
1431 1432                          zio->io_flags |= ZIO_FLAG_NOPWRITE;
1432 1433                          return (ZIO_PIPELINE_CONTINUE);
1433 1434                  }
1434 1435  
1435 1436                  ASSERT(!zp->zp_nopwrite);
1436 1437  
1437 1438                  if (BP_IS_HOLE(bp) || !zp->zp_dedup)
1438 1439                          return (ZIO_PIPELINE_CONTINUE);
1439 1440  
1440 1441                  ASSERT((zio_checksum_table[zp->zp_checksum].ci_flags &
1441 1442                      ZCHECKSUM_FLAG_DEDUP) || zp->zp_dedup_verify);
1442 1443  
1443 1444                  if (BP_GET_CHECKSUM(bp) == zp->zp_checksum &&
1444 1445                      !zp->zp_encrypt) {
1445 1446                          BP_SET_DEDUP(bp, 1);
1446 1447                          zio->io_pipeline |= ZIO_STAGE_DDT_WRITE;
1447 1448                          return (ZIO_PIPELINE_CONTINUE);
1448 1449                  }
1449 1450  
1450 1451                  /*
1451 1452                   * We were unable to handle this as an override bp, treat
1452 1453                   * it as a regular write I/O.
1453 1454                   */
1454 1455                  zio->io_bp_override = NULL;
1455 1456                  *bp = zio->io_bp_orig;
1456 1457                  zio->io_pipeline = zio->io_orig_pipeline;
1457 1458          }
1458 1459  
1459 1460          return (ZIO_PIPELINE_CONTINUE);
1460 1461  }
1461 1462  
1462 1463  static int
1463 1464  zio_write_compress(zio_t *zio)
1464 1465  {
1465 1466          spa_t *spa = zio->io_spa;
1466 1467          zio_prop_t *zp = &zio->io_prop;
1467 1468          enum zio_compress compress = zp->zp_compress;
1468 1469          blkptr_t *bp = zio->io_bp;
1469 1470          uint64_t lsize = zio->io_lsize;
1470 1471          uint64_t psize = zio->io_size;
1471 1472          int pass = 1;
1472 1473  
1473 1474          /*
1474 1475           * If our children haven't all reached the ready stage,
1475 1476           * wait for them and then repeat this pipeline stage.
1476 1477           */
1477 1478          if (zio_wait_for_children(zio, ZIO_CHILD_LOGICAL_BIT |
1478 1479              ZIO_CHILD_GANG_BIT, ZIO_WAIT_READY)) {
1479 1480                  return (ZIO_PIPELINE_STOP);
1480 1481          }
1481 1482  
1482 1483          if (!IO_IS_ALLOCATING(zio))
1483 1484                  return (ZIO_PIPELINE_CONTINUE);
1484 1485  
1485 1486          if (zio->io_children_ready != NULL) {
1486 1487                  /*
1487 1488                   * Now that all our children are ready, run the callback
1488 1489                   * associated with this zio in case it wants to modify the
1489 1490                   * data to be written.
1490 1491                   */
1491 1492                  ASSERT3U(zp->zp_level, >, 0);
1492 1493                  zio->io_children_ready(zio);
1493 1494          }
1494 1495  
1495 1496          ASSERT(zio->io_child_type != ZIO_CHILD_DDT);
1496 1497          ASSERT(zio->io_bp_override == NULL);
1497 1498  
1498 1499          if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg) {
1499 1500                  /*
1500 1501                   * We're rewriting an existing block, which means we're
1501 1502                   * working on behalf of spa_sync().  For spa_sync() to
1502 1503                   * converge, it must eventually be the case that we don't
1503 1504                   * have to allocate new blocks.  But compression changes
1504 1505                   * the blocksize, which forces a reallocate, and makes
1505 1506                   * convergence take longer.  Therefore, after the first
1506 1507                   * few passes, stop compressing to ensure convergence.
1507 1508                   */
1508 1509                  pass = spa_sync_pass(spa);
1509 1510  
1510 1511                  ASSERT(zio->io_txg == spa_syncing_txg(spa));
1511 1512                  ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1512 1513                  ASSERT(!BP_GET_DEDUP(bp));
1513 1514  
1514 1515                  if (pass >= zfs_sync_pass_dont_compress)
1515 1516                          compress = ZIO_COMPRESS_OFF;
1516 1517  
1517 1518                  /* Make sure someone doesn't change their mind on overwrites */
1518 1519                  ASSERT(BP_IS_EMBEDDED(bp) || MIN(zp->zp_copies + BP_IS_GANG(bp),
1519 1520                      spa_max_replication(spa)) == BP_GET_NDVAS(bp));
1520 1521          }
1521 1522  
1522 1523          /* If it's a compressed write that is not raw, compress the buffer. */
1523 1524          if (compress != ZIO_COMPRESS_OFF &&
1524 1525              !(zio->io_flags & ZIO_FLAG_RAW_COMPRESS)) {
1525 1526                  void *cbuf = zio_buf_alloc(lsize);
1526 1527                  psize = zio_compress_data(compress, zio->io_abd, cbuf, lsize);
1527 1528                  if (psize == 0 || psize == lsize) {
1528 1529                          compress = ZIO_COMPRESS_OFF;
1529 1530                          zio_buf_free(cbuf, lsize);
1530 1531                  } else if (!zp->zp_dedup && !zp->zp_encrypt &&
1531 1532                      psize <= BPE_PAYLOAD_SIZE &&
1532 1533                      zp->zp_level == 0 && !DMU_OT_HAS_FILL(zp->zp_type) &&
1533 1534                      spa_feature_is_enabled(spa, SPA_FEATURE_EMBEDDED_DATA)) {
1534 1535                          encode_embedded_bp_compressed(bp,
1535 1536                              cbuf, compress, lsize, psize);
1536 1537                          BPE_SET_ETYPE(bp, BP_EMBEDDED_TYPE_DATA);
1537 1538                          BP_SET_TYPE(bp, zio->io_prop.zp_type);
1538 1539                          BP_SET_LEVEL(bp, zio->io_prop.zp_level);
1539 1540                          zio_buf_free(cbuf, lsize);
1540 1541                          bp->blk_birth = zio->io_txg;
1541 1542                          zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1542 1543                          ASSERT(spa_feature_is_active(spa,
1543 1544                              SPA_FEATURE_EMBEDDED_DATA));
1544 1545                          return (ZIO_PIPELINE_CONTINUE);
1545 1546                  } else {
1546 1547                          /*
1547 1548                           * Round up compressed size up to the ashift
1548 1549                           * of the smallest-ashift device, and zero the tail.
1549 1550                           * This ensures that the compressed size of the BP
1550 1551                           * (and thus compressratio property) are correct,
1551 1552                           * in that we charge for the padding used to fill out
1552 1553                           * the last sector.
1553 1554                           */
1554 1555                          ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
1555 1556                          size_t rounded = (size_t)P2ROUNDUP(psize,
1556 1557                              1ULL << spa->spa_min_ashift);
1557 1558                          if (rounded >= lsize) {
1558 1559                                  compress = ZIO_COMPRESS_OFF;
1559 1560                                  zio_buf_free(cbuf, lsize);
1560 1561                                  psize = lsize;
1561 1562                          } else {
1562 1563                                  abd_t *cdata = abd_get_from_buf(cbuf, lsize);
1563 1564                                  abd_take_ownership_of_buf(cdata, B_TRUE);
1564 1565                                  abd_zero_off(cdata, psize, rounded - psize);
1565 1566                                  psize = rounded;
1566 1567                                  zio_push_transform(zio, cdata,
1567 1568                                      psize, lsize, NULL);
1568 1569                          }
1569 1570                  }
1570 1571  
1571 1572                  /*
1572 1573                   * We were unable to handle this as an override bp, treat
1573 1574                   * it as a regular write I/O.
1574 1575                   */
1575 1576                  zio->io_bp_override = NULL;
1576 1577                  *bp = zio->io_bp_orig;
1577 1578                  zio->io_pipeline = zio->io_orig_pipeline;
1578 1579  
1579 1580          } else if ((zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) != 0 &&
1580 1581              zp->zp_type == DMU_OT_DNODE) {
1581 1582                  /*
1582 1583                   * The DMU actually relies on the zio layer's compression
1583 1584                   * to free metadnode blocks that have had all contained
1584 1585                   * dnodes freed. As a result, even when doing a raw
1585 1586                   * receive, we must check whether the block can be compressed
1586 1587                   * to a hole.
1587 1588                   */
1588 1589                  psize = zio_compress_data(ZIO_COMPRESS_EMPTY,
1589 1590                      zio->io_abd, NULL, lsize);
1590 1591                  if (psize == 0)
1591 1592                          compress = ZIO_COMPRESS_OFF;
1592 1593          } else {
1593 1594                  ASSERT3U(psize, !=, 0);
1594 1595          }
1595 1596  
1596 1597          /*
1597 1598           * The final pass of spa_sync() must be all rewrites, but the first
1598 1599           * few passes offer a trade-off: allocating blocks defers convergence,
1599 1600           * but newly allocated blocks are sequential, so they can be written
1600 1601           * to disk faster.  Therefore, we allow the first few passes of
1601 1602           * spa_sync() to allocate new blocks, but force rewrites after that.
1602 1603           * There should only be a handful of blocks after pass 1 in any case.
1603 1604           */
1604 1605          if (!BP_IS_HOLE(bp) && bp->blk_birth == zio->io_txg &&
1605 1606              BP_GET_PSIZE(bp) == psize &&
1606 1607              pass >= zfs_sync_pass_rewrite) {
1607 1608                  VERIFY3U(psize, !=, 0);
1608 1609                  enum zio_stage gang_stages = zio->io_pipeline & ZIO_GANG_STAGES;
1609 1610                  zio->io_pipeline = ZIO_REWRITE_PIPELINE | gang_stages;
1610 1611                  zio->io_flags |= ZIO_FLAG_IO_REWRITE;
1611 1612          } else {
1612 1613                  BP_ZERO(bp);
1613 1614                  zio->io_pipeline = ZIO_WRITE_PIPELINE;
1614 1615          }
1615 1616  
1616 1617          if (psize == 0) {
1617 1618                  if (zio->io_bp_orig.blk_birth != 0 &&
1618 1619                      spa_feature_is_active(spa, SPA_FEATURE_HOLE_BIRTH)) {
1619 1620                          BP_SET_LSIZE(bp, lsize);
1620 1621                          BP_SET_TYPE(bp, zp->zp_type);
1621 1622                          BP_SET_LEVEL(bp, zp->zp_level);
1622 1623                          BP_SET_BIRTH(bp, zio->io_txg, 0);
1623 1624                  }
1624 1625                  zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
1625 1626          } else {
1626 1627                  ASSERT(zp->zp_checksum != ZIO_CHECKSUM_GANG_HEADER);
1627 1628                  BP_SET_LSIZE(bp, lsize);
1628 1629                  BP_SET_TYPE(bp, zp->zp_type);
1629 1630                  BP_SET_LEVEL(bp, zp->zp_level);
1630 1631                  BP_SET_PSIZE(bp, psize);
1631 1632                  BP_SET_COMPRESS(bp, compress);
1632 1633                  BP_SET_CHECKSUM(bp, zp->zp_checksum);
1633 1634                  BP_SET_DEDUP(bp, zp->zp_dedup);
1634 1635                  BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
1635 1636                  if (zp->zp_dedup) {
1636 1637                          ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1637 1638                          ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1638 1639                          ASSERT(!zp->zp_encrypt ||
1639 1640                              DMU_OT_IS_ENCRYPTED(zp->zp_type));
1640 1641                          zio->io_pipeline = ZIO_DDT_WRITE_PIPELINE;
1641 1642                  }
1642 1643                  if (zp->zp_nopwrite) {
1643 1644                          ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
1644 1645                          ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
1645 1646                          zio->io_pipeline |= ZIO_STAGE_NOP_WRITE;
1646 1647                  }
1647 1648          }
1648 1649          return (ZIO_PIPELINE_CONTINUE);
1649 1650  }
1650 1651  
1651 1652  static int
1652 1653  zio_free_bp_init(zio_t *zio)
1653 1654  {
1654 1655          blkptr_t *bp = zio->io_bp;
1655 1656  
1656 1657          if (zio->io_child_type == ZIO_CHILD_LOGICAL) {
1657 1658                  if (BP_GET_DEDUP(bp))
1658 1659                          zio->io_pipeline = ZIO_DDT_FREE_PIPELINE;
1659 1660          }
1660 1661  
1661 1662          ASSERT3P(zio->io_bp, ==, &zio->io_bp_copy);
1662 1663  
1663 1664          return (ZIO_PIPELINE_CONTINUE);
1664 1665  }
1665 1666  
1666 1667  /*
1667 1668   * ==========================================================================
1668 1669   * Execute the I/O pipeline
1669 1670   * ==========================================================================
1670 1671   */
1671 1672  
1672 1673  static void
1673 1674  zio_taskq_dispatch(zio_t *zio, zio_taskq_type_t q, boolean_t cutinline)
1674 1675  {
1675 1676          spa_t *spa = zio->io_spa;
1676 1677          zio_type_t t = zio->io_type;
1677 1678          int flags = (cutinline ? TQ_FRONT : 0);
1678 1679  
1679 1680          /*
1680 1681           * If we're a config writer or a probe, the normal issue and
1681 1682           * interrupt threads may all be blocked waiting for the config lock.
1682 1683           * In this case, select the otherwise-unused taskq for ZIO_TYPE_NULL.
1683 1684           */
1684 1685          if (zio->io_flags & (ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_PROBE))
1685 1686                  t = ZIO_TYPE_NULL;
1686 1687  
1687 1688          /*
1688 1689           * A similar issue exists for the L2ARC write thread until L2ARC 2.0.
1689 1690           */
1690 1691          if (t == ZIO_TYPE_WRITE && zio->io_vd && zio->io_vd->vdev_aux)
1691 1692                  t = ZIO_TYPE_NULL;
1692 1693  
1693 1694          /*
1694 1695           * If this is a high priority I/O, then use the high priority taskq if
1695 1696           * available.
1696 1697           */
1697 1698          if ((zio->io_priority == ZIO_PRIORITY_NOW ||
1698 1699              zio->io_priority == ZIO_PRIORITY_SYNC_WRITE) &&
1699 1700              spa->spa_zio_taskq[t][q + 1].stqs_count != 0)
1700 1701                  q++;
1701 1702  
1702 1703          ASSERT3U(q, <, ZIO_TASKQ_TYPES);
1703 1704  
1704 1705          /*
1705 1706           * NB: We are assuming that the zio can only be dispatched
1706 1707           * to a single taskq at a time.  It would be a grievous error
1707 1708           * to dispatch the zio to another taskq at the same time.
1708 1709           */
1709 1710          ASSERT(zio->io_tqent.tqent_next == NULL);
1710 1711          spa_taskq_dispatch_ent(spa, t, q, (task_func_t *)zio_execute, zio,
1711 1712              flags, &zio->io_tqent);
1712 1713  }
1713 1714  
1714 1715  static boolean_t
1715 1716  zio_taskq_member(zio_t *zio, zio_taskq_type_t q)
1716 1717  {
1717 1718          kthread_t *executor = zio->io_executor;
1718 1719          spa_t *spa = zio->io_spa;
1719 1720  
1720 1721          for (zio_type_t t = 0; t < ZIO_TYPES; t++) {
1721 1722                  spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
1722 1723                  uint_t i;
1723 1724                  for (i = 0; i < tqs->stqs_count; i++) {
1724 1725                          if (taskq_member(tqs->stqs_taskq[i], executor))
1725 1726                                  return (B_TRUE);
1726 1727                  }
1727 1728          }
1728 1729  
1729 1730          return (B_FALSE);
1730 1731  }
1731 1732  
1732 1733  static int
1733 1734  zio_issue_async(zio_t *zio)
1734 1735  {
1735 1736          zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
1736 1737  
1737 1738          return (ZIO_PIPELINE_STOP);
1738 1739  }
1739 1740  
1740 1741  void
1741 1742  zio_interrupt(zio_t *zio)
1742 1743  {
1743 1744          zio_taskq_dispatch(zio, ZIO_TASKQ_INTERRUPT, B_FALSE);
1744 1745  }
1745 1746  
1746 1747  void
1747 1748  zio_delay_interrupt(zio_t *zio)
1748 1749  {
1749 1750          /*
1750 1751           * The timeout_generic() function isn't defined in userspace, so
1751 1752           * rather than trying to implement the function, the zio delay
1752 1753           * functionality has been disabled for userspace builds.
1753 1754           */
1754 1755  
1755 1756  #ifdef _KERNEL
1756 1757          /*
1757 1758           * If io_target_timestamp is zero, then no delay has been registered
1758 1759           * for this IO, thus jump to the end of this function and "skip" the
1759 1760           * delay; issuing it directly to the zio layer.
1760 1761           */
1761 1762          if (zio->io_target_timestamp != 0) {
1762 1763                  hrtime_t now = gethrtime();
1763 1764  
1764 1765                  if (now >= zio->io_target_timestamp) {
1765 1766                          /*
1766 1767                           * This IO has already taken longer than the target
1767 1768                           * delay to complete, so we don't want to delay it
1768 1769                           * any longer; we "miss" the delay and issue it
1769 1770                           * directly to the zio layer. This is likely due to
1770 1771                           * the target latency being set to a value less than
1771 1772                           * the underlying hardware can satisfy (e.g. delay
1772 1773                           * set to 1ms, but the disks take 10ms to complete an
1773 1774                           * IO request).
1774 1775                           */
1775 1776  
1776 1777                          DTRACE_PROBE2(zio__delay__miss, zio_t *, zio,
1777 1778                              hrtime_t, now);
1778 1779  
1779 1780                          zio_interrupt(zio);
1780 1781                  } else {
1781 1782                          hrtime_t diff = zio->io_target_timestamp - now;
1782 1783  
1783 1784                          DTRACE_PROBE3(zio__delay__hit, zio_t *, zio,
1784 1785                              hrtime_t, now, hrtime_t, diff);
1785 1786  
1786 1787                          (void) timeout_generic(CALLOUT_NORMAL,
1787 1788                              (void (*)(void *))zio_interrupt, zio, diff, 1, 0);
1788 1789                  }
1789 1790  
1790 1791                  return;
1791 1792          }
1792 1793  #endif
1793 1794  
1794 1795          DTRACE_PROBE1(zio__delay__skip, zio_t *, zio);
1795 1796          zio_interrupt(zio);
1796 1797  }
1797 1798  
1798 1799  /*
1799 1800   * Execute the I/O pipeline until one of the following occurs:
1800 1801   *
1801 1802   *      (1) the I/O completes
1802 1803   *      (2) the pipeline stalls waiting for dependent child I/Os
1803 1804   *      (3) the I/O issues, so we're waiting for an I/O completion interrupt
1804 1805   *      (4) the I/O is delegated by vdev-level caching or aggregation
1805 1806   *      (5) the I/O is deferred due to vdev-level queueing
1806 1807   *      (6) the I/O is handed off to another thread.
1807 1808   *
1808 1809   * In all cases, the pipeline stops whenever there's no CPU work; it never
1809 1810   * burns a thread in cv_wait().
1810 1811   *
1811 1812   * There's no locking on io_stage because there's no legitimate way
1812 1813   * for multiple threads to be attempting to process the same I/O.
1813 1814   */
1814 1815  static zio_pipe_stage_t *zio_pipeline[];
1815 1816  
1816 1817  void
1817 1818  zio_execute(zio_t *zio)
1818 1819  {
1819 1820          zio->io_executor = curthread;
1820 1821  
1821 1822          ASSERT3U(zio->io_queued_timestamp, >, 0);
1822 1823  
1823 1824          while (zio->io_stage < ZIO_STAGE_DONE) {
1824 1825                  enum zio_stage pipeline = zio->io_pipeline;
1825 1826                  enum zio_stage stage = zio->io_stage;
1826 1827                  int rv;
1827 1828  
1828 1829                  ASSERT(!MUTEX_HELD(&zio->io_lock));
1829 1830                  ASSERT(ISP2(stage));
1830 1831                  ASSERT(zio->io_stall == NULL);
1831 1832  
1832 1833                  do {
1833 1834                          stage <<= 1;
1834 1835                  } while ((stage & pipeline) == 0);
1835 1836  
1836 1837                  ASSERT(stage <= ZIO_STAGE_DONE);
1837 1838  
1838 1839                  /*
1839 1840                   * If we are in interrupt context and this pipeline stage
1840 1841                   * will grab a config lock that is held across I/O,
1841 1842                   * or may wait for an I/O that needs an interrupt thread
1842 1843                   * to complete, issue async to avoid deadlock.
1843 1844                   *
1844 1845                   * For VDEV_IO_START, we cut in line so that the io will
1845 1846                   * be sent to disk promptly.
1846 1847                   */
1847 1848                  if ((stage & ZIO_BLOCKING_STAGES) && zio->io_vd == NULL &&
1848 1849                      zio_taskq_member(zio, ZIO_TASKQ_INTERRUPT)) {
1849 1850                          boolean_t cut = (stage == ZIO_STAGE_VDEV_IO_START) ?
1850 1851                              zio_requeue_io_start_cut_in_line : B_FALSE;
1851 1852                          zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, cut);
1852 1853                          return;
1853 1854                  }
1854 1855  
1855 1856                  zio->io_stage = stage;
1856 1857                  zio->io_pipeline_trace |= zio->io_stage;
1857 1858                  rv = zio_pipeline[highbit64(stage) - 1](zio);
1858 1859  
1859 1860                  if (rv == ZIO_PIPELINE_STOP)
1860 1861                          return;
1861 1862  
1862 1863                  ASSERT(rv == ZIO_PIPELINE_CONTINUE);
1863 1864          }
1864 1865  }
1865 1866  
1866 1867  /*
1867 1868   * ==========================================================================
1868 1869   * Initiate I/O, either sync or async
1869 1870   * ==========================================================================
1870 1871   */
1871 1872  int
1872 1873  zio_wait(zio_t *zio)
1873 1874  {
1874 1875          int error;
1875 1876  
1876 1877          ASSERT3P(zio->io_stage, ==, ZIO_STAGE_OPEN);
1877 1878          ASSERT3P(zio->io_executor, ==, NULL);
1878 1879  
1879 1880          zio->io_waiter = curthread;
1880 1881          ASSERT0(zio->io_queued_timestamp);
1881 1882          zio->io_queued_timestamp = gethrtime();
1882 1883  
1883 1884          zio_execute(zio);
1884 1885  
1885 1886          mutex_enter(&zio->io_lock);
1886 1887          while (zio->io_executor != NULL)
1887 1888                  cv_wait(&zio->io_cv, &zio->io_lock);
1888 1889          mutex_exit(&zio->io_lock);
1889 1890  
1890 1891          error = zio->io_error;
1891 1892          zio_destroy(zio);
1892 1893  
1893 1894          return (error);
1894 1895  }
1895 1896  
1896 1897  void
1897 1898  zio_nowait(zio_t *zio)
1898 1899  {
1899 1900          ASSERT3P(zio->io_executor, ==, NULL);
1900 1901  
1901 1902          if (zio->io_child_type == ZIO_CHILD_LOGICAL &&
1902 1903              zio_unique_parent(zio) == NULL) {
1903 1904                  /*
1904 1905                   * This is a logical async I/O with no parent to wait for it.
1905 1906                   * We add it to the spa_async_root_zio "Godfather" I/O which
1906 1907                   * will ensure they complete prior to unloading the pool.
1907 1908                   */
1908 1909                  spa_t *spa = zio->io_spa;
1909 1910  
1910 1911                  zio_add_child(spa->spa_async_zio_root[CPU_SEQID], zio);
1911 1912          }
1912 1913  
1913 1914          ASSERT0(zio->io_queued_timestamp);
1914 1915          zio->io_queued_timestamp = gethrtime();
1915 1916          zio_execute(zio);
1916 1917  }
1917 1918  
1918 1919  /*
1919 1920   * ==========================================================================
1920 1921   * Reexecute, cancel, or suspend/resume failed I/O
1921 1922   * ==========================================================================
1922 1923   */
1923 1924  
1924 1925  static void
1925 1926  zio_reexecute(zio_t *pio)
1926 1927  {
1927 1928          zio_t *cio, *cio_next;
1928 1929  
1929 1930          ASSERT(pio->io_child_type == ZIO_CHILD_LOGICAL);
1930 1931          ASSERT(pio->io_orig_stage == ZIO_STAGE_OPEN);
1931 1932          ASSERT(pio->io_gang_leader == NULL);
1932 1933          ASSERT(pio->io_gang_tree == NULL);
1933 1934  
1934 1935          pio->io_flags = pio->io_orig_flags;
1935 1936          pio->io_stage = pio->io_orig_stage;
1936 1937          pio->io_pipeline = pio->io_orig_pipeline;
1937 1938          pio->io_reexecute = 0;
1938 1939          pio->io_flags |= ZIO_FLAG_REEXECUTED;
1939 1940          pio->io_pipeline_trace = 0;
1940 1941          pio->io_error = 0;
1941 1942          for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1942 1943                  pio->io_state[w] = 0;
1943 1944          for (int c = 0; c < ZIO_CHILD_TYPES; c++)
1944 1945                  pio->io_child_error[c] = 0;
1945 1946  
1946 1947          if (IO_IS_ALLOCATING(pio))
1947 1948                  BP_ZERO(pio->io_bp);
1948 1949  
1949 1950          /*
1950 1951           * As we reexecute pio's children, new children could be created.
1951 1952           * New children go to the head of pio's io_child_list, however,
1952 1953           * so we will (correctly) not reexecute them.  The key is that
1953 1954           * the remainder of pio's io_child_list, from 'cio_next' onward,
1954 1955           * cannot be affected by any side effects of reexecuting 'cio'.
1955 1956           */
1956 1957          zio_link_t *zl = NULL;
1957 1958          mutex_enter(&pio->io_lock);
1958 1959          for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
1959 1960                  cio_next = zio_walk_children(pio, &zl);
1960 1961                  for (int w = 0; w < ZIO_WAIT_TYPES; w++)
1961 1962                          pio->io_children[cio->io_child_type][w]++;
1962 1963                  mutex_exit(&pio->io_lock);
1963 1964                  zio_reexecute(cio);
1964 1965                  mutex_enter(&pio->io_lock);
1965 1966          }
1966 1967          mutex_exit(&pio->io_lock);
1967 1968  
1968 1969          /*
1969 1970           * Now that all children have been reexecuted, execute the parent.
1970 1971           * We don't reexecute "The Godfather" I/O here as it's the
1971 1972           * responsibility of the caller to wait on it.
1972 1973           */
1973 1974          if (!(pio->io_flags & ZIO_FLAG_GODFATHER)) {
1974 1975                  pio->io_queued_timestamp = gethrtime();
1975 1976                  zio_execute(pio);
1976 1977          }
  
    | ↓ open down ↓ | 1940 lines elided | ↑ open up ↑ | 
1977 1978  }
1978 1979  
1979 1980  void
1980 1981  zio_suspend(spa_t *spa, zio_t *zio, zio_suspend_reason_t reason)
1981 1982  {
1982 1983          if (spa_get_failmode(spa) == ZIO_FAILURE_MODE_PANIC)
1983 1984                  fm_panic("Pool '%s' has encountered an uncorrectable I/O "
1984 1985                      "failure and the failure mode property for this pool "
1985 1986                      "is set to panic.", spa_name(spa));
1986 1987  
     1988 +        cmn_err(CE_WARN, "Pool '%s' has encountered an uncorrectable I/O "
     1989 +            "failure and has been suspended; `zpool clear` will be required "
     1990 +            "before the pool can be written to.", spa_name(spa));
     1991 +
1987 1992          zfs_ereport_post(FM_EREPORT_ZFS_IO_FAILURE, spa, NULL,
1988 1993              NULL, NULL, 0, 0);
1989 1994  
1990 1995          mutex_enter(&spa->spa_suspend_lock);
1991 1996  
1992 1997          if (spa->spa_suspend_zio_root == NULL)
1993 1998                  spa->spa_suspend_zio_root = zio_root(spa, NULL, NULL,
1994 1999                      ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
1995 2000                      ZIO_FLAG_GODFATHER);
1996 2001  
1997 2002          spa->spa_suspended = reason;
1998 2003  
1999 2004          if (zio != NULL) {
2000 2005                  ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
2001 2006                  ASSERT(zio != spa->spa_suspend_zio_root);
2002 2007                  ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2003 2008                  ASSERT(zio_unique_parent(zio) == NULL);
2004 2009                  ASSERT(zio->io_stage == ZIO_STAGE_DONE);
2005 2010                  zio_add_child(spa->spa_suspend_zio_root, zio);
2006 2011          }
2007 2012  
2008 2013          mutex_exit(&spa->spa_suspend_lock);
2009 2014  }
2010 2015  
2011 2016  int
2012 2017  zio_resume(spa_t *spa)
2013 2018  {
2014 2019          zio_t *pio;
2015 2020  
2016 2021          /*
2017 2022           * Reexecute all previously suspended i/o.
2018 2023           */
2019 2024          mutex_enter(&spa->spa_suspend_lock);
2020 2025          spa->spa_suspended = ZIO_SUSPEND_NONE;
2021 2026          cv_broadcast(&spa->spa_suspend_cv);
2022 2027          pio = spa->spa_suspend_zio_root;
2023 2028          spa->spa_suspend_zio_root = NULL;
2024 2029          mutex_exit(&spa->spa_suspend_lock);
2025 2030  
2026 2031          if (pio == NULL)
2027 2032                  return (0);
2028 2033  
2029 2034          zio_reexecute(pio);
2030 2035          return (zio_wait(pio));
2031 2036  }
2032 2037  
2033 2038  void
2034 2039  zio_resume_wait(spa_t *spa)
2035 2040  {
2036 2041          mutex_enter(&spa->spa_suspend_lock);
2037 2042          while (spa_suspended(spa))
2038 2043                  cv_wait(&spa->spa_suspend_cv, &spa->spa_suspend_lock);
2039 2044          mutex_exit(&spa->spa_suspend_lock);
2040 2045  }
2041 2046  
2042 2047  /*
2043 2048   * ==========================================================================
2044 2049   * Gang blocks.
2045 2050   *
2046 2051   * A gang block is a collection of small blocks that looks to the DMU
2047 2052   * like one large block.  When zio_dva_allocate() cannot find a block
2048 2053   * of the requested size, due to either severe fragmentation or the pool
2049 2054   * being nearly full, it calls zio_write_gang_block() to construct the
2050 2055   * block from smaller fragments.
2051 2056   *
2052 2057   * A gang block consists of a gang header (zio_gbh_phys_t) and up to
2053 2058   * three (SPA_GBH_NBLKPTRS) gang members.  The gang header is just like
2054 2059   * an indirect block: it's an array of block pointers.  It consumes
2055 2060   * only one sector and hence is allocatable regardless of fragmentation.
2056 2061   * The gang header's bps point to its gang members, which hold the data.
2057 2062   *
2058 2063   * Gang blocks are self-checksumming, using the bp's <vdev, offset, txg>
2059 2064   * as the verifier to ensure uniqueness of the SHA256 checksum.
2060 2065   * Critically, the gang block bp's blk_cksum is the checksum of the data,
2061 2066   * not the gang header.  This ensures that data block signatures (needed for
2062 2067   * deduplication) are independent of how the block is physically stored.
2063 2068   *
2064 2069   * Gang blocks can be nested: a gang member may itself be a gang block.
2065 2070   * Thus every gang block is a tree in which root and all interior nodes are
2066 2071   * gang headers, and the leaves are normal blocks that contain user data.
2067 2072   * The root of the gang tree is called the gang leader.
2068 2073   *
2069 2074   * To perform any operation (read, rewrite, free, claim) on a gang block,
2070 2075   * zio_gang_assemble() first assembles the gang tree (minus data leaves)
2071 2076   * in the io_gang_tree field of the original logical i/o by recursively
2072 2077   * reading the gang leader and all gang headers below it.  This yields
2073 2078   * an in-core tree containing the contents of every gang header and the
2074 2079   * bps for every constituent of the gang block.
2075 2080   *
2076 2081   * With the gang tree now assembled, zio_gang_issue() just walks the gang tree
2077 2082   * and invokes a callback on each bp.  To free a gang block, zio_gang_issue()
2078 2083   * calls zio_free_gang() -- a trivial wrapper around zio_free() -- for each bp.
2079 2084   * zio_claim_gang() provides a similarly trivial wrapper for zio_claim().
2080 2085   * zio_read_gang() is a wrapper around zio_read() that omits reading gang
2081 2086   * headers, since we already have those in io_gang_tree.  zio_rewrite_gang()
2082 2087   * performs a zio_rewrite() of the data or, for gang headers, a zio_rewrite()
2083 2088   * of the gang header plus zio_checksum_compute() of the data to update the
2084 2089   * gang header's blk_cksum as described above.
2085 2090   *
2086 2091   * The two-phase assemble/issue model solves the problem of partial failure --
2087 2092   * what if you'd freed part of a gang block but then couldn't read the
2088 2093   * gang header for another part?  Assembling the entire gang tree first
2089 2094   * ensures that all the necessary gang header I/O has succeeded before
2090 2095   * starting the actual work of free, claim, or write.  Once the gang tree
2091 2096   * is assembled, free and claim are in-memory operations that cannot fail.
2092 2097   *
2093 2098   * In the event that a gang write fails, zio_dva_unallocate() walks the
2094 2099   * gang tree to immediately free (i.e. insert back into the space map)
2095 2100   * everything we've allocated.  This ensures that we don't get ENOSPC
2096 2101   * errors during repeated suspend/resume cycles due to a flaky device.
2097 2102   *
2098 2103   * Gang rewrites only happen during sync-to-convergence.  If we can't assemble
2099 2104   * the gang tree, we won't modify the block, so we can safely defer the free
2100 2105   * (knowing that the block is still intact).  If we *can* assemble the gang
2101 2106   * tree, then even if some of the rewrites fail, zio_dva_unallocate() will free
2102 2107   * each constituent bp and we can allocate a new block on the next sync pass.
2103 2108   *
2104 2109   * In all cases, the gang tree allows complete recovery from partial failure.
2105 2110   * ==========================================================================
2106 2111   */
2107 2112  
2108 2113  static void
2109 2114  zio_gang_issue_func_done(zio_t *zio)
2110 2115  {
2111 2116          abd_put(zio->io_abd);
2112 2117  }
2113 2118  
2114 2119  static zio_t *
2115 2120  zio_read_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2116 2121      uint64_t offset)
2117 2122  {
2118 2123          if (gn != NULL)
2119 2124                  return (pio);
2120 2125  
2121 2126          return (zio_read(pio, pio->io_spa, bp, abd_get_offset(data, offset),
2122 2127              BP_GET_PSIZE(bp), zio_gang_issue_func_done,
2123 2128              NULL, pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2124 2129              &pio->io_bookmark));
2125 2130  }
2126 2131  
2127 2132  static zio_t *
2128 2133  zio_rewrite_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2129 2134      uint64_t offset)
2130 2135  {
2131 2136          zio_t *zio;
2132 2137  
2133 2138          if (gn != NULL) {
2134 2139                  abd_t *gbh_abd =
2135 2140                      abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2136 2141                  zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2137 2142                      gbh_abd, SPA_GANGBLOCKSIZE, zio_gang_issue_func_done, NULL,
2138 2143                      pio->io_priority, ZIO_GANG_CHILD_FLAGS(pio),
2139 2144                      &pio->io_bookmark);
2140 2145                  /*
2141 2146                   * As we rewrite each gang header, the pipeline will compute
2142 2147                   * a new gang block header checksum for it; but no one will
2143 2148                   * compute a new data checksum, so we do that here.  The one
2144 2149                   * exception is the gang leader: the pipeline already computed
2145 2150                   * its data checksum because that stage precedes gang assembly.
2146 2151                   * (Presently, nothing actually uses interior data checksums;
2147 2152                   * this is just good hygiene.)
2148 2153                   */
2149 2154                  if (gn != pio->io_gang_leader->io_gang_tree) {
2150 2155                          abd_t *buf = abd_get_offset(data, offset);
2151 2156  
2152 2157                          zio_checksum_compute(zio, BP_GET_CHECKSUM(bp),
2153 2158                              buf, BP_GET_PSIZE(bp));
2154 2159  
2155 2160                          abd_put(buf);
2156 2161                  }
2157 2162                  /*
2158 2163                   * If we are here to damage data for testing purposes,
2159 2164                   * leave the GBH alone so that we can detect the damage.
2160 2165                   */
2161 2166                  if (pio->io_gang_leader->io_flags & ZIO_FLAG_INDUCE_DAMAGE)
2162 2167                          zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
2163 2168          } else {
2164 2169                  zio = zio_rewrite(pio, pio->io_spa, pio->io_txg, bp,
2165 2170                      abd_get_offset(data, offset), BP_GET_PSIZE(bp),
2166 2171                      zio_gang_issue_func_done, NULL, pio->io_priority,
2167 2172                      ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2168 2173          }
2169 2174  
2170 2175          return (zio);
2171 2176  }
2172 2177  
2173 2178  /* ARGSUSED */
2174 2179  static zio_t *
2175 2180  zio_free_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2176 2181      uint64_t offset)
2177 2182  {
2178 2183          return (zio_free_sync(pio, pio->io_spa, pio->io_txg, bp,
2179 2184              ZIO_GANG_CHILD_FLAGS(pio)));
2180 2185  }
2181 2186  
2182 2187  /* ARGSUSED */
2183 2188  static zio_t *
2184 2189  zio_claim_gang(zio_t *pio, blkptr_t *bp, zio_gang_node_t *gn, abd_t *data,
2185 2190      uint64_t offset)
2186 2191  {
2187 2192          return (zio_claim(pio, pio->io_spa, pio->io_txg, bp,
2188 2193              NULL, NULL, ZIO_GANG_CHILD_FLAGS(pio)));
2189 2194  }
2190 2195  
2191 2196  static zio_gang_issue_func_t *zio_gang_issue_func[ZIO_TYPES] = {
2192 2197          NULL,
2193 2198          zio_read_gang,
2194 2199          zio_rewrite_gang,
2195 2200          zio_free_gang,
2196 2201          zio_claim_gang,
2197 2202          NULL
2198 2203  };
2199 2204  
2200 2205  static void zio_gang_tree_assemble_done(zio_t *zio);
2201 2206  
2202 2207  static zio_gang_node_t *
2203 2208  zio_gang_node_alloc(zio_gang_node_t **gnpp)
2204 2209  {
2205 2210          zio_gang_node_t *gn;
2206 2211  
2207 2212          ASSERT(*gnpp == NULL);
2208 2213  
2209 2214          gn = kmem_zalloc(sizeof (*gn), KM_SLEEP);
2210 2215          gn->gn_gbh = zio_buf_alloc(SPA_GANGBLOCKSIZE);
2211 2216          *gnpp = gn;
2212 2217  
2213 2218          return (gn);
2214 2219  }
2215 2220  
2216 2221  static void
2217 2222  zio_gang_node_free(zio_gang_node_t **gnpp)
2218 2223  {
2219 2224          zio_gang_node_t *gn = *gnpp;
2220 2225  
2221 2226          for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2222 2227                  ASSERT(gn->gn_child[g] == NULL);
2223 2228  
2224 2229          zio_buf_free(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2225 2230          kmem_free(gn, sizeof (*gn));
2226 2231          *gnpp = NULL;
2227 2232  }
2228 2233  
2229 2234  static void
2230 2235  zio_gang_tree_free(zio_gang_node_t **gnpp)
2231 2236  {
2232 2237          zio_gang_node_t *gn = *gnpp;
2233 2238  
2234 2239          if (gn == NULL)
2235 2240                  return;
2236 2241  
2237 2242          for (int g = 0; g < SPA_GBH_NBLKPTRS; g++)
2238 2243                  zio_gang_tree_free(&gn->gn_child[g]);
2239 2244  
2240 2245          zio_gang_node_free(gnpp);
2241 2246  }
2242 2247  
2243 2248  static void
2244 2249  zio_gang_tree_assemble(zio_t *gio, blkptr_t *bp, zio_gang_node_t **gnpp)
2245 2250  {
2246 2251          zio_gang_node_t *gn = zio_gang_node_alloc(gnpp);
2247 2252          abd_t *gbh_abd = abd_get_from_buf(gn->gn_gbh, SPA_GANGBLOCKSIZE);
2248 2253  
2249 2254          ASSERT(gio->io_gang_leader == gio);
2250 2255          ASSERT(BP_IS_GANG(bp));
2251 2256  
2252 2257          zio_nowait(zio_read(gio, gio->io_spa, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2253 2258              zio_gang_tree_assemble_done, gn, gio->io_priority,
2254 2259              ZIO_GANG_CHILD_FLAGS(gio), &gio->io_bookmark));
2255 2260  }
2256 2261  
2257 2262  static void
2258 2263  zio_gang_tree_assemble_done(zio_t *zio)
2259 2264  {
2260 2265          zio_t *gio = zio->io_gang_leader;
2261 2266          zio_gang_node_t *gn = zio->io_private;
2262 2267          blkptr_t *bp = zio->io_bp;
2263 2268  
2264 2269          ASSERT(gio == zio_unique_parent(zio));
2265 2270          ASSERT(zio->io_child_count == 0);
2266 2271  
2267 2272          if (zio->io_error)
2268 2273                  return;
2269 2274  
2270 2275          /* this ABD was created from a linear buf in zio_gang_tree_assemble */
2271 2276          if (BP_SHOULD_BYTESWAP(bp))
2272 2277                  byteswap_uint64_array(abd_to_buf(zio->io_abd), zio->io_size);
2273 2278  
2274 2279          ASSERT3P(abd_to_buf(zio->io_abd), ==, gn->gn_gbh);
2275 2280          ASSERT(zio->io_size == SPA_GANGBLOCKSIZE);
2276 2281          ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2277 2282  
2278 2283          abd_put(zio->io_abd);
2279 2284  
2280 2285          for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2281 2286                  blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2282 2287                  if (!BP_IS_GANG(gbp))
2283 2288                          continue;
2284 2289                  zio_gang_tree_assemble(gio, gbp, &gn->gn_child[g]);
2285 2290          }
2286 2291  }
2287 2292  
2288 2293  static void
2289 2294  zio_gang_tree_issue(zio_t *pio, zio_gang_node_t *gn, blkptr_t *bp, abd_t *data,
2290 2295      uint64_t offset)
2291 2296  {
2292 2297          zio_t *gio = pio->io_gang_leader;
2293 2298          zio_t *zio;
2294 2299  
2295 2300          ASSERT(BP_IS_GANG(bp) == !!gn);
2296 2301          ASSERT(BP_GET_CHECKSUM(bp) == BP_GET_CHECKSUM(gio->io_bp));
2297 2302          ASSERT(BP_GET_LSIZE(bp) == BP_GET_PSIZE(bp) || gn == gio->io_gang_tree);
2298 2303  
2299 2304          /*
2300 2305           * If you're a gang header, your data is in gn->gn_gbh.
2301 2306           * If you're a gang member, your data is in 'data' and gn == NULL.
2302 2307           */
2303 2308          zio = zio_gang_issue_func[gio->io_type](pio, bp, gn, data, offset);
2304 2309  
2305 2310          if (gn != NULL) {
2306 2311                  ASSERT(gn->gn_gbh->zg_tail.zec_magic == ZEC_MAGIC);
2307 2312  
2308 2313                  for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
2309 2314                          blkptr_t *gbp = &gn->gn_gbh->zg_blkptr[g];
2310 2315                          if (BP_IS_HOLE(gbp))
2311 2316                                  continue;
2312 2317                          zio_gang_tree_issue(zio, gn->gn_child[g], gbp, data,
2313 2318                              offset);
2314 2319                          offset += BP_GET_PSIZE(gbp);
2315 2320                  }
2316 2321          }
2317 2322  
2318 2323          if (gn == gio->io_gang_tree)
2319 2324                  ASSERT3U(gio->io_size, ==, offset);
2320 2325  
2321 2326          if (zio != pio)
2322 2327                  zio_nowait(zio);
2323 2328  }
2324 2329  
2325 2330  static int
2326 2331  zio_gang_assemble(zio_t *zio)
2327 2332  {
2328 2333          blkptr_t *bp = zio->io_bp;
2329 2334  
2330 2335          ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == NULL);
2331 2336          ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2332 2337  
2333 2338          zio->io_gang_leader = zio;
2334 2339  
2335 2340          zio_gang_tree_assemble(zio, bp, &zio->io_gang_tree);
2336 2341  
2337 2342          return (ZIO_PIPELINE_CONTINUE);
2338 2343  }
2339 2344  
2340 2345  static int
2341 2346  zio_gang_issue(zio_t *zio)
2342 2347  {
2343 2348          blkptr_t *bp = zio->io_bp;
2344 2349  
2345 2350          if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT, ZIO_WAIT_DONE)) {
2346 2351                  return (ZIO_PIPELINE_STOP);
2347 2352          }
2348 2353  
2349 2354          ASSERT(BP_IS_GANG(bp) && zio->io_gang_leader == zio);
2350 2355          ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
2351 2356  
2352 2357          if (zio->io_child_error[ZIO_CHILD_GANG] == 0)
2353 2358                  zio_gang_tree_issue(zio, zio->io_gang_tree, bp, zio->io_abd,
2354 2359                      0);
2355 2360          else
2356 2361                  zio_gang_tree_free(&zio->io_gang_tree);
2357 2362  
2358 2363          zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2359 2364  
2360 2365          return (ZIO_PIPELINE_CONTINUE);
2361 2366  }
2362 2367  
2363 2368  static void
2364 2369  zio_write_gang_member_ready(zio_t *zio)
2365 2370  {
2366 2371          zio_t *pio = zio_unique_parent(zio);
2367 2372          zio_t *gio = zio->io_gang_leader;
2368 2373          dva_t *cdva = zio->io_bp->blk_dva;
2369 2374          dva_t *pdva = pio->io_bp->blk_dva;
2370 2375          uint64_t asize;
2371 2376  
2372 2377          if (BP_IS_HOLE(zio->io_bp))
2373 2378                  return;
2374 2379  
2375 2380          ASSERT(BP_IS_HOLE(&zio->io_bp_orig));
2376 2381  
2377 2382          ASSERT(zio->io_child_type == ZIO_CHILD_GANG);
2378 2383          ASSERT3U(zio->io_prop.zp_copies, ==, gio->io_prop.zp_copies);
2379 2384          ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(zio->io_bp));
2380 2385          ASSERT3U(pio->io_prop.zp_copies, <=, BP_GET_NDVAS(pio->io_bp));
2381 2386          ASSERT3U(BP_GET_NDVAS(zio->io_bp), <=, BP_GET_NDVAS(pio->io_bp));
2382 2387  
2383 2388          mutex_enter(&pio->io_lock);
2384 2389          for (int d = 0; d < BP_GET_NDVAS(zio->io_bp); d++) {
2385 2390                  ASSERT(DVA_GET_GANG(&pdva[d]));
2386 2391                  asize = DVA_GET_ASIZE(&pdva[d]);
2387 2392                  asize += DVA_GET_ASIZE(&cdva[d]);
2388 2393                  DVA_SET_ASIZE(&pdva[d], asize);
2389 2394          }
2390 2395          mutex_exit(&pio->io_lock);
2391 2396  }
2392 2397  
2393 2398  static void
2394 2399  zio_write_gang_done(zio_t *zio)
2395 2400  {
2396 2401          /*
2397 2402           * The io_abd field will be NULL for a zio with no data.  The io_flags
2398 2403           * will initially have the ZIO_FLAG_NODATA bit flag set, but we can't
2399 2404           * check for it here as it is cleared in zio_ready.
2400 2405           */
2401 2406          if (zio->io_abd != NULL)
2402 2407                  abd_put(zio->io_abd);
2403 2408  }
2404 2409  
2405 2410  static int
2406 2411  zio_write_gang_block(zio_t *pio)
2407 2412  {
2408 2413          spa_t *spa = pio->io_spa;
2409 2414          metaslab_class_t *mc = spa_normal_class(spa);
2410 2415          blkptr_t *bp = pio->io_bp;
2411 2416          zio_t *gio = pio->io_gang_leader;
2412 2417          zio_t *zio;
2413 2418          zio_gang_node_t *gn, **gnpp;
2414 2419          zio_gbh_phys_t *gbh;
2415 2420          abd_t *gbh_abd;
2416 2421          uint64_t txg = pio->io_txg;
2417 2422          uint64_t resid = pio->io_size;
2418 2423          uint64_t lsize;
2419 2424          int copies = gio->io_prop.zp_copies;
2420 2425          int gbh_copies = MIN(copies + 1, spa_max_replication(spa));
2421 2426          zio_prop_t zp;
2422 2427          int error;
2423 2428          boolean_t has_data = !(pio->io_flags & ZIO_FLAG_NODATA);
2424 2429  
2425 2430          /*
2426 2431           * encrypted blocks need DVA[2] free so encrypted gang headers can't
2427 2432           * have a third copy.
2428 2433           */
2429 2434          if (gio->io_prop.zp_encrypt && gbh_copies >= SPA_DVAS_PER_BP)
2430 2435                  gbh_copies = SPA_DVAS_PER_BP - 1;
2431 2436  
2432 2437          int flags = METASLAB_HINTBP_FAVOR | METASLAB_GANG_HEADER;
2433 2438          if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2434 2439                  ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2435 2440                  ASSERT(has_data);
2436 2441  
2437 2442                  flags |= METASLAB_ASYNC_ALLOC;
2438 2443                  VERIFY(zfs_refcount_held(&mc->mc_alloc_slots[pio->io_allocator],
2439 2444                      pio));
2440 2445  
2441 2446                  /*
2442 2447                   * The logical zio has already placed a reservation for
2443 2448                   * 'copies' allocation slots but gang blocks may require
2444 2449                   * additional copies. These additional copies
2445 2450                   * (i.e. gbh_copies - copies) are guaranteed to succeed
2446 2451                   * since metaslab_class_throttle_reserve() always allows
2447 2452                   * additional reservations for gang blocks.
2448 2453                   */
2449 2454                  VERIFY(metaslab_class_throttle_reserve(mc, gbh_copies - copies,
2450 2455                      pio->io_allocator, pio, flags));
2451 2456          }
2452 2457  
2453 2458          error = metaslab_alloc(spa, mc, SPA_GANGBLOCKSIZE,
2454 2459              bp, gbh_copies, txg, pio == gio ? NULL : gio->io_bp, flags,
2455 2460              &pio->io_alloc_list, pio, pio->io_allocator);
2456 2461          if (error) {
2457 2462                  if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2458 2463                          ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2459 2464                          ASSERT(has_data);
2460 2465  
2461 2466                          /*
2462 2467                           * If we failed to allocate the gang block header then
2463 2468                           * we remove any additional allocation reservations that
2464 2469                           * we placed here. The original reservation will
2465 2470                           * be removed when the logical I/O goes to the ready
2466 2471                           * stage.
2467 2472                           */
2468 2473                          metaslab_class_throttle_unreserve(mc,
2469 2474                              gbh_copies - copies, pio->io_allocator, pio);
2470 2475                  }
2471 2476                  pio->io_error = error;
2472 2477                  return (ZIO_PIPELINE_CONTINUE);
2473 2478          }
2474 2479  
2475 2480          if (pio == gio) {
2476 2481                  gnpp = &gio->io_gang_tree;
2477 2482          } else {
2478 2483                  gnpp = pio->io_private;
2479 2484                  ASSERT(pio->io_ready == zio_write_gang_member_ready);
2480 2485          }
2481 2486  
2482 2487          gn = zio_gang_node_alloc(gnpp);
2483 2488          gbh = gn->gn_gbh;
2484 2489          bzero(gbh, SPA_GANGBLOCKSIZE);
2485 2490          gbh_abd = abd_get_from_buf(gbh, SPA_GANGBLOCKSIZE);
2486 2491  
2487 2492          /*
2488 2493           * Create the gang header.
2489 2494           */
2490 2495          zio = zio_rewrite(pio, spa, txg, bp, gbh_abd, SPA_GANGBLOCKSIZE,
2491 2496              zio_write_gang_done, NULL, pio->io_priority,
2492 2497              ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2493 2498  
2494 2499          /*
2495 2500           * Create and nowait the gang children.
2496 2501           */
2497 2502          for (int g = 0; resid != 0; resid -= lsize, g++) {
2498 2503                  lsize = P2ROUNDUP(resid / (SPA_GBH_NBLKPTRS - g),
2499 2504                      SPA_MINBLOCKSIZE);
2500 2505                  ASSERT(lsize >= SPA_MINBLOCKSIZE && lsize <= resid);
2501 2506  
2502 2507                  zp.zp_checksum = gio->io_prop.zp_checksum;
2503 2508                  zp.zp_compress = ZIO_COMPRESS_OFF;
2504 2509                  zp.zp_type = DMU_OT_NONE;
2505 2510                  zp.zp_level = 0;
2506 2511                  zp.zp_copies = gio->io_prop.zp_copies;
2507 2512                  zp.zp_dedup = B_FALSE;
2508 2513                  zp.zp_dedup_verify = B_FALSE;
2509 2514                  zp.zp_nopwrite = B_FALSE;
2510 2515                  zp.zp_encrypt = gio->io_prop.zp_encrypt;
2511 2516                  zp.zp_byteorder = gio->io_prop.zp_byteorder;
2512 2517                  bzero(zp.zp_salt, ZIO_DATA_SALT_LEN);
2513 2518                  bzero(zp.zp_iv, ZIO_DATA_IV_LEN);
2514 2519                  bzero(zp.zp_mac, ZIO_DATA_MAC_LEN);
2515 2520  
2516 2521                  zio_t *cio = zio_write(zio, spa, txg, &gbh->zg_blkptr[g],
2517 2522                      has_data ? abd_get_offset(pio->io_abd, pio->io_size -
2518 2523                      resid) : NULL, lsize, lsize, &zp,
2519 2524                      zio_write_gang_member_ready, NULL, NULL,
2520 2525                      zio_write_gang_done, &gn->gn_child[g], pio->io_priority,
2521 2526                      ZIO_GANG_CHILD_FLAGS(pio), &pio->io_bookmark);
2522 2527  
2523 2528                  if (pio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
2524 2529                          ASSERT(pio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
2525 2530                          ASSERT(has_data);
2526 2531  
2527 2532                          /*
2528 2533                           * Gang children won't throttle but we should
2529 2534                           * account for their work, so reserve an allocation
2530 2535                           * slot for them here.
2531 2536                           */
2532 2537                          VERIFY(metaslab_class_throttle_reserve(mc,
2533 2538                              zp.zp_copies, cio->io_allocator, cio, flags));
2534 2539                  }
2535 2540                  zio_nowait(cio);
2536 2541          }
2537 2542  
2538 2543          /*
2539 2544           * Set pio's pipeline to just wait for zio to finish.
2540 2545           */
2541 2546          pio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2542 2547  
2543 2548          zio_nowait(zio);
2544 2549  
2545 2550          return (ZIO_PIPELINE_CONTINUE);
2546 2551  }
2547 2552  
2548 2553  /*
2549 2554   * The zio_nop_write stage in the pipeline determines if allocating a
2550 2555   * new bp is necessary.  The nopwrite feature can handle writes in
2551 2556   * either syncing or open context (i.e. zil writes) and as a result is
2552 2557   * mutually exclusive with dedup.
2553 2558   *
2554 2559   * By leveraging a cryptographically secure checksum, such as SHA256, we
2555 2560   * can compare the checksums of the new data and the old to determine if
2556 2561   * allocating a new block is required.  Note that our requirements for
2557 2562   * cryptographic strength are fairly weak: there can't be any accidental
2558 2563   * hash collisions, but we don't need to be secure against intentional
2559 2564   * (malicious) collisions.  To trigger a nopwrite, you have to be able
2560 2565   * to write the file to begin with, and triggering an incorrect (hash
2561 2566   * collision) nopwrite is no worse than simply writing to the file.
2562 2567   * That said, there are no known attacks against the checksum algorithms
2563 2568   * used for nopwrite, assuming that the salt and the checksums
2564 2569   * themselves remain secret.
2565 2570   */
2566 2571  static int
2567 2572  zio_nop_write(zio_t *zio)
2568 2573  {
2569 2574          blkptr_t *bp = zio->io_bp;
2570 2575          blkptr_t *bp_orig = &zio->io_bp_orig;
2571 2576          zio_prop_t *zp = &zio->io_prop;
2572 2577  
2573 2578          ASSERT(BP_GET_LEVEL(bp) == 0);
2574 2579          ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REWRITE));
2575 2580          ASSERT(zp->zp_nopwrite);
2576 2581          ASSERT(!zp->zp_dedup);
2577 2582          ASSERT(zio->io_bp_override == NULL);
2578 2583          ASSERT(IO_IS_ALLOCATING(zio));
2579 2584  
2580 2585          /*
2581 2586           * Check to see if the original bp and the new bp have matching
2582 2587           * characteristics (i.e. same checksum, compression algorithms, etc).
2583 2588           * If they don't then just continue with the pipeline which will
2584 2589           * allocate a new bp.
2585 2590           */
2586 2591          if (BP_IS_HOLE(bp_orig) ||
2587 2592              !(zio_checksum_table[BP_GET_CHECKSUM(bp)].ci_flags &
2588 2593              ZCHECKSUM_FLAG_NOPWRITE) ||
2589 2594              BP_IS_ENCRYPTED(bp) || BP_IS_ENCRYPTED(bp_orig) ||
2590 2595              BP_GET_CHECKSUM(bp) != BP_GET_CHECKSUM(bp_orig) ||
2591 2596              BP_GET_COMPRESS(bp) != BP_GET_COMPRESS(bp_orig) ||
2592 2597              BP_GET_DEDUP(bp) != BP_GET_DEDUP(bp_orig) ||
2593 2598              zp->zp_copies != BP_GET_NDVAS(bp_orig))
2594 2599                  return (ZIO_PIPELINE_CONTINUE);
2595 2600  
2596 2601          /*
2597 2602           * If the checksums match then reset the pipeline so that we
2598 2603           * avoid allocating a new bp and issuing any I/O.
2599 2604           */
2600 2605          if (ZIO_CHECKSUM_EQUAL(bp->blk_cksum, bp_orig->blk_cksum)) {
2601 2606                  ASSERT(zio_checksum_table[zp->zp_checksum].ci_flags &
2602 2607                      ZCHECKSUM_FLAG_NOPWRITE);
2603 2608                  ASSERT3U(BP_GET_PSIZE(bp), ==, BP_GET_PSIZE(bp_orig));
2604 2609                  ASSERT3U(BP_GET_LSIZE(bp), ==, BP_GET_LSIZE(bp_orig));
2605 2610                  ASSERT(zp->zp_compress != ZIO_COMPRESS_OFF);
2606 2611                  ASSERT(bcmp(&bp->blk_prop, &bp_orig->blk_prop,
2607 2612                      sizeof (uint64_t)) == 0);
2608 2613  
2609 2614                  *bp = *bp_orig;
2610 2615                  zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
2611 2616                  zio->io_flags |= ZIO_FLAG_NOPWRITE;
2612 2617          }
2613 2618  
2614 2619          return (ZIO_PIPELINE_CONTINUE);
2615 2620  }
2616 2621  
2617 2622  /*
2618 2623   * ==========================================================================
2619 2624   * Dedup
2620 2625   * ==========================================================================
2621 2626   */
2622 2627  static void
2623 2628  zio_ddt_child_read_done(zio_t *zio)
2624 2629  {
2625 2630          blkptr_t *bp = zio->io_bp;
2626 2631          ddt_entry_t *dde = zio->io_private;
2627 2632          ddt_phys_t *ddp;
2628 2633          zio_t *pio = zio_unique_parent(zio);
2629 2634  
2630 2635          mutex_enter(&pio->io_lock);
2631 2636          ddp = ddt_phys_select(dde, bp);
2632 2637          if (zio->io_error == 0)
2633 2638                  ddt_phys_clear(ddp);    /* this ddp doesn't need repair */
2634 2639  
2635 2640          if (zio->io_error == 0 && dde->dde_repair_abd == NULL)
2636 2641                  dde->dde_repair_abd = zio->io_abd;
2637 2642          else
2638 2643                  abd_free(zio->io_abd);
2639 2644          mutex_exit(&pio->io_lock);
2640 2645  }
2641 2646  
2642 2647  static int
2643 2648  zio_ddt_read_start(zio_t *zio)
2644 2649  {
2645 2650          blkptr_t *bp = zio->io_bp;
2646 2651  
2647 2652          ASSERT(BP_GET_DEDUP(bp));
2648 2653          ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2649 2654          ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2650 2655  
2651 2656          if (zio->io_child_error[ZIO_CHILD_DDT]) {
2652 2657                  ddt_t *ddt = ddt_select(zio->io_spa, bp);
2653 2658                  ddt_entry_t *dde = ddt_repair_start(ddt, bp);
2654 2659                  ddt_phys_t *ddp = dde->dde_phys;
2655 2660                  ddt_phys_t *ddp_self = ddt_phys_select(dde, bp);
2656 2661                  blkptr_t blk;
2657 2662  
2658 2663                  ASSERT(zio->io_vsd == NULL);
2659 2664                  zio->io_vsd = dde;
2660 2665  
2661 2666                  if (ddp_self == NULL)
2662 2667                          return (ZIO_PIPELINE_CONTINUE);
2663 2668  
2664 2669                  for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
2665 2670                          if (ddp->ddp_phys_birth == 0 || ddp == ddp_self)
2666 2671                                  continue;
2667 2672                          ddt_bp_create(ddt->ddt_checksum, &dde->dde_key, ddp,
2668 2673                              &blk);
2669 2674                          zio_nowait(zio_read(zio, zio->io_spa, &blk,
2670 2675                              abd_alloc_for_io(zio->io_size, B_TRUE),
2671 2676                              zio->io_size, zio_ddt_child_read_done, dde,
2672 2677                              zio->io_priority, ZIO_DDT_CHILD_FLAGS(zio) |
2673 2678                              ZIO_FLAG_DONT_PROPAGATE, &zio->io_bookmark));
2674 2679                  }
2675 2680                  return (ZIO_PIPELINE_CONTINUE);
2676 2681          }
2677 2682  
2678 2683          zio_nowait(zio_read(zio, zio->io_spa, bp,
2679 2684              zio->io_abd, zio->io_size, NULL, NULL, zio->io_priority,
2680 2685              ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark));
2681 2686  
2682 2687          return (ZIO_PIPELINE_CONTINUE);
2683 2688  }
2684 2689  
2685 2690  static int
2686 2691  zio_ddt_read_done(zio_t *zio)
2687 2692  {
2688 2693          blkptr_t *bp = zio->io_bp;
2689 2694  
2690 2695          if (zio_wait_for_children(zio, ZIO_CHILD_DDT_BIT, ZIO_WAIT_DONE)) {
2691 2696                  return (ZIO_PIPELINE_STOP);
2692 2697          }
2693 2698  
2694 2699          ASSERT(BP_GET_DEDUP(bp));
2695 2700          ASSERT(BP_GET_PSIZE(bp) == zio->io_size);
2696 2701          ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
2697 2702  
2698 2703          if (zio->io_child_error[ZIO_CHILD_DDT]) {
2699 2704                  ddt_t *ddt = ddt_select(zio->io_spa, bp);
2700 2705                  ddt_entry_t *dde = zio->io_vsd;
2701 2706                  if (ddt == NULL) {
2702 2707                          ASSERT(spa_load_state(zio->io_spa) != SPA_LOAD_NONE);
2703 2708                          return (ZIO_PIPELINE_CONTINUE);
2704 2709                  }
2705 2710                  if (dde == NULL) {
2706 2711                          zio->io_stage = ZIO_STAGE_DDT_READ_START >> 1;
2707 2712                          zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_FALSE);
2708 2713                          return (ZIO_PIPELINE_STOP);
2709 2714                  }
2710 2715                  if (dde->dde_repair_abd != NULL) {
2711 2716                          abd_copy(zio->io_abd, dde->dde_repair_abd,
2712 2717                              zio->io_size);
2713 2718                          zio->io_child_error[ZIO_CHILD_DDT] = 0;
2714 2719                  }
2715 2720                  ddt_repair_done(ddt, dde);
2716 2721                  zio->io_vsd = NULL;
2717 2722          }
2718 2723  
2719 2724          ASSERT(zio->io_vsd == NULL);
2720 2725  
2721 2726          return (ZIO_PIPELINE_CONTINUE);
2722 2727  }
2723 2728  
2724 2729  static boolean_t
2725 2730  zio_ddt_collision(zio_t *zio, ddt_t *ddt, ddt_entry_t *dde)
2726 2731  {
2727 2732          spa_t *spa = zio->io_spa;
2728 2733          boolean_t do_raw = !!(zio->io_flags & ZIO_FLAG_RAW);
2729 2734  
2730 2735          /* We should never get a raw, override zio */
2731 2736          ASSERT(!(zio->io_bp_override && do_raw));
2732 2737  
2733 2738          /*
2734 2739           * Note: we compare the original data, not the transformed data,
2735 2740           * because when zio->io_bp is an override bp, we will not have
2736 2741           * pushed the I/O transforms.  That's an important optimization
2737 2742           * because otherwise we'd compress/encrypt all dmu_sync() data twice.
2738 2743           * However, we should never get a raw, override zio so in these
2739 2744           * cases we can compare the io_data directly. This is useful because
2740 2745           * it allows us to do dedup verification even if we don't have access
2741 2746           * to the original data (for instance, if the encryption keys aren't
2742 2747           * loaded).
2743 2748           */
2744 2749  
2745 2750          for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2746 2751                  zio_t *lio = dde->dde_lead_zio[p];
2747 2752  
2748 2753                  if (lio != NULL && do_raw) {
2749 2754                          return (lio->io_size != zio->io_size ||
2750 2755                              abd_cmp(zio->io_abd, lio->io_abd,
2751 2756                              zio->io_size) != 0);
2752 2757                  } else if (lio != NULL) {
2753 2758                          return (lio->io_orig_size != zio->io_orig_size ||
2754 2759                              abd_cmp(zio->io_orig_abd, lio->io_orig_abd,
2755 2760                              zio->io_orig_size) != 0);
2756 2761                  }
2757 2762          }
2758 2763  
2759 2764          for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
2760 2765                  ddt_phys_t *ddp = &dde->dde_phys[p];
2761 2766  
2762 2767                  if (ddp->ddp_phys_birth != 0 && do_raw) {
2763 2768                          blkptr_t blk = *zio->io_bp;
2764 2769                          uint64_t psize;
2765 2770                          abd_t *tmpabd;
2766 2771                          int error;
2767 2772  
2768 2773                          ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2769 2774                          psize = BP_GET_PSIZE(&blk);
2770 2775  
2771 2776                          if (psize != zio->io_size)
2772 2777                                  return (B_TRUE);
2773 2778  
2774 2779                          ddt_exit(ddt);
2775 2780  
2776 2781                          tmpabd = abd_alloc_for_io(psize, B_TRUE);
2777 2782  
2778 2783                          error = zio_wait(zio_read(NULL, spa, &blk, tmpabd,
2779 2784                              psize, NULL, NULL, ZIO_PRIORITY_SYNC_READ,
2780 2785                              ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2781 2786                              ZIO_FLAG_RAW, &zio->io_bookmark));
2782 2787  
2783 2788                          if (error == 0) {
2784 2789                                  if (abd_cmp(tmpabd, zio->io_abd, psize) != 0)
2785 2790                                          error = SET_ERROR(ENOENT);
2786 2791                          }
2787 2792  
2788 2793                          abd_free(tmpabd);
2789 2794                          ddt_enter(ddt);
2790 2795                          return (error != 0);
2791 2796                  } else if (ddp->ddp_phys_birth != 0) {
2792 2797                          arc_buf_t *abuf = NULL;
2793 2798                          arc_flags_t aflags = ARC_FLAG_WAIT;
2794 2799                          int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE;
2795 2800                          blkptr_t blk = *zio->io_bp;
2796 2801                          int error;
2797 2802  
2798 2803                          ddt_bp_fill(ddp, &blk, ddp->ddp_phys_birth);
2799 2804  
2800 2805                          if (BP_GET_LSIZE(&blk) != zio->io_orig_size)
2801 2806                                  return (B_TRUE);
2802 2807  
2803 2808                          ddt_exit(ddt);
2804 2809  
2805 2810                          /*
2806 2811                           * Intuitively, it would make more sense to compare
2807 2812                           * io_abd than io_orig_abd in the raw case since you
2808 2813                           * don't want to look at any transformations that have
2809 2814                           * happened to the data. However, for raw I/Os the
2810 2815                           * data will actually be the same in io_abd and
2811 2816                           * io_orig_abd, so all we have to do is issue this as
2812 2817                           * a raw ARC read.
2813 2818                           */
2814 2819                          if (do_raw) {
2815 2820                                  zio_flags |= ZIO_FLAG_RAW;
2816 2821                                  ASSERT3U(zio->io_size, ==, zio->io_orig_size);
2817 2822                                  ASSERT0(abd_cmp(zio->io_abd, zio->io_orig_abd,
2818 2823                                      zio->io_size));
2819 2824                                  ASSERT3P(zio->io_transform_stack, ==, NULL);
2820 2825                          }
2821 2826  
2822 2827                          error = arc_read(NULL, spa, &blk,
2823 2828                              arc_getbuf_func, &abuf, ZIO_PRIORITY_SYNC_READ,
2824 2829                              zio_flags, &aflags, &zio->io_bookmark);
2825 2830  
2826 2831                          if (error == 0) {
2827 2832                                  if (abd_cmp_buf(zio->io_orig_abd, abuf->b_data,
2828 2833                                      zio->io_orig_size) != 0)
2829 2834                                          error = SET_ERROR(ENOENT);
2830 2835                                  arc_buf_destroy(abuf, &abuf);
2831 2836                          }
2832 2837  
2833 2838                          ddt_enter(ddt);
2834 2839                          return (error != 0);
2835 2840                  }
2836 2841          }
2837 2842  
2838 2843          return (B_FALSE);
2839 2844  }
2840 2845  
2841 2846  static void
2842 2847  zio_ddt_child_write_ready(zio_t *zio)
2843 2848  {
2844 2849          int p = zio->io_prop.zp_copies;
2845 2850          ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2846 2851          ddt_entry_t *dde = zio->io_private;
2847 2852          ddt_phys_t *ddp = &dde->dde_phys[p];
2848 2853          zio_t *pio;
2849 2854  
2850 2855          if (zio->io_error)
2851 2856                  return;
2852 2857  
2853 2858          ddt_enter(ddt);
2854 2859  
2855 2860          ASSERT(dde->dde_lead_zio[p] == zio);
2856 2861  
2857 2862          ddt_phys_fill(ddp, zio->io_bp);
2858 2863  
2859 2864          zio_link_t *zl = NULL;
2860 2865          while ((pio = zio_walk_parents(zio, &zl)) != NULL)
2861 2866                  ddt_bp_fill(ddp, pio->io_bp, zio->io_txg);
2862 2867  
2863 2868          ddt_exit(ddt);
2864 2869  }
2865 2870  
2866 2871  static void
2867 2872  zio_ddt_child_write_done(zio_t *zio)
2868 2873  {
2869 2874          int p = zio->io_prop.zp_copies;
2870 2875          ddt_t *ddt = ddt_select(zio->io_spa, zio->io_bp);
2871 2876          ddt_entry_t *dde = zio->io_private;
2872 2877          ddt_phys_t *ddp = &dde->dde_phys[p];
2873 2878  
2874 2879          ddt_enter(ddt);
2875 2880  
2876 2881          ASSERT(ddp->ddp_refcnt == 0);
2877 2882          ASSERT(dde->dde_lead_zio[p] == zio);
2878 2883          dde->dde_lead_zio[p] = NULL;
2879 2884  
2880 2885          if (zio->io_error == 0) {
2881 2886                  zio_link_t *zl = NULL;
2882 2887                  while (zio_walk_parents(zio, &zl) != NULL)
2883 2888                          ddt_phys_addref(ddp);
2884 2889          } else {
2885 2890                  ddt_phys_clear(ddp);
2886 2891          }
2887 2892  
2888 2893          ddt_exit(ddt);
2889 2894  }
2890 2895  
2891 2896  static void
2892 2897  zio_ddt_ditto_write_done(zio_t *zio)
2893 2898  {
2894 2899          int p = DDT_PHYS_DITTO;
2895 2900          zio_prop_t *zp = &zio->io_prop;
2896 2901          blkptr_t *bp = zio->io_bp;
2897 2902          ddt_t *ddt = ddt_select(zio->io_spa, bp);
2898 2903          ddt_entry_t *dde = zio->io_private;
2899 2904          ddt_phys_t *ddp = &dde->dde_phys[p];
2900 2905          ddt_key_t *ddk = &dde->dde_key;
2901 2906  
2902 2907          ddt_enter(ddt);
2903 2908  
2904 2909          ASSERT(ddp->ddp_refcnt == 0);
2905 2910          ASSERT(dde->dde_lead_zio[p] == zio);
2906 2911          dde->dde_lead_zio[p] = NULL;
2907 2912  
2908 2913          if (zio->io_error == 0) {
2909 2914                  ASSERT(ZIO_CHECKSUM_EQUAL(bp->blk_cksum, ddk->ddk_cksum));
2910 2915                  ASSERT(zp->zp_copies < SPA_DVAS_PER_BP);
2911 2916                  ASSERT(zp->zp_copies == BP_GET_NDVAS(bp) - BP_IS_GANG(bp));
2912 2917                  if (ddp->ddp_phys_birth != 0)
2913 2918                          ddt_phys_free(ddt, ddk, ddp, zio->io_txg);
2914 2919                  ddt_phys_fill(ddp, bp);
2915 2920          }
2916 2921  
2917 2922          ddt_exit(ddt);
2918 2923  }
2919 2924  
2920 2925  static int
2921 2926  zio_ddt_write(zio_t *zio)
2922 2927  {
2923 2928          spa_t *spa = zio->io_spa;
2924 2929          blkptr_t *bp = zio->io_bp;
2925 2930          uint64_t txg = zio->io_txg;
2926 2931          zio_prop_t *zp = &zio->io_prop;
2927 2932          int p = zp->zp_copies;
2928 2933          int ditto_copies;
2929 2934          zio_t *cio = NULL;
2930 2935          zio_t *dio = NULL;
2931 2936          ddt_t *ddt = ddt_select(spa, bp);
2932 2937          ddt_entry_t *dde;
2933 2938          ddt_phys_t *ddp;
2934 2939  
2935 2940          ASSERT(BP_GET_DEDUP(bp));
2936 2941          ASSERT(BP_GET_CHECKSUM(bp) == zp->zp_checksum);
2937 2942          ASSERT(BP_IS_HOLE(bp) || zio->io_bp_override);
2938 2943          ASSERT(!(zio->io_bp_override && (zio->io_flags & ZIO_FLAG_RAW)));
2939 2944  
2940 2945          ddt_enter(ddt);
2941 2946          dde = ddt_lookup(ddt, bp, B_TRUE);
2942 2947          ddp = &dde->dde_phys[p];
2943 2948  
2944 2949          if (zp->zp_dedup_verify && zio_ddt_collision(zio, ddt, dde)) {
2945 2950                  /*
2946 2951                   * If we're using a weak checksum, upgrade to a strong checksum
2947 2952                   * and try again.  If we're already using a strong checksum,
2948 2953                   * we can't resolve it, so just convert to an ordinary write.
2949 2954                   * (And automatically e-mail a paper to Nature?)
2950 2955                   */
2951 2956                  if (!(zio_checksum_table[zp->zp_checksum].ci_flags &
2952 2957                      ZCHECKSUM_FLAG_DEDUP)) {
2953 2958                          zp->zp_checksum = spa_dedup_checksum(spa);
2954 2959                          zio_pop_transforms(zio);
2955 2960                          zio->io_stage = ZIO_STAGE_OPEN;
2956 2961                          BP_ZERO(bp);
2957 2962                  } else {
2958 2963                          zp->zp_dedup = B_FALSE;
2959 2964                          BP_SET_DEDUP(bp, B_FALSE);
2960 2965                  }
2961 2966                  ASSERT(!BP_GET_DEDUP(bp));
2962 2967                  zio->io_pipeline = ZIO_WRITE_PIPELINE;
2963 2968                  ddt_exit(ddt);
2964 2969                  return (ZIO_PIPELINE_CONTINUE);
2965 2970          }
2966 2971  
2967 2972          ditto_copies = ddt_ditto_copies_needed(ddt, dde, ddp);
2968 2973          ASSERT(ditto_copies < SPA_DVAS_PER_BP);
2969 2974  
2970 2975          if (ditto_copies > ddt_ditto_copies_present(dde) &&
2971 2976              dde->dde_lead_zio[DDT_PHYS_DITTO] == NULL) {
2972 2977                  zio_prop_t czp = *zp;
2973 2978  
2974 2979                  czp.zp_copies = ditto_copies;
2975 2980  
2976 2981                  /*
2977 2982                   * If we arrived here with an override bp, we won't have run
2978 2983                   * the transform stack, so we won't have the data we need to
2979 2984                   * generate a child i/o.  So, toss the override bp and restart.
2980 2985                   * This is safe, because using the override bp is just an
2981 2986                   * optimization; and it's rare, so the cost doesn't matter.
2982 2987                   */
2983 2988                  if (zio->io_bp_override) {
2984 2989                          zio_pop_transforms(zio);
2985 2990                          zio->io_stage = ZIO_STAGE_OPEN;
2986 2991                          zio->io_pipeline = ZIO_WRITE_PIPELINE;
2987 2992                          zio->io_bp_override = NULL;
2988 2993                          BP_ZERO(bp);
2989 2994                          ddt_exit(ddt);
2990 2995                          return (ZIO_PIPELINE_CONTINUE);
2991 2996                  }
2992 2997  
2993 2998                  dio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
2994 2999                      zio->io_orig_size, zio->io_orig_size, &czp, NULL, NULL,
2995 3000                      NULL, zio_ddt_ditto_write_done, dde, zio->io_priority,
2996 3001                      ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
2997 3002  
2998 3003                  zio_push_transform(dio, zio->io_abd, zio->io_size, 0, NULL);
2999 3004                  dde->dde_lead_zio[DDT_PHYS_DITTO] = dio;
3000 3005          }
3001 3006  
3002 3007          if (ddp->ddp_phys_birth != 0 || dde->dde_lead_zio[p] != NULL) {
3003 3008                  if (ddp->ddp_phys_birth != 0)
3004 3009                          ddt_bp_fill(ddp, bp, txg);
3005 3010                  if (dde->dde_lead_zio[p] != NULL)
3006 3011                          zio_add_child(zio, dde->dde_lead_zio[p]);
3007 3012                  else
3008 3013                          ddt_phys_addref(ddp);
3009 3014          } else if (zio->io_bp_override) {
3010 3015                  ASSERT(bp->blk_birth == txg);
3011 3016                  ASSERT(BP_EQUAL(bp, zio->io_bp_override));
3012 3017                  ddt_phys_fill(ddp, bp);
3013 3018                  ddt_phys_addref(ddp);
3014 3019          } else {
3015 3020                  cio = zio_write(zio, spa, txg, bp, zio->io_orig_abd,
3016 3021                      zio->io_orig_size, zio->io_orig_size, zp,
3017 3022                      zio_ddt_child_write_ready, NULL, NULL,
3018 3023                      zio_ddt_child_write_done, dde, zio->io_priority,
3019 3024                      ZIO_DDT_CHILD_FLAGS(zio), &zio->io_bookmark);
3020 3025  
3021 3026                  zio_push_transform(cio, zio->io_abd, zio->io_size, 0, NULL);
3022 3027                  dde->dde_lead_zio[p] = cio;
3023 3028          }
3024 3029  
3025 3030          ddt_exit(ddt);
3026 3031  
3027 3032          if (cio)
3028 3033                  zio_nowait(cio);
3029 3034          if (dio)
3030 3035                  zio_nowait(dio);
3031 3036  
3032 3037          return (ZIO_PIPELINE_CONTINUE);
3033 3038  }
3034 3039  
3035 3040  ddt_entry_t *freedde; /* for debugging */
3036 3041  
3037 3042  static int
3038 3043  zio_ddt_free(zio_t *zio)
3039 3044  {
3040 3045          spa_t *spa = zio->io_spa;
3041 3046          blkptr_t *bp = zio->io_bp;
3042 3047          ddt_t *ddt = ddt_select(spa, bp);
3043 3048          ddt_entry_t *dde;
3044 3049          ddt_phys_t *ddp;
3045 3050  
3046 3051          ASSERT(BP_GET_DEDUP(bp));
3047 3052          ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
3048 3053  
3049 3054          ddt_enter(ddt);
3050 3055          freedde = dde = ddt_lookup(ddt, bp, B_TRUE);
3051 3056          ddp = ddt_phys_select(dde, bp);
3052 3057          ddt_phys_decref(ddp);
3053 3058          ddt_exit(ddt);
3054 3059  
3055 3060          return (ZIO_PIPELINE_CONTINUE);
3056 3061  }
3057 3062  
3058 3063  /*
3059 3064   * ==========================================================================
3060 3065   * Allocate and free blocks
3061 3066   * ==========================================================================
3062 3067   */
3063 3068  
3064 3069  static zio_t *
3065 3070  zio_io_to_allocate(spa_t *spa, int allocator)
3066 3071  {
3067 3072          zio_t *zio;
3068 3073  
3069 3074          ASSERT(MUTEX_HELD(&spa->spa_alloc_locks[allocator]));
3070 3075  
3071 3076          zio = avl_first(&spa->spa_alloc_trees[allocator]);
3072 3077          if (zio == NULL)
3073 3078                  return (NULL);
3074 3079  
3075 3080          ASSERT(IO_IS_ALLOCATING(zio));
3076 3081  
3077 3082          /*
3078 3083           * Try to place a reservation for this zio. If we're unable to
3079 3084           * reserve then we throttle.
3080 3085           */
3081 3086          ASSERT3U(zio->io_allocator, ==, allocator);
3082 3087          if (!metaslab_class_throttle_reserve(zio->io_metaslab_class,
3083 3088              zio->io_prop.zp_copies, zio->io_allocator, zio, 0)) {
3084 3089                  return (NULL);
3085 3090          }
3086 3091  
3087 3092          avl_remove(&spa->spa_alloc_trees[allocator], zio);
3088 3093          ASSERT3U(zio->io_stage, <, ZIO_STAGE_DVA_ALLOCATE);
3089 3094  
3090 3095          return (zio);
3091 3096  }
3092 3097  
3093 3098  static int
3094 3099  zio_dva_throttle(zio_t *zio)
3095 3100  {
3096 3101          spa_t *spa = zio->io_spa;
3097 3102          zio_t *nio;
3098 3103          metaslab_class_t *mc;
3099 3104  
3100 3105          /* locate an appropriate allocation class */
3101 3106          mc = spa_preferred_class(spa, zio->io_size, zio->io_prop.zp_type,
3102 3107              zio->io_prop.zp_level, zio->io_prop.zp_zpl_smallblk);
3103 3108  
3104 3109          if (zio->io_priority == ZIO_PRIORITY_SYNC_WRITE ||
3105 3110              !mc->mc_alloc_throttle_enabled ||
3106 3111              zio->io_child_type == ZIO_CHILD_GANG ||
3107 3112              zio->io_flags & ZIO_FLAG_NODATA) {
3108 3113                  return (ZIO_PIPELINE_CONTINUE);
3109 3114          }
3110 3115  
3111 3116          ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3112 3117  
3113 3118          ASSERT3U(zio->io_queued_timestamp, >, 0);
3114 3119          ASSERT(zio->io_stage == ZIO_STAGE_DVA_THROTTLE);
3115 3120  
3116 3121          zbookmark_phys_t *bm = &zio->io_bookmark;
3117 3122          /*
3118 3123           * We want to try to use as many allocators as possible to help improve
3119 3124           * performance, but we also want logically adjacent IOs to be physically
3120 3125           * adjacent to improve sequential read performance. We chunk each object
3121 3126           * into 2^20 block regions, and then hash based on the objset, object,
3122 3127           * level, and region to accomplish both of these goals.
3123 3128           */
3124 3129          zio->io_allocator = cityhash4(bm->zb_objset, bm->zb_object,
3125 3130              bm->zb_level, bm->zb_blkid >> 20) % spa->spa_alloc_count;
3126 3131          mutex_enter(&spa->spa_alloc_locks[zio->io_allocator]);
3127 3132          ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3128 3133          zio->io_metaslab_class = mc;
3129 3134          avl_add(&spa->spa_alloc_trees[zio->io_allocator], zio);
3130 3135          nio = zio_io_to_allocate(spa, zio->io_allocator);
3131 3136          mutex_exit(&spa->spa_alloc_locks[zio->io_allocator]);
3132 3137  
3133 3138          if (nio == zio)
3134 3139                  return (ZIO_PIPELINE_CONTINUE);
3135 3140  
3136 3141          if (nio != NULL) {
3137 3142                  ASSERT(nio->io_stage == ZIO_STAGE_DVA_THROTTLE);
3138 3143                  /*
3139 3144                   * We are passing control to a new zio so make sure that
3140 3145                   * it is processed by a different thread. We do this to
3141 3146                   * avoid stack overflows that can occur when parents are
3142 3147                   * throttled and children are making progress. We allow
3143 3148                   * it to go to the head of the taskq since it's already
3144 3149                   * been waiting.
3145 3150                   */
3146 3151                  zio_taskq_dispatch(nio, ZIO_TASKQ_ISSUE, B_TRUE);
3147 3152          }
3148 3153          return (ZIO_PIPELINE_STOP);
3149 3154  }
3150 3155  
3151 3156  static void
3152 3157  zio_allocate_dispatch(spa_t *spa, int allocator)
3153 3158  {
3154 3159          zio_t *zio;
3155 3160  
3156 3161          mutex_enter(&spa->spa_alloc_locks[allocator]);
3157 3162          zio = zio_io_to_allocate(spa, allocator);
3158 3163          mutex_exit(&spa->spa_alloc_locks[allocator]);
3159 3164          if (zio == NULL)
3160 3165                  return;
3161 3166  
3162 3167          ASSERT3U(zio->io_stage, ==, ZIO_STAGE_DVA_THROTTLE);
3163 3168          ASSERT0(zio->io_error);
3164 3169          zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE, B_TRUE);
3165 3170  }
3166 3171  
3167 3172  static int
3168 3173  zio_dva_allocate(zio_t *zio)
3169 3174  {
3170 3175          spa_t *spa = zio->io_spa;
3171 3176          metaslab_class_t *mc;
3172 3177          blkptr_t *bp = zio->io_bp;
3173 3178          int error;
3174 3179          int flags = 0;
3175 3180  
3176 3181          if (zio->io_gang_leader == NULL) {
3177 3182                  ASSERT(zio->io_child_type > ZIO_CHILD_GANG);
3178 3183                  zio->io_gang_leader = zio;
3179 3184          }
3180 3185  
3181 3186          ASSERT(BP_IS_HOLE(bp));
3182 3187          ASSERT0(BP_GET_NDVAS(bp));
3183 3188          ASSERT3U(zio->io_prop.zp_copies, >, 0);
3184 3189          ASSERT3U(zio->io_prop.zp_copies, <=, spa_max_replication(spa));
3185 3190          ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
3186 3191  
3187 3192          if (zio->io_flags & ZIO_FLAG_NODATA)
3188 3193                  flags |= METASLAB_DONT_THROTTLE;
3189 3194          if (zio->io_flags & ZIO_FLAG_GANG_CHILD)
3190 3195                  flags |= METASLAB_GANG_CHILD;
3191 3196          if (zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE)
3192 3197                  flags |= METASLAB_ASYNC_ALLOC;
3193 3198  
3194 3199          /*
3195 3200           * if not already chosen, locate an appropriate allocation class
3196 3201           */
3197 3202          mc = zio->io_metaslab_class;
3198 3203          if (mc == NULL) {
3199 3204                  mc = spa_preferred_class(spa, zio->io_size,
3200 3205                      zio->io_prop.zp_type, zio->io_prop.zp_level,
3201 3206                      zio->io_prop.zp_zpl_smallblk);
3202 3207                  zio->io_metaslab_class = mc;
3203 3208          }
3204 3209  
3205 3210          error = metaslab_alloc(spa, mc, zio->io_size, bp,
3206 3211              zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
3207 3212              &zio->io_alloc_list, zio, zio->io_allocator);
3208 3213  
3209 3214          /*
3210 3215           * Fallback to normal class when an alloc class is full
3211 3216           */
3212 3217          if (error == ENOSPC && mc != spa_normal_class(spa)) {
3213 3218                  /*
3214 3219                   * If throttling, transfer reservation over to normal class.
3215 3220                   * The io_allocator slot can remain the same even though we
3216 3221                   * are switching classes.
3217 3222                   */
3218 3223                  if (mc->mc_alloc_throttle_enabled &&
3219 3224                      (zio->io_flags & ZIO_FLAG_IO_ALLOCATING)) {
3220 3225                          metaslab_class_throttle_unreserve(mc,
3221 3226                              zio->io_prop.zp_copies, zio->io_allocator, zio);
3222 3227                          zio->io_flags &= ~ZIO_FLAG_IO_ALLOCATING;
3223 3228  
3224 3229                          mc = spa_normal_class(spa);
3225 3230                          VERIFY(metaslab_class_throttle_reserve(mc,
3226 3231                              zio->io_prop.zp_copies, zio->io_allocator, zio,
3227 3232                              flags | METASLAB_MUST_RESERVE));
3228 3233                  } else {
3229 3234                          mc = spa_normal_class(spa);
3230 3235                  }
3231 3236                  zio->io_metaslab_class = mc;
3232 3237  
3233 3238                  error = metaslab_alloc(spa, mc, zio->io_size, bp,
3234 3239                      zio->io_prop.zp_copies, zio->io_txg, NULL, flags,
3235 3240                      &zio->io_alloc_list, zio, zio->io_allocator);
3236 3241          }
3237 3242  
3238 3243          if (error != 0) {
3239 3244                  zfs_dbgmsg("%s: metaslab allocation failure: zio %p, "
3240 3245                      "size %llu, error %d", spa_name(spa), zio, zio->io_size,
3241 3246                      error);
3242 3247                  if (error == ENOSPC && zio->io_size > SPA_MINBLOCKSIZE)
3243 3248                          return (zio_write_gang_block(zio));
3244 3249                  zio->io_error = error;
3245 3250          }
3246 3251  
3247 3252          return (ZIO_PIPELINE_CONTINUE);
3248 3253  }
3249 3254  
3250 3255  static int
3251 3256  zio_dva_free(zio_t *zio)
3252 3257  {
3253 3258          metaslab_free(zio->io_spa, zio->io_bp, zio->io_txg, B_FALSE);
3254 3259  
3255 3260          return (ZIO_PIPELINE_CONTINUE);
3256 3261  }
3257 3262  
3258 3263  static int
3259 3264  zio_dva_claim(zio_t *zio)
3260 3265  {
3261 3266          int error;
3262 3267  
3263 3268          error = metaslab_claim(zio->io_spa, zio->io_bp, zio->io_txg);
3264 3269          if (error)
3265 3270                  zio->io_error = error;
3266 3271  
3267 3272          return (ZIO_PIPELINE_CONTINUE);
3268 3273  }
3269 3274  
3270 3275  /*
3271 3276   * Undo an allocation.  This is used by zio_done() when an I/O fails
3272 3277   * and we want to give back the block we just allocated.
3273 3278   * This handles both normal blocks and gang blocks.
3274 3279   */
3275 3280  static void
3276 3281  zio_dva_unallocate(zio_t *zio, zio_gang_node_t *gn, blkptr_t *bp)
3277 3282  {
3278 3283          ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp));
3279 3284          ASSERT(zio->io_bp_override == NULL);
3280 3285  
3281 3286          if (!BP_IS_HOLE(bp))
3282 3287                  metaslab_free(zio->io_spa, bp, bp->blk_birth, B_TRUE);
3283 3288  
3284 3289          if (gn != NULL) {
3285 3290                  for (int g = 0; g < SPA_GBH_NBLKPTRS; g++) {
3286 3291                          zio_dva_unallocate(zio, gn->gn_child[g],
3287 3292                              &gn->gn_gbh->zg_blkptr[g]);
3288 3293                  }
3289 3294          }
3290 3295  }
3291 3296  
3292 3297  /*
3293 3298   * Try to allocate an intent log block.  Return 0 on success, errno on failure.
3294 3299   */
3295 3300  int
3296 3301  zio_alloc_zil(spa_t *spa, objset_t *os, uint64_t txg, blkptr_t *new_bp,
3297 3302      blkptr_t *old_bp, uint64_t size, boolean_t *slog)
3298 3303  {
3299 3304          int error = 1;
3300 3305          zio_alloc_list_t io_alloc_list;
3301 3306  
3302 3307          ASSERT(txg > spa_syncing_txg(spa));
3303 3308  
3304 3309          metaslab_trace_init(&io_alloc_list);
3305 3310  
3306 3311          /*
3307 3312           * Block pointer fields are useful to metaslabs for stats and debugging.
3308 3313           * Fill in the obvious ones before calling into metaslab_alloc().
3309 3314           */
3310 3315          BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3311 3316          BP_SET_PSIZE(new_bp, size);
3312 3317          BP_SET_LEVEL(new_bp, 0);
3313 3318  
3314 3319          /*
3315 3320           * When allocating a zil block, we don't have information about
3316 3321           * the final destination of the block except the objset it's part
3317 3322           * of, so we just hash the objset ID to pick the allocator to get
3318 3323           * some parallelism.
3319 3324           */
3320 3325          error = metaslab_alloc(spa, spa_log_class(spa), size, new_bp, 1,
3321 3326              txg, old_bp, METASLAB_HINTBP_AVOID, &io_alloc_list, NULL,
3322 3327              cityhash4(0, 0, 0,
3323 3328              os->os_dsl_dataset->ds_object) % spa->spa_alloc_count);
3324 3329          if (error == 0) {
3325 3330                  *slog = TRUE;
3326 3331          } else {
3327 3332                  error = metaslab_alloc(spa, spa_normal_class(spa), size,
3328 3333                      new_bp, 1, txg, old_bp, METASLAB_HINTBP_AVOID,
3329 3334                      &io_alloc_list, NULL, cityhash4(0, 0, 0,
3330 3335                      os->os_dsl_dataset->ds_object) % spa->spa_alloc_count);
3331 3336                  if (error == 0)
3332 3337                          *slog = FALSE;
3333 3338          }
3334 3339          metaslab_trace_fini(&io_alloc_list);
3335 3340  
3336 3341          if (error == 0) {
3337 3342                  BP_SET_LSIZE(new_bp, size);
3338 3343                  BP_SET_PSIZE(new_bp, size);
3339 3344                  BP_SET_COMPRESS(new_bp, ZIO_COMPRESS_OFF);
3340 3345                  BP_SET_CHECKSUM(new_bp,
3341 3346                      spa_version(spa) >= SPA_VERSION_SLIM_ZIL
3342 3347                      ? ZIO_CHECKSUM_ZILOG2 : ZIO_CHECKSUM_ZILOG);
3343 3348                  BP_SET_TYPE(new_bp, DMU_OT_INTENT_LOG);
3344 3349                  BP_SET_LEVEL(new_bp, 0);
3345 3350                  BP_SET_DEDUP(new_bp, 0);
3346 3351                  BP_SET_BYTEORDER(new_bp, ZFS_HOST_BYTEORDER);
3347 3352  
3348 3353                  /*
3349 3354                   * encrypted blocks will require an IV and salt. We generate
3350 3355                   * these now since we will not be rewriting the bp at
3351 3356                   * rewrite time.
3352 3357                   */
3353 3358                  if (os->os_encrypted) {
3354 3359                          uint8_t iv[ZIO_DATA_IV_LEN];
3355 3360                          uint8_t salt[ZIO_DATA_SALT_LEN];
3356 3361  
3357 3362                          BP_SET_CRYPT(new_bp, B_TRUE);
3358 3363                          VERIFY0(spa_crypt_get_salt(spa,
3359 3364                              dmu_objset_id(os), salt));
3360 3365                          VERIFY0(zio_crypt_generate_iv(iv));
3361 3366  
3362 3367                          zio_crypt_encode_params_bp(new_bp, salt, iv);
3363 3368                  }
3364 3369          } else {
3365 3370                  zfs_dbgmsg("%s: zil block allocation failure: "
3366 3371                      "size %llu, error %d", spa_name(spa), size, error);
3367 3372          }
3368 3373  
3369 3374          return (error);
3370 3375  }
3371 3376  
3372 3377  /*
3373 3378   * ==========================================================================
3374 3379   * Read and write to physical devices
3375 3380   * ==========================================================================
3376 3381   */
3377 3382  
3378 3383  /*
3379 3384   * Issue an I/O to the underlying vdev. Typically the issue pipeline
3380 3385   * stops after this stage and will resume upon I/O completion.
3381 3386   * However, there are instances where the vdev layer may need to
3382 3387   * continue the pipeline when an I/O was not issued. Since the I/O
3383 3388   * that was sent to the vdev layer might be different than the one
3384 3389   * currently active in the pipeline (see vdev_queue_io()), we explicitly
3385 3390   * force the underlying vdev layers to call either zio_execute() or
3386 3391   * zio_interrupt() to ensure that the pipeline continues with the correct I/O.
3387 3392   */
3388 3393  static int
3389 3394  zio_vdev_io_start(zio_t *zio)
3390 3395  {
3391 3396          vdev_t *vd = zio->io_vd;
3392 3397          uint64_t align;
3393 3398          spa_t *spa = zio->io_spa;
3394 3399  
3395 3400          zio->io_delay = 0;
3396 3401  
3397 3402          ASSERT(zio->io_error == 0);
3398 3403          ASSERT(zio->io_child_error[ZIO_CHILD_VDEV] == 0);
3399 3404  
3400 3405          if (vd == NULL) {
3401 3406                  if (!(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3402 3407                          spa_config_enter(spa, SCL_ZIO, zio, RW_READER);
3403 3408  
3404 3409                  /*
3405 3410                   * The mirror_ops handle multiple DVAs in a single BP.
3406 3411                   */
3407 3412                  vdev_mirror_ops.vdev_op_io_start(zio);
3408 3413                  return (ZIO_PIPELINE_STOP);
3409 3414          }
3410 3415  
3411 3416          ASSERT3P(zio->io_logical, !=, zio);
3412 3417          if (zio->io_type == ZIO_TYPE_WRITE) {
3413 3418                  ASSERT(spa->spa_trust_config);
3414 3419  
3415 3420                  /*
3416 3421                   * Note: the code can handle other kinds of writes,
3417 3422                   * but we don't expect them.
3418 3423                   */
3419 3424                  if (zio->io_vd->vdev_removing) {
3420 3425                          ASSERT(zio->io_flags &
3421 3426                              (ZIO_FLAG_PHYSICAL | ZIO_FLAG_SELF_HEAL |
3422 3427                              ZIO_FLAG_RESILVER | ZIO_FLAG_INDUCE_DAMAGE));
3423 3428                  }
3424 3429          }
3425 3430  
3426 3431          align = 1ULL << vd->vdev_top->vdev_ashift;
3427 3432  
3428 3433          if (!(zio->io_flags & ZIO_FLAG_PHYSICAL) &&
3429 3434              P2PHASE(zio->io_size, align) != 0) {
3430 3435                  /* Transform logical writes to be a full physical block size. */
3431 3436                  uint64_t asize = P2ROUNDUP(zio->io_size, align);
3432 3437                  abd_t *abuf = abd_alloc_sametype(zio->io_abd, asize);
3433 3438                  ASSERT(vd == vd->vdev_top);
3434 3439                  if (zio->io_type == ZIO_TYPE_WRITE) {
3435 3440                          abd_copy(abuf, zio->io_abd, zio->io_size);
3436 3441                          abd_zero_off(abuf, zio->io_size, asize - zio->io_size);
3437 3442                  }
3438 3443                  zio_push_transform(zio, abuf, asize, asize, zio_subblock);
3439 3444          }
3440 3445  
3441 3446          /*
3442 3447           * If this is not a physical io, make sure that it is properly aligned
3443 3448           * before proceeding.
3444 3449           */
3445 3450          if (!(zio->io_flags & ZIO_FLAG_PHYSICAL)) {
3446 3451                  ASSERT0(P2PHASE(zio->io_offset, align));
3447 3452                  ASSERT0(P2PHASE(zio->io_size, align));
3448 3453          } else {
3449 3454                  /*
3450 3455                   * For physical writes, we allow 512b aligned writes and assume
3451 3456                   * the device will perform a read-modify-write as necessary.
3452 3457                   */
3453 3458                  ASSERT0(P2PHASE(zio->io_offset, SPA_MINBLOCKSIZE));
3454 3459                  ASSERT0(P2PHASE(zio->io_size, SPA_MINBLOCKSIZE));
3455 3460          }
3456 3461  
3457 3462          VERIFY(zio->io_type != ZIO_TYPE_WRITE || spa_writeable(spa));
3458 3463  
3459 3464          /*
3460 3465           * If this is a repair I/O, and there's no self-healing involved --
3461 3466           * that is, we're just resilvering what we expect to resilver --
3462 3467           * then don't do the I/O unless zio's txg is actually in vd's DTL.
3463 3468           * This prevents spurious resilvering.
3464 3469           *
3465 3470           * There are a few ways that we can end up creating these spurious
3466 3471           * resilver i/os:
3467 3472           *
3468 3473           * 1. A resilver i/o will be issued if any DVA in the BP has a
3469 3474           * dirty DTL.  The mirror code will issue resilver writes to
3470 3475           * each DVA, including the one(s) that are not on vdevs with dirty
3471 3476           * DTLs.
3472 3477           *
3473 3478           * 2. With nested replication, which happens when we have a
3474 3479           * "replacing" or "spare" vdev that's a child of a mirror or raidz.
3475 3480           * For example, given mirror(replacing(A+B), C), it's likely that
3476 3481           * only A is out of date (it's the new device). In this case, we'll
3477 3482           * read from C, then use the data to resilver A+B -- but we don't
3478 3483           * actually want to resilver B, just A. The top-level mirror has no
3479 3484           * way to know this, so instead we just discard unnecessary repairs
3480 3485           * as we work our way down the vdev tree.
3481 3486           *
3482 3487           * 3. ZTEST also creates mirrors of mirrors, mirrors of raidz, etc.
3483 3488           * The same logic applies to any form of nested replication: ditto
3484 3489           * + mirror, RAID-Z + replacing, etc.
3485 3490           *
3486 3491           * However, indirect vdevs point off to other vdevs which may have
3487 3492           * DTL's, so we never bypass them.  The child i/os on concrete vdevs
3488 3493           * will be properly bypassed instead.
3489 3494           */
3490 3495          if ((zio->io_flags & ZIO_FLAG_IO_REPAIR) &&
3491 3496              !(zio->io_flags & ZIO_FLAG_SELF_HEAL) &&
3492 3497              zio->io_txg != 0 && /* not a delegated i/o */
3493 3498              vd->vdev_ops != &vdev_indirect_ops &&
3494 3499              !vdev_dtl_contains(vd, DTL_PARTIAL, zio->io_txg, 1)) {
3495 3500                  ASSERT(zio->io_type == ZIO_TYPE_WRITE);
3496 3501                  zio_vdev_io_bypass(zio);
3497 3502                  return (ZIO_PIPELINE_CONTINUE);
3498 3503          }
3499 3504  
3500 3505          if (vd->vdev_ops->vdev_op_leaf && (zio->io_type == ZIO_TYPE_READ ||
3501 3506              zio->io_type == ZIO_TYPE_WRITE || zio->io_type == ZIO_TYPE_TRIM)) {
3502 3507  
3503 3508                  if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio))
3504 3509                          return (ZIO_PIPELINE_CONTINUE);
3505 3510  
3506 3511                  if ((zio = vdev_queue_io(zio)) == NULL)
3507 3512                          return (ZIO_PIPELINE_STOP);
3508 3513  
3509 3514                  if (!vdev_accessible(vd, zio)) {
3510 3515                          zio->io_error = SET_ERROR(ENXIO);
3511 3516                          zio_interrupt(zio);
3512 3517                          return (ZIO_PIPELINE_STOP);
3513 3518                  }
3514 3519                  zio->io_delay = gethrtime();
3515 3520          }
3516 3521  
3517 3522          vd->vdev_ops->vdev_op_io_start(zio);
3518 3523          return (ZIO_PIPELINE_STOP);
3519 3524  }
3520 3525  
3521 3526  static int
3522 3527  zio_vdev_io_done(zio_t *zio)
3523 3528  {
3524 3529          vdev_t *vd = zio->io_vd;
3525 3530          vdev_ops_t *ops = vd ? vd->vdev_ops : &vdev_mirror_ops;
3526 3531          boolean_t unexpected_error = B_FALSE;
3527 3532  
3528 3533          if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
3529 3534                  return (ZIO_PIPELINE_STOP);
3530 3535          }
3531 3536  
3532 3537          ASSERT(zio->io_type == ZIO_TYPE_READ ||
3533 3538              zio->io_type == ZIO_TYPE_WRITE || zio->io_type == ZIO_TYPE_TRIM);
3534 3539  
3535 3540          if (zio->io_delay)
3536 3541                  zio->io_delay = gethrtime() - zio->io_delay;
3537 3542  
3538 3543          if (vd != NULL && vd->vdev_ops->vdev_op_leaf) {
3539 3544  
3540 3545                  vdev_queue_io_done(zio);
3541 3546  
3542 3547                  if (zio->io_type == ZIO_TYPE_WRITE)
3543 3548                          vdev_cache_write(zio);
3544 3549  
3545 3550                  if (zio_injection_enabled && zio->io_error == 0)
3546 3551                          zio->io_error = zio_handle_device_injection(vd,
3547 3552                              zio, EIO);
3548 3553  
3549 3554                  if (zio_injection_enabled && zio->io_error == 0)
3550 3555                          zio->io_error = zio_handle_label_injection(zio, EIO);
3551 3556  
3552 3557                  if (zio->io_error && zio->io_type != ZIO_TYPE_TRIM) {
3553 3558                          if (!vdev_accessible(vd, zio)) {
3554 3559                                  zio->io_error = SET_ERROR(ENXIO);
3555 3560                          } else {
3556 3561                                  unexpected_error = B_TRUE;
3557 3562                          }
3558 3563                  }
3559 3564          }
3560 3565  
3561 3566          ops->vdev_op_io_done(zio);
3562 3567  
3563 3568          if (unexpected_error)
3564 3569                  VERIFY(vdev_probe(vd, zio) == NULL);
3565 3570  
3566 3571          return (ZIO_PIPELINE_CONTINUE);
3567 3572  }
3568 3573  
3569 3574  /*
3570 3575   * This function is used to change the priority of an existing zio that is
3571 3576   * currently in-flight. This is used by the arc to upgrade priority in the
3572 3577   * event that a demand read is made for a block that is currently queued
3573 3578   * as a scrub or async read IO. Otherwise, the high priority read request
3574 3579   * would end up having to wait for the lower priority IO.
3575 3580   */
3576 3581  void
3577 3582  zio_change_priority(zio_t *pio, zio_priority_t priority)
3578 3583  {
3579 3584          zio_t *cio, *cio_next;
3580 3585          zio_link_t *zl = NULL;
3581 3586  
3582 3587          ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
3583 3588  
3584 3589          if (pio->io_vd != NULL && pio->io_vd->vdev_ops->vdev_op_leaf) {
3585 3590                  vdev_queue_change_io_priority(pio, priority);
3586 3591          } else {
3587 3592                  pio->io_priority = priority;
3588 3593          }
3589 3594  
3590 3595          mutex_enter(&pio->io_lock);
3591 3596          for (cio = zio_walk_children(pio, &zl); cio != NULL; cio = cio_next) {
3592 3597                  cio_next = zio_walk_children(pio, &zl);
3593 3598                  zio_change_priority(cio, priority);
3594 3599          }
3595 3600          mutex_exit(&pio->io_lock);
3596 3601  }
3597 3602  
3598 3603  /*
3599 3604   * For non-raidz ZIOs, we can just copy aside the bad data read from the
3600 3605   * disk, and use that to finish the checksum ereport later.
3601 3606   */
3602 3607  static void
3603 3608  zio_vsd_default_cksum_finish(zio_cksum_report_t *zcr,
3604 3609      const abd_t *good_buf)
3605 3610  {
3606 3611          /* no processing needed */
3607 3612          zfs_ereport_finish_checksum(zcr, good_buf, zcr->zcr_cbdata, B_FALSE);
3608 3613  }
3609 3614  
3610 3615  /*ARGSUSED*/
3611 3616  void
3612 3617  zio_vsd_default_cksum_report(zio_t *zio, zio_cksum_report_t *zcr, void *ignored)
3613 3618  {
3614 3619          void *abd = abd_alloc_sametype(zio->io_abd, zio->io_size);
3615 3620  
3616 3621          abd_copy(abd, zio->io_abd, zio->io_size);
3617 3622  
3618 3623          zcr->zcr_cbinfo = zio->io_size;
3619 3624          zcr->zcr_cbdata = abd;
3620 3625          zcr->zcr_finish = zio_vsd_default_cksum_finish;
3621 3626          zcr->zcr_free = zio_abd_free;
3622 3627  }
3623 3628  
3624 3629  static int
3625 3630  zio_vdev_io_assess(zio_t *zio)
3626 3631  {
3627 3632          vdev_t *vd = zio->io_vd;
3628 3633  
3629 3634          if (zio_wait_for_children(zio, ZIO_CHILD_VDEV_BIT, ZIO_WAIT_DONE)) {
3630 3635                  return (ZIO_PIPELINE_STOP);
3631 3636          }
3632 3637  
3633 3638          if (vd == NULL && !(zio->io_flags & ZIO_FLAG_CONFIG_WRITER))
3634 3639                  spa_config_exit(zio->io_spa, SCL_ZIO, zio);
3635 3640  
3636 3641          if (zio->io_vsd != NULL) {
3637 3642                  zio->io_vsd_ops->vsd_free(zio);
3638 3643                  zio->io_vsd = NULL;
3639 3644          }
3640 3645  
3641 3646          if (zio_injection_enabled && zio->io_error == 0)
3642 3647                  zio->io_error = zio_handle_fault_injection(zio, EIO);
3643 3648  
3644 3649          /*
3645 3650           * If the I/O failed, determine whether we should attempt to retry it.
3646 3651           *
3647 3652           * On retry, we cut in line in the issue queue, since we don't want
3648 3653           * compression/checksumming/etc. work to prevent our (cheap) IO reissue.
3649 3654           */
3650 3655          if (zio->io_error && vd == NULL &&
3651 3656              !(zio->io_flags & (ZIO_FLAG_DONT_RETRY | ZIO_FLAG_IO_RETRY))) {
3652 3657                  ASSERT(!(zio->io_flags & ZIO_FLAG_DONT_QUEUE)); /* not a leaf */
3653 3658                  ASSERT(!(zio->io_flags & ZIO_FLAG_IO_BYPASS));  /* not a leaf */
3654 3659                  zio->io_error = 0;
3655 3660                  zio->io_flags |= ZIO_FLAG_IO_RETRY |
3656 3661                      ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE;
3657 3662                  zio->io_stage = ZIO_STAGE_VDEV_IO_START >> 1;
3658 3663                  zio_taskq_dispatch(zio, ZIO_TASKQ_ISSUE,
3659 3664                      zio_requeue_io_start_cut_in_line);
3660 3665                  return (ZIO_PIPELINE_STOP);
3661 3666          }
3662 3667  
3663 3668          /*
3664 3669           * If we got an error on a leaf device, convert it to ENXIO
3665 3670           * if the device is not accessible at all.
3666 3671           */
3667 3672          if (zio->io_error && vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3668 3673              !vdev_accessible(vd, zio))
3669 3674                  zio->io_error = SET_ERROR(ENXIO);
3670 3675  
3671 3676          /*
3672 3677           * If we can't write to an interior vdev (mirror or RAID-Z),
3673 3678           * set vdev_cant_write so that we stop trying to allocate from it.
3674 3679           */
3675 3680          if (zio->io_error == ENXIO && zio->io_type == ZIO_TYPE_WRITE &&
3676 3681              vd != NULL && !vd->vdev_ops->vdev_op_leaf) {
3677 3682                  vd->vdev_cant_write = B_TRUE;
3678 3683          }
3679 3684  
3680 3685          /*
3681 3686           * If a cache flush returns ENOTSUP or ENOTTY, we know that no future
3682 3687           * attempts will ever succeed. In this case we set a persistent
3683 3688           * boolean flag so that we don't bother with it in the future.
3684 3689           */
3685 3690          if ((zio->io_error == ENOTSUP || zio->io_error == ENOTTY) &&
3686 3691              zio->io_type == ZIO_TYPE_IOCTL &&
3687 3692              zio->io_cmd == DKIOCFLUSHWRITECACHE && vd != NULL)
3688 3693                  vd->vdev_nowritecache = B_TRUE;
3689 3694  
3690 3695          if (zio->io_error)
3691 3696                  zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
3692 3697  
3693 3698          if (vd != NULL && vd->vdev_ops->vdev_op_leaf &&
3694 3699              zio->io_physdone != NULL) {
3695 3700                  ASSERT(!(zio->io_flags & ZIO_FLAG_DELEGATED));
3696 3701                  ASSERT(zio->io_child_type == ZIO_CHILD_VDEV);
3697 3702                  zio->io_physdone(zio->io_logical);
3698 3703          }
3699 3704  
3700 3705          return (ZIO_PIPELINE_CONTINUE);
3701 3706  }
3702 3707  
3703 3708  void
3704 3709  zio_vdev_io_reissue(zio_t *zio)
3705 3710  {
3706 3711          ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3707 3712          ASSERT(zio->io_error == 0);
3708 3713  
3709 3714          zio->io_stage >>= 1;
3710 3715  }
3711 3716  
3712 3717  void
3713 3718  zio_vdev_io_redone(zio_t *zio)
3714 3719  {
3715 3720          ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_DONE);
3716 3721  
3717 3722          zio->io_stage >>= 1;
3718 3723  }
3719 3724  
3720 3725  void
3721 3726  zio_vdev_io_bypass(zio_t *zio)
3722 3727  {
3723 3728          ASSERT(zio->io_stage == ZIO_STAGE_VDEV_IO_START);
3724 3729          ASSERT(zio->io_error == 0);
3725 3730  
3726 3731          zio->io_flags |= ZIO_FLAG_IO_BYPASS;
3727 3732          zio->io_stage = ZIO_STAGE_VDEV_IO_ASSESS >> 1;
3728 3733  }
3729 3734  
3730 3735  /*
3731 3736   * ==========================================================================
3732 3737   * Encrypt and store encryption parameters
3733 3738   * ==========================================================================
3734 3739   */
3735 3740  
3736 3741  
3737 3742  /*
3738 3743   * This function is used for ZIO_STAGE_ENCRYPT. It is responsible for
3739 3744   * managing the storage of encryption parameters and passing them to the
3740 3745   * lower-level encryption functions.
3741 3746   */
3742 3747  static int
3743 3748  zio_encrypt(zio_t *zio)
3744 3749  {
3745 3750          zio_prop_t *zp = &zio->io_prop;
3746 3751          spa_t *spa = zio->io_spa;
3747 3752          blkptr_t *bp = zio->io_bp;
3748 3753          uint64_t psize = BP_GET_PSIZE(bp);
3749 3754          uint64_t dsobj = zio->io_bookmark.zb_objset;
3750 3755          dmu_object_type_t ot = BP_GET_TYPE(bp);
3751 3756          void *enc_buf = NULL;
3752 3757          abd_t *eabd = NULL;
3753 3758          uint8_t salt[ZIO_DATA_SALT_LEN];
3754 3759          uint8_t iv[ZIO_DATA_IV_LEN];
3755 3760          uint8_t mac[ZIO_DATA_MAC_LEN];
3756 3761          boolean_t no_crypt = B_FALSE;
3757 3762  
3758 3763          /* the root zio already encrypted the data */
3759 3764          if (zio->io_child_type == ZIO_CHILD_GANG)
3760 3765                  return (ZIO_PIPELINE_CONTINUE);
3761 3766  
3762 3767          /* only ZIL blocks are re-encrypted on rewrite */
3763 3768          if (!IO_IS_ALLOCATING(zio) && ot != DMU_OT_INTENT_LOG)
3764 3769                  return (ZIO_PIPELINE_CONTINUE);
3765 3770  
3766 3771          if (!(zp->zp_encrypt || BP_IS_ENCRYPTED(bp))) {
3767 3772                  BP_SET_CRYPT(bp, B_FALSE);
3768 3773                  return (ZIO_PIPELINE_CONTINUE);
3769 3774          }
3770 3775  
3771 3776          /* if we are doing raw encryption set the provided encryption params */
3772 3777          if (zio->io_flags & ZIO_FLAG_RAW_ENCRYPT) {
3773 3778                  ASSERT0(BP_GET_LEVEL(bp));
3774 3779                  BP_SET_CRYPT(bp, B_TRUE);
3775 3780                  BP_SET_BYTEORDER(bp, zp->zp_byteorder);
3776 3781                  if (ot != DMU_OT_OBJSET)
3777 3782                          zio_crypt_encode_mac_bp(bp, zp->zp_mac);
3778 3783  
3779 3784                  /* dnode blocks must be written out in the provided byteorder */
3780 3785                  if (zp->zp_byteorder != ZFS_HOST_BYTEORDER &&
3781 3786                      ot == DMU_OT_DNODE) {
3782 3787                          void *bswap_buf = zio_buf_alloc(psize);
3783 3788                          abd_t *babd = abd_get_from_buf(bswap_buf, psize);
3784 3789  
3785 3790                          ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
3786 3791                          abd_copy_to_buf(bswap_buf, zio->io_abd, psize);
3787 3792                          dmu_ot_byteswap[DMU_OT_BYTESWAP(ot)].ob_func(bswap_buf,
3788 3793                              psize);
3789 3794  
3790 3795                          abd_take_ownership_of_buf(babd, B_TRUE);
3791 3796                          zio_push_transform(zio, babd, psize, psize, NULL);
3792 3797                  }
3793 3798  
3794 3799                  if (DMU_OT_IS_ENCRYPTED(ot))
3795 3800                          zio_crypt_encode_params_bp(bp, zp->zp_salt, zp->zp_iv);
3796 3801                  return (ZIO_PIPELINE_CONTINUE);
3797 3802          }
3798 3803  
3799 3804          /* indirect blocks only maintain a cksum of the lower level MACs */
3800 3805          if (BP_GET_LEVEL(bp) > 0) {
3801 3806                  BP_SET_CRYPT(bp, B_TRUE);
3802 3807                  VERIFY0(zio_crypt_do_indirect_mac_checksum_abd(B_TRUE,
3803 3808                      zio->io_orig_abd, BP_GET_LSIZE(bp), BP_SHOULD_BYTESWAP(bp),
3804 3809                      mac));
3805 3810                  zio_crypt_encode_mac_bp(bp, mac);
3806 3811                  return (ZIO_PIPELINE_CONTINUE);
3807 3812          }
3808 3813  
3809 3814          /*
3810 3815           * Objset blocks are a special case since they have 2 256-bit MACs
3811 3816           * embedded within them.
3812 3817           */
3813 3818          if (ot == DMU_OT_OBJSET) {
3814 3819                  ASSERT0(DMU_OT_IS_ENCRYPTED(ot));
3815 3820                  ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
3816 3821                  BP_SET_CRYPT(bp, B_TRUE);
3817 3822                  VERIFY0(spa_do_crypt_objset_mac_abd(B_TRUE, spa, dsobj,
3818 3823                      zio->io_abd, psize, BP_SHOULD_BYTESWAP(bp)));
3819 3824                  return (ZIO_PIPELINE_CONTINUE);
3820 3825          }
3821 3826  
3822 3827          /* unencrypted object types are only authenticated with a MAC */
3823 3828          if (!DMU_OT_IS_ENCRYPTED(ot)) {
3824 3829                  BP_SET_CRYPT(bp, B_TRUE);
3825 3830                  VERIFY0(spa_do_crypt_mac_abd(B_TRUE, spa, dsobj,
3826 3831                      zio->io_abd, psize, mac));
3827 3832                  zio_crypt_encode_mac_bp(bp, mac);
3828 3833                  return (ZIO_PIPELINE_CONTINUE);
3829 3834          }
3830 3835  
3831 3836          /*
3832 3837           * Later passes of sync-to-convergence may decide to rewrite data
3833 3838           * in place to avoid more disk reallocations. This presents a problem
3834 3839           * for encryption because this consitutes rewriting the new data with
3835 3840           * the same encryption key and IV. However, this only applies to blocks
3836 3841           * in the MOS (particularly the spacemaps) and we do not encrypt the
3837 3842           * MOS. We assert that the zio is allocating or an intent log write
3838 3843           * to enforce this.
3839 3844           */
3840 3845          ASSERT(IO_IS_ALLOCATING(zio) || ot == DMU_OT_INTENT_LOG);
3841 3846          ASSERT(BP_GET_LEVEL(bp) == 0 || ot == DMU_OT_INTENT_LOG);
3842 3847          ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));
3843 3848          ASSERT3U(psize, !=, 0);
3844 3849  
3845 3850          enc_buf = zio_buf_alloc(psize);
3846 3851          eabd = abd_get_from_buf(enc_buf, psize);
3847 3852          abd_take_ownership_of_buf(eabd, B_TRUE);
3848 3853  
3849 3854          /*
3850 3855           * For an explanation of what encryption parameters are stored
3851 3856           * where, see the block comment in zio_crypt.c.
3852 3857           */
3853 3858          if (ot == DMU_OT_INTENT_LOG) {
3854 3859                  zio_crypt_decode_params_bp(bp, salt, iv);
3855 3860          } else {
3856 3861                  BP_SET_CRYPT(bp, B_TRUE);
3857 3862          }
3858 3863  
3859 3864          /* Perform the encryption. This should not fail */
3860 3865          VERIFY0(spa_do_crypt_abd(B_TRUE, spa, &zio->io_bookmark,
3861 3866              BP_GET_TYPE(bp), BP_GET_DEDUP(bp), BP_SHOULD_BYTESWAP(bp),
3862 3867              salt, iv, mac, psize, zio->io_abd, eabd, &no_crypt));
3863 3868  
3864 3869          /* encode encryption metadata into the bp */
3865 3870          if (ot == DMU_OT_INTENT_LOG) {
3866 3871                  /*
3867 3872                   * ZIL blocks store the MAC in the embedded checksum, so the
3868 3873                   * transform must always be applied.
3869 3874                   */
3870 3875                  zio_crypt_encode_mac_zil(enc_buf, mac);
3871 3876                  zio_push_transform(zio, eabd, psize, psize, NULL);
3872 3877          } else {
3873 3878                  BP_SET_CRYPT(bp, B_TRUE);
3874 3879                  zio_crypt_encode_params_bp(bp, salt, iv);
3875 3880                  zio_crypt_encode_mac_bp(bp, mac);
3876 3881  
3877 3882                  if (no_crypt) {
3878 3883                          ASSERT3U(ot, ==, DMU_OT_DNODE);
3879 3884                          abd_free(eabd);
3880 3885                  } else {
3881 3886                          zio_push_transform(zio, eabd, psize, psize, NULL);
3882 3887                  }
3883 3888          }
3884 3889  
3885 3890          return (ZIO_PIPELINE_CONTINUE);
3886 3891  }
3887 3892  
3888 3893  /*
3889 3894   * ==========================================================================
3890 3895   * Generate and verify checksums
3891 3896   * ==========================================================================
3892 3897   */
3893 3898  static int
3894 3899  zio_checksum_generate(zio_t *zio)
3895 3900  {
3896 3901          blkptr_t *bp = zio->io_bp;
3897 3902          enum zio_checksum checksum;
3898 3903  
3899 3904          if (bp == NULL) {
3900 3905                  /*
3901 3906                   * This is zio_write_phys().
3902 3907                   * We're either generating a label checksum, or none at all.
3903 3908                   */
3904 3909                  checksum = zio->io_prop.zp_checksum;
3905 3910  
3906 3911                  if (checksum == ZIO_CHECKSUM_OFF)
3907 3912                          return (ZIO_PIPELINE_CONTINUE);
3908 3913  
3909 3914                  ASSERT(checksum == ZIO_CHECKSUM_LABEL);
3910 3915          } else {
3911 3916                  if (BP_IS_GANG(bp) && zio->io_child_type == ZIO_CHILD_GANG) {
3912 3917                          ASSERT(!IO_IS_ALLOCATING(zio));
3913 3918                          checksum = ZIO_CHECKSUM_GANG_HEADER;
3914 3919                  } else {
3915 3920                          checksum = BP_GET_CHECKSUM(bp);
3916 3921                  }
3917 3922          }
3918 3923  
3919 3924          zio_checksum_compute(zio, checksum, zio->io_abd, zio->io_size);
3920 3925  
3921 3926          return (ZIO_PIPELINE_CONTINUE);
3922 3927  }
3923 3928  
3924 3929  static int
3925 3930  zio_checksum_verify(zio_t *zio)
3926 3931  {
3927 3932          zio_bad_cksum_t info;
3928 3933          blkptr_t *bp = zio->io_bp;
3929 3934          int error;
3930 3935  
3931 3936          ASSERT(zio->io_vd != NULL);
3932 3937  
3933 3938          if (bp == NULL) {
3934 3939                  /*
3935 3940                   * This is zio_read_phys().
3936 3941                   * We're either verifying a label checksum, or nothing at all.
3937 3942                   */
3938 3943                  if (zio->io_prop.zp_checksum == ZIO_CHECKSUM_OFF)
3939 3944                          return (ZIO_PIPELINE_CONTINUE);
3940 3945  
3941 3946                  ASSERT(zio->io_prop.zp_checksum == ZIO_CHECKSUM_LABEL);
3942 3947          }
3943 3948  
3944 3949          if ((error = zio_checksum_error(zio, &info)) != 0) {
3945 3950                  zio->io_error = error;
3946 3951                  if (error == ECKSUM &&
3947 3952                      !(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
3948 3953                          zfs_ereport_start_checksum(zio->io_spa,
3949 3954                              zio->io_vd, &zio->io_bookmark, zio,
3950 3955                              zio->io_offset, zio->io_size, NULL, &info);
3951 3956                  }
3952 3957          }
3953 3958  
3954 3959          return (ZIO_PIPELINE_CONTINUE);
3955 3960  }
3956 3961  
3957 3962  /*
3958 3963   * Called by RAID-Z to ensure we don't compute the checksum twice.
3959 3964   */
3960 3965  void
3961 3966  zio_checksum_verified(zio_t *zio)
3962 3967  {
3963 3968          zio->io_pipeline &= ~ZIO_STAGE_CHECKSUM_VERIFY;
3964 3969  }
3965 3970  
3966 3971  /*
3967 3972   * ==========================================================================
3968 3973   * Error rank.  Error are ranked in the order 0, ENXIO, ECKSUM, EIO, other.
3969 3974   * An error of 0 indicates success.  ENXIO indicates whole-device failure,
3970 3975   * which may be transient (e.g. unplugged) or permament.  ECKSUM and EIO
3971 3976   * indicate errors that are specific to one I/O, and most likely permanent.
3972 3977   * Any other error is presumed to be worse because we weren't expecting it.
3973 3978   * ==========================================================================
3974 3979   */
3975 3980  int
3976 3981  zio_worst_error(int e1, int e2)
3977 3982  {
3978 3983          static int zio_error_rank[] = { 0, ENXIO, ECKSUM, EIO };
3979 3984          int r1, r2;
3980 3985  
3981 3986          for (r1 = 0; r1 < sizeof (zio_error_rank) / sizeof (int); r1++)
3982 3987                  if (e1 == zio_error_rank[r1])
3983 3988                          break;
3984 3989  
3985 3990          for (r2 = 0; r2 < sizeof (zio_error_rank) / sizeof (int); r2++)
3986 3991                  if (e2 == zio_error_rank[r2])
3987 3992                          break;
3988 3993  
3989 3994          return (r1 > r2 ? e1 : e2);
3990 3995  }
3991 3996  
3992 3997  /*
3993 3998   * ==========================================================================
3994 3999   * I/O completion
3995 4000   * ==========================================================================
3996 4001   */
3997 4002  static int
3998 4003  zio_ready(zio_t *zio)
3999 4004  {
4000 4005          blkptr_t *bp = zio->io_bp;
4001 4006          zio_t *pio, *pio_next;
4002 4007          zio_link_t *zl = NULL;
4003 4008  
4004 4009          if (zio_wait_for_children(zio, ZIO_CHILD_GANG_BIT | ZIO_CHILD_DDT_BIT,
4005 4010              ZIO_WAIT_READY)) {
4006 4011                  return (ZIO_PIPELINE_STOP);
4007 4012          }
4008 4013  
4009 4014          if (zio->io_ready) {
4010 4015                  ASSERT(IO_IS_ALLOCATING(zio));
4011 4016                  ASSERT(bp->blk_birth == zio->io_txg || BP_IS_HOLE(bp) ||
4012 4017                      (zio->io_flags & ZIO_FLAG_NOPWRITE));
4013 4018                  ASSERT(zio->io_children[ZIO_CHILD_GANG][ZIO_WAIT_READY] == 0);
4014 4019  
4015 4020                  zio->io_ready(zio);
4016 4021          }
4017 4022  
4018 4023          if (bp != NULL && bp != &zio->io_bp_copy)
4019 4024                  zio->io_bp_copy = *bp;
4020 4025  
4021 4026          if (zio->io_error != 0) {
4022 4027                  zio->io_pipeline = ZIO_INTERLOCK_PIPELINE;
4023 4028  
4024 4029                  if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
4025 4030                          ASSERT(IO_IS_ALLOCATING(zio));
4026 4031                          ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
4027 4032                          ASSERT(zio->io_metaslab_class != NULL);
4028 4033  
4029 4034                          /*
4030 4035                           * We were unable to allocate anything, unreserve and
4031 4036                           * issue the next I/O to allocate.
4032 4037                           */
4033 4038                          metaslab_class_throttle_unreserve(
4034 4039                              zio->io_metaslab_class, zio->io_prop.zp_copies,
4035 4040                              zio->io_allocator, zio);
4036 4041                          zio_allocate_dispatch(zio->io_spa, zio->io_allocator);
4037 4042                  }
4038 4043          }
4039 4044  
4040 4045          mutex_enter(&zio->io_lock);
4041 4046          zio->io_state[ZIO_WAIT_READY] = 1;
4042 4047          pio = zio_walk_parents(zio, &zl);
4043 4048          mutex_exit(&zio->io_lock);
4044 4049  
4045 4050          /*
4046 4051           * As we notify zio's parents, new parents could be added.
4047 4052           * New parents go to the head of zio's io_parent_list, however,
4048 4053           * so we will (correctly) not notify them.  The remainder of zio's
4049 4054           * io_parent_list, from 'pio_next' onward, cannot change because
4050 4055           * all parents must wait for us to be done before they can be done.
4051 4056           */
4052 4057          for (; pio != NULL; pio = pio_next) {
4053 4058                  pio_next = zio_walk_parents(zio, &zl);
4054 4059                  zio_notify_parent(pio, zio, ZIO_WAIT_READY);
4055 4060          }
4056 4061  
4057 4062          if (zio->io_flags & ZIO_FLAG_NODATA) {
4058 4063                  if (BP_IS_GANG(bp)) {
4059 4064                          zio->io_flags &= ~ZIO_FLAG_NODATA;
4060 4065                  } else {
4061 4066                          ASSERT((uintptr_t)zio->io_abd < SPA_MAXBLOCKSIZE);
4062 4067                          zio->io_pipeline &= ~ZIO_VDEV_IO_STAGES;
4063 4068                  }
4064 4069          }
4065 4070  
4066 4071          if (zio_injection_enabled &&
4067 4072              zio->io_spa->spa_syncing_txg == zio->io_txg)
4068 4073                  zio_handle_ignored_writes(zio);
4069 4074  
4070 4075          return (ZIO_PIPELINE_CONTINUE);
4071 4076  }
4072 4077  
4073 4078  /*
4074 4079   * Update the allocation throttle accounting.
4075 4080   */
4076 4081  static void
4077 4082  zio_dva_throttle_done(zio_t *zio)
4078 4083  {
4079 4084          zio_t *lio = zio->io_logical;
4080 4085          zio_t *pio = zio_unique_parent(zio);
4081 4086          vdev_t *vd = zio->io_vd;
4082 4087          int flags = METASLAB_ASYNC_ALLOC;
4083 4088  
4084 4089          ASSERT3P(zio->io_bp, !=, NULL);
4085 4090          ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
4086 4091          ASSERT3U(zio->io_priority, ==, ZIO_PRIORITY_ASYNC_WRITE);
4087 4092          ASSERT3U(zio->io_child_type, ==, ZIO_CHILD_VDEV);
4088 4093          ASSERT(vd != NULL);
4089 4094          ASSERT3P(vd, ==, vd->vdev_top);
4090 4095          ASSERT(!(zio->io_flags & (ZIO_FLAG_IO_REPAIR | ZIO_FLAG_IO_RETRY)));
4091 4096          ASSERT(zio->io_flags & ZIO_FLAG_IO_ALLOCATING);
4092 4097          ASSERT(!(lio->io_flags & ZIO_FLAG_IO_REWRITE));
4093 4098          ASSERT(!(lio->io_orig_flags & ZIO_FLAG_NODATA));
4094 4099  
4095 4100          /*
4096 4101           * Parents of gang children can have two flavors -- ones that
4097 4102           * allocated the gang header (will have ZIO_FLAG_IO_REWRITE set)
4098 4103           * and ones that allocated the constituent blocks. The allocation
4099 4104           * throttle needs to know the allocating parent zio so we must find
4100 4105           * it here.
4101 4106           */
4102 4107          if (pio->io_child_type == ZIO_CHILD_GANG) {
4103 4108                  /*
4104 4109                   * If our parent is a rewrite gang child then our grandparent
4105 4110                   * would have been the one that performed the allocation.
4106 4111                   */
4107 4112                  if (pio->io_flags & ZIO_FLAG_IO_REWRITE)
4108 4113                          pio = zio_unique_parent(pio);
4109 4114                  flags |= METASLAB_GANG_CHILD;
4110 4115          }
4111 4116  
4112 4117          ASSERT(IO_IS_ALLOCATING(pio));
4113 4118          ASSERT3P(zio, !=, zio->io_logical);
4114 4119          ASSERT(zio->io_logical != NULL);
4115 4120          ASSERT(!(zio->io_flags & ZIO_FLAG_IO_REPAIR));
4116 4121          ASSERT0(zio->io_flags & ZIO_FLAG_NOPWRITE);
4117 4122          ASSERT(zio->io_metaslab_class != NULL);
4118 4123  
4119 4124          mutex_enter(&pio->io_lock);
4120 4125          metaslab_group_alloc_decrement(zio->io_spa, vd->vdev_id, pio, flags,
4121 4126              pio->io_allocator, B_TRUE);
4122 4127          mutex_exit(&pio->io_lock);
4123 4128  
4124 4129          metaslab_class_throttle_unreserve(zio->io_metaslab_class, 1,
4125 4130              pio->io_allocator, pio);
4126 4131  
4127 4132          /*
4128 4133           * Call into the pipeline to see if there is more work that
4129 4134           * needs to be done. If there is work to be done it will be
4130 4135           * dispatched to another taskq thread.
4131 4136           */
4132 4137          zio_allocate_dispatch(zio->io_spa, pio->io_allocator);
4133 4138  }
4134 4139  
4135 4140  static int
4136 4141  zio_done(zio_t *zio)
4137 4142  {
4138 4143          spa_t *spa = zio->io_spa;
4139 4144          zio_t *lio = zio->io_logical;
4140 4145          blkptr_t *bp = zio->io_bp;
4141 4146          vdev_t *vd = zio->io_vd;
4142 4147          uint64_t psize = zio->io_size;
4143 4148          zio_t *pio, *pio_next;
4144 4149          zio_link_t *zl = NULL;
4145 4150  
4146 4151          /*
4147 4152           * If our children haven't all completed,
4148 4153           * wait for them and then repeat this pipeline stage.
4149 4154           */
4150 4155          if (zio_wait_for_children(zio, ZIO_CHILD_ALL_BITS, ZIO_WAIT_DONE)) {
4151 4156                  return (ZIO_PIPELINE_STOP);
4152 4157          }
4153 4158  
4154 4159          /*
4155 4160           * If the allocation throttle is enabled, then update the accounting.
4156 4161           * We only track child I/Os that are part of an allocating async
4157 4162           * write. We must do this since the allocation is performed
4158 4163           * by the logical I/O but the actual write is done by child I/Os.
4159 4164           */
4160 4165          if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING &&
4161 4166              zio->io_child_type == ZIO_CHILD_VDEV) {
4162 4167                  ASSERT(zio->io_metaslab_class != NULL);
4163 4168                  ASSERT(zio->io_metaslab_class->mc_alloc_throttle_enabled);
4164 4169                  zio_dva_throttle_done(zio);
4165 4170          }
4166 4171  
4167 4172          /*
4168 4173           * If the allocation throttle is enabled, verify that
4169 4174           * we have decremented the refcounts for every I/O that was throttled.
4170 4175           */
4171 4176          if (zio->io_flags & ZIO_FLAG_IO_ALLOCATING) {
4172 4177                  ASSERT(zio->io_type == ZIO_TYPE_WRITE);
4173 4178                  ASSERT(zio->io_priority == ZIO_PRIORITY_ASYNC_WRITE);
4174 4179                  ASSERT(bp != NULL);
4175 4180  
4176 4181                  metaslab_group_alloc_verify(spa, zio->io_bp, zio,
4177 4182                      zio->io_allocator);
4178 4183                  VERIFY(zfs_refcount_not_held(
4179 4184                      &zio->io_metaslab_class->mc_alloc_slots[zio->io_allocator],
4180 4185                      zio));
4181 4186          }
4182 4187  
4183 4188          for (int c = 0; c < ZIO_CHILD_TYPES; c++)
4184 4189                  for (int w = 0; w < ZIO_WAIT_TYPES; w++)
4185 4190                          ASSERT(zio->io_children[c][w] == 0);
4186 4191  
4187 4192          if (bp != NULL && !BP_IS_EMBEDDED(bp)) {
4188 4193                  ASSERT(bp->blk_pad[0] == 0);
4189 4194                  ASSERT(bp->blk_pad[1] == 0);
4190 4195                  ASSERT(bcmp(bp, &zio->io_bp_copy, sizeof (blkptr_t)) == 0 ||
4191 4196                      (bp == zio_unique_parent(zio)->io_bp));
4192 4197                  if (zio->io_type == ZIO_TYPE_WRITE && !BP_IS_HOLE(bp) &&
4193 4198                      zio->io_bp_override == NULL &&
4194 4199                      !(zio->io_flags & ZIO_FLAG_IO_REPAIR)) {
4195 4200                          ASSERT3U(zio->io_prop.zp_copies, <=, BP_GET_NDVAS(bp));
4196 4201                          ASSERT(BP_COUNT_GANG(bp) == 0 ||
4197 4202                              (BP_COUNT_GANG(bp) == BP_GET_NDVAS(bp)));
4198 4203                  }
4199 4204                  if (zio->io_flags & ZIO_FLAG_NOPWRITE)
4200 4205                          VERIFY(BP_EQUAL(bp, &zio->io_bp_orig));
4201 4206          }
4202 4207  
4203 4208          /*
4204 4209           * If there were child vdev/gang/ddt errors, they apply to us now.
4205 4210           */
4206 4211          zio_inherit_child_errors(zio, ZIO_CHILD_VDEV);
4207 4212          zio_inherit_child_errors(zio, ZIO_CHILD_GANG);
4208 4213          zio_inherit_child_errors(zio, ZIO_CHILD_DDT);
4209 4214  
4210 4215          /*
4211 4216           * If the I/O on the transformed data was successful, generate any
4212 4217           * checksum reports now while we still have the transformed data.
4213 4218           */
4214 4219          if (zio->io_error == 0) {
4215 4220                  while (zio->io_cksum_report != NULL) {
4216 4221                          zio_cksum_report_t *zcr = zio->io_cksum_report;
4217 4222                          uint64_t align = zcr->zcr_align;
4218 4223                          uint64_t asize = P2ROUNDUP(psize, align);
4219 4224                          abd_t *adata = zio->io_abd;
4220 4225  
4221 4226                          if (asize != psize) {
4222 4227                                  adata = abd_alloc(asize, B_TRUE);
4223 4228                                  abd_copy(adata, zio->io_abd, psize);
4224 4229                                  abd_zero_off(adata, psize, asize - psize);
4225 4230                          }
4226 4231  
4227 4232                          zio->io_cksum_report = zcr->zcr_next;
4228 4233                          zcr->zcr_next = NULL;
4229 4234                          zcr->zcr_finish(zcr, adata);
4230 4235                          zfs_ereport_free_checksum(zcr);
4231 4236  
4232 4237                          if (asize != psize)
4233 4238                                  abd_free(adata);
4234 4239                  }
4235 4240          }
4236 4241  
4237 4242          zio_pop_transforms(zio);        /* note: may set zio->io_error */
4238 4243  
4239 4244          vdev_stat_update(zio, psize);
4240 4245  
4241 4246          if (zio->io_delay >= MSEC2NSEC(zio_slow_io_ms)) {
4242 4247                  if (zio->io_vd != NULL && !vdev_is_dead(zio->io_vd)) {
4243 4248                          /*
4244 4249                           * We want to only increment our slow IO counters if
4245 4250                           * the IO is valid (i.e. not if the drive is removed).
4246 4251                           *
4247 4252                           * zfs_ereport_post() will also do these checks, but
4248 4253                           * it can also have other failures, so we need to
4249 4254                           * increment the slow_io counters independent of it.
4250 4255                           */
4251 4256                          if (zfs_ereport_is_valid(FM_EREPORT_ZFS_DELAY,
4252 4257                              zio->io_spa, zio->io_vd, zio)) {
4253 4258                                  mutex_enter(&zio->io_vd->vdev_stat_lock);
4254 4259                                  zio->io_vd->vdev_stat.vs_slow_ios++;
4255 4260                                  mutex_exit(&zio->io_vd->vdev_stat_lock);
4256 4261  
4257 4262                                  zfs_ereport_post(FM_EREPORT_ZFS_DELAY,
4258 4263                                      zio->io_spa, zio->io_vd, &zio->io_bookmark,
4259 4264                                      zio, 0, 0);
4260 4265                          }
4261 4266                  }
4262 4267          }
4263 4268  
4264 4269          if (zio->io_error) {
4265 4270                  /*
4266 4271                   * If this I/O is attached to a particular vdev,
4267 4272                   * generate an error message describing the I/O failure
4268 4273                   * at the block level.  We ignore these errors if the
4269 4274                   * device is currently unavailable.
4270 4275                   */
4271 4276                  if (zio->io_error != ECKSUM && vd != NULL && !vdev_is_dead(vd))
4272 4277                          zfs_ereport_post(FM_EREPORT_ZFS_IO, spa, vd,
4273 4278                              &zio->io_bookmark, zio, 0, 0);
4274 4279  
4275 4280                  if ((zio->io_error == EIO || !(zio->io_flags &
4276 4281                      (ZIO_FLAG_SPECULATIVE | ZIO_FLAG_DONT_PROPAGATE))) &&
4277 4282                      zio == lio) {
4278 4283                          /*
4279 4284                           * For logical I/O requests, tell the SPA to log the
4280 4285                           * error and generate a logical data ereport.
4281 4286                           */
4282 4287                          spa_log_error(spa, &zio->io_bookmark);
4283 4288                          zfs_ereport_post(FM_EREPORT_ZFS_DATA, spa, NULL,
4284 4289                              &zio->io_bookmark, zio, 0, 0);
4285 4290                  }
4286 4291          }
4287 4292  
4288 4293          if (zio->io_error && zio == lio) {
4289 4294                  /*
4290 4295                   * Determine whether zio should be reexecuted.  This will
4291 4296                   * propagate all the way to the root via zio_notify_parent().
4292 4297                   */
4293 4298                  ASSERT(vd == NULL && bp != NULL);
4294 4299                  ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
4295 4300  
4296 4301                  if (IO_IS_ALLOCATING(zio) &&
4297 4302                      !(zio->io_flags & ZIO_FLAG_CANFAIL)) {
4298 4303                          if (zio->io_error != ENOSPC)
4299 4304                                  zio->io_reexecute |= ZIO_REEXECUTE_NOW;
4300 4305                          else
4301 4306                                  zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
4302 4307                  }
4303 4308  
4304 4309                  if ((zio->io_type == ZIO_TYPE_READ ||
4305 4310                      zio->io_type == ZIO_TYPE_FREE) &&
4306 4311                      !(zio->io_flags & ZIO_FLAG_SCAN_THREAD) &&
4307 4312                      zio->io_error == ENXIO &&
4308 4313                      spa_load_state(spa) == SPA_LOAD_NONE &&
4309 4314                      spa_get_failmode(spa) != ZIO_FAILURE_MODE_CONTINUE)
4310 4315                          zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
4311 4316  
4312 4317                  if (!(zio->io_flags & ZIO_FLAG_CANFAIL) && !zio->io_reexecute)
4313 4318                          zio->io_reexecute |= ZIO_REEXECUTE_SUSPEND;
4314 4319  
4315 4320                  /*
4316 4321                   * Here is a possibly good place to attempt to do
4317 4322                   * either combinatorial reconstruction or error correction
4318 4323                   * based on checksums.  It also might be a good place
4319 4324                   * to send out preliminary ereports before we suspend
4320 4325                   * processing.
4321 4326                   */
4322 4327          }
4323 4328  
4324 4329          /*
4325 4330           * If there were logical child errors, they apply to us now.
4326 4331           * We defer this until now to avoid conflating logical child
4327 4332           * errors with errors that happened to the zio itself when
4328 4333           * updating vdev stats and reporting FMA events above.
4329 4334           */
4330 4335          zio_inherit_child_errors(zio, ZIO_CHILD_LOGICAL);
4331 4336  
4332 4337          if ((zio->io_error || zio->io_reexecute) &&
4333 4338              IO_IS_ALLOCATING(zio) && zio->io_gang_leader == zio &&
4334 4339              !(zio->io_flags & (ZIO_FLAG_IO_REWRITE | ZIO_FLAG_NOPWRITE)))
4335 4340                  zio_dva_unallocate(zio, zio->io_gang_tree, bp);
4336 4341  
4337 4342          zio_gang_tree_free(&zio->io_gang_tree);
4338 4343  
4339 4344          /*
4340 4345           * Godfather I/Os should never suspend.
4341 4346           */
4342 4347          if ((zio->io_flags & ZIO_FLAG_GODFATHER) &&
4343 4348              (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND))
4344 4349                  zio->io_reexecute = 0;
4345 4350  
4346 4351          if (zio->io_reexecute) {
4347 4352                  /*
4348 4353                   * This is a logical I/O that wants to reexecute.
4349 4354                   *
4350 4355                   * Reexecute is top-down.  When an i/o fails, if it's not
4351 4356                   * the root, it simply notifies its parent and sticks around.
4352 4357                   * The parent, seeing that it still has children in zio_done(),
4353 4358                   * does the same.  This percolates all the way up to the root.
4354 4359                   * The root i/o will reexecute or suspend the entire tree.
4355 4360                   *
4356 4361                   * This approach ensures that zio_reexecute() honors
4357 4362                   * all the original i/o dependency relationships, e.g.
4358 4363                   * parents not executing until children are ready.
4359 4364                   */
4360 4365                  ASSERT(zio->io_child_type == ZIO_CHILD_LOGICAL);
4361 4366  
4362 4367                  zio->io_gang_leader = NULL;
4363 4368  
4364 4369                  mutex_enter(&zio->io_lock);
4365 4370                  zio->io_state[ZIO_WAIT_DONE] = 1;
4366 4371                  mutex_exit(&zio->io_lock);
4367 4372  
4368 4373                  /*
4369 4374                   * "The Godfather" I/O monitors its children but is
4370 4375                   * not a true parent to them. It will track them through
4371 4376                   * the pipeline but severs its ties whenever they get into
4372 4377                   * trouble (e.g. suspended). This allows "The Godfather"
4373 4378                   * I/O to return status without blocking.
4374 4379                   */
4375 4380                  zl = NULL;
4376 4381                  for (pio = zio_walk_parents(zio, &zl); pio != NULL;
4377 4382                      pio = pio_next) {
4378 4383                          zio_link_t *remove_zl = zl;
4379 4384                          pio_next = zio_walk_parents(zio, &zl);
4380 4385  
4381 4386                          if ((pio->io_flags & ZIO_FLAG_GODFATHER) &&
4382 4387                              (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND)) {
4383 4388                                  zio_remove_child(pio, zio, remove_zl);
4384 4389                                  zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
4385 4390                          }
4386 4391                  }
4387 4392  
4388 4393                  if ((pio = zio_unique_parent(zio)) != NULL) {
4389 4394                          /*
4390 4395                           * We're not a root i/o, so there's nothing to do
4391 4396                           * but notify our parent.  Don't propagate errors
4392 4397                           * upward since we haven't permanently failed yet.
4393 4398                           */
4394 4399                          ASSERT(!(zio->io_flags & ZIO_FLAG_GODFATHER));
4395 4400                          zio->io_flags |= ZIO_FLAG_DONT_PROPAGATE;
4396 4401                          zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
4397 4402                  } else if (zio->io_reexecute & ZIO_REEXECUTE_SUSPEND) {
4398 4403                          /*
4399 4404                           * We'd fail again if we reexecuted now, so suspend
4400 4405                           * until conditions improve (e.g. device comes online).
4401 4406                           */
4402 4407                          zio_suspend(zio->io_spa, zio, ZIO_SUSPEND_IOERR);
4403 4408                  } else {
4404 4409                          /*
4405 4410                           * Reexecution is potentially a huge amount of work.
4406 4411                           * Hand it off to the otherwise-unused claim taskq.
4407 4412                           */
4408 4413                          ASSERT(zio->io_tqent.tqent_next == NULL);
4409 4414                          spa_taskq_dispatch_ent(spa, ZIO_TYPE_CLAIM,
4410 4415                              ZIO_TASKQ_ISSUE, (task_func_t *)zio_reexecute, zio,
4411 4416                              0, &zio->io_tqent);
4412 4417                  }
4413 4418                  return (ZIO_PIPELINE_STOP);
4414 4419          }
4415 4420  
4416 4421          ASSERT(zio->io_child_count == 0);
4417 4422          ASSERT(zio->io_reexecute == 0);
4418 4423          ASSERT(zio->io_error == 0 || (zio->io_flags & ZIO_FLAG_CANFAIL));
4419 4424  
4420 4425          /*
4421 4426           * Report any checksum errors, since the I/O is complete.
4422 4427           */
4423 4428          while (zio->io_cksum_report != NULL) {
4424 4429                  zio_cksum_report_t *zcr = zio->io_cksum_report;
4425 4430                  zio->io_cksum_report = zcr->zcr_next;
4426 4431                  zcr->zcr_next = NULL;
4427 4432                  zcr->zcr_finish(zcr, NULL);
4428 4433                  zfs_ereport_free_checksum(zcr);
4429 4434          }
4430 4435  
4431 4436          /*
4432 4437           * It is the responsibility of the done callback to ensure that this
4433 4438           * particular zio is no longer discoverable for adoption, and as
4434 4439           * such, cannot acquire any new parents.
4435 4440           */
4436 4441          if (zio->io_done)
4437 4442                  zio->io_done(zio);
4438 4443  
4439 4444          mutex_enter(&zio->io_lock);
4440 4445          zio->io_state[ZIO_WAIT_DONE] = 1;
4441 4446          mutex_exit(&zio->io_lock);
4442 4447  
4443 4448          zl = NULL;
4444 4449          for (pio = zio_walk_parents(zio, &zl); pio != NULL; pio = pio_next) {
4445 4450                  zio_link_t *remove_zl = zl;
4446 4451                  pio_next = zio_walk_parents(zio, &zl);
4447 4452                  zio_remove_child(pio, zio, remove_zl);
4448 4453                  zio_notify_parent(pio, zio, ZIO_WAIT_DONE);
4449 4454          }
4450 4455  
4451 4456          if (zio->io_waiter != NULL) {
4452 4457                  mutex_enter(&zio->io_lock);
4453 4458                  zio->io_executor = NULL;
4454 4459                  cv_broadcast(&zio->io_cv);
4455 4460                  mutex_exit(&zio->io_lock);
4456 4461          } else {
4457 4462                  zio_destroy(zio);
4458 4463          }
4459 4464  
4460 4465          return (ZIO_PIPELINE_STOP);
4461 4466  }
4462 4467  
4463 4468  /*
4464 4469   * ==========================================================================
4465 4470   * I/O pipeline definition
4466 4471   * ==========================================================================
4467 4472   */
4468 4473  static zio_pipe_stage_t *zio_pipeline[] = {
4469 4474          NULL,
4470 4475          zio_read_bp_init,
4471 4476          zio_write_bp_init,
4472 4477          zio_free_bp_init,
4473 4478          zio_issue_async,
4474 4479          zio_write_compress,
4475 4480          zio_encrypt,
4476 4481          zio_checksum_generate,
4477 4482          zio_nop_write,
4478 4483          zio_ddt_read_start,
4479 4484          zio_ddt_read_done,
4480 4485          zio_ddt_write,
4481 4486          zio_ddt_free,
4482 4487          zio_gang_assemble,
4483 4488          zio_gang_issue,
4484 4489          zio_dva_throttle,
4485 4490          zio_dva_allocate,
4486 4491          zio_dva_free,
4487 4492          zio_dva_claim,
4488 4493          zio_ready,
4489 4494          zio_vdev_io_start,
4490 4495          zio_vdev_io_done,
4491 4496          zio_vdev_io_assess,
4492 4497          zio_checksum_verify,
4493 4498          zio_done
4494 4499  };
4495 4500  
4496 4501  
4497 4502  
4498 4503  
4499 4504  /*
4500 4505   * Compare two zbookmark_phys_t's to see which we would reach first in a
4501 4506   * pre-order traversal of the object tree.
4502 4507   *
4503 4508   * This is simple in every case aside from the meta-dnode object. For all other
4504 4509   * objects, we traverse them in order (object 1 before object 2, and so on).
4505 4510   * However, all of these objects are traversed while traversing object 0, since
4506 4511   * the data it points to is the list of objects.  Thus, we need to convert to a
4507 4512   * canonical representation so we can compare meta-dnode bookmarks to
4508 4513   * non-meta-dnode bookmarks.
4509 4514   *
4510 4515   * We do this by calculating "equivalents" for each field of the zbookmark.
4511 4516   * zbookmarks outside of the meta-dnode use their own object and level, and
4512 4517   * calculate the level 0 equivalent (the first L0 blkid that is contained in the
4513 4518   * blocks this bookmark refers to) by multiplying their blkid by their span
4514 4519   * (the number of L0 blocks contained within one block at their level).
4515 4520   * zbookmarks inside the meta-dnode calculate their object equivalent
4516 4521   * (which is L0equiv * dnodes per data block), use 0 for their L0equiv, and use
4517 4522   * level + 1<<31 (any value larger than a level could ever be) for their level.
4518 4523   * This causes them to always compare before a bookmark in their object
4519 4524   * equivalent, compare appropriately to bookmarks in other objects, and to
4520 4525   * compare appropriately to other bookmarks in the meta-dnode.
4521 4526   */
4522 4527  int
4523 4528  zbookmark_compare(uint16_t dbss1, uint8_t ibs1, uint16_t dbss2, uint8_t ibs2,
4524 4529      const zbookmark_phys_t *zb1, const zbookmark_phys_t *zb2)
4525 4530  {
4526 4531          /*
4527 4532           * These variables represent the "equivalent" values for the zbookmark,
4528 4533           * after converting zbookmarks inside the meta dnode to their
4529 4534           * normal-object equivalents.
4530 4535           */
4531 4536          uint64_t zb1obj, zb2obj;
4532 4537          uint64_t zb1L0, zb2L0;
4533 4538          uint64_t zb1level, zb2level;
4534 4539  
4535 4540          if (zb1->zb_object == zb2->zb_object &&
4536 4541              zb1->zb_level == zb2->zb_level &&
4537 4542              zb1->zb_blkid == zb2->zb_blkid)
4538 4543                  return (0);
4539 4544  
4540 4545          /*
4541 4546           * BP_SPANB calculates the span in blocks.
4542 4547           */
4543 4548          zb1L0 = (zb1->zb_blkid) * BP_SPANB(ibs1, zb1->zb_level);
4544 4549          zb2L0 = (zb2->zb_blkid) * BP_SPANB(ibs2, zb2->zb_level);
4545 4550  
4546 4551          if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
4547 4552                  zb1obj = zb1L0 * (dbss1 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4548 4553                  zb1L0 = 0;
4549 4554                  zb1level = zb1->zb_level + COMPARE_META_LEVEL;
4550 4555          } else {
4551 4556                  zb1obj = zb1->zb_object;
4552 4557                  zb1level = zb1->zb_level;
4553 4558          }
4554 4559  
4555 4560          if (zb2->zb_object == DMU_META_DNODE_OBJECT) {
4556 4561                  zb2obj = zb2L0 * (dbss2 << (SPA_MINBLOCKSHIFT - DNODE_SHIFT));
4557 4562                  zb2L0 = 0;
4558 4563                  zb2level = zb2->zb_level + COMPARE_META_LEVEL;
4559 4564          } else {
4560 4565                  zb2obj = zb2->zb_object;
4561 4566                  zb2level = zb2->zb_level;
4562 4567          }
4563 4568  
4564 4569          /* Now that we have a canonical representation, do the comparison. */
4565 4570          if (zb1obj != zb2obj)
4566 4571                  return (zb1obj < zb2obj ? -1 : 1);
4567 4572          else if (zb1L0 != zb2L0)
4568 4573                  return (zb1L0 < zb2L0 ? -1 : 1);
4569 4574          else if (zb1level != zb2level)
4570 4575                  return (zb1level > zb2level ? -1 : 1);
4571 4576          /*
4572 4577           * This can (theoretically) happen if the bookmarks have the same object
4573 4578           * and level, but different blkids, if the block sizes are not the same.
4574 4579           * There is presently no way to change the indirect block sizes
4575 4580           */
4576 4581          return (0);
4577 4582  }
4578 4583  
4579 4584  /*
4580 4585   *  This function checks the following: given that last_block is the place that
4581 4586   *  our traversal stopped last time, does that guarantee that we've visited
4582 4587   *  every node under subtree_root?  Therefore, we can't just use the raw output
4583 4588   *  of zbookmark_compare.  We have to pass in a modified version of
4584 4589   *  subtree_root; by incrementing the block id, and then checking whether
4585 4590   *  last_block is before or equal to that, we can tell whether or not having
4586 4591   *  visited last_block implies that all of subtree_root's children have been
4587 4592   *  visited.
4588 4593   */
4589 4594  boolean_t
4590 4595  zbookmark_subtree_completed(const dnode_phys_t *dnp,
4591 4596      const zbookmark_phys_t *subtree_root, const zbookmark_phys_t *last_block)
4592 4597  {
4593 4598          zbookmark_phys_t mod_zb = *subtree_root;
4594 4599          mod_zb.zb_blkid++;
4595 4600          ASSERT(last_block->zb_level == 0);
4596 4601  
4597 4602          /* The objset_phys_t isn't before anything. */
4598 4603          if (dnp == NULL)
4599 4604                  return (B_FALSE);
4600 4605  
4601 4606          /*
4602 4607           * We pass in 1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT) for the
4603 4608           * data block size in sectors, because that variable is only used if
4604 4609           * the bookmark refers to a block in the meta-dnode.  Since we don't
4605 4610           * know without examining it what object it refers to, and there's no
4606 4611           * harm in passing in this value in other cases, we always pass it in.
4607 4612           *
4608 4613           * We pass in 0 for the indirect block size shift because zb2 must be
4609 4614           * level 0.  The indirect block size is only used to calculate the span
4610 4615           * of the bookmark, but since the bookmark must be level 0, the span is
4611 4616           * always 1, so the math works out.
4612 4617           *
4613 4618           * If you make changes to how the zbookmark_compare code works, be sure
4614 4619           * to make sure that this code still works afterwards.
4615 4620           */
4616 4621          return (zbookmark_compare(dnp->dn_datablkszsec, dnp->dn_indblkshift,
4617 4622              1ULL << (DNODE_BLOCK_SHIFT - SPA_MINBLOCKSHIFT), 0, &mod_zb,
4618 4623              last_block) <= 0);
4619 4624  }
  
    | ↓ open down ↓ | 2623 lines elided | ↑ open up ↑ | 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX