4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
27 #include <sys/zfs_context.h>
29 #include <sys/spa_impl.h>
33 #include <sys/dmu_tx.h>
35 #include <sys/dsl_pool.h>
36 #include <sys/zio_checksum.h>
37 #include <sys/zio_compress.h>
38 #include <sys/dsl_scan.h>
41 static kmem_cache_t *ddt_cache;
42 static kmem_cache_t *ddt_entry_cache;
45 * Enable/disable prefetching of dedup-ed blocks which are going to be freed.
47 int zfs_dedup_prefetch = 0;
49 static const ddt_ops_t *ddt_ops[DDT_TYPES] = {
53 static const char *ddt_class_name[DDT_CLASSES] = {
60 ddt_object_create(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
63 spa_t *spa = ddt->ddt_spa;
64 objset_t *os = ddt->ddt_os;
65 uint64_t *objectp = &ddt->ddt_object[type][class];
66 boolean_t prehash = zio_checksum_table[ddt->ddt_checksum].ci_flags &
68 char name[DDT_NAMELEN];
70 ddt_object_name(ddt, type, class, name);
72 ASSERT(*objectp == 0);
73 VERIFY(ddt_ops[type]->ddt_op_create(os, objectp, tx, prehash) == 0);
74 ASSERT(*objectp != 0);
76 VERIFY(zap_add(os, DMU_POOL_DIRECTORY_OBJECT, name,
77 sizeof (uint64_t), 1, objectp, tx) == 0);
79 VERIFY(zap_add(os, spa->spa_ddt_stat_object, name,
80 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
81 &ddt->ddt_histogram[type][class], tx) == 0);
85 ddt_object_destroy(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
88 spa_t *spa = ddt->ddt_spa;
89 objset_t *os = ddt->ddt_os;
90 uint64_t *objectp = &ddt->ddt_object[type][class];
92 char name[DDT_NAMELEN];
94 ddt_object_name(ddt, type, class, name);
96 ASSERT(*objectp != 0);
97 ASSERT(ddt_histogram_empty(&ddt->ddt_histogram[type][class]));
98 VERIFY(ddt_object_count(ddt, type, class, &count) == 0 && count == 0);
99 VERIFY(zap_remove(os, DMU_POOL_DIRECTORY_OBJECT, name, tx) == 0);
100 VERIFY(zap_remove(os, spa->spa_ddt_stat_object, name, tx) == 0);
101 VERIFY(ddt_ops[type]->ddt_op_destroy(os, *objectp, tx) == 0);
102 bzero(&ddt->ddt_object_stats[type][class], sizeof (ddt_object_t));
108 ddt_object_load(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
110 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
111 dmu_object_info_t doi;
113 char name[DDT_NAMELEN];
116 ddt_object_name(ddt, type, class, name);
118 error = zap_lookup(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT, name,
119 sizeof (uint64_t), 1, &ddt->ddt_object[type][class]);
123 error = zap_lookup(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
124 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
125 &ddt->ddt_histogram[type][class]);
130 * Seed the cached statistics.
132 error = ddt_object_info(ddt, type, class, &doi);
136 error = ddt_object_count(ddt, type, class, &count);
140 ddo->ddo_count = count;
141 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
142 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
148 ddt_object_sync(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
151 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
152 dmu_object_info_t doi;
154 char name[DDT_NAMELEN];
156 ddt_object_name(ddt, type, class, name);
158 VERIFY(zap_update(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
159 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
160 &ddt->ddt_histogram[type][class], tx) == 0);
163 * Cache DDT statistics; this is the only time they'll change.
165 VERIFY(ddt_object_info(ddt, type, class, &doi) == 0);
166 VERIFY(ddt_object_count(ddt, type, class, &count) == 0);
168 ddo->ddo_count = count;
169 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
170 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
174 ddt_object_lookup(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
177 if (!ddt_object_exists(ddt, type, class))
178 return (SET_ERROR(ENOENT));
180 return (ddt_ops[type]->ddt_op_lookup(ddt->ddt_os,
181 ddt->ddt_object[type][class], dde));
185 ddt_object_prefetch(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
188 if (!ddt_object_exists(ddt, type, class))
191 ddt_ops[type]->ddt_op_prefetch(ddt->ddt_os,
192 ddt->ddt_object[type][class], dde);
196 ddt_object_update(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
197 ddt_entry_t *dde, dmu_tx_t *tx)
199 ASSERT(ddt_object_exists(ddt, type, class));
201 return (ddt_ops[type]->ddt_op_update(ddt->ddt_os,
202 ddt->ddt_object[type][class], dde, tx));
206 ddt_object_remove(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
207 ddt_entry_t *dde, dmu_tx_t *tx)
209 ASSERT(ddt_object_exists(ddt, type, class));
211 return (ddt_ops[type]->ddt_op_remove(ddt->ddt_os,
212 ddt->ddt_object[type][class], dde, tx));
216 ddt_object_walk(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
217 uint64_t *walk, ddt_entry_t *dde)
219 ASSERT(ddt_object_exists(ddt, type, class));
221 return (ddt_ops[type]->ddt_op_walk(ddt->ddt_os,
222 ddt->ddt_object[type][class], dde, walk));
226 ddt_object_count(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
229 ASSERT(ddt_object_exists(ddt, type, class));
231 return (ddt_ops[type]->ddt_op_count(ddt->ddt_os,
232 ddt->ddt_object[type][class], count));
236 ddt_object_info(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
237 dmu_object_info_t *doi)
239 if (!ddt_object_exists(ddt, type, class))
240 return (SET_ERROR(ENOENT));
242 return (dmu_object_info(ddt->ddt_os, ddt->ddt_object[type][class],
247 ddt_object_exists(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
249 return (!!ddt->ddt_object[type][class]);
253 ddt_object_name(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
256 (void) sprintf(name, DMU_POOL_DDT,
257 zio_checksum_table[ddt->ddt_checksum].ci_name,
258 ddt_ops[type]->ddt_op_name, ddt_class_name[class]);
262 ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, uint64_t txg)
266 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
267 bp->blk_dva[d] = ddp->ddp_dva[d];
268 BP_SET_BIRTH(bp, txg, ddp->ddp_phys_birth);
272 * The bp created via this function may be used for repairs and scrub, but it
273 * will be missing the salt / IV required to do a full decrypting read.
276 ddt_bp_create(enum zio_checksum checksum,
277 const ddt_key_t *ddk, const ddt_phys_t *ddp, blkptr_t *bp)
282 ddt_bp_fill(ddp, bp, ddp->ddp_phys_birth);
284 bp->blk_cksum = ddk->ddk_cksum;
286 BP_SET_LSIZE(bp, DDK_GET_LSIZE(ddk));
287 BP_SET_PSIZE(bp, DDK_GET_PSIZE(ddk));
288 BP_SET_COMPRESS(bp, DDK_GET_COMPRESS(ddk));
289 BP_SET_CRYPT(bp, DDK_GET_CRYPT(ddk));
291 BP_SET_CHECKSUM(bp, checksum);
292 BP_SET_TYPE(bp, DMU_OT_DEDUP);
295 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
299 ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp)
301 ddk->ddk_cksum = bp->blk_cksum;
304 ASSERT(BP_IS_ENCRYPTED(bp) || !BP_USES_CRYPT(bp));
306 DDK_SET_LSIZE(ddk, BP_GET_LSIZE(bp));
307 DDK_SET_PSIZE(ddk, BP_GET_PSIZE(bp));
308 DDK_SET_COMPRESS(ddk, BP_GET_COMPRESS(bp));
309 DDK_SET_CRYPT(ddk, BP_USES_CRYPT(bp));
313 ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp)
315 ASSERT(ddp->ddp_phys_birth == 0);
317 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
318 ddp->ddp_dva[d] = bp->blk_dva[d];
319 ddp->ddp_phys_birth = BP_PHYSICAL_BIRTH(bp);
323 ddt_phys_clear(ddt_phys_t *ddp)
325 bzero(ddp, sizeof (*ddp));
329 ddt_phys_addref(ddt_phys_t *ddp)
335 ddt_phys_decref(ddt_phys_t *ddp)
338 ASSERT(ddp->ddp_refcnt > 0);
344 ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, uint64_t txg)
348 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
351 * We clear the dedup bit so that zio_free() will actually free the
352 * space, rather than just decrementing the refcount in the DDT.
354 BP_SET_DEDUP(&blk, 0);
357 zio_free(ddt->ddt_spa, txg, &blk);
361 ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp)
363 ddt_phys_t *ddp = (ddt_phys_t *)dde->dde_phys;
365 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
366 if (DVA_EQUAL(BP_IDENTITY(bp), &ddp->ddp_dva[0]) &&
367 BP_PHYSICAL_BIRTH(bp) == ddp->ddp_phys_birth)
374 ddt_phys_total_refcnt(const ddt_entry_t *dde)
378 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++)
379 refcnt += dde->dde_phys[p].ddp_refcnt;
385 ddt_stat_generate(ddt_t *ddt, ddt_entry_t *dde, ddt_stat_t *dds)
387 spa_t *spa = ddt->ddt_spa;
388 ddt_phys_t *ddp = dde->dde_phys;
389 ddt_key_t *ddk = &dde->dde_key;
390 uint64_t lsize = DDK_GET_LSIZE(ddk);
391 uint64_t psize = DDK_GET_PSIZE(ddk);
393 bzero(dds, sizeof (*dds));
395 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
397 uint64_t refcnt = ddp->ddp_refcnt;
399 if (ddp->ddp_phys_birth == 0)
402 for (int d = 0; d < DDE_GET_NDVAS(dde); d++)
403 dsize += dva_get_dsize_sync(spa, &ddp->ddp_dva[d]);
405 dds->dds_blocks += 1;
406 dds->dds_lsize += lsize;
407 dds->dds_psize += psize;
408 dds->dds_dsize += dsize;
410 dds->dds_ref_blocks += refcnt;
411 dds->dds_ref_lsize += lsize * refcnt;
412 dds->dds_ref_psize += psize * refcnt;
413 dds->dds_ref_dsize += dsize * refcnt;
418 ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg)
420 const uint64_t *s = (const uint64_t *)src;
421 uint64_t *d = (uint64_t *)dst;
422 uint64_t *d_end = (uint64_t *)(dst + 1);
424 ASSERT(neg == 0 || neg == -1ULL); /* add or subtract */
427 *d++ += (*s++ ^ neg) - neg;
431 ddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg)
434 ddt_histogram_t *ddh;
437 ddt_stat_generate(ddt, dde, &dds);
439 bucket = highbit64(dds.dds_ref_blocks) - 1;
442 ddh = &ddt->ddt_histogram[dde->dde_type][dde->dde_class];
444 ddt_stat_add(&ddh->ddh_stat[bucket], &dds, neg);
448 ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src)
450 for (int h = 0; h < 64; h++)
451 ddt_stat_add(&dst->ddh_stat[h], &src->ddh_stat[h], 0);
455 ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh)
457 bzero(dds, sizeof (*dds));
459 for (int h = 0; h < 64; h++)
460 ddt_stat_add(dds, &ddh->ddh_stat[h], 0);
464 ddt_histogram_empty(const ddt_histogram_t *ddh)
466 const uint64_t *s = (const uint64_t *)ddh;
467 const uint64_t *s_end = (const uint64_t *)(ddh + 1);
477 ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total)
479 /* Sum the statistics we cached in ddt_object_sync(). */
480 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
481 ddt_t *ddt = spa->spa_ddt[c];
482 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
483 for (enum ddt_class class = 0; class < DDT_CLASSES;
486 &ddt->ddt_object_stats[type][class];
487 ddo_total->ddo_count += ddo->ddo_count;
488 ddo_total->ddo_dspace += ddo->ddo_dspace;
489 ddo_total->ddo_mspace += ddo->ddo_mspace;
494 /* ... and compute the averages. */
495 if (ddo_total->ddo_count != 0) {
496 ddo_total->ddo_dspace /= ddo_total->ddo_count;
497 ddo_total->ddo_mspace /= ddo_total->ddo_count;
502 ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh)
504 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
505 ddt_t *ddt = spa->spa_ddt[c];
506 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
507 for (enum ddt_class class = 0; class < DDT_CLASSES;
509 ddt_histogram_add(ddh,
510 &ddt->ddt_histogram_cache[type][class]);
517 ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total)
519 ddt_histogram_t *ddh_total;
521 ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
522 ddt_get_dedup_histogram(spa, ddh_total);
523 ddt_histogram_stat(dds_total, ddh_total);
524 kmem_free(ddh_total, sizeof (ddt_histogram_t));
528 ddt_get_dedup_dspace(spa_t *spa)
530 ddt_stat_t dds_total;
532 if (spa->spa_dedup_dspace != ~0ULL)
533 return (spa->spa_dedup_dspace);
535 bzero(&dds_total, sizeof (ddt_stat_t));
537 /* Calculate and cache the stats */
538 ddt_get_dedup_stats(spa, &dds_total);
539 spa->spa_dedup_dspace = dds_total.dds_ref_dsize - dds_total.dds_dsize;
540 return (spa->spa_dedup_dspace);
544 ddt_get_pool_dedup_ratio(spa_t *spa)
546 ddt_stat_t dds_total = { 0 };
548 ddt_get_dedup_stats(spa, &dds_total);
549 if (dds_total.dds_dsize == 0)
552 return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize);
556 ddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde, ddt_phys_t *ddp_willref)
558 spa_t *spa = ddt->ddt_spa;
559 uint64_t total_refcnt = 0;
560 uint64_t ditto = spa->spa_dedup_ditto;
561 int total_copies = 0;
562 int desired_copies = 0;
563 int copies_needed = 0;
565 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
566 ddt_phys_t *ddp = &dde->dde_phys[p];
567 zio_t *zio = dde->dde_lead_zio[p];
568 uint64_t refcnt = ddp->ddp_refcnt; /* committed refs */
570 refcnt += zio->io_parent_count; /* pending refs */
571 if (ddp == ddp_willref)
572 refcnt++; /* caller's ref */
574 total_refcnt += refcnt;
579 if (ditto == 0 || ditto > UINT32_MAX)
582 if (total_refcnt >= 1)
584 if (total_refcnt >= ditto)
586 if (total_refcnt >= ditto * ditto)
589 copies_needed = MAX(desired_copies, total_copies) - total_copies;
591 /* encrypted blocks store their IV in DVA[2] */
592 if (DDK_GET_CRYPT(&dde->dde_key))
593 copies_needed = MIN(copies_needed, SPA_DVAS_PER_BP - 1);
595 return (copies_needed);
599 ddt_ditto_copies_present(ddt_entry_t *dde)
601 ddt_phys_t *ddp = &dde->dde_phys[DDT_PHYS_DITTO];
602 dva_t *dva = ddp->ddp_dva;
603 int copies = 0 - DVA_GET_GANG(dva);
605 for (int d = 0; d < DDE_GET_NDVAS(dde); d++, dva++)
606 if (DVA_IS_VALID(dva))
609 ASSERT(copies >= 0 && copies < SPA_DVAS_PER_BP);
615 ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len)
617 uchar_t *version = dst++;
618 int cpfunc = ZIO_COMPRESS_ZLE;
619 zio_compress_info_t *ci = &zio_compress_table[cpfunc];
622 ASSERT(d_len >= s_len + 1); /* no compression plus version byte */
624 c_len = ci->ci_compress(src, dst, s_len, d_len - 1, ci->ci_level);
626 if (c_len == s_len) {
627 cpfunc = ZIO_COMPRESS_OFF;
628 bcopy(src, dst, s_len);
633 if (ZFS_HOST_BYTEORDER)
634 *version |= DDT_COMPRESS_BYTEORDER_MASK;
640 ddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len)
642 uchar_t version = *src++;
643 int cpfunc = version & DDT_COMPRESS_FUNCTION_MASK;
644 zio_compress_info_t *ci = &zio_compress_table[cpfunc];
646 if (ci->ci_decompress != NULL)
647 (void) ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level);
649 bcopy(src, dst, d_len);
651 if (((version & DDT_COMPRESS_BYTEORDER_MASK) != 0) !=
652 (ZFS_HOST_BYTEORDER != 0))
653 byteswap_uint64_array(dst, d_len);
657 ddt_select_by_checksum(spa_t *spa, enum zio_checksum c)
659 return (spa->spa_ddt[c]);
663 ddt_select(spa_t *spa, const blkptr_t *bp)
665 return (spa->spa_ddt[BP_GET_CHECKSUM(bp)]);
669 ddt_enter(ddt_t *ddt)
671 mutex_enter(&ddt->ddt_lock);
677 mutex_exit(&ddt->ddt_lock);
683 ddt_cache = kmem_cache_create("ddt_cache",
684 sizeof (ddt_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
685 ddt_entry_cache = kmem_cache_create("ddt_entry_cache",
686 sizeof (ddt_entry_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
692 kmem_cache_destroy(ddt_entry_cache);
693 kmem_cache_destroy(ddt_cache);
697 ddt_alloc(const ddt_key_t *ddk)
701 dde = kmem_cache_alloc(ddt_entry_cache, KM_SLEEP);
702 bzero(dde, sizeof (ddt_entry_t));
703 cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL);
711 ddt_free(ddt_entry_t *dde)
713 ASSERT(!dde->dde_loading);
715 for (int p = 0; p < DDT_PHYS_TYPES; p++)
716 ASSERT(dde->dde_lead_zio[p] == NULL);
718 if (dde->dde_repair_abd != NULL)
719 abd_free(dde->dde_repair_abd);
721 cv_destroy(&dde->dde_cv);
722 kmem_cache_free(ddt_entry_cache, dde);
726 ddt_remove(ddt_t *ddt, ddt_entry_t *dde)
728 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
730 avl_remove(&ddt->ddt_tree, dde);
735 ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add)
737 ddt_entry_t *dde, dde_search;
739 enum ddt_class class;
743 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
745 ddt_key_fill(&dde_search.dde_key, bp);
747 dde = avl_find(&ddt->ddt_tree, &dde_search, &where);
751 dde = ddt_alloc(&dde_search.dde_key);
752 avl_insert(&ddt->ddt_tree, dde, where);
755 while (dde->dde_loading)
756 cv_wait(&dde->dde_cv, &ddt->ddt_lock);
761 dde->dde_loading = B_TRUE;
767 for (type = 0; type < DDT_TYPES; type++) {
768 for (class = 0; class < DDT_CLASSES; class++) {
769 error = ddt_object_lookup(ddt, type, class, dde);
770 if (error != ENOENT) {
781 ASSERT(dde->dde_loaded == B_FALSE);
782 ASSERT(dde->dde_loading == B_TRUE);
784 dde->dde_type = type; /* will be DDT_TYPES if no entry found */
785 dde->dde_class = class; /* will be DDT_CLASSES if no entry found */
786 dde->dde_loaded = B_TRUE;
787 dde->dde_loading = B_FALSE;
790 ddt_stat_update(ddt, dde, -1ULL);
792 cv_broadcast(&dde->dde_cv);
798 ddt_prefetch(spa_t *spa, const blkptr_t *bp)
803 if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp))
807 * We only remove the DDT once all tables are empty and only
808 * prefetch dedup blocks when there are entries in the DDT.
809 * Thus no locking is required as the DDT can't disappear on us.
811 ddt = ddt_select(spa, bp);
812 ddt_key_fill(&dde.dde_key, bp);
814 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
815 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
816 ddt_object_prefetch(ddt, type, class, &dde);
822 * Opaque struct used for ddt_key comparison
824 #define DDT_KEY_CMP_LEN (sizeof (ddt_key_t) / sizeof (uint16_t))
826 typedef struct ddt_key_cmp {
827 uint16_t u16[DDT_KEY_CMP_LEN];
831 ddt_entry_compare(const void *x1, const void *x2)
833 const ddt_entry_t *dde1 = x1;
834 const ddt_entry_t *dde2 = x2;
835 const ddt_key_cmp_t *k1 = (const ddt_key_cmp_t *)&dde1->dde_key;
836 const ddt_key_cmp_t *k2 = (const ddt_key_cmp_t *)&dde2->dde_key;
839 for (int i = 0; i < DDT_KEY_CMP_LEN; i++) {
840 cmp = (int32_t)k1->u16[i] - (int32_t)k2->u16[i];
845 return (AVL_ISIGN(cmp));
849 ddt_table_alloc(spa_t *spa, enum zio_checksum c)
853 ddt = kmem_cache_alloc(ddt_cache, KM_SLEEP);
854 bzero(ddt, sizeof (ddt_t));
856 mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL);
857 avl_create(&ddt->ddt_tree, ddt_entry_compare,
858 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
859 avl_create(&ddt->ddt_repair_tree, ddt_entry_compare,
860 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
861 ddt->ddt_checksum = c;
863 ddt->ddt_os = spa->spa_meta_objset;
869 ddt_table_free(ddt_t *ddt)
871 ASSERT(avl_numnodes(&ddt->ddt_tree) == 0);
872 ASSERT(avl_numnodes(&ddt->ddt_repair_tree) == 0);
873 avl_destroy(&ddt->ddt_tree);
874 avl_destroy(&ddt->ddt_repair_tree);
875 mutex_destroy(&ddt->ddt_lock);
876 kmem_cache_free(ddt_cache, ddt);
880 ddt_create(spa_t *spa)
882 spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM;
884 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++)
885 spa->spa_ddt[c] = ddt_table_alloc(spa, c);
895 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
896 DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
897 &spa->spa_ddt_stat_object);
900 return (error == ENOENT ? 0 : error);
902 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
903 ddt_t *ddt = spa->spa_ddt[c];
904 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
905 for (enum ddt_class class = 0; class < DDT_CLASSES;
907 error = ddt_object_load(ddt, type, class);
908 if (error != 0 && error != ENOENT)
914 * Seed the cached histograms.
916 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
917 sizeof (ddt->ddt_histogram));
918 spa->spa_dedup_dspace = ~0ULL;
925 ddt_unload(spa_t *spa)
927 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
928 if (spa->spa_ddt[c]) {
929 ddt_table_free(spa->spa_ddt[c]);
930 spa->spa_ddt[c] = NULL;
936 ddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp)
941 if (!BP_GET_DEDUP(bp))
944 if (max_class == DDT_CLASS_UNIQUE)
947 ddt = spa->spa_ddt[BP_GET_CHECKSUM(bp)];
948 dde = kmem_cache_alloc(ddt_entry_cache, KM_SLEEP);
950 ddt_key_fill(&(dde->dde_key), bp);
952 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
953 for (enum ddt_class class = 0; class <= max_class; class++) {
954 if (ddt_object_lookup(ddt, type, class, dde) == 0) {
955 kmem_cache_free(ddt_entry_cache, dde);
961 kmem_cache_free(ddt_entry_cache, dde);
966 ddt_repair_start(ddt_t *ddt, const blkptr_t *bp)
971 ddt_key_fill(&ddk, bp);
973 dde = ddt_alloc(&ddk);
975 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
976 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
978 * We can only do repair if there are multiple copies
979 * of the block. For anything in the UNIQUE class,
980 * there's definitely only one copy, so don't even try.
982 if (class != DDT_CLASS_UNIQUE &&
983 ddt_object_lookup(ddt, type, class, dde) == 0)
988 bzero(dde->dde_phys, sizeof (dde->dde_phys));
994 ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde)
1000 if (dde->dde_repair_abd != NULL && spa_writeable(ddt->ddt_spa) &&
1001 avl_find(&ddt->ddt_repair_tree, dde, &where) == NULL)
1002 avl_insert(&ddt->ddt_repair_tree, dde, where);
1010 ddt_repair_entry_done(zio_t *zio)
1012 ddt_entry_t *rdde = zio->io_private;
1018 ddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio)
1020 ddt_phys_t *ddp = dde->dde_phys;
1021 ddt_phys_t *rddp = rdde->dde_phys;
1022 ddt_key_t *ddk = &dde->dde_key;
1023 ddt_key_t *rddk = &rdde->dde_key;
1027 zio = zio_null(rio, rio->io_spa, NULL,
1028 ddt_repair_entry_done, rdde, rio->io_flags);
1030 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) {
1031 if (ddp->ddp_phys_birth == 0 ||
1032 ddp->ddp_phys_birth != rddp->ddp_phys_birth ||
1033 bcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva)))
1035 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
1036 zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk,
1037 rdde->dde_repair_abd, DDK_GET_PSIZE(rddk), NULL, NULL,
1038 ZIO_PRIORITY_SYNC_WRITE, ZIO_DDT_CHILD_FLAGS(zio), NULL));
1045 ddt_repair_table(ddt_t *ddt, zio_t *rio)
1047 spa_t *spa = ddt->ddt_spa;
1048 ddt_entry_t *dde, *rdde_next, *rdde;
1049 avl_tree_t *t = &ddt->ddt_repair_tree;
1052 if (spa_sync_pass(spa) > 1)
1056 for (rdde = avl_first(t); rdde != NULL; rdde = rdde_next) {
1057 rdde_next = AVL_NEXT(t, rdde);
1058 avl_remove(&ddt->ddt_repair_tree, rdde);
1060 ddt_bp_create(ddt->ddt_checksum, &rdde->dde_key, NULL, &blk);
1061 dde = ddt_repair_start(ddt, &blk);
1062 ddt_repair_entry(ddt, dde, rdde, rio);
1063 ddt_repair_done(ddt, dde);
1070 ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
1072 dsl_pool_t *dp = ddt->ddt_spa->spa_dsl_pool;
1073 ddt_phys_t *ddp = dde->dde_phys;
1074 ddt_key_t *ddk = &dde->dde_key;
1075 enum ddt_type otype = dde->dde_type;
1076 enum ddt_type ntype = DDT_TYPE_CURRENT;
1077 enum ddt_class oclass = dde->dde_class;
1078 enum ddt_class nclass;
1079 uint64_t total_refcnt = 0;
1081 ASSERT(dde->dde_loaded);
1082 ASSERT(!dde->dde_loading);
1084 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1085 ASSERT(dde->dde_lead_zio[p] == NULL);
1086 if (ddp->ddp_phys_birth == 0) {
1087 ASSERT(ddp->ddp_refcnt == 0);
1090 if (p == DDT_PHYS_DITTO) {
1091 if (ddt_ditto_copies_needed(ddt, dde, NULL) == 0)
1092 ddt_phys_free(ddt, ddk, ddp, txg);
1095 if (ddp->ddp_refcnt == 0)
1096 ddt_phys_free(ddt, ddk, ddp, txg);
1097 total_refcnt += ddp->ddp_refcnt;
1100 if (dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth != 0)
1101 nclass = DDT_CLASS_DITTO;
1102 else if (total_refcnt > 1)
1103 nclass = DDT_CLASS_DUPLICATE;
1105 nclass = DDT_CLASS_UNIQUE;
1107 if (otype != DDT_TYPES &&
1108 (otype != ntype || oclass != nclass || total_refcnt == 0)) {
1109 VERIFY(ddt_object_remove(ddt, otype, oclass, dde, tx) == 0);
1110 ASSERT(ddt_object_lookup(ddt, otype, oclass, dde) == ENOENT);
1113 if (total_refcnt != 0) {
1114 dde->dde_type = ntype;
1115 dde->dde_class = nclass;
1116 ddt_stat_update(ddt, dde, 0);
1117 if (!ddt_object_exists(ddt, ntype, nclass))
1118 ddt_object_create(ddt, ntype, nclass, tx);
1119 VERIFY(ddt_object_update(ddt, ntype, nclass, dde, tx) == 0);
1122 * If the class changes, the order that we scan this bp
1123 * changes. If it decreases, we could miss it, so
1124 * scan it right now. (This covers both class changing
1125 * while we are doing ddt_walk(), and when we are
1128 if (nclass < oclass) {
1129 dsl_scan_ddt_entry(dp->dp_scan,
1130 ddt->ddt_checksum, dde, tx);
1136 ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
1138 spa_t *spa = ddt->ddt_spa;
1140 void *cookie = NULL;
1142 if (avl_numnodes(&ddt->ddt_tree) == 0)
1145 ASSERT(spa->spa_uberblock.ub_version >= SPA_VERSION_DEDUP);
1147 if (spa->spa_ddt_stat_object == 0) {
1148 spa->spa_ddt_stat_object = zap_create_link(ddt->ddt_os,
1149 DMU_OT_DDT_STATS, DMU_POOL_DIRECTORY_OBJECT,
1150 DMU_POOL_DDT_STATS, tx);
1153 while ((dde = avl_destroy_nodes(&ddt->ddt_tree, &cookie)) != NULL) {
1154 ddt_sync_entry(ddt, dde, tx, txg);
1158 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1159 uint64_t add, count = 0;
1160 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
1161 if (ddt_object_exists(ddt, type, class)) {
1162 ddt_object_sync(ddt, type, class, tx);
1163 VERIFY(ddt_object_count(ddt, type, class,
1168 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
1169 if (count == 0 && ddt_object_exists(ddt, type, class))
1170 ddt_object_destroy(ddt, type, class, tx);
1174 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
1175 sizeof (ddt->ddt_histogram));
1176 spa->spa_dedup_dspace = ~0ULL;
1180 ddt_sync(spa_t *spa, uint64_t txg)
1182 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1186 ASSERT(spa_syncing_txg(spa) == txg);
1188 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1190 rio = zio_root(spa, NULL, NULL,
1191 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SELF_HEAL);
1194 * This function may cause an immediate scan of ddt blocks (see
1195 * the comment above dsl_scan_ddt() for details). We set the
1196 * scan's root zio here so that we can wait for any scan IOs in
1197 * addition to the regular ddt IOs.
1199 ASSERT3P(scn->scn_zio_root, ==, NULL);
1200 scn->scn_zio_root = rio;
1202 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1203 ddt_t *ddt = spa->spa_ddt[c];
1206 ddt_sync_table(ddt, tx, txg);
1207 ddt_repair_table(ddt, rio);
1210 (void) zio_wait(rio);
1211 scn->scn_zio_root = NULL;
1217 ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde)
1222 ddt_t *ddt = spa->spa_ddt[ddb->ddb_checksum];
1224 if (ddt_object_exists(ddt, ddb->ddb_type,
1226 error = ddt_object_walk(ddt,
1227 ddb->ddb_type, ddb->ddb_class,
1228 &ddb->ddb_cursor, dde);
1230 dde->dde_type = ddb->ddb_type;
1231 dde->dde_class = ddb->ddb_class;
1234 if (error != ENOENT)
1236 ddb->ddb_cursor = 0;
1237 } while (++ddb->ddb_checksum < ZIO_CHECKSUM_FUNCTIONS);
1238 ddb->ddb_checksum = 0;
1239 } while (++ddb->ddb_type < DDT_TYPES);
1241 } while (++ddb->ddb_class < DDT_CLASSES);
1243 return (SET_ERROR(ENOENT));
1246 #if defined(_KERNEL)
1247 module_param(zfs_dedup_prefetch, int, 0644);
1248 MODULE_PARM_DESC(zfs_dedup_prefetch, "Enable prefetching dedup-ed blks");