]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/ddt.c
ddt: modernise assertions
[mirror_zfs.git] / module / zfs / ddt.c
index f3ccc94c859231e38d6bf3adb9d0f3869ddf060b..05b28acbcd37ba5912bcbb8e4f63ff229c848647 100644 (file)
@@ -6,7 +6,7 @@
  * You may not use this file except in compliance with the License.
  *
  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
- * or http://www.opensolaris.org/os/licensing.
+ * or https://opensource.org/licenses/CDDL-1.0.
  * See the License for the specific language governing permissions
  * and limitations under the License.
  *
@@ -22,6 +22,7 @@
 /*
  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
+ * Copyright (c) 2022 by Pawel Jakub Dawidek
  */
 
 #include <sys/zfs_context.h>
@@ -46,11 +47,11 @@ static kmem_cache_t *ddt_entry_cache;
  */
 int zfs_dedup_prefetch = 0;
 
-static const ddt_ops_t *ddt_ops[DDT_TYPES] = {
+static const ddt_ops_t *const ddt_ops[DDT_TYPES] = {
        &ddt_zap_ops,
 };
 
-static const char *ddt_class_name[DDT_CLASSES] = {
+static const char *const ddt_class_name[DDT_CLASSES] = {
        "ditto",
        "duplicate",
        "unique",
@@ -69,16 +70,16 @@ ddt_object_create(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
 
        ddt_object_name(ddt, type, class, name);
 
-       ASSERT(*objectp == 0);
-       VERIFY(ddt_ops[type]->ddt_op_create(os, objectp, tx, prehash) == 0);
-       ASSERT(*objectp != 0);
+       ASSERT3U(*objectp, ==, 0);
+       VERIFY0(ddt_ops[type]->ddt_op_create(os, objectp, tx, prehash));
+       ASSERT3U(*objectp, !=, 0);
 
-       VERIFY(zap_add(os, DMU_POOL_DIRECTORY_OBJECT, name,
-           sizeof (uint64_t), 1, objectp, tx) == 0);
+       VERIFY0(zap_add(os, DMU_POOL_DIRECTORY_OBJECT, name,
+           sizeof (uint64_t), 1, objectp, tx));
 
-       VERIFY(zap_add(os, spa->spa_ddt_stat_object, name,
+       VERIFY0(zap_add(os, spa->spa_ddt_stat_object, name,
            sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
-           &ddt->ddt_histogram[type][class], tx) == 0);
+           &ddt->ddt_histogram[type][class], tx));
 }
 
 static void
@@ -93,13 +94,14 @@ ddt_object_destroy(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
 
        ddt_object_name(ddt, type, class, name);
 
-       ASSERT(*objectp != 0);
+       ASSERT3U(*objectp, !=, 0);
        ASSERT(ddt_histogram_empty(&ddt->ddt_histogram[type][class]));
-       VERIFY(ddt_object_count(ddt, type, class, &count) == 0 && count == 0);
-       VERIFY(zap_remove(os, DMU_POOL_DIRECTORY_OBJECT, name, tx) == 0);
-       VERIFY(zap_remove(os, spa->spa_ddt_stat_object, name, tx) == 0);
-       VERIFY(ddt_ops[type]->ddt_op_destroy(os, *objectp, tx) == 0);
-       bzero(&ddt->ddt_object_stats[type][class], sizeof (ddt_object_t));
+       VERIFY0(ddt_object_count(ddt, type, class, &count));
+       VERIFY0(count);
+       VERIFY0(zap_remove(os, DMU_POOL_DIRECTORY_OBJECT, name, tx));
+       VERIFY0(zap_remove(os, spa->spa_ddt_stat_object, name, tx));
+       VERIFY0(ddt_ops[type]->ddt_op_destroy(os, *objectp, tx));
+       memset(&ddt->ddt_object_stats[type][class], 0, sizeof (ddt_object_t));
 
        *objectp = 0;
 }
@@ -155,15 +157,15 @@ ddt_object_sync(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
 
        ddt_object_name(ddt, type, class, name);
 
-       VERIFY(zap_update(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
+       VERIFY0(zap_update(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
            sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
-           &ddt->ddt_histogram[type][class], tx) == 0);
+           &ddt->ddt_histogram[type][class], tx));
 
        /*
         * Cache DDT statistics; this is the only time they'll change.
         */
-       VERIFY(ddt_object_info(ddt, type, class, &doi) == 0);
-       VERIFY(ddt_object_count(ddt, type, class, &count) == 0);
+       VERIFY0(ddt_object_info(ddt, type, class, &doi));
+       VERIFY0(ddt_object_count(ddt, type, class, &count));
 
        ddo->ddo_count = count;
        ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
@@ -253,7 +255,7 @@ void
 ddt_object_name(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
     char *name)
 {
-       (void) sprintf(name, DMU_POOL_DDT,
+       (void) snprintf(name, DDT_NAMELEN, DMU_POOL_DDT,
            zio_checksum_table[ddt->ddt_checksum].ci_name,
            ddt_ops[type]->ddt_op_name, ddt_class_name[class]);
 }
@@ -261,10 +263,9 @@ ddt_object_name(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
 void
 ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, uint64_t txg)
 {
-       int d;
-       ASSERT(txg != 0);
+       ASSERT3U(txg, !=, 0);
 
-       for (d = 0; d < SPA_DVAS_PER_BP; d++)
+       for (int d = 0; d < SPA_DVAS_PER_BP; d++)
                bp->blk_dva[d] = ddp->ddp_dva[d];
        BP_SET_BIRTH(bp, txg, ddp->ddp_phys_birth);
 }
@@ -292,7 +293,7 @@ ddt_bp_create(enum zio_checksum checksum,
        BP_SET_CHECKSUM(bp, checksum);
        BP_SET_TYPE(bp, DMU_OT_DEDUP);
        BP_SET_LEVEL(bp, 0);
-       BP_SET_DEDUP(bp, 0);
+       BP_SET_DEDUP(bp, 1);
        BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
 }
 
@@ -313,10 +314,9 @@ ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp)
 void
 ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp)
 {
-       int d;
-       ASSERT(ddp->ddp_phys_birth == 0);
+       ASSERT0(ddp->ddp_phys_birth);
 
-       for (d = 0; d < SPA_DVAS_PER_BP; d++)
+       for (int d = 0; d < SPA_DVAS_PER_BP; d++)
                ddp->ddp_dva[d] = bp->blk_dva[d];
        ddp->ddp_phys_birth = BP_PHYSICAL_BIRTH(bp);
 }
@@ -324,7 +324,7 @@ ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp)
 void
 ddt_phys_clear(ddt_phys_t *ddp)
 {
-       bzero(ddp, sizeof (*ddp));
+       memset(ddp, 0, sizeof (*ddp));
 }
 
 void
@@ -337,7 +337,7 @@ void
 ddt_phys_decref(ddt_phys_t *ddp)
 {
        if (ddp) {
-               ASSERT(ddp->ddp_refcnt > 0);
+               ASSERT3U(ddp->ddp_refcnt, >, 0);
                ddp->ddp_refcnt--;
        }
 }
@@ -348,6 +348,13 @@ ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, uint64_t txg)
        blkptr_t blk;
 
        ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
+
+       /*
+        * We clear the dedup bit so that zio_free() will actually free the
+        * space, rather than just decrementing the refcount in the DDT.
+        */
+       BP_SET_DEDUP(&blk, 0);
+
        ddt_phys_clear(ddp);
        zio_free(ddt->ddt_spa, txg, &blk);
 }
@@ -356,9 +363,8 @@ ddt_phys_t *
 ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp)
 {
        ddt_phys_t *ddp = (ddt_phys_t *)dde->dde_phys;
-       int p;
 
-       for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
+       for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
                if (DVA_EQUAL(BP_IDENTITY(bp), &ddp->ddp_dva[0]) &&
                    BP_PHYSICAL_BIRTH(bp) == ddp->ddp_phys_birth)
                        return (ddp);
@@ -370,9 +376,8 @@ uint64_t
 ddt_phys_total_refcnt(const ddt_entry_t *dde)
 {
        uint64_t refcnt = 0;
-       int p;
 
-       for (p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++)
+       for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++)
                refcnt += dde->dde_phys[p].ddp_refcnt;
 
        return (refcnt);
@@ -386,18 +391,17 @@ ddt_stat_generate(ddt_t *ddt, ddt_entry_t *dde, ddt_stat_t *dds)
        ddt_key_t *ddk = &dde->dde_key;
        uint64_t lsize = DDK_GET_LSIZE(ddk);
        uint64_t psize = DDK_GET_PSIZE(ddk);
-       int p, d;
 
-       bzero(dds, sizeof (*dds));
+       memset(dds, 0, sizeof (*dds));
 
-       for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
+       for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
                uint64_t dsize = 0;
                uint64_t refcnt = ddp->ddp_refcnt;
 
                if (ddp->ddp_phys_birth == 0)
                        continue;
 
-               for (d = 0; d < DDE_GET_NDVAS(dde); d++)
+               for (int d = 0; d < DDE_GET_NDVAS(dde); d++)
                        dsize += dva_get_dsize_sync(spa, &ddp->ddp_dva[d]);
 
                dds->dds_blocks += 1;
@@ -421,8 +425,8 @@ ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg)
 
        ASSERT(neg == 0 || neg == -1ULL);       /* add or subtract */
 
-       while (d < d_end)
-               *d++ += (*s++ ^ neg) - neg;
+       for (int i = 0; i < d_end - d; i++)
+               d[i] += (s[i] ^ neg) - neg;
 }
 
 static void
@@ -435,7 +439,7 @@ ddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg)
        ddt_stat_generate(ddt, dde, &dds);
 
        bucket = highbit64(dds.dds_ref_blocks) - 1;
-       ASSERT(bucket >= 0);
+       ASSERT3U(bucket, >=, 0);
 
        ddh = &ddt->ddt_histogram[dde->dde_type][dde->dde_class];
 
@@ -445,20 +449,16 @@ ddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg)
 void
 ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src)
 {
-       int h;
-
-       for (h = 0; h < 64; h++)
+       for (int h = 0; h < 64; h++)
                ddt_stat_add(&dst->ddh_stat[h], &src->ddh_stat[h], 0);
 }
 
 void
 ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh)
 {
-       int h;
-
-       bzero(dds, sizeof (*dds));
+       memset(dds, 0, sizeof (*dds));
 
-       for (h = 0; h < 64; h++)
+       for (int h = 0; h < 64; h++)
                ddt_stat_add(dds, &ddh->ddh_stat[h], 0);
 }
 
@@ -478,15 +478,11 @@ ddt_histogram_empty(const ddt_histogram_t *ddh)
 void
 ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total)
 {
-       enum zio_checksum c;
-       enum ddt_type type;
-       enum ddt_class class;
-
        /* Sum the statistics we cached in ddt_object_sync(). */
-       for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
+       for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
                ddt_t *ddt = spa->spa_ddt[c];
-               for (type = 0; type < DDT_TYPES; type++) {
-                       for (class = 0; class < DDT_CLASSES;
+               for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
+                       for (enum ddt_class class = 0; class < DDT_CLASSES;
                            class++) {
                                ddt_object_t *ddo =
                                    &ddt->ddt_object_stats[type][class];
@@ -507,14 +503,10 @@ ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total)
 void
 ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh)
 {
-       enum zio_checksum c;
-       enum ddt_type type;
-       enum ddt_class class;
-
-       for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
+       for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
                ddt_t *ddt = spa->spa_ddt[c];
-               for (type = 0; type < DDT_TYPES; type++) {
-                       for (class = 0; class < DDT_CLASSES;
+               for (enum ddt_type type = 0; type < DDT_TYPES && ddt; type++) {
+                       for (enum ddt_class class = 0; class < DDT_CLASSES;
                            class++) {
                                ddt_histogram_add(ddh,
                                    &ddt->ddt_histogram_cache[type][class]);
@@ -542,7 +534,7 @@ ddt_get_dedup_dspace(spa_t *spa)
        if (spa->spa_dedup_dspace != ~0ULL)
                return (spa->spa_dedup_dspace);
 
-       bzero(&dds_total, sizeof (ddt_stat_t));
+       memset(&dds_total, 0, sizeof (ddt_stat_t));
 
        /* Calculate and cache the stats */
        ddt_get_dedup_stats(spa, &dds_total);
@@ -562,67 +554,6 @@ ddt_get_pool_dedup_ratio(spa_t *spa)
        return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize);
 }
 
-int
-ddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde, ddt_phys_t *ddp_willref)
-{
-       spa_t *spa = ddt->ddt_spa;
-       uint64_t total_refcnt = 0;
-       uint64_t ditto = spa->spa_dedup_ditto;
-       int total_copies = 0;
-       int desired_copies = 0;
-       int copies_needed = 0;
-       int p;
-
-       for (p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
-               ddt_phys_t *ddp = &dde->dde_phys[p];
-               zio_t *zio = dde->dde_lead_zio[p];
-               uint64_t refcnt = ddp->ddp_refcnt;      /* committed refs */
-               if (zio != NULL)
-                       refcnt += zio->io_parent_count; /* pending refs */
-               if (ddp == ddp_willref)
-                       refcnt++;                       /* caller's ref */
-               if (refcnt != 0) {
-                       total_refcnt += refcnt;
-                       total_copies += p;
-               }
-       }
-
-       if (ditto == 0 || ditto > UINT32_MAX)
-               ditto = UINT32_MAX;
-
-       if (total_refcnt >= 1)
-               desired_copies++;
-       if (total_refcnt >= ditto)
-               desired_copies++;
-       if (total_refcnt >= ditto * ditto)
-               desired_copies++;
-
-       copies_needed = MAX(desired_copies, total_copies) - total_copies;
-
-       /* encrypted blocks store their IV in DVA[2] */
-       if (DDK_GET_CRYPT(&dde->dde_key))
-               copies_needed = MIN(copies_needed, SPA_DVAS_PER_BP - 1);
-
-       return (copies_needed);
-}
-
-int
-ddt_ditto_copies_present(ddt_entry_t *dde)
-{
-       ddt_phys_t *ddp = &dde->dde_phys[DDT_PHYS_DITTO];
-       dva_t *dva = ddp->ddp_dva;
-       int copies = 0 - DVA_GET_GANG(dva);
-       int d;
-
-       for (d = 0; d < DDE_GET_NDVAS(dde); d++, dva++)
-               if (DVA_IS_VALID(dva))
-                       copies++;
-
-       ASSERT(copies >= 0 && copies < SPA_DVAS_PER_BP);
-
-       return (copies);
-}
-
 size_t
 ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len)
 {
@@ -631,17 +562,16 @@ ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len)
        zio_compress_info_t *ci = &zio_compress_table[cpfunc];
        size_t c_len;
 
-       ASSERT(d_len >= s_len + 1);     /* no compression plus version byte */
+       ASSERT3U(d_len, >=, s_len + 1); /* no compression plus version byte */
 
        c_len = ci->ci_compress(src, dst, s_len, d_len - 1, ci->ci_level);
 
        if (c_len == s_len) {
                cpfunc = ZIO_COMPRESS_OFF;
-               bcopy(src, dst, s_len);
+               memcpy(dst, src, s_len);
        }
 
        *version = cpfunc;
-       /* CONSTCOND */
        if (ZFS_HOST_BYTEORDER)
                *version |= DDT_COMPRESS_BYTEORDER_MASK;
 
@@ -658,19 +588,13 @@ ddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len)
        if (ci->ci_decompress != NULL)
                (void) ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level);
        else
-               bcopy(src, dst, d_len);
+               memcpy(dst, src, d_len);
 
        if (((version & DDT_COMPRESS_BYTEORDER_MASK) != 0) !=
            (ZFS_HOST_BYTEORDER != 0))
                byteswap_uint64_array(dst, d_len);
 }
 
-ddt_t *
-ddt_select_by_checksum(spa_t *spa, enum zio_checksum c)
-{
-       return (spa->spa_ddt[c]);
-}
-
 ddt_t *
 ddt_select(spa_t *spa, const blkptr_t *bp)
 {
@@ -711,7 +635,7 @@ ddt_alloc(const ddt_key_t *ddk)
        ddt_entry_t *dde;
 
        dde = kmem_cache_alloc(ddt_entry_cache, KM_SLEEP);
-       bzero(dde, sizeof (ddt_entry_t));
+       memset(dde, 0, sizeof (ddt_entry_t));
        cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL);
 
        dde->dde_key = *ddk;
@@ -722,12 +646,10 @@ ddt_alloc(const ddt_key_t *ddk)
 static void
 ddt_free(ddt_entry_t *dde)
 {
-       int p;
-
        ASSERT(!dde->dde_loading);
 
-       for (p = 0; p < DDT_PHYS_TYPES; p++)
-               ASSERT(dde->dde_lead_zio[p] == NULL);
+       for (int p = 0; p < DDT_PHYS_TYPES; p++)
+               ASSERT3P(dde->dde_lead_zio[p], ==, NULL);
 
        if (dde->dde_repair_abd != NULL)
                abd_free(dde->dde_repair_abd);
@@ -781,19 +703,19 @@ ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add)
        for (type = 0; type < DDT_TYPES; type++) {
                for (class = 0; class < DDT_CLASSES; class++) {
                        error = ddt_object_lookup(ddt, type, class, dde);
-                       if (error != ENOENT)
+                       if (error != ENOENT) {
+                               ASSERT0(error);
                                break;
+                       }
                }
                if (error != ENOENT)
                        break;
        }
 
-       ASSERT(error == 0 || error == ENOENT);
-
        ddt_enter(ddt);
 
-       ASSERT(dde->dde_loaded == B_FALSE);
-       ASSERT(dde->dde_loading == B_TRUE);
+       ASSERT(!dde->dde_loaded);
+       ASSERT(dde->dde_loading);
 
        dde->dde_type = type;   /* will be DDT_TYPES if no entry found */
        dde->dde_class = class; /* will be DDT_CLASSES if no entry found */
@@ -813,8 +735,6 @@ ddt_prefetch(spa_t *spa, const blkptr_t *bp)
 {
        ddt_t *ddt;
        ddt_entry_t dde;
-       enum ddt_type type;
-       enum ddt_class class;
 
        if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp))
                return;
@@ -827,8 +747,8 @@ ddt_prefetch(spa_t *spa, const blkptr_t *bp)
        ddt = ddt_select(spa, bp);
        ddt_key_fill(&dde.dde_key, bp);
 
-       for (type = 0; type < DDT_TYPES; type++) {
-               for (class = 0; class < DDT_CLASSES; class++) {
+       for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
+               for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
                        ddt_object_prefetch(ddt, type, class, &dde);
                }
        }
@@ -851,15 +771,14 @@ ddt_entry_compare(const void *x1, const void *x2)
        const ddt_key_cmp_t *k1 = (const ddt_key_cmp_t *)&dde1->dde_key;
        const ddt_key_cmp_t *k2 = (const ddt_key_cmp_t *)&dde2->dde_key;
        int32_t cmp = 0;
-       int i;
 
-       for (i = 0; i < DDT_KEY_CMP_LEN; i++) {
+       for (int i = 0; i < DDT_KEY_CMP_LEN; i++) {
                cmp = (int32_t)k1->u16[i] - (int32_t)k2->u16[i];
                if (likely(cmp))
                        break;
        }
 
-       return (AVL_ISIGN(cmp));
+       return (TREE_ISIGN(cmp));
 }
 
 static ddt_t *
@@ -868,7 +787,7 @@ ddt_table_alloc(spa_t *spa, enum zio_checksum c)
        ddt_t *ddt;
 
        ddt = kmem_cache_alloc(ddt_cache, KM_SLEEP);
-       bzero(ddt, sizeof (ddt_t));
+       memset(ddt, 0, sizeof (ddt_t));
 
        mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL);
        avl_create(&ddt->ddt_tree, ddt_entry_compare,
@@ -885,8 +804,8 @@ ddt_table_alloc(spa_t *spa, enum zio_checksum c)
 static void
 ddt_table_free(ddt_t *ddt)
 {
-       ASSERT(avl_numnodes(&ddt->ddt_tree) == 0);
-       ASSERT(avl_numnodes(&ddt->ddt_repair_tree) == 0);
+       ASSERT0(avl_numnodes(&ddt->ddt_tree));
+       ASSERT0(avl_numnodes(&ddt->ddt_repair_tree));
        avl_destroy(&ddt->ddt_tree);
        avl_destroy(&ddt->ddt_repair_tree);
        mutex_destroy(&ddt->ddt_lock);
@@ -896,20 +815,15 @@ ddt_table_free(ddt_t *ddt)
 void
 ddt_create(spa_t *spa)
 {
-       enum zio_checksum c;
-
        spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM;
 
-       for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++)
+       for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++)
                spa->spa_ddt[c] = ddt_table_alloc(spa, c);
 }
 
 int
 ddt_load(spa_t *spa)
 {
-       enum zio_checksum c;
-       enum ddt_type type;
-       enum ddt_class class;
        int error;
 
        ddt_create(spa);
@@ -921,10 +835,10 @@ ddt_load(spa_t *spa)
        if (error)
                return (error == ENOENT ? 0 : error);
 
-       for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
+       for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
                ddt_t *ddt = spa->spa_ddt[c];
-               for (type = 0; type < DDT_TYPES; type++) {
-                       for (class = 0; class < DDT_CLASSES;
+               for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
+                       for (enum ddt_class class = 0; class < DDT_CLASSES;
                            class++) {
                                error = ddt_object_load(ddt, type, class);
                                if (error != 0 && error != ENOENT)
@@ -935,7 +849,7 @@ ddt_load(spa_t *spa)
                /*
                 * Seed the cached histograms.
                 */
-               bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
+               memcpy(&ddt->ddt_histogram_cache, ddt->ddt_histogram,
                    sizeof (ddt->ddt_histogram));
                spa->spa_dedup_dspace = ~0ULL;
        }
@@ -946,9 +860,7 @@ ddt_load(spa_t *spa)
 void
 ddt_unload(spa_t *spa)
 {
-       enum zio_checksum c;
-
-       for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
+       for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
                if (spa->spa_ddt[c]) {
                        ddt_table_free(spa->spa_ddt[c]);
                        spa->spa_ddt[c] = NULL;
@@ -961,8 +873,6 @@ ddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp)
 {
        ddt_t *ddt;
        ddt_entry_t *dde;
-       enum ddt_type type;
-       enum ddt_class class;
 
        if (!BP_GET_DEDUP(bp))
                return (B_FALSE);
@@ -975,8 +885,8 @@ ddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp)
 
        ddt_key_fill(&(dde->dde_key), bp);
 
-       for (type = 0; type < DDT_TYPES; type++) {
-               for (class = 0; class <= max_class; class++) {
+       for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
+               for (enum ddt_class class = 0; class <= max_class; class++) {
                        if (ddt_object_lookup(ddt, type, class, dde) == 0) {
                                kmem_cache_free(ddt_entry_cache, dde);
                                return (B_TRUE);
@@ -993,15 +903,13 @@ ddt_repair_start(ddt_t *ddt, const blkptr_t *bp)
 {
        ddt_key_t ddk;
        ddt_entry_t *dde;
-       enum ddt_type type;
-       enum ddt_class class;
 
        ddt_key_fill(&ddk, bp);
 
        dde = ddt_alloc(&ddk);
 
-       for (type = 0; type < DDT_TYPES; type++) {
-               for (class = 0; class < DDT_CLASSES; class++) {
+       for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
+               for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
                        /*
                         * We can only do repair if there are multiple copies
                         * of the block.  For anything in the UNIQUE class,
@@ -1013,7 +921,7 @@ ddt_repair_start(ddt_t *ddt, const blkptr_t *bp)
                }
        }
 
-       bzero(dde->dde_phys, sizeof (dde->dde_phys));
+       memset(dde->dde_phys, 0, sizeof (dde->dde_phys));
 
        return (dde);
 }
@@ -1051,15 +959,14 @@ ddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio)
        ddt_key_t *rddk = &rdde->dde_key;
        zio_t *zio;
        blkptr_t blk;
-       int p;
 
        zio = zio_null(rio, rio->io_spa, NULL,
            ddt_repair_entry_done, rdde, rio->io_flags);
 
-       for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) {
+       for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) {
                if (ddp->ddp_phys_birth == 0 ||
                    ddp->ddp_phys_birth != rddp->ddp_phys_birth ||
-                   bcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva)))
+                   memcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva)))
                        continue;
                ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
                zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk,
@@ -1106,20 +1013,22 @@ ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
        enum ddt_class oclass = dde->dde_class;
        enum ddt_class nclass;
        uint64_t total_refcnt = 0;
-       int p;
 
        ASSERT(dde->dde_loaded);
        ASSERT(!dde->dde_loading);
 
-       for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
-               ASSERT(dde->dde_lead_zio[p] == NULL);
+       for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
+               ASSERT3P(dde->dde_lead_zio[p], ==, NULL);
                if (ddp->ddp_phys_birth == 0) {
-                       ASSERT(ddp->ddp_refcnt == 0);
+                       ASSERT0(ddp->ddp_refcnt);
                        continue;
                }
                if (p == DDT_PHYS_DITTO) {
-                       if (ddt_ditto_copies_needed(ddt, dde, NULL) == 0)
-                               ddt_phys_free(ddt, ddk, ddp, txg);
+                       /*
+                        * Note, we no longer create DDT-DITTO blocks, but we
+                        * don't want to leak any written by older software.
+                        */
+                       ddt_phys_free(ddt, ddk, ddp, txg);
                        continue;
                }
                if (ddp->ddp_refcnt == 0)
@@ -1127,17 +1036,18 @@ ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
                total_refcnt += ddp->ddp_refcnt;
        }
 
-       if (dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth != 0)
-               nclass = DDT_CLASS_DITTO;
-       else if (total_refcnt > 1)
+       /* We do not create new DDT-DITTO blocks. */
+       ASSERT0(dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth);
+       if (total_refcnt > 1)
                nclass = DDT_CLASS_DUPLICATE;
        else
                nclass = DDT_CLASS_UNIQUE;
 
        if (otype != DDT_TYPES &&
            (otype != ntype || oclass != nclass || total_refcnt == 0)) {
-               VERIFY(ddt_object_remove(ddt, otype, oclass, dde, tx) == 0);
-               ASSERT(ddt_object_lookup(ddt, otype, oclass, dde) == ENOENT);
+               VERIFY0(ddt_object_remove(ddt, otype, oclass, dde, tx));
+               ASSERT3U(
+                   ddt_object_lookup(ddt, otype, oclass, dde), ==, ENOENT);
        }
 
        if (total_refcnt != 0) {
@@ -1146,7 +1056,7 @@ ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
                ddt_stat_update(ddt, dde, 0);
                if (!ddt_object_exists(ddt, ntype, nclass))
                        ddt_object_create(ddt, ntype, nclass, tx);
-               VERIFY(ddt_object_update(ddt, ntype, nclass, dde, tx) == 0);
+               VERIFY0(ddt_object_update(ddt, ntype, nclass, dde, tx));
 
                /*
                 * If the class changes, the order that we scan this bp
@@ -1168,13 +1078,11 @@ ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
        spa_t *spa = ddt->ddt_spa;
        ddt_entry_t *dde;
        void *cookie = NULL;
-       enum ddt_type type;
-       enum ddt_class class;
 
        if (avl_numnodes(&ddt->ddt_tree) == 0)
                return;
 
-       ASSERT(spa->spa_uberblock.ub_version >= SPA_VERSION_DEDUP);
+       ASSERT3U(spa->spa_uberblock.ub_version, >=, SPA_VERSION_DEDUP);
 
        if (spa->spa_ddt_stat_object == 0) {
                spa->spa_ddt_stat_object = zap_create_link(ddt->ddt_os,
@@ -1187,23 +1095,23 @@ ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
                ddt_free(dde);
        }
 
-       for (type = 0; type < DDT_TYPES; type++) {
+       for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
                uint64_t add, count = 0;
-               for (class = 0; class < DDT_CLASSES; class++) {
+               for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
                        if (ddt_object_exists(ddt, type, class)) {
                                ddt_object_sync(ddt, type, class, tx);
-                               VERIFY(ddt_object_count(ddt, type, class,
-                                   &add) == 0);
+                               VERIFY0(ddt_object_count(ddt, type, class,
+                                   &add));
                                count += add;
                        }
                }
-               for (class = 0; class < DDT_CLASSES; class++) {
+               for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
                        if (count == 0 && ddt_object_exists(ddt, type, class))
                                ddt_object_destroy(ddt, type, class, tx);
                }
        }
 
-       bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
+       memcpy(&ddt->ddt_histogram_cache, ddt->ddt_histogram,
            sizeof (ddt->ddt_histogram));
        spa->spa_dedup_dspace = ~0ULL;
 }
@@ -1211,16 +1119,27 @@ ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
 void
 ddt_sync(spa_t *spa, uint64_t txg)
 {
+       dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
        dmu_tx_t *tx;
-       zio_t *rio = zio_root(spa, NULL, NULL,
-           ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
-       enum zio_checksum c;
+       zio_t *rio;
 
-       ASSERT(spa_syncing_txg(spa) == txg);
+       ASSERT3U(spa_syncing_txg(spa), ==, txg);
 
        tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
 
-       for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
+       rio = zio_root(spa, NULL, NULL,
+           ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SELF_HEAL);
+
+       /*
+        * This function may cause an immediate scan of ddt blocks (see
+        * the comment above dsl_scan_ddt() for details). We set the
+        * scan's root zio here so that we can wait for any scan IOs in
+        * addition to the regular ddt IOs.
+        */
+       ASSERT3P(scn->scn_zio_root, ==, NULL);
+       scn->scn_zio_root = rio;
+
+       for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
                ddt_t *ddt = spa->spa_ddt[c];
                if (ddt == NULL)
                        continue;
@@ -1229,6 +1148,7 @@ ddt_sync(spa_t *spa, uint64_t txg)
        }
 
        (void) zio_wait(rio);
+       scn->scn_zio_root = NULL;
 
        dmu_tx_commit(tx);
 }
@@ -1263,7 +1183,68 @@ ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde)
        return (SET_ERROR(ENOENT));
 }
 
-#if defined(_KERNEL) && defined(HAVE_SPL)
-module_param(zfs_dedup_prefetch, int, 0644);
-MODULE_PARM_DESC(zfs_dedup_prefetch, "Enable prefetching dedup-ed blks");
-#endif
+/*
+ * This function is used by Block Cloning (brt.c) to increase reference
+ * counter for the DDT entry if the block is already in DDT.
+ *
+ * Return false if the block, despite having the D bit set, is not present
+ * in the DDT. Currently this is not possible but might be in the future.
+ * See the comment below.
+ */
+boolean_t
+ddt_addref(spa_t *spa, const blkptr_t *bp)
+{
+       ddt_t *ddt;
+       ddt_entry_t *dde;
+       boolean_t result;
+
+       spa_config_enter(spa, SCL_ZIO, FTAG, RW_READER);
+       ddt = ddt_select(spa, bp);
+       ddt_enter(ddt);
+
+       dde = ddt_lookup(ddt, bp, B_TRUE);
+       ASSERT3P(dde, !=, NULL);
+
+       if (dde->dde_type < DDT_TYPES) {
+               ddt_phys_t *ddp;
+
+               ASSERT3S(dde->dde_class, <, DDT_CLASSES);
+
+               ddp = &dde->dde_phys[BP_GET_NDVAS(bp)];
+
+               /*
+                * This entry already existed (dde_type is real), so it must
+                * have refcnt >0 at the start of this txg. We are called from
+                * brt_pending_apply(), before frees are issued, so the refcnt
+                * can't be lowered yet. Therefore, it must be >0. We assert
+                * this because if the order of BRT and DDT interactions were
+                * ever to change and the refcnt was ever zero here, then
+                * likely further action is required to fill out the DDT entry,
+                * and this is a place that is likely to be missed in testing.
+                */
+               ASSERT3U(ddp->ddp_refcnt, >, 0);
+
+               ddt_phys_addref(ddp);
+               result = B_TRUE;
+       } else {
+               /*
+                * At the time of implementating this if the block has the
+                * DEDUP flag set it must exist in the DEDUP table, but
+                * there are many advocates that want ability to remove
+                * entries from DDT with refcnt=1. If this will happen,
+                * we may have a block with the DEDUP set, but which doesn't
+                * have a corresponding entry in the DDT. Be ready.
+                */
+               ASSERT3S(dde->dde_class, ==, DDT_CLASSES);
+               ddt_remove(ddt, dde);
+               result = B_FALSE;
+       }
+
+       ddt_exit(ddt);
+       spa_config_exit(spa, SCL_ZIO, FTAG);
+
+       return (result);
+}
+
+ZFS_MODULE_PARAM(zfs_dedup, zfs_dedup_, prefetch, INT, ZMOD_RW,
+       "Enable prefetching dedup-ed blks");