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>
42 * Enable/disable prefetching of dedup-ed blocks which are going to be freed.
44 int zfs_dedup_prefetch = 1;
46 SYSCTL_DECL(_vfs_zfs);
47 SYSCTL_NODE(_vfs_zfs, OID_AUTO, dedup, CTLFLAG_RW, 0, "ZFS DEDUP");
48 SYSCTL_INT(_vfs_zfs_dedup, OID_AUTO, prefetch, CTLFLAG_RWTUN, &zfs_dedup_prefetch,
49 0, "Enable/disable prefetching of dedup-ed blocks which are going to be freed");
51 static const ddt_ops_t *ddt_ops[DDT_TYPES] = {
55 static const char *ddt_class_name[DDT_CLASSES] = {
62 ddt_object_create(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
65 spa_t *spa = ddt->ddt_spa;
66 objset_t *os = ddt->ddt_os;
67 uint64_t *objectp = &ddt->ddt_object[type][class];
68 boolean_t prehash = zio_checksum_table[ddt->ddt_checksum].ci_flags &
70 char name[DDT_NAMELEN];
72 ddt_object_name(ddt, type, class, name);
74 ASSERT(*objectp == 0);
75 VERIFY(ddt_ops[type]->ddt_op_create(os, objectp, tx, prehash) == 0);
76 ASSERT(*objectp != 0);
78 VERIFY(zap_add(os, DMU_POOL_DIRECTORY_OBJECT, name,
79 sizeof (uint64_t), 1, objectp, tx) == 0);
81 VERIFY(zap_add(os, spa->spa_ddt_stat_object, name,
82 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
83 &ddt->ddt_histogram[type][class], tx) == 0);
87 ddt_object_destroy(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
90 spa_t *spa = ddt->ddt_spa;
91 objset_t *os = ddt->ddt_os;
92 uint64_t *objectp = &ddt->ddt_object[type][class];
94 char name[DDT_NAMELEN];
96 ddt_object_name(ddt, type, class, name);
98 ASSERT(*objectp != 0);
99 VERIFY(ddt_object_count(ddt, type, class, &count) == 0 && count == 0);
100 ASSERT(ddt_histogram_empty(&ddt->ddt_histogram[type][class]));
101 VERIFY(zap_remove(os, DMU_POOL_DIRECTORY_OBJECT, name, tx) == 0);
102 VERIFY(zap_remove(os, spa->spa_ddt_stat_object, name, tx) == 0);
103 VERIFY(ddt_ops[type]->ddt_op_destroy(os, *objectp, tx) == 0);
104 bzero(&ddt->ddt_object_stats[type][class], sizeof (ddt_object_t));
110 ddt_object_load(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
112 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
113 dmu_object_info_t doi;
115 char name[DDT_NAMELEN];
118 ddt_object_name(ddt, type, class, name);
120 error = zap_lookup(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT, name,
121 sizeof (uint64_t), 1, &ddt->ddt_object[type][class]);
126 VERIFY0(zap_lookup(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
127 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
128 &ddt->ddt_histogram[type][class]));
131 * Seed the cached statistics.
133 VERIFY(ddt_object_info(ddt, type, class, &doi) == 0);
135 error = ddt_object_count(ddt, type, class, &count);
139 ddo->ddo_count = count;
140 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
141 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
147 ddt_object_sync(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
150 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
151 dmu_object_info_t doi;
153 char name[DDT_NAMELEN];
155 ddt_object_name(ddt, type, class, name);
157 VERIFY(zap_update(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
158 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
159 &ddt->ddt_histogram[type][class], tx) == 0);
162 * Cache DDT statistics; this is the only time they'll change.
164 VERIFY(ddt_object_info(ddt, type, class, &doi) == 0);
165 VERIFY(ddt_object_count(ddt, type, class, &count) == 0);
167 ddo->ddo_count = count;
168 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
169 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
173 ddt_object_lookup(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
176 if (!ddt_object_exists(ddt, type, class))
177 return (SET_ERROR(ENOENT));
179 return (ddt_ops[type]->ddt_op_lookup(ddt->ddt_os,
180 ddt->ddt_object[type][class], dde));
184 ddt_object_prefetch(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
187 if (!ddt_object_exists(ddt, type, class))
190 ddt_ops[type]->ddt_op_prefetch(ddt->ddt_os,
191 ddt->ddt_object[type][class], dde);
195 ddt_object_update(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
196 ddt_entry_t *dde, dmu_tx_t *tx)
198 ASSERT(ddt_object_exists(ddt, type, class));
200 return (ddt_ops[type]->ddt_op_update(ddt->ddt_os,
201 ddt->ddt_object[type][class], dde, tx));
205 ddt_object_remove(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
206 ddt_entry_t *dde, dmu_tx_t *tx)
208 ASSERT(ddt_object_exists(ddt, type, class));
210 return (ddt_ops[type]->ddt_op_remove(ddt->ddt_os,
211 ddt->ddt_object[type][class], dde, tx));
215 ddt_object_walk(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
216 uint64_t *walk, ddt_entry_t *dde)
218 ASSERT(ddt_object_exists(ddt, type, class));
220 return (ddt_ops[type]->ddt_op_walk(ddt->ddt_os,
221 ddt->ddt_object[type][class], dde, walk));
225 ddt_object_count(ddt_t *ddt, enum ddt_type type, enum ddt_class class, uint64_t *count)
227 ASSERT(ddt_object_exists(ddt, type, class));
229 return (ddt_ops[type]->ddt_op_count(ddt->ddt_os,
230 ddt->ddt_object[type][class], count));
234 ddt_object_info(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
235 dmu_object_info_t *doi)
237 if (!ddt_object_exists(ddt, type, class))
238 return (SET_ERROR(ENOENT));
240 return (dmu_object_info(ddt->ddt_os, ddt->ddt_object[type][class],
245 ddt_object_exists(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
247 return (!!ddt->ddt_object[type][class]);
251 ddt_object_name(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
254 (void) sprintf(name, DMU_POOL_DDT,
255 zio_checksum_table[ddt->ddt_checksum].ci_name,
256 ddt_ops[type]->ddt_op_name, ddt_class_name[class]);
260 ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, uint64_t txg)
264 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
265 bp->blk_dva[d] = ddp->ddp_dva[d];
266 BP_SET_BIRTH(bp, txg, ddp->ddp_phys_birth);
270 ddt_bp_create(enum zio_checksum checksum,
271 const ddt_key_t *ddk, const ddt_phys_t *ddp, blkptr_t *bp)
276 ddt_bp_fill(ddp, bp, ddp->ddp_phys_birth);
278 bp->blk_cksum = ddk->ddk_cksum;
281 BP_SET_LSIZE(bp, DDK_GET_LSIZE(ddk));
282 BP_SET_PSIZE(bp, DDK_GET_PSIZE(ddk));
283 BP_SET_COMPRESS(bp, DDK_GET_COMPRESS(ddk));
284 BP_SET_CHECKSUM(bp, checksum);
285 BP_SET_TYPE(bp, DMU_OT_DEDUP);
288 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
292 ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp)
294 ddk->ddk_cksum = bp->blk_cksum;
297 DDK_SET_LSIZE(ddk, BP_GET_LSIZE(bp));
298 DDK_SET_PSIZE(ddk, BP_GET_PSIZE(bp));
299 DDK_SET_COMPRESS(ddk, BP_GET_COMPRESS(bp));
303 ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp)
305 ASSERT(ddp->ddp_phys_birth == 0);
307 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
308 ddp->ddp_dva[d] = bp->blk_dva[d];
309 ddp->ddp_phys_birth = BP_PHYSICAL_BIRTH(bp);
313 ddt_phys_clear(ddt_phys_t *ddp)
315 bzero(ddp, sizeof (*ddp));
319 ddt_phys_addref(ddt_phys_t *ddp)
325 ddt_phys_decref(ddt_phys_t *ddp)
327 ASSERT((int64_t)ddp->ddp_refcnt > 0);
332 ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, uint64_t txg)
336 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
338 zio_free(ddt->ddt_spa, txg, &blk);
342 ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp)
344 ddt_phys_t *ddp = (ddt_phys_t *)dde->dde_phys;
346 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
347 if (DVA_EQUAL(BP_IDENTITY(bp), &ddp->ddp_dva[0]) &&
348 BP_PHYSICAL_BIRTH(bp) == ddp->ddp_phys_birth)
355 ddt_phys_total_refcnt(const ddt_entry_t *dde)
359 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++)
360 refcnt += dde->dde_phys[p].ddp_refcnt;
366 ddt_stat_generate(ddt_t *ddt, ddt_entry_t *dde, ddt_stat_t *dds)
368 spa_t *spa = ddt->ddt_spa;
369 ddt_phys_t *ddp = dde->dde_phys;
370 ddt_key_t *ddk = &dde->dde_key;
371 uint64_t lsize = DDK_GET_LSIZE(ddk);
372 uint64_t psize = DDK_GET_PSIZE(ddk);
374 bzero(dds, sizeof (*dds));
376 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
378 uint64_t refcnt = ddp->ddp_refcnt;
380 if (ddp->ddp_phys_birth == 0)
383 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
384 dsize += dva_get_dsize_sync(spa, &ddp->ddp_dva[d]);
386 dds->dds_blocks += 1;
387 dds->dds_lsize += lsize;
388 dds->dds_psize += psize;
389 dds->dds_dsize += dsize;
391 dds->dds_ref_blocks += refcnt;
392 dds->dds_ref_lsize += lsize * refcnt;
393 dds->dds_ref_psize += psize * refcnt;
394 dds->dds_ref_dsize += dsize * refcnt;
399 ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg)
401 const uint64_t *s = (const uint64_t *)src;
402 uint64_t *d = (uint64_t *)dst;
403 uint64_t *d_end = (uint64_t *)(dst + 1);
405 ASSERT(neg == 0 || neg == -1ULL); /* add or subtract */
408 *d++ += (*s++ ^ neg) - neg;
412 ddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg)
415 ddt_histogram_t *ddh;
418 ddt_stat_generate(ddt, dde, &dds);
420 bucket = highbit64(dds.dds_ref_blocks) - 1;
423 ddh = &ddt->ddt_histogram[dde->dde_type][dde->dde_class];
425 ddt_stat_add(&ddh->ddh_stat[bucket], &dds, neg);
429 ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src)
431 for (int h = 0; h < 64; h++)
432 ddt_stat_add(&dst->ddh_stat[h], &src->ddh_stat[h], 0);
436 ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh)
438 bzero(dds, sizeof (*dds));
440 for (int h = 0; h < 64; h++)
441 ddt_stat_add(dds, &ddh->ddh_stat[h], 0);
445 ddt_histogram_empty(const ddt_histogram_t *ddh)
447 const uint64_t *s = (const uint64_t *)ddh;
448 const uint64_t *s_end = (const uint64_t *)(ddh + 1);
458 ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total)
460 /* Sum the statistics we cached in ddt_object_sync(). */
461 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
462 ddt_t *ddt = spa->spa_ddt[c];
463 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
464 for (enum ddt_class class = 0; class < DDT_CLASSES;
467 &ddt->ddt_object_stats[type][class];
468 ddo_total->ddo_count += ddo->ddo_count;
469 ddo_total->ddo_dspace += ddo->ddo_dspace;
470 ddo_total->ddo_mspace += ddo->ddo_mspace;
475 /* ... and compute the averages. */
476 if (ddo_total->ddo_count != 0) {
477 ddo_total->ddo_dspace /= ddo_total->ddo_count;
478 ddo_total->ddo_mspace /= ddo_total->ddo_count;
483 ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh)
485 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
486 ddt_t *ddt = spa->spa_ddt[c];
487 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
488 for (enum ddt_class class = 0; class < DDT_CLASSES;
490 ddt_histogram_add(ddh,
491 &ddt->ddt_histogram_cache[type][class]);
498 ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total)
500 ddt_histogram_t *ddh_total;
502 ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
503 ddt_get_dedup_histogram(spa, ddh_total);
504 ddt_histogram_stat(dds_total, ddh_total);
505 kmem_free(ddh_total, sizeof (ddt_histogram_t));
509 ddt_get_dedup_dspace(spa_t *spa)
511 ddt_stat_t dds_total = { 0 };
513 ddt_get_dedup_stats(spa, &dds_total);
514 return (dds_total.dds_ref_dsize - dds_total.dds_dsize);
518 ddt_get_pool_dedup_ratio(spa_t *spa)
520 ddt_stat_t dds_total = { 0 };
522 ddt_get_dedup_stats(spa, &dds_total);
523 if (dds_total.dds_dsize == 0)
526 return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize);
530 ddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde, ddt_phys_t *ddp_willref)
532 spa_t *spa = ddt->ddt_spa;
533 uint64_t total_refcnt = 0;
534 uint64_t ditto = spa->spa_dedup_ditto;
535 int total_copies = 0;
536 int desired_copies = 0;
538 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
539 ddt_phys_t *ddp = &dde->dde_phys[p];
540 zio_t *zio = dde->dde_lead_zio[p];
541 uint64_t refcnt = ddp->ddp_refcnt; /* committed refs */
543 refcnt += zio->io_parent_count; /* pending refs */
544 if (ddp == ddp_willref)
545 refcnt++; /* caller's ref */
547 total_refcnt += refcnt;
552 if (ditto == 0 || ditto > UINT32_MAX)
555 if (total_refcnt >= 1)
557 if (total_refcnt >= ditto)
559 if (total_refcnt >= ditto * ditto)
562 return (MAX(desired_copies, total_copies) - total_copies);
566 ddt_ditto_copies_present(ddt_entry_t *dde)
568 ddt_phys_t *ddp = &dde->dde_phys[DDT_PHYS_DITTO];
569 dva_t *dva = ddp->ddp_dva;
570 int copies = 0 - DVA_GET_GANG(dva);
572 for (int d = 0; d < SPA_DVAS_PER_BP; d++, dva++)
573 if (DVA_IS_VALID(dva))
576 ASSERT(copies >= 0 && copies < SPA_DVAS_PER_BP);
582 ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len)
584 uchar_t *version = dst++;
585 int cpfunc = ZIO_COMPRESS_ZLE;
586 zio_compress_info_t *ci = &zio_compress_table[cpfunc];
589 ASSERT(d_len >= s_len + 1); /* no compression plus version byte */
591 c_len = ci->ci_compress(src, dst, s_len, d_len - 1, ci->ci_level);
593 if (c_len == s_len) {
594 cpfunc = ZIO_COMPRESS_OFF;
595 bcopy(src, dst, s_len);
600 if (ZFS_HOST_BYTEORDER)
601 *version |= DDT_COMPRESS_BYTEORDER_MASK;
607 ddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len)
609 uchar_t version = *src++;
610 int cpfunc = version & DDT_COMPRESS_FUNCTION_MASK;
611 zio_compress_info_t *ci = &zio_compress_table[cpfunc];
613 if (ci->ci_decompress != NULL)
614 (void) ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level);
616 bcopy(src, dst, d_len);
618 if (((version & DDT_COMPRESS_BYTEORDER_MASK) != 0) !=
619 (ZFS_HOST_BYTEORDER != 0))
620 byteswap_uint64_array(dst, d_len);
624 ddt_select_by_checksum(spa_t *spa, enum zio_checksum c)
626 return (spa->spa_ddt[c]);
630 ddt_select(spa_t *spa, const blkptr_t *bp)
632 return (spa->spa_ddt[BP_GET_CHECKSUM(bp)]);
636 ddt_enter(ddt_t *ddt)
638 mutex_enter(&ddt->ddt_lock);
644 mutex_exit(&ddt->ddt_lock);
648 ddt_alloc(const ddt_key_t *ddk)
652 dde = kmem_zalloc(sizeof (ddt_entry_t), KM_SLEEP);
653 cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL);
661 ddt_free(ddt_entry_t *dde)
663 ASSERT(!dde->dde_loading);
665 for (int p = 0; p < DDT_PHYS_TYPES; p++)
666 ASSERT(dde->dde_lead_zio[p] == NULL);
668 if (dde->dde_repair_abd != NULL)
669 abd_free(dde->dde_repair_abd);
671 cv_destroy(&dde->dde_cv);
672 kmem_free(dde, sizeof (*dde));
676 ddt_remove(ddt_t *ddt, ddt_entry_t *dde)
678 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
680 avl_remove(&ddt->ddt_tree, dde);
685 ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add)
687 ddt_entry_t *dde, dde_search;
689 enum ddt_class class;
693 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
695 ddt_key_fill(&dde_search.dde_key, bp);
697 dde = avl_find(&ddt->ddt_tree, &dde_search, &where);
701 dde = ddt_alloc(&dde_search.dde_key);
702 avl_insert(&ddt->ddt_tree, dde, where);
705 while (dde->dde_loading)
706 cv_wait(&dde->dde_cv, &ddt->ddt_lock);
711 dde->dde_loading = B_TRUE;
717 for (type = 0; type < DDT_TYPES; type++) {
718 for (class = 0; class < DDT_CLASSES; class++) {
719 error = ddt_object_lookup(ddt, type, class, dde);
720 if (error != ENOENT) {
731 ASSERT(dde->dde_loaded == B_FALSE);
732 ASSERT(dde->dde_loading == B_TRUE);
734 dde->dde_type = type; /* will be DDT_TYPES if no entry found */
735 dde->dde_class = class; /* will be DDT_CLASSES if no entry found */
736 dde->dde_loaded = B_TRUE;
737 dde->dde_loading = B_FALSE;
740 ddt_stat_update(ddt, dde, -1ULL);
742 cv_broadcast(&dde->dde_cv);
748 ddt_prefetch(spa_t *spa, const blkptr_t *bp)
753 if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp))
757 * We only remove the DDT once all tables are empty and only
758 * prefetch dedup blocks when there are entries in the DDT.
759 * Thus no locking is required as the DDT can't disappear on us.
761 ddt = ddt_select(spa, bp);
762 ddt_key_fill(&dde.dde_key, bp);
764 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
765 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
766 ddt_object_prefetch(ddt, type, class, &dde);
772 ddt_entry_compare(const void *x1, const void *x2)
774 const ddt_entry_t *dde1 = x1;
775 const ddt_entry_t *dde2 = x2;
776 const uint64_t *u1 = (const uint64_t *)&dde1->dde_key;
777 const uint64_t *u2 = (const uint64_t *)&dde2->dde_key;
779 for (int i = 0; i < DDT_KEY_WORDS; i++) {
790 ddt_table_alloc(spa_t *spa, enum zio_checksum c)
794 ddt = kmem_zalloc(sizeof (*ddt), KM_SLEEP);
796 mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL);
797 avl_create(&ddt->ddt_tree, ddt_entry_compare,
798 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
799 avl_create(&ddt->ddt_repair_tree, ddt_entry_compare,
800 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
801 ddt->ddt_checksum = c;
803 ddt->ddt_os = spa->spa_meta_objset;
809 ddt_table_free(ddt_t *ddt)
811 ASSERT(avl_numnodes(&ddt->ddt_tree) == 0);
812 ASSERT(avl_numnodes(&ddt->ddt_repair_tree) == 0);
813 avl_destroy(&ddt->ddt_tree);
814 avl_destroy(&ddt->ddt_repair_tree);
815 mutex_destroy(&ddt->ddt_lock);
816 kmem_free(ddt, sizeof (*ddt));
820 ddt_create(spa_t *spa)
822 spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM;
824 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++)
825 spa->spa_ddt[c] = ddt_table_alloc(spa, c);
835 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
836 DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
837 &spa->spa_ddt_stat_object);
840 return (error == ENOENT ? 0 : error);
842 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
843 ddt_t *ddt = spa->spa_ddt[c];
844 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
845 for (enum ddt_class class = 0; class < DDT_CLASSES;
847 error = ddt_object_load(ddt, type, class);
848 if (error != 0 && error != ENOENT)
854 * Seed the cached histograms.
856 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
857 sizeof (ddt->ddt_histogram));
864 ddt_unload(spa_t *spa)
866 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
867 if (spa->spa_ddt[c]) {
868 ddt_table_free(spa->spa_ddt[c]);
869 spa->spa_ddt[c] = NULL;
875 ddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp)
880 if (!BP_GET_DEDUP(bp))
883 if (max_class == DDT_CLASS_UNIQUE)
886 ddt = spa->spa_ddt[BP_GET_CHECKSUM(bp)];
888 ddt_key_fill(&dde.dde_key, bp);
890 for (enum ddt_type type = 0; type < DDT_TYPES; type++)
891 for (enum ddt_class class = 0; class <= max_class; class++)
892 if (ddt_object_lookup(ddt, type, class, &dde) == 0)
899 ddt_repair_start(ddt_t *ddt, const blkptr_t *bp)
904 ddt_key_fill(&ddk, bp);
906 dde = ddt_alloc(&ddk);
908 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
909 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
911 * We can only do repair if there are multiple copies
912 * of the block. For anything in the UNIQUE class,
913 * there's definitely only one copy, so don't even try.
915 if (class != DDT_CLASS_UNIQUE &&
916 ddt_object_lookup(ddt, type, class, dde) == 0)
921 bzero(dde->dde_phys, sizeof (dde->dde_phys));
927 ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde)
933 if (dde->dde_repair_abd != NULL && spa_writeable(ddt->ddt_spa) &&
934 avl_find(&ddt->ddt_repair_tree, dde, &where) == NULL)
935 avl_insert(&ddt->ddt_repair_tree, dde, where);
943 ddt_repair_entry_done(zio_t *zio)
945 ddt_entry_t *rdde = zio->io_private;
951 ddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio)
953 ddt_phys_t *ddp = dde->dde_phys;
954 ddt_phys_t *rddp = rdde->dde_phys;
955 ddt_key_t *ddk = &dde->dde_key;
956 ddt_key_t *rddk = &rdde->dde_key;
960 zio = zio_null(rio, rio->io_spa, NULL,
961 ddt_repair_entry_done, rdde, rio->io_flags);
963 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) {
964 if (ddp->ddp_phys_birth == 0 ||
965 ddp->ddp_phys_birth != rddp->ddp_phys_birth ||
966 bcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva)))
968 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
969 zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk,
970 rdde->dde_repair_abd, DDK_GET_PSIZE(rddk), NULL, NULL,
971 ZIO_PRIORITY_SYNC_WRITE, ZIO_DDT_CHILD_FLAGS(zio), NULL));
978 ddt_repair_table(ddt_t *ddt, zio_t *rio)
980 spa_t *spa = ddt->ddt_spa;
981 ddt_entry_t *dde, *rdde_next, *rdde;
982 avl_tree_t *t = &ddt->ddt_repair_tree;
985 if (spa_sync_pass(spa) > 1)
989 for (rdde = avl_first(t); rdde != NULL; rdde = rdde_next) {
990 rdde_next = AVL_NEXT(t, rdde);
991 avl_remove(&ddt->ddt_repair_tree, rdde);
993 ddt_bp_create(ddt->ddt_checksum, &rdde->dde_key, NULL, &blk);
994 dde = ddt_repair_start(ddt, &blk);
995 ddt_repair_entry(ddt, dde, rdde, rio);
996 ddt_repair_done(ddt, dde);
1003 ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
1005 dsl_pool_t *dp = ddt->ddt_spa->spa_dsl_pool;
1006 ddt_phys_t *ddp = dde->dde_phys;
1007 ddt_key_t *ddk = &dde->dde_key;
1008 enum ddt_type otype = dde->dde_type;
1009 enum ddt_type ntype = DDT_TYPE_CURRENT;
1010 enum ddt_class oclass = dde->dde_class;
1011 enum ddt_class nclass;
1012 uint64_t total_refcnt = 0;
1014 ASSERT(dde->dde_loaded);
1015 ASSERT(!dde->dde_loading);
1017 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1018 ASSERT(dde->dde_lead_zio[p] == NULL);
1019 ASSERT((int64_t)ddp->ddp_refcnt >= 0);
1020 if (ddp->ddp_phys_birth == 0) {
1021 ASSERT(ddp->ddp_refcnt == 0);
1024 if (p == DDT_PHYS_DITTO) {
1025 if (ddt_ditto_copies_needed(ddt, dde, NULL) == 0)
1026 ddt_phys_free(ddt, ddk, ddp, txg);
1029 if (ddp->ddp_refcnt == 0)
1030 ddt_phys_free(ddt, ddk, ddp, txg);
1031 total_refcnt += ddp->ddp_refcnt;
1034 if (dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth != 0)
1035 nclass = DDT_CLASS_DITTO;
1036 else if (total_refcnt > 1)
1037 nclass = DDT_CLASS_DUPLICATE;
1039 nclass = DDT_CLASS_UNIQUE;
1041 if (otype != DDT_TYPES &&
1042 (otype != ntype || oclass != nclass || total_refcnt == 0)) {
1043 VERIFY(ddt_object_remove(ddt, otype, oclass, dde, tx) == 0);
1044 ASSERT(ddt_object_lookup(ddt, otype, oclass, dde) == ENOENT);
1047 if (total_refcnt != 0) {
1048 dde->dde_type = ntype;
1049 dde->dde_class = nclass;
1050 ddt_stat_update(ddt, dde, 0);
1051 if (!ddt_object_exists(ddt, ntype, nclass))
1052 ddt_object_create(ddt, ntype, nclass, tx);
1053 VERIFY(ddt_object_update(ddt, ntype, nclass, dde, tx) == 0);
1056 * If the class changes, the order that we scan this bp
1057 * changes. If it decreases, we could miss it, so
1058 * scan it right now. (This covers both class changing
1059 * while we are doing ddt_walk(), and when we are
1062 if (nclass < oclass) {
1063 dsl_scan_ddt_entry(dp->dp_scan,
1064 ddt->ddt_checksum, dde, tx);
1070 ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
1072 spa_t *spa = ddt->ddt_spa;
1074 void *cookie = NULL;
1076 if (avl_numnodes(&ddt->ddt_tree) == 0)
1079 ASSERT(spa->spa_uberblock.ub_version >= SPA_VERSION_DEDUP);
1081 if (spa->spa_ddt_stat_object == 0) {
1082 spa->spa_ddt_stat_object = zap_create_link(ddt->ddt_os,
1083 DMU_OT_DDT_STATS, DMU_POOL_DIRECTORY_OBJECT,
1084 DMU_POOL_DDT_STATS, tx);
1087 while ((dde = avl_destroy_nodes(&ddt->ddt_tree, &cookie)) != NULL) {
1088 ddt_sync_entry(ddt, dde, tx, txg);
1092 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1093 uint64_t add, count = 0;
1094 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
1095 if (ddt_object_exists(ddt, type, class)) {
1096 ddt_object_sync(ddt, type, class, tx);
1097 VERIFY(ddt_object_count(ddt, type, class,
1102 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
1103 if (count == 0 && ddt_object_exists(ddt, type, class))
1104 ddt_object_destroy(ddt, type, class, tx);
1108 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
1109 sizeof (ddt->ddt_histogram));
1113 ddt_sync(spa_t *spa, uint64_t txg)
1115 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1119 ASSERT(spa_syncing_txg(spa) == txg);
1121 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1123 rio = zio_root(spa, NULL, NULL,
1124 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SELF_HEAL);
1127 * This function may cause an immediate scan of ddt blocks (see
1128 * the comment above dsl_scan_ddt() for details). We set the
1129 * scan's root zio here so that we can wait for any scan IOs in
1130 * addition to the regular ddt IOs.
1132 ASSERT3P(scn->scn_zio_root, ==, NULL);
1133 scn->scn_zio_root = rio;
1135 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1136 ddt_t *ddt = spa->spa_ddt[c];
1139 ddt_sync_table(ddt, tx, txg);
1140 ddt_repair_table(ddt, rio);
1143 (void) zio_wait(rio);
1144 scn->scn_zio_root = NULL;
1150 ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde)
1155 ddt_t *ddt = spa->spa_ddt[ddb->ddb_checksum];
1157 if (ddt_object_exists(ddt, ddb->ddb_type,
1159 error = ddt_object_walk(ddt,
1160 ddb->ddb_type, ddb->ddb_class,
1161 &ddb->ddb_cursor, dde);
1163 dde->dde_type = ddb->ddb_type;
1164 dde->dde_class = ddb->ddb_class;
1167 if (error != ENOENT)
1169 ddb->ddb_cursor = 0;
1170 } while (++ddb->ddb_checksum < ZIO_CHECKSUM_FUNCTIONS);
1171 ddb->ddb_checksum = 0;
1172 } while (++ddb->ddb_type < DDT_TYPES);
1174 } while (++ddb->ddb_class < DDT_CLASSES);
1176 return (SET_ERROR(ENOENT));