Print this page
    
3006 VERIFY[S,U,P] and ASSERT[S,U,P] frequently check if first argument is zero
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/fs/zfs/dsl_scan.c
          +++ new/usr/src/uts/common/fs/zfs/dsl_scan.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
  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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  23   23   * Copyright (c) 2012 by Delphix. All rights reserved.
  24   24   */
  25   25  
  26   26  #include <sys/dsl_scan.h>
  27   27  #include <sys/dsl_pool.h>
  28   28  #include <sys/dsl_dataset.h>
  29   29  #include <sys/dsl_prop.h>
  30   30  #include <sys/dsl_dir.h>
  31   31  #include <sys/dsl_synctask.h>
  32   32  #include <sys/dnode.h>
  33   33  #include <sys/dmu_tx.h>
  34   34  #include <sys/dmu_objset.h>
  35   35  #include <sys/arc.h>
  36   36  #include <sys/zap.h>
  37   37  #include <sys/zio.h>
  38   38  #include <sys/zfs_context.h>
  39   39  #include <sys/fs/zfs.h>
  40   40  #include <sys/zfs_znode.h>
  41   41  #include <sys/spa_impl.h>
  42   42  #include <sys/vdev_impl.h>
  43   43  #include <sys/zil_impl.h>
  44   44  #include <sys/zio_checksum.h>
  45   45  #include <sys/ddt.h>
  46   46  #include <sys/sa.h>
  47   47  #include <sys/sa_impl.h>
  48   48  #include <sys/zfeature.h>
  49   49  #ifdef _KERNEL
  50   50  #include <sys/zfs_vfsops.h>
  51   51  #endif
  52   52  
  53   53  typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *, const zbookmark_t *);
  54   54  
  55   55  static scan_cb_t dsl_scan_defrag_cb;
  56   56  static scan_cb_t dsl_scan_scrub_cb;
  57   57  static scan_cb_t dsl_scan_remove_cb;
  58   58  static dsl_syncfunc_t dsl_scan_cancel_sync;
  59   59  static void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *tx);
  60   60  
  61   61  int zfs_top_maxinflight = 32;           /* maximum I/Os per top-level */
  62   62  int zfs_resilver_delay = 2;             /* number of ticks to delay resilver */
  63   63  int zfs_scrub_delay = 4;                /* number of ticks to delay scrub */
  64   64  int zfs_scan_idle = 50;                 /* idle window in clock ticks */
  65   65  
  66   66  int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
  67   67  int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
  68   68  int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver per txg */
  69   69  boolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
  70   70  boolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable srub prefetching */
  71   71  enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
  72   72  int dsl_scan_delay_completion = B_FALSE; /* set to delay scan completion */
  73   73  
  74   74  #define DSL_SCAN_IS_SCRUB_RESILVER(scn) \
  75   75          ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
  76   76          (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
  77   77  
  78   78  extern int zfs_txg_timeout;
  79   79  
  80   80  /* the order has to match pool_scan_type */
  81   81  static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
  82   82          NULL,
  83   83          dsl_scan_scrub_cb,      /* POOL_SCAN_SCRUB */
  84   84          dsl_scan_scrub_cb,      /* POOL_SCAN_RESILVER */
  85   85  };
  86   86  
  87   87  int
  88   88  dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
  89   89  {
  90   90          int err;
  91   91          dsl_scan_t *scn;
  92   92          spa_t *spa = dp->dp_spa;
  93   93          uint64_t f;
  94   94  
  95   95          scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
  96   96          scn->scn_dp = dp;
  97   97  
  98   98          err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
  99   99              "scrub_func", sizeof (uint64_t), 1, &f);
 100  100          if (err == 0) {
 101  101                  /*
 102  102                   * There was an old-style scrub in progress.  Restart a
 103  103                   * new-style scrub from the beginning.
 104  104                   */
 105  105                  scn->scn_restart_txg = txg;
 106  106                  zfs_dbgmsg("old-style scrub was in progress; "
 107  107                      "restarting new-style scrub in txg %llu",
 108  108                      scn->scn_restart_txg);
 109  109  
 110  110                  /*
 111  111                   * Load the queue obj from the old location so that it
 112  112                   * can be freed by dsl_scan_done().
 113  113                   */
 114  114                  (void) zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
 115  115                      "scrub_queue", sizeof (uint64_t), 1,
 116  116                      &scn->scn_phys.scn_queue_obj);
 117  117          } else {
 118  118                  err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
 119  119                      DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
 120  120                      &scn->scn_phys);
 121  121                  if (err == ENOENT)
 122  122                          return (0);
 123  123                  else if (err)
 124  124                          return (err);
 125  125  
 126  126                  if (scn->scn_phys.scn_state == DSS_SCANNING &&
 127  127                      spa_prev_software_version(dp->dp_spa) < SPA_VERSION_SCAN) {
 128  128                          /*
 129  129                           * A new-type scrub was in progress on an old
 130  130                           * pool, and the pool was accessed by old
 131  131                           * software.  Restart from the beginning, since
 132  132                           * the old software may have changed the pool in
 133  133                           * the meantime.
 134  134                           */
 135  135                          scn->scn_restart_txg = txg;
 136  136                          zfs_dbgmsg("new-style scrub was modified "
 137  137                              "by old software; restarting in txg %llu",
 138  138                              scn->scn_restart_txg);
 139  139                  }
 140  140          }
 141  141  
 142  142          spa_scan_stat_init(spa);
 143  143          return (0);
 144  144  }
 145  145  
 146  146  void
 147  147  dsl_scan_fini(dsl_pool_t *dp)
 148  148  {
 149  149          if (dp->dp_scan) {
 150  150                  kmem_free(dp->dp_scan, sizeof (dsl_scan_t));
 151  151                  dp->dp_scan = NULL;
 152  152          }
 153  153  }
 154  154  
 155  155  /* ARGSUSED */
 156  156  static int
 157  157  dsl_scan_setup_check(void *arg1, void *arg2, dmu_tx_t *tx)
 158  158  {
 159  159          dsl_scan_t *scn = arg1;
 160  160  
 161  161          if (scn->scn_phys.scn_state == DSS_SCANNING)
 162  162                  return (EBUSY);
 163  163  
 164  164          return (0);
 165  165  }
 166  166  
 167  167  /* ARGSUSED */
 168  168  static void
 169  169  dsl_scan_setup_sync(void *arg1, void *arg2, dmu_tx_t *tx)
 170  170  {
 171  171          dsl_scan_t *scn = arg1;
 172  172          pool_scan_func_t *funcp = arg2;
 173  173          dmu_object_type_t ot = 0;
 174  174          dsl_pool_t *dp = scn->scn_dp;
 175  175          spa_t *spa = dp->dp_spa;
 176  176  
 177  177          ASSERT(scn->scn_phys.scn_state != DSS_SCANNING);
 178  178          ASSERT(*funcp > POOL_SCAN_NONE && *funcp < POOL_SCAN_FUNCS);
 179  179          bzero(&scn->scn_phys, sizeof (scn->scn_phys));
 180  180          scn->scn_phys.scn_func = *funcp;
 181  181          scn->scn_phys.scn_state = DSS_SCANNING;
 182  182          scn->scn_phys.scn_min_txg = 0;
 183  183          scn->scn_phys.scn_max_txg = tx->tx_txg;
 184  184          scn->scn_phys.scn_ddt_class_max = DDT_CLASSES - 1; /* the entire DDT */
 185  185          scn->scn_phys.scn_start_time = gethrestime_sec();
 186  186          scn->scn_phys.scn_errors = 0;
 187  187          scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
 188  188          scn->scn_restart_txg = 0;
 189  189          spa_scan_stat_init(spa);
 190  190  
 191  191          if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
 192  192                  scn->scn_phys.scn_ddt_class_max = zfs_scrub_ddt_class_max;
 193  193  
 194  194                  /* rewrite all disk labels */
 195  195                  vdev_config_dirty(spa->spa_root_vdev);
 196  196  
 197  197                  if (vdev_resilver_needed(spa->spa_root_vdev,
 198  198                      &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
 199  199                          spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
 200  200                  } else {
 201  201                          spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START);
 202  202                  }
 203  203  
 204  204                  spa->spa_scrub_started = B_TRUE;
 205  205                  /*
 206  206                   * If this is an incremental scrub, limit the DDT scrub phase
 207  207                   * to just the auto-ditto class (for correctness); the rest
 208  208                   * of the scrub should go faster using top-down pruning.
 209  209                   */
 210  210                  if (scn->scn_phys.scn_min_txg > TXG_INITIAL)
 211  211                          scn->scn_phys.scn_ddt_class_max = DDT_CLASS_DITTO;
 212  212  
 213  213          }
 214  214  
 215  215          /* back to the generic stuff */
 216  216  
 217  217          if (dp->dp_blkstats == NULL) {
 218  218                  dp->dp_blkstats =
 219  219                      kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
 220  220          }
 221  221          bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
 222  222  
 223  223          if (spa_version(spa) < SPA_VERSION_DSL_SCRUB)
 224  224                  ot = DMU_OT_ZAP_OTHER;
 225  225  
 226  226          scn->scn_phys.scn_queue_obj = zap_create(dp->dp_meta_objset,
 227  227              ot ? ot : DMU_OT_SCAN_QUEUE, DMU_OT_NONE, 0, tx);
 228  228  
 229  229          dsl_scan_sync_state(scn, tx);
 230  230  
 231  231          spa_history_log_internal(spa, "scan setup", tx,
 232  232              "func=%u mintxg=%llu maxtxg=%llu",
 233  233              *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
 234  234  }
 235  235  
 236  236  /* ARGSUSED */
 237  237  static void
 238  238  dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
 239  239  {
 240  240          static const char *old_names[] = {
 241  241                  "scrub_bookmark",
 242  242                  "scrub_ddt_bookmark",
 243  243                  "scrub_ddt_class_max",
 244  244                  "scrub_queue",
 245  245                  "scrub_min_txg",
 246  246                  "scrub_max_txg",
 247  247                  "scrub_func",
 248  248                  "scrub_errors",
 249  249                  NULL
 250  250          };
 251  251  
 252  252          dsl_pool_t *dp = scn->scn_dp;
 253  253          spa_t *spa = dp->dp_spa;
 254  254          int i;
 255  255  
 256  256          /* Remove any remnants of an old-style scrub. */
 257  257          for (i = 0; old_names[i]; i++) {
 258  258                  (void) zap_remove(dp->dp_meta_objset,
 259  259                      DMU_POOL_DIRECTORY_OBJECT, old_names[i], tx);
 260  260          }
 261  261  
 262  262          if (scn->scn_phys.scn_queue_obj != 0) {
 263  263                  VERIFY(0 == dmu_object_free(dp->dp_meta_objset,
 264  264                      scn->scn_phys.scn_queue_obj, tx));
 265  265                  scn->scn_phys.scn_queue_obj = 0;
 266  266          }
 267  267  
 268  268          /*
 269  269           * If we were "restarted" from a stopped state, don't bother
 270  270           * with anything else.
 271  271           */
 272  272          if (scn->scn_phys.scn_state != DSS_SCANNING)
 273  273                  return;
 274  274  
 275  275          if (complete)
 276  276                  scn->scn_phys.scn_state = DSS_FINISHED;
 277  277          else
 278  278                  scn->scn_phys.scn_state = DSS_CANCELED;
 279  279  
 280  280          spa_history_log_internal(spa, "scan done", tx,
 281  281              "complete=%u", complete);
 282  282  
 283  283          if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
 284  284                  mutex_enter(&spa->spa_scrub_lock);
 285  285                  while (spa->spa_scrub_inflight > 0) {
 286  286                          cv_wait(&spa->spa_scrub_io_cv,
 287  287                              &spa->spa_scrub_lock);
 288  288                  }
 289  289                  mutex_exit(&spa->spa_scrub_lock);
 290  290                  spa->spa_scrub_started = B_FALSE;
 291  291                  spa->spa_scrub_active = B_FALSE;
 292  292  
 293  293                  /*
 294  294                   * If the scrub/resilver completed, update all DTLs to
 295  295                   * reflect this.  Whether it succeeded or not, vacate
 296  296                   * all temporary scrub DTLs.
 297  297                   */
 298  298                  vdev_dtl_reassess(spa->spa_root_vdev, tx->tx_txg,
 299  299                      complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
 300  300                  if (complete) {
 301  301                          spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
 302  302                              ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
 303  303                  }
 304  304                  spa_errlog_rotate(spa);
 305  305  
 306  306                  /*
 307  307                   * We may have finished replacing a device.
 308  308                   * Let the async thread assess this and handle the detach.
 309  309                   */
 310  310                  spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
 311  311          }
 312  312  
 313  313          scn->scn_phys.scn_end_time = gethrestime_sec();
 314  314  }
 315  315  
 316  316  /* ARGSUSED */
 317  317  static int
 318  318  dsl_scan_cancel_check(void *arg1, void *arg2, dmu_tx_t *tx)
 319  319  {
 320  320          dsl_scan_t *scn = arg1;
 321  321  
 322  322          if (scn->scn_phys.scn_state != DSS_SCANNING)
 323  323                  return (ENOENT);
 324  324          return (0);
 325  325  }
 326  326  
 327  327  /* ARGSUSED */
 328  328  static void
 329  329  dsl_scan_cancel_sync(void *arg1, void *arg2, dmu_tx_t *tx)
 330  330  {
 331  331          dsl_scan_t *scn = arg1;
 332  332  
 333  333          dsl_scan_done(scn, B_FALSE, tx);
 334  334          dsl_scan_sync_state(scn, tx);
 335  335  }
 336  336  
 337  337  int
 338  338  dsl_scan_cancel(dsl_pool_t *dp)
 339  339  {
 340  340          boolean_t complete = B_FALSE;
 341  341          int err;
 342  342  
 343  343          err = dsl_sync_task_do(dp, dsl_scan_cancel_check,
 344  344              dsl_scan_cancel_sync, dp->dp_scan, &complete, 3);
 345  345          return (err);
 346  346  }
 347  347  
 348  348  static void dsl_scan_visitbp(blkptr_t *bp,
 349  349      const zbookmark_t *zb, dnode_phys_t *dnp, arc_buf_t *pbuf,
 350  350      dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
 351  351      dmu_tx_t *tx);
 352  352  static void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds,
 353  353      dmu_objset_type_t ostype,
 354  354      dnode_phys_t *dnp, arc_buf_t *buf, uint64_t object, dmu_tx_t *tx);
 355  355  
 356  356  void
 357  357  dsl_free(dsl_pool_t *dp, uint64_t txg, const blkptr_t *bp)
 358  358  {
 359  359          zio_free(dp->dp_spa, txg, bp);
 360  360  }
 361  361  
 362  362  void
 363  363  dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
 364  364  {
 365  365          ASSERT(dsl_pool_sync_context(dp));
 366  366          zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags));
 367  367  }
 368  368  
 369  369  int
 370  370  dsl_read(zio_t *pio, spa_t *spa, const blkptr_t *bpp, arc_buf_t *pbuf,
 371  371      arc_done_func_t *done, void *private, int priority, int zio_flags,
 372  372      uint32_t *arc_flags, const zbookmark_t *zb)
 373  373  {
 374  374          return (arc_read(pio, spa, bpp, pbuf, done, private,
 375  375              priority, zio_flags, arc_flags, zb));
 376  376  }
 377  377  
 378  378  int
 379  379  dsl_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bpp,
 380  380      arc_done_func_t *done, void *private, int priority, int zio_flags,
 381  381      uint32_t *arc_flags, const zbookmark_t *zb)
 382  382  {
 383  383          return (arc_read_nolock(pio, spa, bpp, done, private,
 384  384              priority, zio_flags, arc_flags, zb));
 385  385  }
 386  386  
 387  387  static uint64_t
 388  388  dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
 389  389  {
 390  390          uint64_t smt = ds->ds_dir->dd_pool->dp_scan->scn_phys.scn_max_txg;
 391  391          if (dsl_dataset_is_snapshot(ds))
 392  392                  return (MIN(smt, ds->ds_phys->ds_creation_txg));
 393  393          return (smt);
 394  394  }
 395  395  
 396  396  static void
 397  397  dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
 398  398  {
 399  399          VERIFY(0 == zap_update(scn->scn_dp->dp_meta_objset,
 400  400              DMU_POOL_DIRECTORY_OBJECT,
 401  401              DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
 402  402              &scn->scn_phys, tx));
 403  403  }
 404  404  
 405  405  static boolean_t
 406  406  dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_t *zb)
 407  407  {
 408  408          uint64_t elapsed_nanosecs;
 409  409          int mintime;
 410  410  
 411  411          /* we never skip user/group accounting objects */
 412  412          if (zb && (int64_t)zb->zb_object < 0)
 413  413                  return (B_FALSE);
 414  414  
 415  415          if (scn->scn_pausing)
 416  416                  return (B_TRUE); /* we're already pausing */
 417  417  
 418  418          if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark))
 419  419                  return (B_FALSE); /* we're resuming */
 420  420  
 421  421          /* We only know how to resume from level-0 blocks. */
 422  422          if (zb && zb->zb_level != 0)
 423  423                  return (B_FALSE);
 424  424  
 425  425          mintime = (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) ?
 426  426              zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
 427  427          elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
 428  428          if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
 429  429              (elapsed_nanosecs / MICROSEC > mintime &&
 430  430              txg_sync_waiting(scn->scn_dp)) ||
 431  431              spa_shutting_down(scn->scn_dp->dp_spa)) {
 432  432                  if (zb) {
 433  433                          dprintf("pausing at bookmark %llx/%llx/%llx/%llx\n",
 434  434                              (longlong_t)zb->zb_objset,
 435  435                              (longlong_t)zb->zb_object,
 436  436                              (longlong_t)zb->zb_level,
 437  437                              (longlong_t)zb->zb_blkid);
 438  438                          scn->scn_phys.scn_bookmark = *zb;
 439  439                  }
 440  440                  dprintf("pausing at DDT bookmark %llx/%llx/%llx/%llx\n",
 441  441                      (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
 442  442                      (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
 443  443                      (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
 444  444                      (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
 445  445                  scn->scn_pausing = B_TRUE;
 446  446                  return (B_TRUE);
 447  447          }
 448  448          return (B_FALSE);
 449  449  }
 450  450  
 451  451  typedef struct zil_scan_arg {
 452  452          dsl_pool_t      *zsa_dp;
 453  453          zil_header_t    *zsa_zh;
 454  454  } zil_scan_arg_t;
 455  455  
 456  456  /* ARGSUSED */
 457  457  static int
 458  458  dsl_scan_zil_block(zilog_t *zilog, blkptr_t *bp, void *arg, uint64_t claim_txg)
 459  459  {
 460  460          zil_scan_arg_t *zsa = arg;
 461  461          dsl_pool_t *dp = zsa->zsa_dp;
 462  462          dsl_scan_t *scn = dp->dp_scan;
 463  463          zil_header_t *zh = zsa->zsa_zh;
 464  464          zbookmark_t zb;
 465  465  
 466  466          if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
 467  467                  return (0);
 468  468  
 469  469          /*
 470  470           * One block ("stubby") can be allocated a long time ago; we
 471  471           * want to visit that one because it has been allocated
 472  472           * (on-disk) even if it hasn't been claimed (even though for
 473  473           * scrub there's nothing to do to it).
 474  474           */
 475  475          if (claim_txg == 0 && bp->blk_birth >= spa_first_txg(dp->dp_spa))
 476  476                  return (0);
 477  477  
 478  478          SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
 479  479              ZB_ZIL_OBJECT, ZB_ZIL_LEVEL, bp->blk_cksum.zc_word[ZIL_ZC_SEQ]);
 480  480  
 481  481          VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
 482  482          return (0);
 483  483  }
 484  484  
 485  485  /* ARGSUSED */
 486  486  static int
 487  487  dsl_scan_zil_record(zilog_t *zilog, lr_t *lrc, void *arg, uint64_t claim_txg)
 488  488  {
 489  489          if (lrc->lrc_txtype == TX_WRITE) {
 490  490                  zil_scan_arg_t *zsa = arg;
 491  491                  dsl_pool_t *dp = zsa->zsa_dp;
 492  492                  dsl_scan_t *scn = dp->dp_scan;
 493  493                  zil_header_t *zh = zsa->zsa_zh;
 494  494                  lr_write_t *lr = (lr_write_t *)lrc;
 495  495                  blkptr_t *bp = &lr->lr_blkptr;
 496  496                  zbookmark_t zb;
 497  497  
 498  498                  if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
 499  499                          return (0);
 500  500  
 501  501                  /*
 502  502                   * birth can be < claim_txg if this record's txg is
 503  503                   * already txg sync'ed (but this log block contains
 504  504                   * other records that are not synced)
 505  505                   */
 506  506                  if (claim_txg == 0 || bp->blk_birth < claim_txg)
 507  507                          return (0);
 508  508  
 509  509                  SET_BOOKMARK(&zb, zh->zh_log.blk_cksum.zc_word[ZIL_ZC_OBJSET],
 510  510                      lr->lr_foid, ZB_ZIL_LEVEL,
 511  511                      lr->lr_offset / BP_GET_LSIZE(bp));
 512  512  
 513  513                  VERIFY(0 == scan_funcs[scn->scn_phys.scn_func](dp, bp, &zb));
 514  514          }
 515  515          return (0);
 516  516  }
 517  517  
 518  518  static void
 519  519  dsl_scan_zil(dsl_pool_t *dp, zil_header_t *zh)
 520  520  {
 521  521          uint64_t claim_txg = zh->zh_claim_txg;
 522  522          zil_scan_arg_t zsa = { dp, zh };
 523  523          zilog_t *zilog;
 524  524  
 525  525          /*
 526  526           * We only want to visit blocks that have been claimed but not yet
 527  527           * replayed (or, in read-only mode, blocks that *would* be claimed).
 528  528           */
 529  529          if (claim_txg == 0 && spa_writeable(dp->dp_spa))
 530  530                  return;
 531  531  
 532  532          zilog = zil_alloc(dp->dp_meta_objset, zh);
 533  533  
 534  534          (void) zil_parse(zilog, dsl_scan_zil_block, dsl_scan_zil_record, &zsa,
 535  535              claim_txg);
 536  536  
 537  537          zil_free(zilog);
 538  538  }
 539  539  
 540  540  /* ARGSUSED */
 541  541  static void
 542  542  dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
 543  543      uint64_t objset, uint64_t object, uint64_t blkid)
 544  544  {
 545  545          zbookmark_t czb;
 546  546          uint32_t flags = ARC_NOWAIT | ARC_PREFETCH;
 547  547  
 548  548          if (zfs_no_scrub_prefetch)
 549  549                  return;
 550  550  
 551  551          if (BP_IS_HOLE(bp) || bp->blk_birth <= scn->scn_phys.scn_min_txg ||
 552  552              (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_DNODE))
 553  553                  return;
 554  554  
 555  555          SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
 556  556  
 557  557          /*
 558  558           * XXX need to make sure all of these arc_read() prefetches are
 559  559           * done before setting xlateall (similar to dsl_read())
 560  560           */
 561  561          (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
 562  562              buf, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
 563  563              ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
 564  564  }
 565  565  
 566  566  static boolean_t
 567  567  dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
 568  568      const zbookmark_t *zb)
 569  569  {
 570  570          /*
 571  571           * We never skip over user/group accounting objects (obj<0)
 572  572           */
 573  573          if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
 574  574              (int64_t)zb->zb_object >= 0) {
 575  575                  /*
 576  576                   * If we already visited this bp & everything below (in
 577  577                   * a prior txg sync), don't bother doing it again.
 578  578                   */
 579  579                  if (zbookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
 580  580                          return (B_TRUE);
 581  581  
 582  582                  /*
 583  583                   * If we found the block we're trying to resume from, or
 584  584                   * we went past it to a different object, zero it out to
 585  585                   * indicate that it's OK to start checking for pausing
 586  586                   * again.
 587  587                   */
 588  588                  if (bcmp(zb, &scn->scn_phys.scn_bookmark, sizeof (*zb)) == 0 ||
 589  589                      zb->zb_object > scn->scn_phys.scn_bookmark.zb_object) {
 590  590                          dprintf("resuming at %llx/%llx/%llx/%llx\n",
 591  591                              (longlong_t)zb->zb_objset,
 592  592                              (longlong_t)zb->zb_object,
 593  593                              (longlong_t)zb->zb_level,
 594  594                              (longlong_t)zb->zb_blkid);
 595  595                          bzero(&scn->scn_phys.scn_bookmark, sizeof (*zb));
 596  596                  }
 597  597          }
 598  598          return (B_FALSE);
 599  599  }
 600  600  
 601  601  /*
 602  602   * Return nonzero on i/o error.
 603  603   * Return new buf to write out in *bufp.
 604  604   */
 605  605  static int
 606  606  dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
 607  607      dnode_phys_t *dnp, const blkptr_t *bp,
 608  608      const zbookmark_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
 609  609  {
 610  610          dsl_pool_t *dp = scn->scn_dp;
 611  611          int zio_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD;
 612  612          int err;
 613  613  
 614  614          if (BP_GET_LEVEL(bp) > 0) {
 615  615                  uint32_t flags = ARC_WAIT;
 616  616                  int i;
 617  617                  blkptr_t *cbp;
 618  618                  int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
 619  619  
 620  620                  err = arc_read_nolock(NULL, dp->dp_spa, bp,
 621  621                      arc_getbuf_func, bufp,
 622  622                      ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
 623  623                  if (err) {
 624  624                          scn->scn_phys.scn_errors++;
 625  625                          return (err);
 626  626                  }
 627  627                  for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
 628  628                          dsl_scan_prefetch(scn, *bufp, cbp, zb->zb_objset,
 629  629                              zb->zb_object, zb->zb_blkid * epb + i);
 630  630                  }
 631  631                  for (i = 0, cbp = (*bufp)->b_data; i < epb; i++, cbp++) {
 632  632                          zbookmark_t czb;
 633  633  
 634  634                          SET_BOOKMARK(&czb, zb->zb_objset, zb->zb_object,
 635  635                              zb->zb_level - 1,
 636  636                              zb->zb_blkid * epb + i);
 637  637                          dsl_scan_visitbp(cbp, &czb, dnp,
 638  638                              *bufp, ds, scn, ostype, tx);
 639  639                  }
 640  640          } else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
 641  641                  uint32_t flags = ARC_WAIT;
 642  642  
 643  643                  err = arc_read_nolock(NULL, dp->dp_spa, bp,
 644  644                      arc_getbuf_func, bufp,
 645  645                      ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
 646  646                  if (err) {
 647  647                          scn->scn_phys.scn_errors++;
 648  648                          return (err);
 649  649                  }
 650  650          } else if (BP_GET_TYPE(bp) == DMU_OT_DNODE) {
 651  651                  uint32_t flags = ARC_WAIT;
 652  652                  dnode_phys_t *cdnp;
 653  653                  int i, j;
 654  654                  int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
 655  655  
 656  656                  err = arc_read_nolock(NULL, dp->dp_spa, bp,
 657  657                      arc_getbuf_func, bufp,
 658  658                      ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
 659  659                  if (err) {
 660  660                          scn->scn_phys.scn_errors++;
 661  661                          return (err);
 662  662                  }
 663  663                  for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
 664  664                          for (j = 0; j < cdnp->dn_nblkptr; j++) {
 665  665                                  blkptr_t *cbp = &cdnp->dn_blkptr[j];
 666  666                                  dsl_scan_prefetch(scn, *bufp, cbp,
 667  667                                      zb->zb_objset, zb->zb_blkid * epb + i, j);
 668  668                          }
 669  669                  }
 670  670                  for (i = 0, cdnp = (*bufp)->b_data; i < epb; i++, cdnp++) {
 671  671                          dsl_scan_visitdnode(scn, ds, ostype,
 672  672                              cdnp, *bufp, zb->zb_blkid * epb + i, tx);
 673  673                  }
 674  674  
 675  675          } else if (BP_GET_TYPE(bp) == DMU_OT_OBJSET) {
 676  676                  uint32_t flags = ARC_WAIT;
 677  677                  objset_phys_t *osp;
 678  678  
 679  679                  err = arc_read_nolock(NULL, dp->dp_spa, bp,
 680  680                      arc_getbuf_func, bufp,
 681  681                      ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
 682  682                  if (err) {
 683  683                          scn->scn_phys.scn_errors++;
 684  684                          return (err);
 685  685                  }
 686  686  
 687  687                  osp = (*bufp)->b_data;
 688  688  
 689  689                  dsl_scan_visitdnode(scn, ds, osp->os_type,
 690  690                      &osp->os_meta_dnode, *bufp, DMU_META_DNODE_OBJECT, tx);
 691  691  
 692  692                  if (OBJSET_BUF_HAS_USERUSED(*bufp)) {
 693  693                          /*
 694  694                           * We also always visit user/group accounting
 695  695                           * objects, and never skip them, even if we are
 696  696                           * pausing.  This is necessary so that the space
 697  697                           * deltas from this txg get integrated.
 698  698                           */
 699  699                          dsl_scan_visitdnode(scn, ds, osp->os_type,
 700  700                              &osp->os_groupused_dnode, *bufp,
 701  701                              DMU_GROUPUSED_OBJECT, tx);
 702  702                          dsl_scan_visitdnode(scn, ds, osp->os_type,
 703  703                              &osp->os_userused_dnode, *bufp,
 704  704                              DMU_USERUSED_OBJECT, tx);
 705  705                  }
 706  706          }
 707  707  
 708  708          return (0);
 709  709  }
 710  710  
 711  711  static void
 712  712  dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
 713  713      dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
 714  714      uint64_t object, dmu_tx_t *tx)
 715  715  {
 716  716          int j;
 717  717  
 718  718          for (j = 0; j < dnp->dn_nblkptr; j++) {
 719  719                  zbookmark_t czb;
 720  720  
 721  721                  SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
 722  722                      dnp->dn_nlevels - 1, j);
 723  723                  dsl_scan_visitbp(&dnp->dn_blkptr[j],
 724  724                      &czb, dnp, buf, ds, scn, ostype, tx);
 725  725          }
 726  726  
 727  727          if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) {
 728  728                  zbookmark_t czb;
 729  729                  SET_BOOKMARK(&czb, ds ? ds->ds_object : 0, object,
 730  730                      0, DMU_SPILL_BLKID);
 731  731                  dsl_scan_visitbp(&dnp->dn_spill,
 732  732                      &czb, dnp, buf, ds, scn, ostype, tx);
 733  733          }
 734  734  }
 735  735  
 736  736  /*
 737  737   * The arguments are in this order because mdb can only print the
 738  738   * first 5; we want them to be useful.
 739  739   */
 740  740  static void
 741  741  dsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
 742  742      dnode_phys_t *dnp, arc_buf_t *pbuf,
 743  743      dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
 744  744      dmu_tx_t *tx)
 745  745  {
 746  746          dsl_pool_t *dp = scn->scn_dp;
 747  747          arc_buf_t *buf = NULL;
 748  748          blkptr_t bp_toread = *bp;
 749  749  
 750  750          /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
 751  751  
 752  752          if (dsl_scan_check_pause(scn, zb))
 753  753                  return;
 754  754  
 755  755          if (dsl_scan_check_resume(scn, dnp, zb))
 756  756                  return;
 757  757  
 758  758          if (bp->blk_birth == 0)
 759  759                  return;
 760  760  
 761  761          scn->scn_visited_this_txg++;
 762  762  
 763  763          dprintf_bp(bp,
 764  764              "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
 765  765              ds, ds ? ds->ds_object : 0,
 766  766              zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
 767  767              pbuf, bp);
 768  768  
 769  769          if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
 770  770                  return;
 771  771  
 772  772          if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx,
 773  773              &buf) != 0)
 774  774                  return;
 775  775  
 776  776          /*
 777  777           * If dsl_scan_ddt() has aready visited this block, it will have
 778  778           * already done any translations or scrubbing, so don't call the
 779  779           * callback again.
 780  780           */
 781  781          if (ddt_class_contains(dp->dp_spa,
 782  782              scn->scn_phys.scn_ddt_class_max, bp)) {
 783  783                  ASSERT(buf == NULL);
 784  784                  return;
 785  785          }
 786  786  
 787  787          /*
 788  788           * If this block is from the future (after cur_max_txg), then we
 789  789           * are doing this on behalf of a deleted snapshot, and we will
 790  790           * revisit the future block on the next pass of this dataset.
 791  791           * Don't scan it now unless we need to because something
 792  792           * under it was modified.
 793  793           */
 794  794          if (bp->blk_birth <= scn->scn_phys.scn_cur_max_txg) {
 795  795                  scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
 796  796          }
 797  797          if (buf)
 798  798                  (void) arc_buf_remove_ref(buf, &buf);
 799  799  }
 800  800  
 801  801  static void
 802  802  dsl_scan_visit_rootbp(dsl_scan_t *scn, dsl_dataset_t *ds, blkptr_t *bp,
 803  803      dmu_tx_t *tx)
 804  804  {
 805  805          zbookmark_t zb;
 806  806  
 807  807          SET_BOOKMARK(&zb, ds ? ds->ds_object : DMU_META_OBJSET,
 808  808              ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
 809  809          dsl_scan_visitbp(bp, &zb, NULL, NULL,
 810  810              ds, scn, DMU_OST_NONE, tx);
 811  811  
 812  812          dprintf_ds(ds, "finished scan%s", "");
 813  813  }
 814  814  
 815  815  void
 816  816  dsl_scan_ds_destroyed(dsl_dataset_t *ds, dmu_tx_t *tx)
 817  817  {
 818  818          dsl_pool_t *dp = ds->ds_dir->dd_pool;
 819  819          dsl_scan_t *scn = dp->dp_scan;
 820  820          uint64_t mintxg;
 821  821  
 822  822          if (scn->scn_phys.scn_state != DSS_SCANNING)
 823  823                  return;
 824  824  
 825  825          if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
 826  826                  if (dsl_dataset_is_snapshot(ds)) {
 827  827                          /* Note, scn_cur_{min,max}_txg stays the same. */
 828  828                          scn->scn_phys.scn_bookmark.zb_objset =
 829  829                              ds->ds_phys->ds_next_snap_obj;
 830  830                          zfs_dbgmsg("destroying ds %llu; currently traversing; "
 831  831                              "reset zb_objset to %llu",
 832  832                              (u_longlong_t)ds->ds_object,
 833  833                              (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
 834  834                          scn->scn_phys.scn_flags |= DSF_VISIT_DS_AGAIN;
  
    | 
      ↓ open down ↓ | 
    834 lines elided | 
    
      ↑ open up ↑ | 
  
 835  835                  } else {
 836  836                          SET_BOOKMARK(&scn->scn_phys.scn_bookmark,
 837  837                              ZB_DESTROYED_OBJSET, 0, 0, 0);
 838  838                          zfs_dbgmsg("destroying ds %llu; currently traversing; "
 839  839                              "reset bookmark to -1,0,0,0",
 840  840                              (u_longlong_t)ds->ds_object);
 841  841                  }
 842  842          } else if (zap_lookup_int_key(dp->dp_meta_objset,
 843  843              scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
 844  844                  ASSERT3U(ds->ds_phys->ds_num_children, <=, 1);
 845      -                VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
      845 +                VERIFY0(zap_remove_int(dp->dp_meta_objset,
 846  846                      scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
 847  847                  if (dsl_dataset_is_snapshot(ds)) {
 848  848                          /*
 849  849                           * We keep the same mintxg; it could be >
 850  850                           * ds_creation_txg if the previous snapshot was
 851  851                           * deleted too.
 852  852                           */
 853  853                          VERIFY(zap_add_int_key(dp->dp_meta_objset,
 854  854                              scn->scn_phys.scn_queue_obj,
 855  855                              ds->ds_phys->ds_next_snap_obj, mintxg, tx) == 0);
 856  856                          zfs_dbgmsg("destroying ds %llu; in queue; "
 857  857                              "replacing with %llu",
 858  858                              (u_longlong_t)ds->ds_object,
 859  859                              (u_longlong_t)ds->ds_phys->ds_next_snap_obj);
 860  860                  } else {
 861  861                          zfs_dbgmsg("destroying ds %llu; in queue; removing",
 862  862                              (u_longlong_t)ds->ds_object);
 863  863                  }
 864  864          } else {
 865  865                  zfs_dbgmsg("destroying ds %llu; ignoring",
 866  866                      (u_longlong_t)ds->ds_object);
 867  867          }
 868  868  
 869  869          /*
 870  870           * dsl_scan_sync() should be called after this, and should sync
 871  871           * out our changed state, but just to be safe, do it here.
 872  872           */
 873  873          dsl_scan_sync_state(scn, tx);
 874  874  }
 875  875  
 876  876  void
 877  877  dsl_scan_ds_snapshotted(dsl_dataset_t *ds, dmu_tx_t *tx)
 878  878  {
 879  879          dsl_pool_t *dp = ds->ds_dir->dd_pool;
 880  880          dsl_scan_t *scn = dp->dp_scan;
 881  881          uint64_t mintxg;
 882  882  
 883  883          if (scn->scn_phys.scn_state != DSS_SCANNING)
 884  884                  return;
 885  885  
 886  886          ASSERT(ds->ds_phys->ds_prev_snap_obj != 0);
  
    | 
      ↓ open down ↓ | 
    31 lines elided | 
    
      ↑ open up ↑ | 
  
 887  887  
 888  888          if (scn->scn_phys.scn_bookmark.zb_objset == ds->ds_object) {
 889  889                  scn->scn_phys.scn_bookmark.zb_objset =
 890  890                      ds->ds_phys->ds_prev_snap_obj;
 891  891                  zfs_dbgmsg("snapshotting ds %llu; currently traversing; "
 892  892                      "reset zb_objset to %llu",
 893  893                      (u_longlong_t)ds->ds_object,
 894  894                      (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
 895  895          } else if (zap_lookup_int_key(dp->dp_meta_objset,
 896  896              scn->scn_phys.scn_queue_obj, ds->ds_object, &mintxg) == 0) {
 897      -                VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
      897 +                VERIFY0(zap_remove_int(dp->dp_meta_objset,
 898  898                      scn->scn_phys.scn_queue_obj, ds->ds_object, tx));
 899  899                  VERIFY(zap_add_int_key(dp->dp_meta_objset,
 900  900                      scn->scn_phys.scn_queue_obj,
 901  901                      ds->ds_phys->ds_prev_snap_obj, mintxg, tx) == 0);
 902  902                  zfs_dbgmsg("snapshotting ds %llu; in queue; "
 903  903                      "replacing with %llu",
 904  904                      (u_longlong_t)ds->ds_object,
 905  905                      (u_longlong_t)ds->ds_phys->ds_prev_snap_obj);
 906  906          }
 907  907          dsl_scan_sync_state(scn, tx);
 908  908  }
 909  909  
 910  910  void
 911  911  dsl_scan_ds_clone_swapped(dsl_dataset_t *ds1, dsl_dataset_t *ds2, dmu_tx_t *tx)
 912  912  {
 913  913          dsl_pool_t *dp = ds1->ds_dir->dd_pool;
 914  914          dsl_scan_t *scn = dp->dp_scan;
 915  915          uint64_t mintxg;
 916  916  
 917  917          if (scn->scn_phys.scn_state != DSS_SCANNING)
 918  918                  return;
 919  919  
 920  920          if (scn->scn_phys.scn_bookmark.zb_objset == ds1->ds_object) {
 921  921                  scn->scn_phys.scn_bookmark.zb_objset = ds2->ds_object;
 922  922                  zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
 923  923                      "reset zb_objset to %llu",
 924  924                      (u_longlong_t)ds1->ds_object,
 925  925                      (u_longlong_t)ds2->ds_object);
 926  926          } else if (scn->scn_phys.scn_bookmark.zb_objset == ds2->ds_object) {
 927  927                  scn->scn_phys.scn_bookmark.zb_objset = ds1->ds_object;
 928  928                  zfs_dbgmsg("clone_swap ds %llu; currently traversing; "
 929  929                      "reset zb_objset to %llu",
  
    | 
      ↓ open down ↓ | 
    22 lines elided | 
    
      ↑ open up ↑ | 
  
 930  930                      (u_longlong_t)ds2->ds_object,
 931  931                      (u_longlong_t)ds1->ds_object);
 932  932          }
 933  933  
 934  934          if (zap_lookup_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
 935  935              ds1->ds_object, &mintxg) == 0) {
 936  936                  int err;
 937  937  
 938  938                  ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
 939  939                  ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
 940      -                VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
      940 +                VERIFY0(zap_remove_int(dp->dp_meta_objset,
 941  941                      scn->scn_phys.scn_queue_obj, ds1->ds_object, tx));
 942  942                  err = zap_add_int_key(dp->dp_meta_objset,
 943  943                      scn->scn_phys.scn_queue_obj, ds2->ds_object, mintxg, tx);
 944  944                  VERIFY(err == 0 || err == EEXIST);
 945  945                  if (err == EEXIST) {
 946  946                          /* Both were there to begin with */
 947  947                          VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
 948  948                              scn->scn_phys.scn_queue_obj,
 949  949                              ds1->ds_object, mintxg, tx));
 950  950                  }
 951  951                  zfs_dbgmsg("clone_swap ds %llu; in queue; "
 952  952                      "replacing with %llu",
 953  953                      (u_longlong_t)ds1->ds_object,
 954  954                      (u_longlong_t)ds2->ds_object);
 955  955          } else if (zap_lookup_int_key(dp->dp_meta_objset,
 956  956              scn->scn_phys.scn_queue_obj, ds2->ds_object, &mintxg) == 0) {
 957  957                  ASSERT3U(mintxg, ==, ds1->ds_phys->ds_prev_snap_txg);
 958  958                  ASSERT3U(mintxg, ==, ds2->ds_phys->ds_prev_snap_txg);
 959      -                VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
      959 +                VERIFY0(zap_remove_int(dp->dp_meta_objset,
 960  960                      scn->scn_phys.scn_queue_obj, ds2->ds_object, tx));
 961  961                  VERIFY(0 == zap_add_int_key(dp->dp_meta_objset,
 962  962                      scn->scn_phys.scn_queue_obj, ds1->ds_object, mintxg, tx));
 963  963                  zfs_dbgmsg("clone_swap ds %llu; in queue; "
 964  964                      "replacing with %llu",
 965  965                      (u_longlong_t)ds2->ds_object,
 966  966                      (u_longlong_t)ds1->ds_object);
 967  967          }
 968  968  
 969  969          dsl_scan_sync_state(scn, tx);
 970  970  }
 971  971  
 972  972  struct enqueue_clones_arg {
 973  973          dmu_tx_t *tx;
 974  974          uint64_t originobj;
 975  975  };
 976  976  
 977  977  /* ARGSUSED */
 978  978  static int
 979  979  enqueue_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
 980  980  {
 981  981          struct enqueue_clones_arg *eca = arg;
 982  982          dsl_dataset_t *ds;
 983  983          int err;
 984  984          dsl_pool_t *dp = spa->spa_dsl_pool;
 985  985          dsl_scan_t *scn = dp->dp_scan;
 986  986  
 987  987          err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
 988  988          if (err)
 989  989                  return (err);
 990  990  
 991  991          if (ds->ds_dir->dd_phys->dd_origin_obj == eca->originobj) {
 992  992                  while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
 993  993                          dsl_dataset_t *prev;
 994  994                          err = dsl_dataset_hold_obj(dp,
 995  995                              ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
 996  996  
 997  997                          dsl_dataset_rele(ds, FTAG);
 998  998                          if (err)
 999  999                                  return (err);
1000 1000                          ds = prev;
1001 1001                  }
1002 1002                  VERIFY(zap_add_int_key(dp->dp_meta_objset,
1003 1003                      scn->scn_phys.scn_queue_obj, ds->ds_object,
1004 1004                      ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
1005 1005          }
1006 1006          dsl_dataset_rele(ds, FTAG);
  
    | 
      ↓ open down ↓ | 
    37 lines elided | 
    
      ↑ open up ↑ | 
  
1007 1007          return (0);
1008 1008  }
1009 1009  
1010 1010  static void
1011 1011  dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
1012 1012  {
1013 1013          dsl_pool_t *dp = scn->scn_dp;
1014 1014          dsl_dataset_t *ds;
1015 1015          objset_t *os;
1016 1016  
1017      -        VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
     1017 +        VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1018 1018  
1019 1019          if (dmu_objset_from_ds(ds, &os))
1020 1020                  goto out;
1021 1021  
1022 1022          /*
1023 1023           * Only the ZIL in the head (non-snapshot) is valid.  Even though
1024 1024           * snapshots can have ZIL block pointers (which may be the same
1025 1025           * BP as in the head), they must be ignored.  So we traverse the
1026 1026           * ZIL here, rather than in scan_recurse(), because the regular
1027 1027           * snapshot block-sharing rules don't apply to it.
1028 1028           */
1029 1029          if (DSL_SCAN_IS_SCRUB_RESILVER(scn) && !dsl_dataset_is_snapshot(ds))
1030 1030                  dsl_scan_zil(dp, &os->os_zil_header);
1031 1031  
1032 1032          /*
1033 1033           * Iterate over the bps in this ds.
1034 1034           */
1035 1035          dmu_buf_will_dirty(ds->ds_dbuf, tx);
1036 1036          dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
1037 1037  
1038 1038          char *dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_SLEEP);
1039 1039          dsl_dataset_name(ds, dsname);
1040 1040          zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
1041 1041              "pausing=%u",
1042 1042              (longlong_t)dsobj, dsname,
1043 1043              (longlong_t)scn->scn_phys.scn_cur_min_txg,
1044 1044              (longlong_t)scn->scn_phys.scn_cur_max_txg,
1045 1045              (int)scn->scn_pausing);
1046 1046          kmem_free(dsname, ZFS_MAXNAMELEN);
1047 1047  
1048 1048          if (scn->scn_pausing)
1049 1049                  goto out;
1050 1050  
1051 1051          /*
1052 1052           * We've finished this pass over this dataset.
1053 1053           */
1054 1054  
1055 1055          /*
1056 1056           * If we did not completely visit this dataset, do another pass.
1057 1057           */
1058 1058          if (scn->scn_phys.scn_flags & DSF_VISIT_DS_AGAIN) {
1059 1059                  zfs_dbgmsg("incomplete pass; visiting again");
1060 1060                  scn->scn_phys.scn_flags &= ~DSF_VISIT_DS_AGAIN;
1061 1061                  VERIFY(zap_add_int_key(dp->dp_meta_objset,
1062 1062                      scn->scn_phys.scn_queue_obj, ds->ds_object,
1063 1063                      scn->scn_phys.scn_cur_max_txg, tx) == 0);
1064 1064                  goto out;
1065 1065          }
1066 1066  
1067 1067          /*
1068 1068           * Add descendent datasets to work queue.
1069 1069           */
1070 1070          if (ds->ds_phys->ds_next_snap_obj != 0) {
1071 1071                  VERIFY(zap_add_int_key(dp->dp_meta_objset,
1072 1072                      scn->scn_phys.scn_queue_obj, ds->ds_phys->ds_next_snap_obj,
1073 1073                      ds->ds_phys->ds_creation_txg, tx) == 0);
1074 1074          }
1075 1075          if (ds->ds_phys->ds_num_children > 1) {
1076 1076                  boolean_t usenext = B_FALSE;
1077 1077                  if (ds->ds_phys->ds_next_clones_obj != 0) {
1078 1078                          uint64_t count;
1079 1079                          /*
1080 1080                           * A bug in a previous version of the code could
1081 1081                           * cause upgrade_clones_cb() to not set
1082 1082                           * ds_next_snap_obj when it should, leading to a
1083 1083                           * missing entry.  Therefore we can only use the
1084 1084                           * next_clones_obj when its count is correct.
1085 1085                           */
1086 1086                          int err = zap_count(dp->dp_meta_objset,
1087 1087                              ds->ds_phys->ds_next_clones_obj, &count);
1088 1088                          if (err == 0 &&
1089 1089                              count == ds->ds_phys->ds_num_children - 1)
1090 1090                                  usenext = B_TRUE;
1091 1091                  }
1092 1092  
1093 1093                  if (usenext) {
1094 1094                          VERIFY(zap_join_key(dp->dp_meta_objset,
1095 1095                              ds->ds_phys->ds_next_clones_obj,
1096 1096                              scn->scn_phys.scn_queue_obj,
1097 1097                              ds->ds_phys->ds_creation_txg, tx) == 0);
1098 1098                  } else {
1099 1099                          struct enqueue_clones_arg eca;
1100 1100                          eca.tx = tx;
1101 1101                          eca.originobj = ds->ds_object;
1102 1102  
1103 1103                          (void) dmu_objset_find_spa(ds->ds_dir->dd_pool->dp_spa,
1104 1104                              NULL, enqueue_clones_cb, &eca, DS_FIND_CHILDREN);
1105 1105                  }
1106 1106          }
1107 1107  
1108 1108  out:
1109 1109          dsl_dataset_rele(ds, FTAG);
1110 1110  }
1111 1111  
1112 1112  /* ARGSUSED */
1113 1113  static int
1114 1114  enqueue_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
1115 1115  {
1116 1116          dmu_tx_t *tx = arg;
1117 1117          dsl_dataset_t *ds;
1118 1118          int err;
1119 1119          dsl_pool_t *dp = spa->spa_dsl_pool;
1120 1120          dsl_scan_t *scn = dp->dp_scan;
1121 1121  
1122 1122          err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
1123 1123          if (err)
1124 1124                  return (err);
1125 1125  
1126 1126          while (ds->ds_phys->ds_prev_snap_obj != 0) {
1127 1127                  dsl_dataset_t *prev;
1128 1128                  err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
1129 1129                      FTAG, &prev);
1130 1130                  if (err) {
1131 1131                          dsl_dataset_rele(ds, FTAG);
1132 1132                          return (err);
1133 1133                  }
1134 1134  
1135 1135                  /*
1136 1136                   * If this is a clone, we don't need to worry about it for now.
1137 1137                   */
1138 1138                  if (prev->ds_phys->ds_next_snap_obj != ds->ds_object) {
1139 1139                          dsl_dataset_rele(ds, FTAG);
1140 1140                          dsl_dataset_rele(prev, FTAG);
1141 1141                          return (0);
1142 1142                  }
1143 1143                  dsl_dataset_rele(ds, FTAG);
1144 1144                  ds = prev;
1145 1145          }
1146 1146  
1147 1147          VERIFY(zap_add_int_key(dp->dp_meta_objset, scn->scn_phys.scn_queue_obj,
1148 1148              ds->ds_object, ds->ds_phys->ds_prev_snap_txg, tx) == 0);
1149 1149          dsl_dataset_rele(ds, FTAG);
1150 1150          return (0);
1151 1151  }
1152 1152  
1153 1153  /*
1154 1154   * Scrub/dedup interaction.
1155 1155   *
1156 1156   * If there are N references to a deduped block, we don't want to scrub it
1157 1157   * N times -- ideally, we should scrub it exactly once.
1158 1158   *
1159 1159   * We leverage the fact that the dde's replication class (enum ddt_class)
1160 1160   * is ordered from highest replication class (DDT_CLASS_DITTO) to lowest
1161 1161   * (DDT_CLASS_UNIQUE) so that we may walk the DDT in that order.
1162 1162   *
1163 1163   * To prevent excess scrubbing, the scrub begins by walking the DDT
1164 1164   * to find all blocks with refcnt > 1, and scrubs each of these once.
1165 1165   * Since there are two replication classes which contain blocks with
1166 1166   * refcnt > 1, we scrub the highest replication class (DDT_CLASS_DITTO) first.
1167 1167   * Finally the top-down scrub begins, only visiting blocks with refcnt == 1.
1168 1168   *
1169 1169   * There would be nothing more to say if a block's refcnt couldn't change
1170 1170   * during a scrub, but of course it can so we must account for changes
1171 1171   * in a block's replication class.
1172 1172   *
1173 1173   * Here's an example of what can occur:
1174 1174   *
1175 1175   * If a block has refcnt > 1 during the DDT scrub phase, but has refcnt == 1
1176 1176   * when visited during the top-down scrub phase, it will be scrubbed twice.
1177 1177   * This negates our scrub optimization, but is otherwise harmless.
1178 1178   *
1179 1179   * If a block has refcnt == 1 during the DDT scrub phase, but has refcnt > 1
1180 1180   * on each visit during the top-down scrub phase, it will never be scrubbed.
1181 1181   * To catch this, ddt_sync_entry() notifies the scrub code whenever a block's
1182 1182   * reference class transitions to a higher level (i.e DDT_CLASS_UNIQUE to
1183 1183   * DDT_CLASS_DUPLICATE); if it transitions from refcnt == 1 to refcnt > 1
1184 1184   * while a scrub is in progress, it scrubs the block right then.
1185 1185   */
1186 1186  static void
1187 1187  dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
1188 1188  {
1189 1189          ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
1190 1190          ddt_entry_t dde = { 0 };
1191 1191          int error;
1192 1192          uint64_t n = 0;
1193 1193  
1194 1194          while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
1195 1195                  ddt_t *ddt;
1196 1196  
1197 1197                  if (ddb->ddb_class > scn->scn_phys.scn_ddt_class_max)
1198 1198                          break;
1199 1199                  dprintf("visiting ddb=%llu/%llu/%llu/%llx\n",
1200 1200                      (longlong_t)ddb->ddb_class,
1201 1201                      (longlong_t)ddb->ddb_type,
1202 1202                      (longlong_t)ddb->ddb_checksum,
1203 1203                      (longlong_t)ddb->ddb_cursor);
1204 1204  
1205 1205                  /* There should be no pending changes to the dedup table */
1206 1206                  ddt = scn->scn_dp->dp_spa->spa_ddt[ddb->ddb_checksum];
1207 1207                  ASSERT(avl_first(&ddt->ddt_tree) == NULL);
1208 1208  
1209 1209                  dsl_scan_ddt_entry(scn, ddb->ddb_checksum, &dde, tx);
1210 1210                  n++;
1211 1211  
1212 1212                  if (dsl_scan_check_pause(scn, NULL))
1213 1213                          break;
1214 1214          }
1215 1215  
1216 1216          zfs_dbgmsg("scanned %llu ddt entries with class_max = %u; pausing=%u",
1217 1217              (longlong_t)n, (int)scn->scn_phys.scn_ddt_class_max,
1218 1218              (int)scn->scn_pausing);
1219 1219  
1220 1220          ASSERT(error == 0 || error == ENOENT);
1221 1221          ASSERT(error != ENOENT ||
1222 1222              ddb->ddb_class > scn->scn_phys.scn_ddt_class_max);
1223 1223  }
1224 1224  
1225 1225  /* ARGSUSED */
1226 1226  void
1227 1227  dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
1228 1228      ddt_entry_t *dde, dmu_tx_t *tx)
1229 1229  {
1230 1230          const ddt_key_t *ddk = &dde->dde_key;
1231 1231          ddt_phys_t *ddp = dde->dde_phys;
1232 1232          blkptr_t bp;
1233 1233          zbookmark_t zb = { 0 };
1234 1234  
1235 1235          if (scn->scn_phys.scn_state != DSS_SCANNING)
1236 1236                  return;
1237 1237  
1238 1238          for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1239 1239                  if (ddp->ddp_phys_birth == 0 ||
1240 1240                      ddp->ddp_phys_birth > scn->scn_phys.scn_cur_max_txg)
1241 1241                          continue;
1242 1242                  ddt_bp_create(checksum, ddk, ddp, &bp);
1243 1243  
1244 1244                  scn->scn_visited_this_txg++;
1245 1245                  scan_funcs[scn->scn_phys.scn_func](scn->scn_dp, &bp, &zb);
1246 1246          }
1247 1247  }
1248 1248  
1249 1249  static void
1250 1250  dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
1251 1251  {
1252 1252          dsl_pool_t *dp = scn->scn_dp;
1253 1253          zap_cursor_t zc;
1254 1254          zap_attribute_t za;
1255 1255  
1256 1256          if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1257 1257              scn->scn_phys.scn_ddt_class_max) {
1258 1258                  scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1259 1259                  scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1260 1260                  dsl_scan_ddt(scn, tx);
1261 1261                  if (scn->scn_pausing)
1262 1262                          return;
1263 1263          }
1264 1264  
1265 1265          if (scn->scn_phys.scn_bookmark.zb_objset == DMU_META_OBJSET) {
1266 1266                  /* First do the MOS & ORIGIN */
1267 1267  
1268 1268                  scn->scn_phys.scn_cur_min_txg = scn->scn_phys.scn_min_txg;
1269 1269                  scn->scn_phys.scn_cur_max_txg = scn->scn_phys.scn_max_txg;
1270 1270                  dsl_scan_visit_rootbp(scn, NULL,
1271 1271                      &dp->dp_meta_rootbp, tx);
1272 1272                  spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
1273 1273                  if (scn->scn_pausing)
1274 1274                          return;
1275 1275  
1276 1276                  if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
1277 1277                          VERIFY(0 == dmu_objset_find_spa(dp->dp_spa,
1278 1278                              NULL, enqueue_cb, tx, DS_FIND_CHILDREN));
1279 1279                  } else {
1280 1280                          dsl_scan_visitds(scn,
1281 1281                              dp->dp_origin_snap->ds_object, tx);
1282 1282                  }
1283 1283                  ASSERT(!scn->scn_pausing);
1284 1284          } else if (scn->scn_phys.scn_bookmark.zb_objset !=
1285 1285              ZB_DESTROYED_OBJSET) {
1286 1286                  /*
1287 1287                   * If we were paused, continue from here.  Note if the
1288 1288                   * ds we were paused on was deleted, the zb_objset may
1289 1289                   * be -1, so we will skip this and find a new objset
1290 1290                   * below.
1291 1291                   */
1292 1292                  dsl_scan_visitds(scn, scn->scn_phys.scn_bookmark.zb_objset, tx);
1293 1293                  if (scn->scn_pausing)
1294 1294                          return;
1295 1295          }
1296 1296  
1297 1297          /*
1298 1298           * In case we were paused right at the end of the ds, zero the
1299 1299           * bookmark so we don't think that we're still trying to resume.
1300 1300           */
  
    | 
      ↓ open down ↓ | 
    273 lines elided | 
    
      ↑ open up ↑ | 
  
1301 1301          bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_t));
1302 1302  
1303 1303          /* keep pulling things out of the zap-object-as-queue */
1304 1304          while (zap_cursor_init(&zc, dp->dp_meta_objset,
1305 1305              scn->scn_phys.scn_queue_obj),
1306 1306              zap_cursor_retrieve(&zc, &za) == 0) {
1307 1307                  dsl_dataset_t *ds;
1308 1308                  uint64_t dsobj;
1309 1309  
1310 1310                  dsobj = strtonum(za.za_name, NULL);
1311      -                VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
     1311 +                VERIFY0(zap_remove_int(dp->dp_meta_objset,
1312 1312                      scn->scn_phys.scn_queue_obj, dsobj, tx));
1313 1313  
1314 1314                  /* Set up min/max txg */
1315      -                VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
     1315 +                VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
1316 1316                  if (za.za_first_integer != 0) {
1317 1317                          scn->scn_phys.scn_cur_min_txg =
1318 1318                              MAX(scn->scn_phys.scn_min_txg,
1319 1319                              za.za_first_integer);
1320 1320                  } else {
1321 1321                          scn->scn_phys.scn_cur_min_txg =
1322 1322                              MAX(scn->scn_phys.scn_min_txg,
1323 1323                              ds->ds_phys->ds_prev_snap_txg);
1324 1324                  }
1325 1325                  scn->scn_phys.scn_cur_max_txg = dsl_scan_ds_maxtxg(ds);
1326 1326                  dsl_dataset_rele(ds, FTAG);
1327 1327  
1328 1328                  dsl_scan_visitds(scn, dsobj, tx);
1329 1329                  zap_cursor_fini(&zc);
1330 1330                  if (scn->scn_pausing)
1331 1331                          return;
1332 1332          }
1333 1333          zap_cursor_fini(&zc);
1334 1334  }
1335 1335  
1336 1336  static boolean_t
1337 1337  dsl_scan_free_should_pause(dsl_scan_t *scn)
1338 1338  {
1339 1339          uint64_t elapsed_nanosecs;
1340 1340  
1341 1341          elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
1342 1342          return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
1343 1343              (elapsed_nanosecs / MICROSEC > zfs_free_min_time_ms &&
1344 1344              txg_sync_waiting(scn->scn_dp)) ||
1345 1345              spa_shutting_down(scn->scn_dp->dp_spa));
1346 1346  }
1347 1347  
1348 1348  static int
1349 1349  dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
1350 1350  {
1351 1351          dsl_scan_t *scn = arg;
1352 1352  
1353 1353          if (!scn->scn_is_bptree ||
1354 1354              (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
1355 1355                  if (dsl_scan_free_should_pause(scn))
1356 1356                          return (ERESTART);
1357 1357          }
1358 1358  
1359 1359          zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
1360 1360              dmu_tx_get_txg(tx), bp, 0));
1361 1361          dsl_dir_diduse_space(tx->tx_pool->dp_free_dir, DD_USED_HEAD,
1362 1362              -bp_get_dsize_sync(scn->scn_dp->dp_spa, bp),
1363 1363              -BP_GET_PSIZE(bp), -BP_GET_UCSIZE(bp), tx);
1364 1364          scn->scn_visited_this_txg++;
1365 1365          return (0);
1366 1366  }
1367 1367  
1368 1368  boolean_t
1369 1369  dsl_scan_active(dsl_scan_t *scn)
1370 1370  {
1371 1371          spa_t *spa = scn->scn_dp->dp_spa;
1372 1372          uint64_t used = 0, comp, uncomp;
1373 1373  
1374 1374          if (spa->spa_load_state != SPA_LOAD_NONE)
1375 1375                  return (B_FALSE);
1376 1376          if (spa_shutting_down(spa))
1377 1377                  return (B_FALSE);
1378 1378  
1379 1379          if (scn->scn_phys.scn_state == DSS_SCANNING)
1380 1380                  return (B_TRUE);
1381 1381  
1382 1382          if (spa_feature_is_active(spa,
1383 1383              &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
1384 1384                  return (B_TRUE);
1385 1385          }
1386 1386          if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1387 1387                  (void) bpobj_space(&scn->scn_dp->dp_free_bpobj,
1388 1388                      &used, &comp, &uncomp);
1389 1389          }
1390 1390          return (used != 0);
1391 1391  }
1392 1392  
1393 1393  void
1394 1394  dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
1395 1395  {
1396 1396          dsl_scan_t *scn = dp->dp_scan;
1397 1397          spa_t *spa = dp->dp_spa;
1398 1398          int err;
1399 1399  
1400 1400          /*
1401 1401           * Check for scn_restart_txg before checking spa_load_state, so
1402 1402           * that we can restart an old-style scan while the pool is being
1403 1403           * imported (see dsl_scan_init).
1404 1404           */
1405 1405          if (scn->scn_restart_txg != 0 &&
1406 1406              scn->scn_restart_txg <= tx->tx_txg) {
1407 1407                  pool_scan_func_t func = POOL_SCAN_SCRUB;
1408 1408                  dsl_scan_done(scn, B_FALSE, tx);
1409 1409                  if (vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL))
1410 1410                          func = POOL_SCAN_RESILVER;
1411 1411                  zfs_dbgmsg("restarting scan func=%u txg=%llu",
1412 1412                      func, tx->tx_txg);
1413 1413                  dsl_scan_setup_sync(scn, &func, tx);
1414 1414          }
1415 1415  
1416 1416          if (!dsl_scan_active(scn) ||
1417 1417              spa_sync_pass(dp->dp_spa) > 1)
1418 1418                  return;
1419 1419  
1420 1420          scn->scn_visited_this_txg = 0;
1421 1421          scn->scn_pausing = B_FALSE;
1422 1422          scn->scn_sync_start_time = gethrtime();
1423 1423          spa->spa_scrub_active = B_TRUE;
1424 1424  
1425 1425          /*
1426 1426           * First process the free list.  If we pause the free, don't do
  
    | 
      ↓ open down ↓ | 
    101 lines elided | 
    
      ↑ open up ↑ | 
  
1427 1427           * any scanning.  This ensures that there is no free list when
1428 1428           * we are scanning, so the scan code doesn't have to worry about
1429 1429           * traversing it.
1430 1430           */
1431 1431          if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
1432 1432                  scn->scn_is_bptree = B_FALSE;
1433 1433                  scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1434 1434                      NULL, ZIO_FLAG_MUSTSUCCEED);
1435 1435                  err = bpobj_iterate(&dp->dp_free_bpobj,
1436 1436                      dsl_scan_free_block_cb, scn, tx);
1437      -                VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
     1437 +                VERIFY0(zio_wait(scn->scn_zio_root));
1438 1438  
1439 1439                  if (err == 0 && spa_feature_is_active(spa,
1440 1440                      &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
1441 1441                          scn->scn_is_bptree = B_TRUE;
1442 1442                          scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1443 1443                              NULL, ZIO_FLAG_MUSTSUCCEED);
1444 1444                          err = bptree_iterate(dp->dp_meta_objset,
1445 1445                              dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb,
1446 1446                              scn, tx);
1447      -                        VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
     1447 +                        VERIFY0(zio_wait(scn->scn_zio_root));
1448 1448                          if (err != 0)
1449 1449                                  return;
1450 1450  
1451 1451                          /* disable async destroy feature */
1452 1452                          spa_feature_decr(spa,
1453 1453                              &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY], tx);
1454 1454                          ASSERT(!spa_feature_is_active(spa,
1455 1455                              &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY]));
1456      -                        VERIFY3U(0, ==, zap_remove(dp->dp_meta_objset,
     1456 +                        VERIFY0(zap_remove(dp->dp_meta_objset,
1457 1457                              DMU_POOL_DIRECTORY_OBJECT,
1458 1458                              DMU_POOL_BPTREE_OBJ, tx));
1459      -                        VERIFY3U(0, ==, bptree_free(dp->dp_meta_objset,
     1459 +                        VERIFY0(bptree_free(dp->dp_meta_objset,
1460 1460                              dp->dp_bptree_obj, tx));
1461 1461                          dp->dp_bptree_obj = 0;
1462 1462                  }
1463 1463                  if (scn->scn_visited_this_txg) {
1464 1464                          zfs_dbgmsg("freed %llu blocks in %llums from "
1465 1465                              "free_bpobj/bptree txg %llu",
1466 1466                              (longlong_t)scn->scn_visited_this_txg,
1467 1467                              (longlong_t)
1468 1468                              (gethrtime() - scn->scn_sync_start_time) / MICROSEC,
1469 1469                              (longlong_t)tx->tx_txg);
1470 1470                          scn->scn_visited_this_txg = 0;
1471 1471                          /*
1472 1472                           * Re-sync the ddt so that we can further modify
1473 1473                           * it when doing bprewrite.
1474 1474                           */
1475 1475                          ddt_sync(spa, tx->tx_txg);
1476 1476                  }
1477 1477                  if (err == ERESTART)
1478 1478                          return;
1479 1479          }
1480 1480  
1481 1481          if (scn->scn_phys.scn_state != DSS_SCANNING)
1482 1482                  return;
1483 1483  
1484 1484          if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
1485 1485              scn->scn_phys.scn_ddt_class_max) {
1486 1486                  zfs_dbgmsg("doing scan sync txg %llu; "
1487 1487                      "ddt bm=%llu/%llu/%llu/%llx",
1488 1488                      (longlong_t)tx->tx_txg,
1489 1489                      (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_class,
1490 1490                      (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_type,
1491 1491                      (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_checksum,
1492 1492                      (longlong_t)scn->scn_phys.scn_ddt_bookmark.ddb_cursor);
1493 1493                  ASSERT(scn->scn_phys.scn_bookmark.zb_objset == 0);
1494 1494                  ASSERT(scn->scn_phys.scn_bookmark.zb_object == 0);
1495 1495                  ASSERT(scn->scn_phys.scn_bookmark.zb_level == 0);
1496 1496                  ASSERT(scn->scn_phys.scn_bookmark.zb_blkid == 0);
1497 1497          } else {
1498 1498                  zfs_dbgmsg("doing scan sync txg %llu; bm=%llu/%llu/%llu/%llu",
1499 1499                      (longlong_t)tx->tx_txg,
1500 1500                      (longlong_t)scn->scn_phys.scn_bookmark.zb_objset,
1501 1501                      (longlong_t)scn->scn_phys.scn_bookmark.zb_object,
1502 1502                      (longlong_t)scn->scn_phys.scn_bookmark.zb_level,
1503 1503                      (longlong_t)scn->scn_phys.scn_bookmark.zb_blkid);
1504 1504          }
1505 1505  
1506 1506          scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
1507 1507              NULL, ZIO_FLAG_CANFAIL);
1508 1508          dsl_scan_visit(scn, tx);
1509 1509          (void) zio_wait(scn->scn_zio_root);
1510 1510          scn->scn_zio_root = NULL;
1511 1511  
1512 1512          zfs_dbgmsg("visited %llu blocks in %llums",
1513 1513              (longlong_t)scn->scn_visited_this_txg,
1514 1514              (longlong_t)(gethrtime() - scn->scn_sync_start_time) / MICROSEC);
1515 1515  
1516 1516          if (!scn->scn_pausing) {
1517 1517                  /* finished with scan. */
1518 1518                  zfs_dbgmsg("finished scan txg %llu", (longlong_t)tx->tx_txg);
1519 1519                  dsl_scan_done(scn, B_TRUE, tx);
1520 1520          }
1521 1521  
1522 1522          if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
1523 1523                  mutex_enter(&spa->spa_scrub_lock);
1524 1524                  while (spa->spa_scrub_inflight > 0) {
1525 1525                          cv_wait(&spa->spa_scrub_io_cv,
1526 1526                              &spa->spa_scrub_lock);
1527 1527                  }
1528 1528                  mutex_exit(&spa->spa_scrub_lock);
1529 1529          }
1530 1530  
1531 1531          dsl_scan_sync_state(scn, tx);
1532 1532  }
1533 1533  
1534 1534  /*
1535 1535   * This will start a new scan, or restart an existing one.
1536 1536   */
1537 1537  void
1538 1538  dsl_resilver_restart(dsl_pool_t *dp, uint64_t txg)
1539 1539  {
1540 1540          if (txg == 0) {
1541 1541                  dmu_tx_t *tx;
1542 1542                  tx = dmu_tx_create_dd(dp->dp_mos_dir);
1543 1543                  VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
1544 1544  
1545 1545                  txg = dmu_tx_get_txg(tx);
1546 1546                  dp->dp_scan->scn_restart_txg = txg;
1547 1547                  dmu_tx_commit(tx);
1548 1548          } else {
1549 1549                  dp->dp_scan->scn_restart_txg = txg;
1550 1550          }
1551 1551          zfs_dbgmsg("restarting resilver txg=%llu", txg);
1552 1552  }
1553 1553  
1554 1554  boolean_t
1555 1555  dsl_scan_resilvering(dsl_pool_t *dp)
1556 1556  {
1557 1557          return (dp->dp_scan->scn_phys.scn_state == DSS_SCANNING &&
1558 1558              dp->dp_scan->scn_phys.scn_func == POOL_SCAN_RESILVER);
1559 1559  }
1560 1560  
1561 1561  /*
1562 1562   * scrub consumers
1563 1563   */
1564 1564  
1565 1565  static void
1566 1566  count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
1567 1567  {
1568 1568          int i;
1569 1569  
1570 1570          /*
1571 1571           * If we resume after a reboot, zab will be NULL; don't record
1572 1572           * incomplete stats in that case.
1573 1573           */
1574 1574          if (zab == NULL)
1575 1575                  return;
1576 1576  
1577 1577          for (i = 0; i < 4; i++) {
1578 1578                  int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
1579 1579                  int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
1580 1580                  if (t & DMU_OT_NEWTYPE)
1581 1581                          t = DMU_OT_OTHER;
1582 1582                  zfs_blkstat_t *zb = &zab->zab_type[l][t];
1583 1583                  int equal;
1584 1584  
1585 1585                  zb->zb_count++;
1586 1586                  zb->zb_asize += BP_GET_ASIZE(bp);
1587 1587                  zb->zb_lsize += BP_GET_LSIZE(bp);
1588 1588                  zb->zb_psize += BP_GET_PSIZE(bp);
1589 1589                  zb->zb_gangs += BP_COUNT_GANG(bp);
1590 1590  
1591 1591                  switch (BP_GET_NDVAS(bp)) {
1592 1592                  case 2:
1593 1593                          if (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1594 1594                              DVA_GET_VDEV(&bp->blk_dva[1]))
1595 1595                                  zb->zb_ditto_2_of_2_samevdev++;
1596 1596                          break;
1597 1597                  case 3:
1598 1598                          equal = (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1599 1599                              DVA_GET_VDEV(&bp->blk_dva[1])) +
1600 1600                              (DVA_GET_VDEV(&bp->blk_dva[0]) ==
1601 1601                              DVA_GET_VDEV(&bp->blk_dva[2])) +
1602 1602                              (DVA_GET_VDEV(&bp->blk_dva[1]) ==
1603 1603                              DVA_GET_VDEV(&bp->blk_dva[2]));
1604 1604                          if (equal == 1)
1605 1605                                  zb->zb_ditto_2_of_3_samevdev++;
1606 1606                          else if (equal == 3)
1607 1607                                  zb->zb_ditto_3_of_3_samevdev++;
1608 1608                          break;
1609 1609                  }
1610 1610          }
1611 1611  }
1612 1612  
1613 1613  static void
1614 1614  dsl_scan_scrub_done(zio_t *zio)
1615 1615  {
1616 1616          spa_t *spa = zio->io_spa;
1617 1617  
1618 1618          zio_data_buf_free(zio->io_data, zio->io_size);
1619 1619  
1620 1620          mutex_enter(&spa->spa_scrub_lock);
1621 1621          spa->spa_scrub_inflight--;
1622 1622          cv_broadcast(&spa->spa_scrub_io_cv);
1623 1623  
1624 1624          if (zio->io_error && (zio->io_error != ECKSUM ||
1625 1625              !(zio->io_flags & ZIO_FLAG_SPECULATIVE))) {
1626 1626                  spa->spa_dsl_pool->dp_scan->scn_phys.scn_errors++;
1627 1627          }
1628 1628          mutex_exit(&spa->spa_scrub_lock);
1629 1629  }
1630 1630  
1631 1631  static int
1632 1632  dsl_scan_scrub_cb(dsl_pool_t *dp,
1633 1633      const blkptr_t *bp, const zbookmark_t *zb)
1634 1634  {
1635 1635          dsl_scan_t *scn = dp->dp_scan;
1636 1636          size_t size = BP_GET_PSIZE(bp);
1637 1637          spa_t *spa = dp->dp_spa;
1638 1638          uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
1639 1639          boolean_t needs_io;
1640 1640          int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
1641 1641          int zio_priority;
1642 1642          int scan_delay = 0;
1643 1643  
1644 1644          if (phys_birth <= scn->scn_phys.scn_min_txg ||
1645 1645              phys_birth >= scn->scn_phys.scn_max_txg)
1646 1646                  return (0);
1647 1647  
1648 1648          count_block(dp->dp_blkstats, bp);
1649 1649  
1650 1650          ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
1651 1651          if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
1652 1652                  zio_flags |= ZIO_FLAG_SCRUB;
1653 1653                  zio_priority = ZIO_PRIORITY_SCRUB;
1654 1654                  needs_io = B_TRUE;
1655 1655                  scan_delay = zfs_scrub_delay;
1656 1656          } else if (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) {
1657 1657                  zio_flags |= ZIO_FLAG_RESILVER;
1658 1658                  zio_priority = ZIO_PRIORITY_RESILVER;
1659 1659                  needs_io = B_FALSE;
1660 1660                  scan_delay = zfs_resilver_delay;
1661 1661          }
1662 1662  
1663 1663          /* If it's an intent log block, failure is expected. */
1664 1664          if (zb->zb_level == ZB_ZIL_LEVEL)
1665 1665                  zio_flags |= ZIO_FLAG_SPECULATIVE;
1666 1666  
1667 1667          for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
1668 1668                  vdev_t *vd = vdev_lookup_top(spa,
1669 1669                      DVA_GET_VDEV(&bp->blk_dva[d]));
1670 1670  
1671 1671                  /*
1672 1672                   * Keep track of how much data we've examined so that
1673 1673                   * zpool(1M) status can make useful progress reports.
1674 1674                   */
1675 1675                  scn->scn_phys.scn_examined += DVA_GET_ASIZE(&bp->blk_dva[d]);
1676 1676                  spa->spa_scan_pass_exam += DVA_GET_ASIZE(&bp->blk_dva[d]);
1677 1677  
1678 1678                  /* if it's a resilver, this may not be in the target range */
1679 1679                  if (!needs_io) {
1680 1680                          if (DVA_GET_GANG(&bp->blk_dva[d])) {
1681 1681                                  /*
1682 1682                                   * Gang members may be spread across multiple
1683 1683                                   * vdevs, so the best estimate we have is the
1684 1684                                   * scrub range, which has already been checked.
1685 1685                                   * XXX -- it would be better to change our
1686 1686                                   * allocation policy to ensure that all
1687 1687                                   * gang members reside on the same vdev.
1688 1688                                   */
1689 1689                                  needs_io = B_TRUE;
1690 1690                          } else {
1691 1691                                  needs_io = vdev_dtl_contains(vd, DTL_PARTIAL,
1692 1692                                      phys_birth, 1);
1693 1693                          }
1694 1694                  }
1695 1695          }
1696 1696  
1697 1697          if (needs_io && !zfs_no_scrub_io) {
1698 1698                  vdev_t *rvd = spa->spa_root_vdev;
1699 1699                  uint64_t maxinflight = rvd->vdev_children * zfs_top_maxinflight;
1700 1700                  void *data = zio_data_buf_alloc(size);
1701 1701  
1702 1702                  mutex_enter(&spa->spa_scrub_lock);
1703 1703                  while (spa->spa_scrub_inflight >= maxinflight)
1704 1704                          cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1705 1705                  spa->spa_scrub_inflight++;
1706 1706                  mutex_exit(&spa->spa_scrub_lock);
1707 1707  
1708 1708                  /*
1709 1709                   * If we're seeing recent (zfs_scan_idle) "important" I/Os
1710 1710                   * then throttle our workload to limit the impact of a scan.
1711 1711                   */
1712 1712                  if (ddi_get_lbolt64() - spa->spa_last_io <= zfs_scan_idle)
1713 1713                          delay(scan_delay);
1714 1714  
1715 1715                  zio_nowait(zio_read(NULL, spa, bp, data, size,
1716 1716                      dsl_scan_scrub_done, NULL, zio_priority,
1717 1717                      zio_flags, zb));
1718 1718          }
1719 1719  
1720 1720          /* do not relocate this block */
1721 1721          return (0);
1722 1722  }
1723 1723  
1724 1724  int
1725 1725  dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
1726 1726  {
1727 1727          spa_t *spa = dp->dp_spa;
1728 1728  
1729 1729          /*
1730 1730           * Purge all vdev caches and probe all devices.  We do this here
1731 1731           * rather than in sync context because this requires a writer lock
1732 1732           * on the spa_config lock, which we can't do from sync context.  The
1733 1733           * spa_scrub_reopen flag indicates that vdev_open() should not
1734 1734           * attempt to start another scrub.
1735 1735           */
1736 1736          spa_vdev_state_enter(spa, SCL_NONE);
1737 1737          spa->spa_scrub_reopen = B_TRUE;
1738 1738          vdev_reopen(spa->spa_root_vdev);
1739 1739          spa->spa_scrub_reopen = B_FALSE;
1740 1740          (void) spa_vdev_state_exit(spa, NULL, 0);
1741 1741  
1742 1742          return (dsl_sync_task_do(dp, dsl_scan_setup_check,
1743 1743              dsl_scan_setup_sync, dp->dp_scan, &func, 0));
1744 1744  }
  
    | 
      ↓ open down ↓ | 
    275 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX