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, 2014 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 * Enable/disable prefetching of dedup-ed blocks which are going to be freed.
43 int zfs_dedup_prefetch = 1;
45 SYSCTL_DECL(_vfs_zfs);
46 SYSCTL_NODE(_vfs_zfs, OID_AUTO, dedup, CTLFLAG_RW, 0, "ZFS DEDUP");
47 SYSCTL_INT(_vfs_zfs_dedup, OID_AUTO, prefetch, CTLFLAG_RWTUN, &zfs_dedup_prefetch,
48 0, "Enable/disable prefetching of dedup-ed blocks which are going to be freed");
50 static const ddt_ops_t *ddt_ops[DDT_TYPES] = {
54 static const char *ddt_class_name[DDT_CLASSES] = {
61 ddt_object_create(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
64 spa_t *spa = ddt->ddt_spa;
65 objset_t *os = ddt->ddt_os;
66 uint64_t *objectp = &ddt->ddt_object[type][class];
67 boolean_t prehash = zio_checksum_table[ddt->ddt_checksum].ci_dedup;
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 VERIFY(ddt_object_count(ddt, type, class, &count) == 0 && count == 0);
98 ASSERT(ddt_histogram_empty(&ddt->ddt_histogram[type][class]));
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]);
124 VERIFY0(zap_lookup(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
125 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
126 &ddt->ddt_histogram[type][class]));
129 * Seed the cached statistics.
131 VERIFY(ddt_object_info(ddt, type, class, &doi) == 0);
133 error = ddt_object_count(ddt, type, class, &count);
137 ddo->ddo_count = count;
138 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
139 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
145 ddt_object_sync(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
148 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
149 dmu_object_info_t doi;
151 char name[DDT_NAMELEN];
153 ddt_object_name(ddt, type, class, name);
155 VERIFY(zap_update(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
156 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
157 &ddt->ddt_histogram[type][class], tx) == 0);
160 * Cache DDT statistics; this is the only time they'll change.
162 VERIFY(ddt_object_info(ddt, type, class, &doi) == 0);
163 VERIFY(ddt_object_count(ddt, type, class, &count) == 0);
165 ddo->ddo_count = count;
166 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
167 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
171 ddt_object_lookup(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
174 if (!ddt_object_exists(ddt, type, class))
175 return (SET_ERROR(ENOENT));
177 return (ddt_ops[type]->ddt_op_lookup(ddt->ddt_os,
178 ddt->ddt_object[type][class], dde));
182 ddt_object_prefetch(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
185 if (!ddt_object_exists(ddt, type, class))
188 ddt_ops[type]->ddt_op_prefetch(ddt->ddt_os,
189 ddt->ddt_object[type][class], dde);
193 ddt_object_update(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
194 ddt_entry_t *dde, dmu_tx_t *tx)
196 ASSERT(ddt_object_exists(ddt, type, class));
198 return (ddt_ops[type]->ddt_op_update(ddt->ddt_os,
199 ddt->ddt_object[type][class], dde, tx));
203 ddt_object_remove(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
204 ddt_entry_t *dde, dmu_tx_t *tx)
206 ASSERT(ddt_object_exists(ddt, type, class));
208 return (ddt_ops[type]->ddt_op_remove(ddt->ddt_os,
209 ddt->ddt_object[type][class], dde, tx));
213 ddt_object_walk(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
214 uint64_t *walk, ddt_entry_t *dde)
216 ASSERT(ddt_object_exists(ddt, type, class));
218 return (ddt_ops[type]->ddt_op_walk(ddt->ddt_os,
219 ddt->ddt_object[type][class], dde, walk));
223 ddt_object_count(ddt_t *ddt, enum ddt_type type, enum ddt_class class, uint64_t *count)
225 ASSERT(ddt_object_exists(ddt, type, class));
227 return (ddt_ops[type]->ddt_op_count(ddt->ddt_os,
228 ddt->ddt_object[type][class], count));
232 ddt_object_info(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
233 dmu_object_info_t *doi)
235 if (!ddt_object_exists(ddt, type, class))
236 return (SET_ERROR(ENOENT));
238 return (dmu_object_info(ddt->ddt_os, ddt->ddt_object[type][class],
243 ddt_object_exists(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
245 return (!!ddt->ddt_object[type][class]);
249 ddt_object_name(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
252 (void) sprintf(name, DMU_POOL_DDT,
253 zio_checksum_table[ddt->ddt_checksum].ci_name,
254 ddt_ops[type]->ddt_op_name, ddt_class_name[class]);
258 ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, uint64_t txg)
262 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
263 bp->blk_dva[d] = ddp->ddp_dva[d];
264 BP_SET_BIRTH(bp, txg, ddp->ddp_phys_birth);
268 ddt_bp_create(enum zio_checksum checksum,
269 const ddt_key_t *ddk, const ddt_phys_t *ddp, blkptr_t *bp)
274 ddt_bp_fill(ddp, bp, ddp->ddp_phys_birth);
276 bp->blk_cksum = ddk->ddk_cksum;
279 BP_SET_LSIZE(bp, DDK_GET_LSIZE(ddk));
280 BP_SET_PSIZE(bp, DDK_GET_PSIZE(ddk));
281 BP_SET_COMPRESS(bp, DDK_GET_COMPRESS(ddk));
282 BP_SET_CHECKSUM(bp, checksum);
283 BP_SET_TYPE(bp, DMU_OT_DEDUP);
286 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
290 ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp)
292 ddk->ddk_cksum = bp->blk_cksum;
295 DDK_SET_LSIZE(ddk, BP_GET_LSIZE(bp));
296 DDK_SET_PSIZE(ddk, BP_GET_PSIZE(bp));
297 DDK_SET_COMPRESS(ddk, BP_GET_COMPRESS(bp));
301 ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp)
303 ASSERT(ddp->ddp_phys_birth == 0);
305 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
306 ddp->ddp_dva[d] = bp->blk_dva[d];
307 ddp->ddp_phys_birth = BP_PHYSICAL_BIRTH(bp);
311 ddt_phys_clear(ddt_phys_t *ddp)
313 bzero(ddp, sizeof (*ddp));
317 ddt_phys_addref(ddt_phys_t *ddp)
323 ddt_phys_decref(ddt_phys_t *ddp)
325 ASSERT((int64_t)ddp->ddp_refcnt > 0);
330 ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, uint64_t txg)
334 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
336 zio_free(ddt->ddt_spa, txg, &blk);
340 ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp)
342 ddt_phys_t *ddp = (ddt_phys_t *)dde->dde_phys;
344 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
345 if (DVA_EQUAL(BP_IDENTITY(bp), &ddp->ddp_dva[0]) &&
346 BP_PHYSICAL_BIRTH(bp) == ddp->ddp_phys_birth)
353 ddt_phys_total_refcnt(const ddt_entry_t *dde)
357 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++)
358 refcnt += dde->dde_phys[p].ddp_refcnt;
364 ddt_stat_generate(ddt_t *ddt, ddt_entry_t *dde, ddt_stat_t *dds)
366 spa_t *spa = ddt->ddt_spa;
367 ddt_phys_t *ddp = dde->dde_phys;
368 ddt_key_t *ddk = &dde->dde_key;
369 uint64_t lsize = DDK_GET_LSIZE(ddk);
370 uint64_t psize = DDK_GET_PSIZE(ddk);
372 bzero(dds, sizeof (*dds));
374 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
376 uint64_t refcnt = ddp->ddp_refcnt;
378 if (ddp->ddp_phys_birth == 0)
381 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
382 dsize += dva_get_dsize_sync(spa, &ddp->ddp_dva[d]);
384 dds->dds_blocks += 1;
385 dds->dds_lsize += lsize;
386 dds->dds_psize += psize;
387 dds->dds_dsize += dsize;
389 dds->dds_ref_blocks += refcnt;
390 dds->dds_ref_lsize += lsize * refcnt;
391 dds->dds_ref_psize += psize * refcnt;
392 dds->dds_ref_dsize += dsize * refcnt;
397 ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg)
399 const uint64_t *s = (const uint64_t *)src;
400 uint64_t *d = (uint64_t *)dst;
401 uint64_t *d_end = (uint64_t *)(dst + 1);
403 ASSERT(neg == 0 || neg == -1ULL); /* add or subtract */
406 *d++ += (*s++ ^ neg) - neg;
410 ddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg)
413 ddt_histogram_t *ddh;
416 ddt_stat_generate(ddt, dde, &dds);
418 bucket = highbit64(dds.dds_ref_blocks) - 1;
421 ddh = &ddt->ddt_histogram[dde->dde_type][dde->dde_class];
423 ddt_stat_add(&ddh->ddh_stat[bucket], &dds, neg);
427 ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src)
429 for (int h = 0; h < 64; h++)
430 ddt_stat_add(&dst->ddh_stat[h], &src->ddh_stat[h], 0);
434 ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh)
436 bzero(dds, sizeof (*dds));
438 for (int h = 0; h < 64; h++)
439 ddt_stat_add(dds, &ddh->ddh_stat[h], 0);
443 ddt_histogram_empty(const ddt_histogram_t *ddh)
445 const uint64_t *s = (const uint64_t *)ddh;
446 const uint64_t *s_end = (const uint64_t *)(ddh + 1);
456 ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total)
458 /* Sum the statistics we cached in ddt_object_sync(). */
459 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
460 ddt_t *ddt = spa->spa_ddt[c];
461 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
462 for (enum ddt_class class = 0; class < DDT_CLASSES;
465 &ddt->ddt_object_stats[type][class];
466 ddo_total->ddo_count += ddo->ddo_count;
467 ddo_total->ddo_dspace += ddo->ddo_dspace;
468 ddo_total->ddo_mspace += ddo->ddo_mspace;
473 /* ... and compute the averages. */
474 if (ddo_total->ddo_count != 0) {
475 ddo_total->ddo_dspace /= ddo_total->ddo_count;
476 ddo_total->ddo_mspace /= ddo_total->ddo_count;
481 ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh)
483 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
484 ddt_t *ddt = spa->spa_ddt[c];
485 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
486 for (enum ddt_class class = 0; class < DDT_CLASSES;
488 ddt_histogram_add(ddh,
489 &ddt->ddt_histogram_cache[type][class]);
496 ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total)
498 ddt_histogram_t *ddh_total;
500 ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
501 ddt_get_dedup_histogram(spa, ddh_total);
502 ddt_histogram_stat(dds_total, ddh_total);
503 kmem_free(ddh_total, sizeof (ddt_histogram_t));
507 ddt_get_dedup_dspace(spa_t *spa)
509 ddt_stat_t dds_total = { 0 };
511 ddt_get_dedup_stats(spa, &dds_total);
512 return (dds_total.dds_ref_dsize - dds_total.dds_dsize);
516 ddt_get_pool_dedup_ratio(spa_t *spa)
518 ddt_stat_t dds_total = { 0 };
520 ddt_get_dedup_stats(spa, &dds_total);
521 if (dds_total.dds_dsize == 0)
524 return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize);
528 ddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde, ddt_phys_t *ddp_willref)
530 spa_t *spa = ddt->ddt_spa;
531 uint64_t total_refcnt = 0;
532 uint64_t ditto = spa->spa_dedup_ditto;
533 int total_copies = 0;
534 int desired_copies = 0;
536 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
537 ddt_phys_t *ddp = &dde->dde_phys[p];
538 zio_t *zio = dde->dde_lead_zio[p];
539 uint64_t refcnt = ddp->ddp_refcnt; /* committed refs */
541 refcnt += zio->io_parent_count; /* pending refs */
542 if (ddp == ddp_willref)
543 refcnt++; /* caller's ref */
545 total_refcnt += refcnt;
550 if (ditto == 0 || ditto > UINT32_MAX)
553 if (total_refcnt >= 1)
555 if (total_refcnt >= ditto)
557 if (total_refcnt >= ditto * ditto)
560 return (MAX(desired_copies, total_copies) - total_copies);
564 ddt_ditto_copies_present(ddt_entry_t *dde)
566 ddt_phys_t *ddp = &dde->dde_phys[DDT_PHYS_DITTO];
567 dva_t *dva = ddp->ddp_dva;
568 int copies = 0 - DVA_GET_GANG(dva);
570 for (int d = 0; d < SPA_DVAS_PER_BP; d++, dva++)
571 if (DVA_IS_VALID(dva))
574 ASSERT(copies >= 0 && copies < SPA_DVAS_PER_BP);
580 ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len)
582 uchar_t *version = dst++;
583 int cpfunc = ZIO_COMPRESS_ZLE;
584 zio_compress_info_t *ci = &zio_compress_table[cpfunc];
587 ASSERT(d_len >= s_len + 1); /* no compression plus version byte */
589 c_len = ci->ci_compress(src, dst, s_len, d_len - 1, ci->ci_level);
591 if (c_len == s_len) {
592 cpfunc = ZIO_COMPRESS_OFF;
593 bcopy(src, dst, s_len);
598 if (ZFS_HOST_BYTEORDER)
599 *version |= DDT_COMPRESS_BYTEORDER_MASK;
605 ddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len)
607 uchar_t version = *src++;
608 int cpfunc = version & DDT_COMPRESS_FUNCTION_MASK;
609 zio_compress_info_t *ci = &zio_compress_table[cpfunc];
611 if (ci->ci_decompress != NULL)
612 (void) ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level);
614 bcopy(src, dst, d_len);
616 if (((version & DDT_COMPRESS_BYTEORDER_MASK) != 0) !=
617 (ZFS_HOST_BYTEORDER != 0))
618 byteswap_uint64_array(dst, d_len);
622 ddt_select_by_checksum(spa_t *spa, enum zio_checksum c)
624 return (spa->spa_ddt[c]);
628 ddt_select(spa_t *spa, const blkptr_t *bp)
630 return (spa->spa_ddt[BP_GET_CHECKSUM(bp)]);
634 ddt_enter(ddt_t *ddt)
636 mutex_enter(&ddt->ddt_lock);
642 mutex_exit(&ddt->ddt_lock);
646 ddt_alloc(const ddt_key_t *ddk)
650 dde = kmem_zalloc(sizeof (ddt_entry_t), KM_SLEEP);
651 cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL);
659 ddt_free(ddt_entry_t *dde)
661 ASSERT(!dde->dde_loading);
663 for (int p = 0; p < DDT_PHYS_TYPES; p++)
664 ASSERT(dde->dde_lead_zio[p] == NULL);
666 if (dde->dde_repair_data != NULL)
667 zio_buf_free(dde->dde_repair_data,
668 DDK_GET_PSIZE(&dde->dde_key));
670 cv_destroy(&dde->dde_cv);
671 kmem_free(dde, sizeof (*dde));
675 ddt_remove(ddt_t *ddt, ddt_entry_t *dde)
677 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
679 avl_remove(&ddt->ddt_tree, dde);
684 ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add)
686 ddt_entry_t *dde, dde_search;
688 enum ddt_class class;
692 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
694 ddt_key_fill(&dde_search.dde_key, bp);
696 dde = avl_find(&ddt->ddt_tree, &dde_search, &where);
700 dde = ddt_alloc(&dde_search.dde_key);
701 avl_insert(&ddt->ddt_tree, dde, where);
704 while (dde->dde_loading)
705 cv_wait(&dde->dde_cv, &ddt->ddt_lock);
710 dde->dde_loading = B_TRUE;
716 for (type = 0; type < DDT_TYPES; type++) {
717 for (class = 0; class < DDT_CLASSES; class++) {
718 error = ddt_object_lookup(ddt, type, class, dde);
726 ASSERT(error == 0 || error == ENOENT);
730 ASSERT(dde->dde_loaded == B_FALSE);
731 ASSERT(dde->dde_loading == B_TRUE);
733 dde->dde_type = type; /* will be DDT_TYPES if no entry found */
734 dde->dde_class = class; /* will be DDT_CLASSES if no entry found */
735 dde->dde_loaded = B_TRUE;
736 dde->dde_loading = B_FALSE;
739 ddt_stat_update(ddt, dde, -1ULL);
741 cv_broadcast(&dde->dde_cv);
747 ddt_prefetch(spa_t *spa, const blkptr_t *bp)
752 if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp))
756 * We only remove the DDT once all tables are empty and only
757 * prefetch dedup blocks when there are entries in the DDT.
758 * Thus no locking is required as the DDT can't disappear on us.
760 ddt = ddt_select(spa, bp);
761 ddt_key_fill(&dde.dde_key, bp);
763 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
764 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
765 ddt_object_prefetch(ddt, type, class, &dde);
771 ddt_entry_compare(const void *x1, const void *x2)
773 const ddt_entry_t *dde1 = x1;
774 const ddt_entry_t *dde2 = x2;
775 const uint64_t *u1 = (const uint64_t *)&dde1->dde_key;
776 const uint64_t *u2 = (const uint64_t *)&dde2->dde_key;
778 for (int i = 0; i < DDT_KEY_WORDS; i++) {
789 ddt_table_alloc(spa_t *spa, enum zio_checksum c)
793 ddt = kmem_zalloc(sizeof (*ddt), KM_SLEEP);
795 mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL);
796 avl_create(&ddt->ddt_tree, ddt_entry_compare,
797 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
798 avl_create(&ddt->ddt_repair_tree, ddt_entry_compare,
799 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
800 ddt->ddt_checksum = c;
802 ddt->ddt_os = spa->spa_meta_objset;
808 ddt_table_free(ddt_t *ddt)
810 ASSERT(avl_numnodes(&ddt->ddt_tree) == 0);
811 ASSERT(avl_numnodes(&ddt->ddt_repair_tree) == 0);
812 avl_destroy(&ddt->ddt_tree);
813 avl_destroy(&ddt->ddt_repair_tree);
814 mutex_destroy(&ddt->ddt_lock);
815 kmem_free(ddt, sizeof (*ddt));
819 ddt_create(spa_t *spa)
821 spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM;
823 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++)
824 spa->spa_ddt[c] = ddt_table_alloc(spa, c);
834 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
835 DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
836 &spa->spa_ddt_stat_object);
839 return (error == ENOENT ? 0 : error);
841 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
842 ddt_t *ddt = spa->spa_ddt[c];
843 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
844 for (enum ddt_class class = 0; class < DDT_CLASSES;
846 error = ddt_object_load(ddt, type, class);
847 if (error != 0 && error != ENOENT)
853 * Seed the cached histograms.
855 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
856 sizeof (ddt->ddt_histogram));
863 ddt_unload(spa_t *spa)
865 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
866 if (spa->spa_ddt[c]) {
867 ddt_table_free(spa->spa_ddt[c]);
868 spa->spa_ddt[c] = NULL;
874 ddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp)
879 if (!BP_GET_DEDUP(bp))
882 if (max_class == DDT_CLASS_UNIQUE)
885 ddt = spa->spa_ddt[BP_GET_CHECKSUM(bp)];
887 ddt_key_fill(&dde.dde_key, bp);
889 for (enum ddt_type type = 0; type < DDT_TYPES; type++)
890 for (enum ddt_class class = 0; class <= max_class; class++)
891 if (ddt_object_lookup(ddt, type, class, &dde) == 0)
898 ddt_repair_start(ddt_t *ddt, const blkptr_t *bp)
903 ddt_key_fill(&ddk, bp);
905 dde = ddt_alloc(&ddk);
907 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
908 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
910 * We can only do repair if there are multiple copies
911 * of the block. For anything in the UNIQUE class,
912 * there's definitely only one copy, so don't even try.
914 if (class != DDT_CLASS_UNIQUE &&
915 ddt_object_lookup(ddt, type, class, dde) == 0)
920 bzero(dde->dde_phys, sizeof (dde->dde_phys));
926 ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde)
932 if (dde->dde_repair_data != NULL && spa_writeable(ddt->ddt_spa) &&
933 avl_find(&ddt->ddt_repair_tree, dde, &where) == NULL)
934 avl_insert(&ddt->ddt_repair_tree, dde, where);
942 ddt_repair_entry_done(zio_t *zio)
944 ddt_entry_t *rdde = zio->io_private;
950 ddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio)
952 ddt_phys_t *ddp = dde->dde_phys;
953 ddt_phys_t *rddp = rdde->dde_phys;
954 ddt_key_t *ddk = &dde->dde_key;
955 ddt_key_t *rddk = &rdde->dde_key;
959 zio = zio_null(rio, rio->io_spa, NULL,
960 ddt_repair_entry_done, rdde, rio->io_flags);
962 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) {
963 if (ddp->ddp_phys_birth == 0 ||
964 ddp->ddp_phys_birth != rddp->ddp_phys_birth ||
965 bcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva)))
967 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
968 zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk,
969 rdde->dde_repair_data, DDK_GET_PSIZE(rddk), NULL, NULL,
970 ZIO_PRIORITY_SYNC_WRITE, ZIO_DDT_CHILD_FLAGS(zio), NULL));
977 ddt_repair_table(ddt_t *ddt, zio_t *rio)
979 spa_t *spa = ddt->ddt_spa;
980 ddt_entry_t *dde, *rdde_next, *rdde;
981 avl_tree_t *t = &ddt->ddt_repair_tree;
984 if (spa_sync_pass(spa) > 1)
988 for (rdde = avl_first(t); rdde != NULL; rdde = rdde_next) {
989 rdde_next = AVL_NEXT(t, rdde);
990 avl_remove(&ddt->ddt_repair_tree, rdde);
992 ddt_bp_create(ddt->ddt_checksum, &rdde->dde_key, NULL, &blk);
993 dde = ddt_repair_start(ddt, &blk);
994 ddt_repair_entry(ddt, dde, rdde, rio);
995 ddt_repair_done(ddt, dde);
1002 ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
1004 dsl_pool_t *dp = ddt->ddt_spa->spa_dsl_pool;
1005 ddt_phys_t *ddp = dde->dde_phys;
1006 ddt_key_t *ddk = &dde->dde_key;
1007 enum ddt_type otype = dde->dde_type;
1008 enum ddt_type ntype = DDT_TYPE_CURRENT;
1009 enum ddt_class oclass = dde->dde_class;
1010 enum ddt_class nclass;
1011 uint64_t total_refcnt = 0;
1013 ASSERT(dde->dde_loaded);
1014 ASSERT(!dde->dde_loading);
1016 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1017 ASSERT(dde->dde_lead_zio[p] == NULL);
1018 ASSERT((int64_t)ddp->ddp_refcnt >= 0);
1019 if (ddp->ddp_phys_birth == 0) {
1020 ASSERT(ddp->ddp_refcnt == 0);
1023 if (p == DDT_PHYS_DITTO) {
1024 if (ddt_ditto_copies_needed(ddt, dde, NULL) == 0)
1025 ddt_phys_free(ddt, ddk, ddp, txg);
1028 if (ddp->ddp_refcnt == 0)
1029 ddt_phys_free(ddt, ddk, ddp, txg);
1030 total_refcnt += ddp->ddp_refcnt;
1033 if (dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth != 0)
1034 nclass = DDT_CLASS_DITTO;
1035 else if (total_refcnt > 1)
1036 nclass = DDT_CLASS_DUPLICATE;
1038 nclass = DDT_CLASS_UNIQUE;
1040 if (otype != DDT_TYPES &&
1041 (otype != ntype || oclass != nclass || total_refcnt == 0)) {
1042 VERIFY(ddt_object_remove(ddt, otype, oclass, dde, tx) == 0);
1043 ASSERT(ddt_object_lookup(ddt, otype, oclass, dde) == ENOENT);
1046 if (total_refcnt != 0) {
1047 dde->dde_type = ntype;
1048 dde->dde_class = nclass;
1049 ddt_stat_update(ddt, dde, 0);
1050 if (!ddt_object_exists(ddt, ntype, nclass))
1051 ddt_object_create(ddt, ntype, nclass, tx);
1052 VERIFY(ddt_object_update(ddt, ntype, nclass, dde, tx) == 0);
1055 * If the class changes, the order that we scan this bp
1056 * changes. If it decreases, we could miss it, so
1057 * scan it right now. (This covers both class changing
1058 * while we are doing ddt_walk(), and when we are
1061 if (nclass < oclass) {
1062 dsl_scan_ddt_entry(dp->dp_scan,
1063 ddt->ddt_checksum, dde, tx);
1069 ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
1071 spa_t *spa = ddt->ddt_spa;
1073 void *cookie = NULL;
1075 if (avl_numnodes(&ddt->ddt_tree) == 0)
1078 ASSERT(spa->spa_uberblock.ub_version >= SPA_VERSION_DEDUP);
1080 if (spa->spa_ddt_stat_object == 0) {
1081 spa->spa_ddt_stat_object = zap_create_link(ddt->ddt_os,
1082 DMU_OT_DDT_STATS, DMU_POOL_DIRECTORY_OBJECT,
1083 DMU_POOL_DDT_STATS, tx);
1086 while ((dde = avl_destroy_nodes(&ddt->ddt_tree, &cookie)) != NULL) {
1087 ddt_sync_entry(ddt, dde, tx, txg);
1091 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
1092 uint64_t add, count = 0;
1093 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
1094 if (ddt_object_exists(ddt, type, class)) {
1095 ddt_object_sync(ddt, type, class, tx);
1096 VERIFY(ddt_object_count(ddt, type, class,
1101 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
1102 if (count == 0 && ddt_object_exists(ddt, type, class))
1103 ddt_object_destroy(ddt, type, class, tx);
1107 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
1108 sizeof (ddt->ddt_histogram));
1112 ddt_sync(spa_t *spa, uint64_t txg)
1115 zio_t *rio = zio_root(spa, NULL, NULL,
1116 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1118 ASSERT(spa_syncing_txg(spa) == txg);
1120 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1122 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1123 ddt_t *ddt = spa->spa_ddt[c];
1126 ddt_sync_table(ddt, tx, txg);
1127 ddt_repair_table(ddt, rio);
1130 (void) zio_wait(rio);
1136 ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde)
1141 ddt_t *ddt = spa->spa_ddt[ddb->ddb_checksum];
1143 if (ddt_object_exists(ddt, ddb->ddb_type,
1145 error = ddt_object_walk(ddt,
1146 ddb->ddb_type, ddb->ddb_class,
1147 &ddb->ddb_cursor, dde);
1149 dde->dde_type = ddb->ddb_type;
1150 dde->dde_class = ddb->ddb_class;
1153 if (error != ENOENT)
1155 ddb->ddb_cursor = 0;
1156 } while (++ddb->ddb_checksum < ZIO_CHECKSUM_FUNCTIONS);
1157 ddb->ddb_checksum = 0;
1158 } while (++ddb->ddb_type < DDT_TYPES);
1160 } while (++ddb->ddb_class < DDT_CLASSES);
1162 return (SET_ERROR(ENOENT));