]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/dsl_scan.c
cstyle: Resolve C style issues
[mirror_zfs.git] / module / zfs / dsl_scan.c
index 56d41083673eba5d2cf9d47baedb63f06e43364e..ea04507813f7d38336f35d5946cded6c5c6da0d9 100644 (file)
@@ -20,6 +20,7 @@
  */
 /*
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013 by Delphix. All rights reserved.
  */
 
 #include <sys/dsl_scan.h>
 #include <sys/ddt.h>
 #include <sys/sa.h>
 #include <sys/sa_impl.h>
+#include <sys/zfeature.h>
 #ifdef _KERNEL
 #include <sys/zfs_vfsops.h>
 #endif
 
 typedef int (scan_cb_t)(dsl_pool_t *, const blkptr_t *, const zbookmark_t *);
 
-static scan_cb_t dsl_scan_defrag_cb;
 static scan_cb_t dsl_scan_scrub_cb;
-static scan_cb_t dsl_scan_remove_cb;
-static dsl_syncfunc_t dsl_scan_cancel_sync;
+static void dsl_scan_cancel_sync(void *, dmu_tx_t *);
 static void dsl_scan_sync_state(dsl_scan_t *, dmu_tx_t *tx);
 
 int zfs_top_maxinflight = 32;          /* maximum I/Os per top-level */
@@ -64,8 +64,8 @@ int zfs_scan_idle = 50;                       /* idle window in clock ticks */
 int zfs_scan_min_time_ms = 1000; /* min millisecs to scrub per txg */
 int zfs_free_min_time_ms = 1000; /* min millisecs to free per txg */
 int zfs_resilver_min_time_ms = 3000; /* min millisecs to resilver per txg */
-boolean_t zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
-boolean_t zfs_no_scrub_prefetch = B_FALSE; /* set to disable srub prefetching */
+int zfs_no_scrub_io = B_FALSE; /* set to disable scrub i/o */
+int zfs_no_scrub_prefetch = B_FALSE; /* set to disable srub prefetching */
 enum ddt_class zfs_scrub_ddt_class_max = DDT_CLASS_DUPLICATE;
 int dsl_scan_delay_completion = B_FALSE; /* set to delay scan completion */
 
@@ -73,8 +73,6 @@ int dsl_scan_delay_completion = B_FALSE; /* set to delay scan completion */
        ((scn)->scn_phys.scn_func == POOL_SCAN_SCRUB || \
        (scn)->scn_phys.scn_func == POOL_SCAN_RESILVER)
 
-extern int zfs_txg_timeout;
-
 /* the order has to match pool_scan_type */
 static scan_cb_t *scan_funcs[POOL_SCAN_FUNCS] = {
        NULL,
@@ -93,6 +91,15 @@ dsl_scan_init(dsl_pool_t *dp, uint64_t txg)
        scn = dp->dp_scan = kmem_zalloc(sizeof (dsl_scan_t), KM_SLEEP);
        scn->scn_dp = dp;
 
+       /*
+        * It's possible that we're resuming a scan after a reboot so
+        * make sure that the scan_async_destroying flag is initialized
+        * appropriately.
+        */
+       ASSERT(!scn->scn_async_destroying);
+       scn->scn_async_destroying = spa_feature_is_active(dp->dp_spa,
+           &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY]);
+
        err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
            "scrub_func", sizeof (uint64_t), 1, &f);
        if (err == 0) {
@@ -152,22 +159,21 @@ dsl_scan_fini(dsl_pool_t *dp)
 
 /* ARGSUSED */
 static int
-dsl_scan_setup_check(void *arg1, void *arg2, dmu_tx_t *tx)
+dsl_scan_setup_check(void *arg, dmu_tx_t *tx)
 {
-       dsl_scan_t *scn = arg1;
+       dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
 
        if (scn->scn_phys.scn_state == DSS_SCANNING)
-               return (EBUSY);
+               return (SET_ERROR(EBUSY));
 
        return (0);
 }
 
-/* ARGSUSED */
 static void
-dsl_scan_setup_sync(void *arg1, void *arg2, dmu_tx_t *tx)
+dsl_scan_setup_sync(void *arg, dmu_tx_t *tx)
 {
-       dsl_scan_t *scn = arg1;
-       pool_scan_func_t *funcp = arg2;
+       dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
+       pool_scan_func_t *funcp = arg;
        dmu_object_type_t ot = 0;
        dsl_pool_t *dp = scn->scn_dp;
        spa_t *spa = dp->dp_spa;
@@ -184,6 +190,7 @@ dsl_scan_setup_sync(void *arg1, void *arg2, dmu_tx_t *tx)
        scn->scn_phys.scn_errors = 0;
        scn->scn_phys.scn_to_examine = spa->spa_root_vdev->vdev_stat.vs_alloc;
        scn->scn_restart_txg = 0;
+       scn->scn_done_txg = 0;
        spa_scan_stat_init(spa);
 
        if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
@@ -194,9 +201,11 @@ dsl_scan_setup_sync(void *arg1, void *arg2, dmu_tx_t *tx)
 
                if (vdev_resilver_needed(spa->spa_root_vdev,
                    &scn->scn_phys.scn_min_txg, &scn->scn_phys.scn_max_txg)) {
-                       spa_event_notify(spa, NULL, ESC_ZFS_RESILVER_START);
+                       spa_event_notify(spa, NULL,
+                           FM_EREPORT_ZFS_RESILVER_START);
                } else {
-                       spa_event_notify(spa, NULL, ESC_ZFS_SCRUB_START);
+                       spa_event_notify(spa, NULL,
+                           FM_EREPORT_ZFS_SCRUB_START);
                }
 
                spa->spa_scrub_started = B_TRUE;
@@ -213,8 +222,8 @@ dsl_scan_setup_sync(void *arg1, void *arg2, dmu_tx_t *tx)
        /* back to the generic stuff */
 
        if (dp->dp_blkstats == NULL) {
-               dp->dp_blkstats =
-                   kmem_alloc(sizeof (zfs_all_blkstats_t), KM_SLEEP);
+               dp->dp_blkstats = kmem_alloc(sizeof (zfs_all_blkstats_t),
+                   KM_PUSHPAGE | KM_NODEBUG);
        }
        bzero(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
 
@@ -226,7 +235,7 @@ dsl_scan_setup_sync(void *arg1, void *arg2, dmu_tx_t *tx)
 
        dsl_scan_sync_state(scn, tx);
 
-       spa_history_log_internal(LOG_POOL_SCAN, spa, tx,
+       spa_history_log_internal(spa, "scan setup", tx,
            "func=%u mintxg=%llu maxtxg=%llu",
            *funcp, scn->scn_phys.scn_min_txg, scn->scn_phys.scn_max_txg);
 }
@@ -275,7 +284,7 @@ dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
        else
                scn->scn_phys.scn_state = DSS_CANCELED;
 
-       spa_history_log_internal(LOG_POOL_SCAN_DONE, spa, tx,
+       spa_history_log_internal(spa, "scan done", tx,
            "complete=%u", complete);
 
        if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
@@ -297,7 +306,8 @@ dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
                    complete ? scn->scn_phys.scn_max_txg : 0, B_TRUE);
                if (complete) {
                        spa_event_notify(spa, NULL, scn->scn_phys.scn_min_txg ?
-                           ESC_ZFS_RESILVER_FINISH : ESC_ZFS_SCRUB_FINISH);
+                           FM_EREPORT_ZFS_RESILVER_FINISH :
+                           FM_EREPORT_ZFS_SCRUB_FINISH);
                }
                spa_errlog_rotate(spa);
 
@@ -313,20 +323,20 @@ dsl_scan_done(dsl_scan_t *scn, boolean_t complete, dmu_tx_t *tx)
 
 /* ARGSUSED */
 static int
-dsl_scan_cancel_check(void *arg1, void *arg2, dmu_tx_t *tx)
+dsl_scan_cancel_check(void *arg, dmu_tx_t *tx)
 {
-       dsl_scan_t *scn = arg1;
+       dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
 
        if (scn->scn_phys.scn_state != DSS_SCANNING)
-               return (ENOENT);
+               return (SET_ERROR(ENOENT));
        return (0);
 }
 
 /* ARGSUSED */
 static void
-dsl_scan_cancel_sync(void *arg1, void *arg2, dmu_tx_t *tx)
+dsl_scan_cancel_sync(void *arg, dmu_tx_t *tx)
 {
-       dsl_scan_t *scn = arg1;
+       dsl_scan_t *scn = dmu_tx_pool(tx)->dp_scan;
 
        dsl_scan_done(scn, B_FALSE, tx);
        dsl_scan_sync_state(scn, tx);
@@ -335,20 +345,16 @@ dsl_scan_cancel_sync(void *arg1, void *arg2, dmu_tx_t *tx)
 int
 dsl_scan_cancel(dsl_pool_t *dp)
 {
-       boolean_t complete = B_FALSE;
-       int err;
-
-       err = dsl_sync_task_do(dp, dsl_scan_cancel_check,
-           dsl_scan_cancel_sync, dp->dp_scan, &complete, 3);
-       return (err);
+       return (dsl_sync_task(spa_name(dp->dp_spa), dsl_scan_cancel_check,
+           dsl_scan_cancel_sync, NULL, 3));
 }
 
 static void dsl_scan_visitbp(blkptr_t *bp,
     const zbookmark_t *zb, dnode_phys_t *dnp, arc_buf_t *pbuf,
     dsl_dataset_t *ds, dsl_scan_t *scn, dmu_objset_type_t ostype,
     dmu_tx_t *tx);
-static void dsl_scan_visitdnode(dsl_scan_t *, dsl_dataset_t *ds,
-    dmu_objset_type_t ostype,
+inline __attribute__((always_inline)) static void dsl_scan_visitdnode(
+    dsl_scan_t *, dsl_dataset_t *ds, dmu_objset_type_t ostype,
     dnode_phys_t *dnp, arc_buf_t *buf, uint64_t object, dmu_tx_t *tx);
 
 void
@@ -364,73 +370,6 @@ dsl_free_sync(zio_t *pio, dsl_pool_t *dp, uint64_t txg, const blkptr_t *bpp)
        zio_nowait(zio_free_sync(pio, dp->dp_spa, txg, bpp, pio->io_flags));
 }
 
-int
-dsl_read(zio_t *pio, spa_t *spa, const blkptr_t *bpp, arc_buf_t *pbuf,
-    arc_done_func_t *done, void *private, int priority, int zio_flags,
-    uint32_t *arc_flags, const zbookmark_t *zb)
-{
-       return (arc_read(pio, spa, bpp, pbuf, done, private,
-           priority, zio_flags, arc_flags, zb));
-}
-
-int
-dsl_read_nolock(zio_t *pio, spa_t *spa, const blkptr_t *bpp,
-    arc_done_func_t *done, void *private, int priority, int zio_flags,
-    uint32_t *arc_flags, const zbookmark_t *zb)
-{
-       return (arc_read_nolock(pio, spa, bpp, done, private,
-           priority, zio_flags, arc_flags, zb));
-}
-
-static boolean_t
-bookmark_is_zero(const zbookmark_t *zb)
-{
-       return (zb->zb_objset == 0 && zb->zb_object == 0 &&
-           zb->zb_level == 0 && zb->zb_blkid == 0);
-}
-
-/* dnp is the dnode for zb1->zb_object */
-static boolean_t
-bookmark_is_before(const dnode_phys_t *dnp, const zbookmark_t *zb1,
-    const zbookmark_t *zb2)
-{
-       uint64_t zb1nextL0, zb2thisobj;
-
-       ASSERT(zb1->zb_objset == zb2->zb_objset);
-       ASSERT(zb2->zb_level == 0);
-
-       /*
-        * A bookmark in the deadlist is considered to be after
-        * everything else.
-        */
-       if (zb2->zb_object == DMU_DEADLIST_OBJECT)
-               return (B_TRUE);
-
-       /* The objset_phys_t isn't before anything. */
-       if (dnp == NULL)
-               return (B_FALSE);
-
-       zb1nextL0 = (zb1->zb_blkid + 1) <<
-           ((zb1->zb_level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT));
-
-       zb2thisobj = zb2->zb_object ? zb2->zb_object :
-           zb2->zb_blkid << (DNODE_BLOCK_SHIFT - DNODE_SHIFT);
-
-       if (zb1->zb_object == DMU_META_DNODE_OBJECT) {
-               uint64_t nextobj = zb1nextL0 *
-                   (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT) >> DNODE_SHIFT;
-               return (nextobj <= zb2thisobj);
-       }
-
-       if (zb1->zb_object < zb2thisobj)
-               return (B_TRUE);
-       if (zb1->zb_object > zb2thisobj)
-               return (B_FALSE);
-       if (zb2->zb_object == DMU_META_DNODE_OBJECT)
-               return (B_FALSE);
-       return (zb1nextL0 <= zb2->zb_blkid);
-}
-
 static uint64_t
 dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
 {
@@ -443,7 +382,7 @@ dsl_scan_ds_maxtxg(dsl_dataset_t *ds)
 static void
 dsl_scan_sync_state(dsl_scan_t *scn, dmu_tx_t *tx)
 {
-       VERIFY(0 == zap_update(scn->scn_dp->dp_meta_objset,
+       VERIFY0(zap_update(scn->scn_dp->dp_meta_objset,
            DMU_POOL_DIRECTORY_OBJECT,
            DMU_POOL_SCAN, sizeof (uint64_t), SCAN_PHYS_NUMINTS,
            &scn->scn_phys, tx));
@@ -462,7 +401,7 @@ dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_t *zb)
        if (scn->scn_pausing)
                return (B_TRUE); /* we're already pausing */
 
-       if (!bookmark_is_zero(&scn->scn_phys.scn_bookmark))
+       if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark))
                return (B_FALSE); /* we're resuming */
 
        /* We only know how to resume from level-0 blocks. */
@@ -473,7 +412,7 @@ dsl_scan_check_pause(dsl_scan_t *scn, const zbookmark_t *zb)
            zfs_resilver_min_time_ms : zfs_scan_min_time_ms;
        elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
        if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
-           (elapsed_nanosecs / MICROSEC > mintime &&
+           (NSEC2MSEC(elapsed_nanosecs) > mintime &&
            txg_sync_waiting(scn->scn_dp)) ||
            spa_shutting_down(scn->scn_dp->dp_spa)) {
                if (zb) {
@@ -601,12 +540,8 @@ dsl_scan_prefetch(dsl_scan_t *scn, arc_buf_t *buf, blkptr_t *bp,
 
        SET_BOOKMARK(&czb, objset, object, BP_GET_LEVEL(bp), blkid);
 
-       /*
-        * XXX need to make sure all of these arc_read() prefetches are
-        * done before setting xlateall (similar to dsl_read())
-        */
        (void) arc_read(scn->scn_zio_root, scn->scn_dp->dp_spa, bp,
-           buf, NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
+           NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
            ZIO_FLAG_CANFAIL | ZIO_FLAG_SCAN_THREAD, &flags, &czb);
 }
 
@@ -617,13 +552,13 @@ dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
        /*
         * We never skip over user/group accounting objects (obj<0)
         */
-       if (!bookmark_is_zero(&scn->scn_phys.scn_bookmark) &&
+       if (!ZB_IS_ZERO(&scn->scn_phys.scn_bookmark) &&
            (int64_t)zb->zb_object >= 0) {
                /*
                 * If we already visited this bp & everything below (in
                 * a prior txg sync), don't bother doing it again.
                 */
-               if (bookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
+               if (zbookmark_is_before(dnp, zb, &scn->scn_phys.scn_bookmark))
                        return (B_TRUE);
 
                /*
@@ -649,7 +584,7 @@ dsl_scan_check_resume(dsl_scan_t *scn, const dnode_phys_t *dnp,
  * Return nonzero on i/o error.
  * Return new buf to write out in *bufp.
  */
-static int
+inline __attribute__((always_inline)) static int
 dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
     dnode_phys_t *dnp, const blkptr_t *bp,
     const zbookmark_t *zb, dmu_tx_t *tx, arc_buf_t **bufp)
@@ -664,8 +599,7 @@ dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
                blkptr_t *cbp;
                int epb = BP_GET_LSIZE(bp) >> SPA_BLKPTRSHIFT;
 
-               err = arc_read_nolock(NULL, dp->dp_spa, bp,
-                   arc_getbuf_func, bufp,
+               err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
                    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
                if (err) {
                        scn->scn_phys.scn_errors++;
@@ -687,8 +621,7 @@ dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
        } else if (BP_GET_TYPE(bp) == DMU_OT_USERGROUP_USED) {
                uint32_t flags = ARC_WAIT;
 
-               err = arc_read_nolock(NULL, dp->dp_spa, bp,
-                   arc_getbuf_func, bufp,
+               err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
                    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
                if (err) {
                        scn->scn_phys.scn_errors++;
@@ -700,8 +633,7 @@ dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
                int i, j;
                int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
 
-               err = arc_read_nolock(NULL, dp->dp_spa, bp,
-                   arc_getbuf_func, bufp,
+               err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
                    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
                if (err) {
                        scn->scn_phys.scn_errors++;
@@ -723,8 +655,7 @@ dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
                uint32_t flags = ARC_WAIT;
                objset_phys_t *osp;
 
-               err = arc_read_nolock(NULL, dp->dp_spa, bp,
-                   arc_getbuf_func, bufp,
+               err = arc_read(NULL, dp->dp_spa, bp, arc_getbuf_func, bufp,
                    ZIO_PRIORITY_ASYNC_READ, zio_flags, &flags, zb);
                if (err) {
                        scn->scn_phys.scn_errors++;
@@ -755,7 +686,7 @@ dsl_scan_recurse(dsl_scan_t *scn, dsl_dataset_t *ds, dmu_objset_type_t ostype,
        return (0);
 }
 
-static void
+inline __attribute__((always_inline)) static void
 dsl_scan_visitdnode(dsl_scan_t *scn, dsl_dataset_t *ds,
     dmu_objset_type_t ostype, dnode_phys_t *dnp, arc_buf_t *buf,
     uint64_t object, dmu_tx_t *tx)
@@ -792,49 +723,43 @@ dsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
 {
        dsl_pool_t *dp = scn->scn_dp;
        arc_buf_t *buf = NULL;
-       blkptr_t bp_toread = *bp;
+       blkptr_t *bp_toread;
+
+       bp_toread = kmem_alloc(sizeof (blkptr_t), KM_PUSHPAGE);
+       *bp_toread = *bp;
 
        /* ASSERT(pbuf == NULL || arc_released(pbuf)); */
 
        if (dsl_scan_check_pause(scn, zb))
-               return;
+               goto out;
 
        if (dsl_scan_check_resume(scn, dnp, zb))
-               return;
+               goto out;
 
        if (bp->blk_birth == 0)
-               return;
+               goto out;
 
        scn->scn_visited_this_txg++;
 
-       dprintf_bp(bp,
-           "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
-           ds, ds ? ds->ds_object : 0,
-           zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
-           pbuf, bp);
+       /*
+        * This debugging is commented out to conserve stack space.  This
+        * function is called recursively and the debugging addes several
+        * bytes to the stack for each call.  It can be commented back in
+        * if required to debug an issue in dsl_scan_visitbp().
+        *
+        * dprintf_bp(bp,
+        *    "visiting ds=%p/%llu zb=%llx/%llx/%llx/%llx buf=%p bp=%p",
+        *    ds, ds ? ds->ds_object : 0,
+        *    zb->zb_objset, zb->zb_object, zb->zb_level, zb->zb_blkid,
+        *    pbuf, bp);
+        */
 
        if (bp->blk_birth <= scn->scn_phys.scn_cur_min_txg)
-               return;
-
-       if (BP_GET_TYPE(bp) != DMU_OT_USERGROUP_USED) {
-               /*
-                * For non-user-accounting blocks, we need to read the
-                * new bp (from a deleted snapshot, found in
-                * check_existing_xlation).  If we used the old bp,
-                * pointers inside this block from before we resumed
-                * would be untranslated.
-                *
-                * For user-accounting blocks, we need to read the old
-                * bp, because we will apply the entire space delta to
-                * it (original untranslated -> translations from
-                * deleted snap -> now).
-                */
-               bp_toread = *bp;
-       }
+               goto out;
 
-       if (dsl_scan_recurse(scn, ds, ostype, dnp, &bp_toread, zb, tx,
+       if (dsl_scan_recurse(scn, ds, ostype, dnp, bp_toread, zb, tx,
            &buf) != 0)
-               return;
+               goto out;
 
        /*
         * If dsl_scan_ddt() has aready visited this block, it will have
@@ -844,7 +769,7 @@ dsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
        if (ddt_class_contains(dp->dp_spa,
            scn->scn_phys.scn_ddt_class_max, bp)) {
                ASSERT(buf == NULL);
-               return;
+               goto out;
        }
 
        /*
@@ -854,11 +779,13 @@ dsl_scan_visitbp(blkptr_t *bp, const zbookmark_t *zb,
         * Don't scan it now unless we need to because something
         * under it was modified.
         */
-       if (bp->blk_birth <= scn->scn_phys.scn_cur_max_txg) {
+       if (BP_PHYSICAL_BIRTH(bp) <= scn->scn_phys.scn_cur_max_txg) {
                scan_funcs[scn->scn_phys.scn_func](dp, bp, zb);
        }
        if (buf)
                (void) arc_buf_remove_ref(buf, &buf);
+out:
+       kmem_free(bp_toread, sizeof (blkptr_t));
 }
 
 static void
@@ -1039,33 +966,33 @@ struct enqueue_clones_arg {
 
 /* ARGSUSED */
 static int
-enqueue_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
+enqueue_clones_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
 {
        struct enqueue_clones_arg *eca = arg;
        dsl_dataset_t *ds;
        int err;
-       dsl_pool_t *dp = spa->spa_dsl_pool;
        dsl_scan_t *scn = dp->dp_scan;
 
-       err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
+       if (hds->ds_dir->dd_phys->dd_origin_obj != eca->originobj)
+               return (0);
+
+       err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
        if (err)
                return (err);
 
-       if (ds->ds_dir->dd_phys->dd_origin_obj == eca->originobj) {
-               while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
-                       dsl_dataset_t *prev;
-                       err = dsl_dataset_hold_obj(dp,
-                           ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
+       while (ds->ds_phys->ds_prev_snap_obj != eca->originobj) {
+               dsl_dataset_t *prev;
+               err = dsl_dataset_hold_obj(dp,
+                   ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
 
-                       dsl_dataset_rele(ds, FTAG);
-                       if (err)
-                               return (err);
-                       ds = prev;
-               }
-               VERIFY(zap_add_int_key(dp->dp_meta_objset,
-                   scn->scn_phys.scn_queue_obj, ds->ds_object,
-                   ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
+               dsl_dataset_rele(ds, FTAG);
+               if (err)
+                       return (err);
+               ds = prev;
        }
+       VERIFY(zap_add_int_key(dp->dp_meta_objset,
+           scn->scn_phys.scn_queue_obj, ds->ds_object,
+           ds->ds_phys->ds_prev_snap_txg, eca->tx) == 0);
        dsl_dataset_rele(ds, FTAG);
        return (0);
 }
@@ -1076,6 +1003,7 @@ dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
        dsl_pool_t *dp = scn->scn_dp;
        dsl_dataset_t *ds;
        objset_t *os;
+       char *dsname;
 
        VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
 
@@ -1098,7 +1026,7 @@ dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
        dmu_buf_will_dirty(ds->ds_dbuf, tx);
        dsl_scan_visit_rootbp(scn, ds, &ds->ds_phys->ds_bp, tx);
 
-       char *dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_SLEEP);
+       dsname = kmem_alloc(ZFS_MAXNAMELEN, KM_PUSHPAGE);
        dsl_dataset_name(ds, dsname);
        zfs_dbgmsg("scanned dataset %llu (%s) with min=%llu max=%llu; "
            "pausing=%u",
@@ -1154,17 +1082,17 @@ dsl_scan_visitds(dsl_scan_t *scn, uint64_t dsobj, dmu_tx_t *tx)
                }
 
                if (usenext) {
-                       VERIFY(zap_join_key(dp->dp_meta_objset,
+                       VERIFY0(zap_join_key(dp->dp_meta_objset,
                            ds->ds_phys->ds_next_clones_obj,
                            scn->scn_phys.scn_queue_obj,
-                           ds->ds_phys->ds_creation_txg, tx) == 0);
+                           ds->ds_phys->ds_creation_txg, tx));
                } else {
                        struct enqueue_clones_arg eca;
                        eca.tx = tx;
                        eca.originobj = ds->ds_object;
 
-                       (void) dmu_objset_find_spa(ds->ds_dir->dd_pool->dp_spa,
-                           NULL, enqueue_clones_cb, &eca, DS_FIND_CHILDREN);
+                       VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
+                           enqueue_clones_cb, &eca, DS_FIND_CHILDREN));
                }
        }
 
@@ -1174,15 +1102,14 @@ out:
 
 /* ARGSUSED */
 static int
-enqueue_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
+enqueue_cb(dsl_pool_t *dp, dsl_dataset_t *hds, void *arg)
 {
        dmu_tx_t *tx = arg;
        dsl_dataset_t *ds;
        int err;
-       dsl_pool_t *dp = spa->spa_dsl_pool;
        dsl_scan_t *scn = dp->dp_scan;
 
-       err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
+       err = dsl_dataset_hold_obj(dp, hds->ds_object, FTAG, &ds);
        if (err)
                return (err);
 
@@ -1250,10 +1177,12 @@ static void
 dsl_scan_ddt(dsl_scan_t *scn, dmu_tx_t *tx)
 {
        ddt_bookmark_t *ddb = &scn->scn_phys.scn_ddt_bookmark;
-       ddt_entry_t dde = { 0 };
+       ddt_entry_t dde;
        int error;
        uint64_t n = 0;
 
+       bzero(&dde, sizeof (ddt_entry_t));
+
        while ((error = ddt_walk(scn->scn_dp->dp_spa, ddb, &dde)) == 0) {
                ddt_t *ddt;
 
@@ -1294,13 +1223,14 @@ dsl_scan_ddt_entry(dsl_scan_t *scn, enum zio_checksum checksum,
        ddt_phys_t *ddp = dde->dde_phys;
        blkptr_t bp;
        zbookmark_t zb = { 0 };
+       int p;
 
        if (scn->scn_phys.scn_state != DSS_SCANNING)
                return;
 
-       for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
+       for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
                if (ddp->ddp_phys_birth == 0 ||
-                   ddp->ddp_phys_birth > scn->scn_phys.scn_cur_max_txg)
+                   ddp->ddp_phys_birth > scn->scn_phys.scn_max_txg)
                        continue;
                ddt_bp_create(checksum, ddk, ddp, &bp);
 
@@ -1313,8 +1243,8 @@ static void
 dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
 {
        dsl_pool_t *dp = scn->scn_dp;
-       zap_cursor_t zc;
-       zap_attribute_t za;
+       zap_cursor_t *zc;
+       zap_attribute_t *za;
 
        if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
            scn->scn_phys.scn_ddt_class_max) {
@@ -1337,8 +1267,8 @@ dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
                        return;
 
                if (spa_version(dp->dp_spa) < SPA_VERSION_DSL_SCRUB) {
-                       VERIFY(0 == dmu_objset_find_spa(dp->dp_spa,
-                           NULL, enqueue_cb, tx, DS_FIND_CHILDREN));
+                       VERIFY0(dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
+                           enqueue_cb, tx, DS_FIND_CHILDREN));
                } else {
                        dsl_scan_visitds(scn,
                            dp->dp_origin_snap->ds_object, tx);
@@ -1362,24 +1292,26 @@ dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
         * bookmark so we don't think that we're still trying to resume.
         */
        bzero(&scn->scn_phys.scn_bookmark, sizeof (zbookmark_t));
+       zc = kmem_alloc(sizeof (zap_cursor_t), KM_PUSHPAGE);
+       za = kmem_alloc(sizeof (zap_attribute_t), KM_PUSHPAGE);
 
        /* keep pulling things out of the zap-object-as-queue */
-       while (zap_cursor_init(&zc, dp->dp_meta_objset,
+       while (zap_cursor_init(zc, dp->dp_meta_objset,
            scn->scn_phys.scn_queue_obj),
-           zap_cursor_retrieve(&zc, &za) == 0) {
+           zap_cursor_retrieve(zc, za) == 0) {
                dsl_dataset_t *ds;
                uint64_t dsobj;
 
-               dsobj = strtonum(za.za_name, NULL);
+               dsobj = strtonum(za->za_name, NULL);
                VERIFY3U(0, ==, zap_remove_int(dp->dp_meta_objset,
                    scn->scn_phys.scn_queue_obj, dsobj, tx));
 
                /* Set up min/max txg */
                VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
-               if (za.za_first_integer != 0) {
+               if (za->za_first_integer != 0) {
                        scn->scn_phys.scn_cur_min_txg =
                            MAX(scn->scn_phys.scn_min_txg,
-                           za.za_first_integer);
+                           za->za_first_integer);
                } else {
                        scn->scn_phys.scn_cur_min_txg =
                            MAX(scn->scn_phys.scn_min_txg,
@@ -1389,26 +1321,41 @@ dsl_scan_visit(dsl_scan_t *scn, dmu_tx_t *tx)
                dsl_dataset_rele(ds, FTAG);
 
                dsl_scan_visitds(scn, dsobj, tx);
-               zap_cursor_fini(&zc);
+               zap_cursor_fini(zc);
                if (scn->scn_pausing)
-                       return;
+                       goto out;
        }
-       zap_cursor_fini(&zc);
+       zap_cursor_fini(zc);
+out:
+       kmem_free(za, sizeof (zap_attribute_t));
+       kmem_free(zc, sizeof (zap_cursor_t));
 }
 
-static int
-dsl_scan_free_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
+static boolean_t
+dsl_scan_free_should_pause(dsl_scan_t *scn)
 {
-       dsl_scan_t *scn = arg;
        uint64_t elapsed_nanosecs;
 
-       elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
+       if (zfs_recover)
+               return (B_FALSE);
 
-       if (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
-           (elapsed_nanosecs / MICROSEC > zfs_free_min_time_ms &&
+       elapsed_nanosecs = gethrtime() - scn->scn_sync_start_time;
+       return (elapsed_nanosecs / NANOSEC > zfs_txg_timeout ||
+           (NSEC2MSEC(elapsed_nanosecs) > zfs_free_min_time_ms &&
            txg_sync_waiting(scn->scn_dp)) ||
-           spa_shutting_down(scn->scn_dp->dp_spa))
-               return (ERESTART);
+           spa_shutting_down(scn->scn_dp->dp_spa));
+}
+
+static int
+dsl_scan_free_block_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
+{
+       dsl_scan_t *scn = arg;
+
+       if (!scn->scn_is_bptree ||
+           (BP_GET_LEVEL(bp) == 0 && BP_GET_TYPE(bp) != DMU_OT_OBJSET)) {
+               if (dsl_scan_free_should_pause(scn))
+                       return (SET_ERROR(ERESTART));
+       }
 
        zio_nowait(zio_free_sync(scn->scn_zio_root, scn->scn_dp->dp_spa,
            dmu_tx_get_txg(tx), bp, 0));
@@ -1430,7 +1377,8 @@ dsl_scan_active(dsl_scan_t *scn)
        if (spa_shutting_down(spa))
                return (B_FALSE);
 
-       if (scn->scn_phys.scn_state == DSS_SCANNING)
+       if (scn->scn_phys.scn_state == DSS_SCANNING ||
+           scn->scn_async_destroying)
                return (B_TRUE);
 
        if (spa_version(scn->scn_dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
@@ -1460,7 +1408,7 @@ dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
                        func = POOL_SCAN_RESILVER;
                zfs_dbgmsg("restarting scan func=%u txg=%llu",
                    func, tx->tx_txg);
-               dsl_scan_setup_sync(scn, &func, tx);
+               dsl_scan_setup_sync(&func, tx);
        }
 
        if (!dsl_scan_active(scn) ||
@@ -1479,17 +1427,45 @@ dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
         * traversing it.
         */
        if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
+               scn->scn_is_bptree = B_FALSE;
                scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
                    NULL, ZIO_FLAG_MUSTSUCCEED);
                err = bpobj_iterate(&dp->dp_free_bpobj,
-                   dsl_scan_free_cb, scn, tx);
+                   dsl_scan_free_block_cb, scn, tx);
                VERIFY3U(0, ==, zio_wait(scn->scn_zio_root));
+
+               if (err == 0 && spa_feature_is_active(spa,
+                   &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
+                       ASSERT(scn->scn_async_destroying);
+                       scn->scn_is_bptree = B_TRUE;
+                       scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
+                           NULL, ZIO_FLAG_MUSTSUCCEED);
+                       err = bptree_iterate(dp->dp_meta_objset,
+                           dp->dp_bptree_obj, B_TRUE, dsl_scan_free_block_cb,
+                           scn, tx);
+                       VERIFY0(zio_wait(scn->scn_zio_root));
+
+                       if (err == 0) {
+                               zfeature_info_t *feat = &spa_feature_table
+                                   [SPA_FEATURE_ASYNC_DESTROY];
+                               /* finished; deactivate async destroy feature */
+                               spa_feature_decr(spa, feat, tx);
+                               ASSERT(!spa_feature_is_active(spa, feat));
+                               VERIFY0(zap_remove(dp->dp_meta_objset,
+                                   DMU_POOL_DIRECTORY_OBJECT,
+                                   DMU_POOL_BPTREE_OBJ, tx));
+                               VERIFY0(bptree_free(dp->dp_meta_objset,
+                                   dp->dp_bptree_obj, tx));
+                               dp->dp_bptree_obj = 0;
+                               scn->scn_async_destroying = B_FALSE;
+                       }
+               }
                if (scn->scn_visited_this_txg) {
                        zfs_dbgmsg("freed %llu blocks in %llums from "
-                           "free_bpobj txg %llu",
+                           "free_bpobj/bptree txg %llu",
                            (longlong_t)scn->scn_visited_this_txg,
                            (longlong_t)
-                           (gethrtime() - scn->scn_sync_start_time) / MICROSEC,
+                           NSEC2MSEC(gethrtime() - scn->scn_sync_start_time),
                            (longlong_t)tx->tx_txg);
                        scn->scn_visited_this_txg = 0;
                        /*
@@ -1505,6 +1481,16 @@ dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
        if (scn->scn_phys.scn_state != DSS_SCANNING)
                return;
 
+       if (scn->scn_done_txg == tx->tx_txg) {
+               ASSERT(!scn->scn_pausing);
+               /* finished with scan. */
+               zfs_dbgmsg("txg %llu scan complete", tx->tx_txg);
+               dsl_scan_done(scn, B_TRUE, tx);
+               ASSERT3U(spa->spa_scrub_inflight, ==, 0);
+               dsl_scan_sync_state(scn, tx);
+               return;
+       }
+
        if (scn->scn_phys.scn_ddt_bookmark.ddb_class <=
            scn->scn_phys.scn_ddt_class_max) {
                zfs_dbgmsg("doing scan sync txg %llu; "
@@ -1529,18 +1515,20 @@ dsl_scan_sync(dsl_pool_t *dp, dmu_tx_t *tx)
 
        scn->scn_zio_root = zio_root(dp->dp_spa, NULL,
            NULL, ZIO_FLAG_CANFAIL);
+       dsl_pool_config_enter(dp, FTAG);
        dsl_scan_visit(scn, tx);
+       dsl_pool_config_exit(dp, FTAG);
        (void) zio_wait(scn->scn_zio_root);
        scn->scn_zio_root = NULL;
 
        zfs_dbgmsg("visited %llu blocks in %llums",
            (longlong_t)scn->scn_visited_this_txg,
-           (longlong_t)(gethrtime() - scn->scn_sync_start_time) / MICROSEC);
+           (longlong_t)NSEC2MSEC(gethrtime() - scn->scn_sync_start_time));
 
        if (!scn->scn_pausing) {
-               /* finished with scan. */
-               zfs_dbgmsg("finished scan txg %llu", (longlong_t)tx->tx_txg);
-               dsl_scan_done(scn, B_TRUE, tx);
+               scn->scn_done_txg = tx->tx_txg + 1;
+               zfs_dbgmsg("txg %llu traversal complete, waiting till txg %llu",
+                   tx->tx_txg, scn->scn_done_txg);
        }
 
        if (DSL_SCAN_IS_SCRUB_RESILVER(scn)) {
@@ -1601,9 +1589,13 @@ count_block(zfs_all_blkstats_t *zab, const blkptr_t *bp)
        for (i = 0; i < 4; i++) {
                int l = (i < 2) ? BP_GET_LEVEL(bp) : DN_MAX_LEVELS;
                int t = (i & 1) ? BP_GET_TYPE(bp) : DMU_OT_TOTAL;
-               zfs_blkstat_t *zb = &zab->zab_type[l][t];
                int equal;
+               zfs_blkstat_t *zb;
 
+               if (t & DMU_OT_NEWTYPE)
+                       t = DMU_OT_OTHER;
+
+               zb = &zab->zab_type[l][t];
                zb->zb_count++;
                zb->zb_asize += BP_GET_ASIZE(bp);
                zb->zb_lsize += BP_GET_LSIZE(bp);
@@ -1658,10 +1650,10 @@ dsl_scan_scrub_cb(dsl_pool_t *dp,
        size_t size = BP_GET_PSIZE(bp);
        spa_t *spa = dp->dp_spa;
        uint64_t phys_birth = BP_PHYSICAL_BIRTH(bp);
-       boolean_t needs_io;
+       boolean_t needs_io = B_FALSE;
        int zio_flags = ZIO_FLAG_SCAN_THREAD | ZIO_FLAG_RAW | ZIO_FLAG_CANFAIL;
-       int zio_priority;
        int scan_delay = 0;
+       int d;
 
        if (phys_birth <= scn->scn_phys.scn_min_txg ||
            phys_birth >= scn->scn_phys.scn_max_txg)
@@ -1672,12 +1664,11 @@ dsl_scan_scrub_cb(dsl_pool_t *dp,
        ASSERT(DSL_SCAN_IS_SCRUB_RESILVER(scn));
        if (scn->scn_phys.scn_func == POOL_SCAN_SCRUB) {
                zio_flags |= ZIO_FLAG_SCRUB;
-               zio_priority = ZIO_PRIORITY_SCRUB;
                needs_io = B_TRUE;
                scan_delay = zfs_scrub_delay;
-       } else if (scn->scn_phys.scn_func == POOL_SCAN_RESILVER) {
+       } else {
+               ASSERT3U(scn->scn_phys.scn_func, ==, POOL_SCAN_RESILVER);
                zio_flags |= ZIO_FLAG_RESILVER;
-               zio_priority = ZIO_PRIORITY_RESILVER;
                needs_io = B_FALSE;
                scan_delay = zfs_resilver_delay;
        }
@@ -1686,7 +1677,7 @@ dsl_scan_scrub_cb(dsl_pool_t *dp,
        if (zb->zb_level == ZB_ZIL_LEVEL)
                zio_flags |= ZIO_FLAG_SPECULATIVE;
 
-       for (int d = 0; d < BP_GET_NDVAS(bp); d++) {
+       for (d = 0; d < BP_GET_NDVAS(bp); d++) {
                vdev_t *vd = vdev_lookup_top(spa,
                    DVA_GET_VDEV(&bp->blk_dva[d]));
 
@@ -1735,7 +1726,7 @@ dsl_scan_scrub_cb(dsl_pool_t *dp,
                        delay(scan_delay);
 
                zio_nowait(zio_read(NULL, spa, bp, data, size,
-                   dsl_scan_scrub_done, NULL, zio_priority,
+                   dsl_scan_scrub_done, NULL, ZIO_PRIORITY_SCRUB,
                    zio_flags, zb));
        }
 
@@ -1761,6 +1752,35 @@ dsl_scan(dsl_pool_t *dp, pool_scan_func_t func)
        spa->spa_scrub_reopen = B_FALSE;
        (void) spa_vdev_state_exit(spa, NULL, 0);
 
-       return (dsl_sync_task_do(dp, dsl_scan_setup_check,
-           dsl_scan_setup_sync, dp->dp_scan, &func, 0));
+       return (dsl_sync_task(spa_name(spa), dsl_scan_setup_check,
+           dsl_scan_setup_sync, &func, 0));
 }
+
+#if defined(_KERNEL) && defined(HAVE_SPL)
+module_param(zfs_top_maxinflight, int, 0644);
+MODULE_PARM_DESC(zfs_top_maxinflight, "Max I/Os per top-level");
+
+module_param(zfs_resilver_delay, int, 0644);
+MODULE_PARM_DESC(zfs_resilver_delay, "Number of ticks to delay resilver");
+
+module_param(zfs_scrub_delay, int, 0644);
+MODULE_PARM_DESC(zfs_scrub_delay, "Number of ticks to delay scrub");
+
+module_param(zfs_scan_idle, int, 0644);
+MODULE_PARM_DESC(zfs_scan_idle, "Idle window in clock ticks");
+
+module_param(zfs_scan_min_time_ms, int, 0644);
+MODULE_PARM_DESC(zfs_scan_min_time_ms, "Min millisecs to scrub per txg");
+
+module_param(zfs_free_min_time_ms, int, 0644);
+MODULE_PARM_DESC(zfs_free_min_time_ms, "Min millisecs to free per txg");
+
+module_param(zfs_resilver_min_time_ms, int, 0644);
+MODULE_PARM_DESC(zfs_resilver_min_time_ms, "Min millisecs to resilver per txg");
+
+module_param(zfs_no_scrub_io, int, 0644);
+MODULE_PARM_DESC(zfs_no_scrub_io, "Set to disable scrub I/O");
+
+module_param(zfs_no_scrub_prefetch, int, 0644);
+MODULE_PARM_DESC(zfs_no_scrub_prefetch, "Set to disable scrub prefetching");
+#endif