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 https://opensource.org/licenses/CDDL-1.0.
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.
25 * Copyright (c) 2022 by Pawel Jakub Dawidek
28 #include <sys/zfs_context.h>
30 #include <sys/spa_impl.h>
33 #include <sys/ddt_impl.h>
35 #include <sys/dmu_tx.h>
37 #include <sys/dsl_pool.h>
38 #include <sys/zio_checksum.h>
39 #include <sys/dsl_scan.h>
42 static kmem_cache_t
*ddt_cache
;
43 static kmem_cache_t
*ddt_entry_cache
;
46 * Enable/disable prefetching of dedup-ed blocks which are going to be freed.
48 int zfs_dedup_prefetch
= 0;
50 static const ddt_ops_t
*const ddt_ops
[DDT_TYPES
] = {
54 static const char *const ddt_class_name
[DDT_CLASSES
] = {
61 ddt_object_create(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
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_flags
&
69 char name
[DDT_NAMELEN
];
71 ddt_object_name(ddt
, type
, class, name
);
73 ASSERT3U(*objectp
, ==, 0);
74 VERIFY0(ddt_ops
[type
]->ddt_op_create(os
, objectp
, tx
, prehash
));
75 ASSERT3U(*objectp
, !=, 0);
77 VERIFY0(zap_add(os
, DMU_POOL_DIRECTORY_OBJECT
, name
,
78 sizeof (uint64_t), 1, objectp
, tx
));
80 VERIFY0(zap_add(os
, spa
->spa_ddt_stat_object
, name
,
81 sizeof (uint64_t), sizeof (ddt_histogram_t
) / sizeof (uint64_t),
82 &ddt
->ddt_histogram
[type
][class], tx
));
86 ddt_object_destroy(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class,
89 spa_t
*spa
= ddt
->ddt_spa
;
90 objset_t
*os
= ddt
->ddt_os
;
91 uint64_t *objectp
= &ddt
->ddt_object
[type
][class];
93 char name
[DDT_NAMELEN
];
95 ddt_object_name(ddt
, type
, class, name
);
97 ASSERT3U(*objectp
, !=, 0);
98 ASSERT(ddt_histogram_empty(&ddt
->ddt_histogram
[type
][class]));
99 VERIFY0(ddt_object_count(ddt
, type
, class, &count
));
101 VERIFY0(zap_remove(os
, DMU_POOL_DIRECTORY_OBJECT
, name
, tx
));
102 VERIFY0(zap_remove(os
, spa
->spa_ddt_stat_object
, name
, tx
));
103 VERIFY0(ddt_ops
[type
]->ddt_op_destroy(os
, *objectp
, tx
));
104 memset(&ddt
->ddt_object_stats
[type
][class], 0, sizeof (ddt_object_t
));
110 ddt_object_load(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
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]);
125 error
= zap_lookup(ddt
->ddt_os
, ddt
->ddt_spa
->spa_ddt_stat_object
, name
,
126 sizeof (uint64_t), sizeof (ddt_histogram_t
) / sizeof (uint64_t),
127 &ddt
->ddt_histogram
[type
][class]);
132 * Seed the cached statistics.
134 error
= ddt_object_info(ddt
, type
, class, &doi
);
138 error
= ddt_object_count(ddt
, type
, class, &count
);
142 ddo
->ddo_count
= count
;
143 ddo
->ddo_dspace
= doi
.doi_physical_blocks_512
<< 9;
144 ddo
->ddo_mspace
= doi
.doi_fill_count
* doi
.doi_data_block_size
;
150 ddt_object_sync(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class,
153 ddt_object_t
*ddo
= &ddt
->ddt_object_stats
[type
][class];
154 dmu_object_info_t doi
;
156 char name
[DDT_NAMELEN
];
158 ddt_object_name(ddt
, type
, class, name
);
160 VERIFY0(zap_update(ddt
->ddt_os
, ddt
->ddt_spa
->spa_ddt_stat_object
, name
,
161 sizeof (uint64_t), sizeof (ddt_histogram_t
) / sizeof (uint64_t),
162 &ddt
->ddt_histogram
[type
][class], tx
));
165 * Cache DDT statistics; this is the only time they'll change.
167 VERIFY0(ddt_object_info(ddt
, type
, class, &doi
));
168 VERIFY0(ddt_object_count(ddt
, type
, class, &count
));
170 ddo
->ddo_count
= count
;
171 ddo
->ddo_dspace
= doi
.doi_physical_blocks_512
<< 9;
172 ddo
->ddo_mspace
= doi
.doi_fill_count
* doi
.doi_data_block_size
;
176 ddt_object_exists(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class)
178 return (!!ddt
->ddt_object
[type
][class]);
182 ddt_object_lookup(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class,
185 if (!ddt_object_exists(ddt
, type
, class))
186 return (SET_ERROR(ENOENT
));
188 return (ddt_ops
[type
]->ddt_op_lookup(ddt
->ddt_os
,
189 ddt
->ddt_object
[type
][class], &dde
->dde_key
,
190 dde
->dde_phys
, sizeof (dde
->dde_phys
)));
194 ddt_object_contains(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class,
195 const ddt_key_t
*ddk
)
197 if (!ddt_object_exists(ddt
, type
, class))
198 return (SET_ERROR(ENOENT
));
200 return (ddt_ops
[type
]->ddt_op_contains(ddt
->ddt_os
,
201 ddt
->ddt_object
[type
][class], ddk
));
205 ddt_object_prefetch(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class,
206 const ddt_key_t
*ddk
)
208 if (!ddt_object_exists(ddt
, type
, class))
211 ddt_ops
[type
]->ddt_op_prefetch(ddt
->ddt_os
,
212 ddt
->ddt_object
[type
][class], ddk
);
216 ddt_object_update(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class,
217 ddt_entry_t
*dde
, dmu_tx_t
*tx
)
219 ASSERT(ddt_object_exists(ddt
, type
, class));
221 return (ddt_ops
[type
]->ddt_op_update(ddt
->ddt_os
,
222 ddt
->ddt_object
[type
][class], &dde
->dde_key
, dde
->dde_phys
,
223 sizeof (dde
->dde_phys
), tx
));
227 ddt_object_remove(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class,
228 const ddt_key_t
*ddk
, dmu_tx_t
*tx
)
230 ASSERT(ddt_object_exists(ddt
, type
, class));
232 return (ddt_ops
[type
]->ddt_op_remove(ddt
->ddt_os
,
233 ddt
->ddt_object
[type
][class], ddk
, tx
));
237 ddt_object_walk(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class,
238 uint64_t *walk
, ddt_entry_t
*dde
)
240 ASSERT(ddt_object_exists(ddt
, type
, class));
242 return (ddt_ops
[type
]->ddt_op_walk(ddt
->ddt_os
,
243 ddt
->ddt_object
[type
][class], walk
, &dde
->dde_key
,
244 dde
->dde_phys
, sizeof (dde
->dde_phys
)));
248 ddt_object_count(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class,
251 ASSERT(ddt_object_exists(ddt
, type
, class));
253 return (ddt_ops
[type
]->ddt_op_count(ddt
->ddt_os
,
254 ddt
->ddt_object
[type
][class], count
));
258 ddt_object_info(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class,
259 dmu_object_info_t
*doi
)
261 if (!ddt_object_exists(ddt
, type
, class))
262 return (SET_ERROR(ENOENT
));
264 return (dmu_object_info(ddt
->ddt_os
, ddt
->ddt_object
[type
][class],
269 ddt_object_name(ddt_t
*ddt
, ddt_type_t type
, ddt_class_t
class,
272 (void) snprintf(name
, DDT_NAMELEN
, DMU_POOL_DDT
,
273 zio_checksum_table
[ddt
->ddt_checksum
].ci_name
,
274 ddt_ops
[type
]->ddt_op_name
, ddt_class_name
[class]);
278 ddt_bp_fill(const ddt_phys_t
*ddp
, blkptr_t
*bp
, uint64_t txg
)
280 ASSERT3U(txg
, !=, 0);
282 for (int d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
283 bp
->blk_dva
[d
] = ddp
->ddp_dva
[d
];
284 BP_SET_BIRTH(bp
, txg
, ddp
->ddp_phys_birth
);
288 * The bp created via this function may be used for repairs and scrub, but it
289 * will be missing the salt / IV required to do a full decrypting read.
292 ddt_bp_create(enum zio_checksum checksum
,
293 const ddt_key_t
*ddk
, const ddt_phys_t
*ddp
, blkptr_t
*bp
)
298 ddt_bp_fill(ddp
, bp
, ddp
->ddp_phys_birth
);
300 bp
->blk_cksum
= ddk
->ddk_cksum
;
302 BP_SET_LSIZE(bp
, DDK_GET_LSIZE(ddk
));
303 BP_SET_PSIZE(bp
, DDK_GET_PSIZE(ddk
));
304 BP_SET_COMPRESS(bp
, DDK_GET_COMPRESS(ddk
));
305 BP_SET_CRYPT(bp
, DDK_GET_CRYPT(ddk
));
307 BP_SET_CHECKSUM(bp
, checksum
);
308 BP_SET_TYPE(bp
, DMU_OT_DEDUP
);
311 BP_SET_BYTEORDER(bp
, ZFS_HOST_BYTEORDER
);
315 ddt_key_fill(ddt_key_t
*ddk
, const blkptr_t
*bp
)
317 ddk
->ddk_cksum
= bp
->blk_cksum
;
320 ASSERT(BP_IS_ENCRYPTED(bp
) || !BP_USES_CRYPT(bp
));
322 DDK_SET_LSIZE(ddk
, BP_GET_LSIZE(bp
));
323 DDK_SET_PSIZE(ddk
, BP_GET_PSIZE(bp
));
324 DDK_SET_COMPRESS(ddk
, BP_GET_COMPRESS(bp
));
325 DDK_SET_CRYPT(ddk
, BP_USES_CRYPT(bp
));
329 ddt_phys_fill(ddt_phys_t
*ddp
, const blkptr_t
*bp
)
331 ASSERT0(ddp
->ddp_phys_birth
);
333 for (int d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
334 ddp
->ddp_dva
[d
] = bp
->blk_dva
[d
];
335 ddp
->ddp_phys_birth
= BP_PHYSICAL_BIRTH(bp
);
339 ddt_phys_clear(ddt_phys_t
*ddp
)
341 memset(ddp
, 0, sizeof (*ddp
));
345 ddt_phys_addref(ddt_phys_t
*ddp
)
351 ddt_phys_decref(ddt_phys_t
*ddp
)
354 ASSERT3U(ddp
->ddp_refcnt
, >, 0);
360 ddt_phys_free(ddt_t
*ddt
, ddt_key_t
*ddk
, ddt_phys_t
*ddp
, uint64_t txg
)
364 ddt_bp_create(ddt
->ddt_checksum
, ddk
, ddp
, &blk
);
367 * We clear the dedup bit so that zio_free() will actually free the
368 * space, rather than just decrementing the refcount in the DDT.
370 BP_SET_DEDUP(&blk
, 0);
373 zio_free(ddt
->ddt_spa
, txg
, &blk
);
377 ddt_phys_select(const ddt_entry_t
*dde
, const blkptr_t
*bp
)
379 ddt_phys_t
*ddp
= (ddt_phys_t
*)dde
->dde_phys
;
381 for (int p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
382 if (DVA_EQUAL(BP_IDENTITY(bp
), &ddp
->ddp_dva
[0]) &&
383 BP_PHYSICAL_BIRTH(bp
) == ddp
->ddp_phys_birth
)
390 ddt_phys_total_refcnt(const ddt_entry_t
*dde
)
394 for (int p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++)
395 refcnt
+= dde
->dde_phys
[p
].ddp_refcnt
;
401 ddt_select(spa_t
*spa
, const blkptr_t
*bp
)
403 return (spa
->spa_ddt
[BP_GET_CHECKSUM(bp
)]);
407 ddt_enter(ddt_t
*ddt
)
409 mutex_enter(&ddt
->ddt_lock
);
415 mutex_exit(&ddt
->ddt_lock
);
421 ddt_cache
= kmem_cache_create("ddt_cache",
422 sizeof (ddt_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
423 ddt_entry_cache
= kmem_cache_create("ddt_entry_cache",
424 sizeof (ddt_entry_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
430 kmem_cache_destroy(ddt_entry_cache
);
431 kmem_cache_destroy(ddt_cache
);
435 ddt_alloc(const ddt_key_t
*ddk
)
439 dde
= kmem_cache_alloc(ddt_entry_cache
, KM_SLEEP
);
440 memset(dde
, 0, sizeof (ddt_entry_t
));
441 cv_init(&dde
->dde_cv
, NULL
, CV_DEFAULT
, NULL
);
449 ddt_free(ddt_entry_t
*dde
)
451 ASSERT(!dde
->dde_loading
);
453 for (int p
= 0; p
< DDT_PHYS_TYPES
; p
++)
454 ASSERT3P(dde
->dde_lead_zio
[p
], ==, NULL
);
456 if (dde
->dde_repair_abd
!= NULL
)
457 abd_free(dde
->dde_repair_abd
);
459 cv_destroy(&dde
->dde_cv
);
460 kmem_cache_free(ddt_entry_cache
, dde
);
464 ddt_remove(ddt_t
*ddt
, ddt_entry_t
*dde
)
466 ASSERT(MUTEX_HELD(&ddt
->ddt_lock
));
468 avl_remove(&ddt
->ddt_tree
, dde
);
473 ddt_lookup(ddt_t
*ddt
, const blkptr_t
*bp
, boolean_t add
)
482 ASSERT(MUTEX_HELD(&ddt
->ddt_lock
));
484 ddt_key_fill(&search
, bp
);
486 dde
= avl_find(&ddt
->ddt_tree
, &search
, &where
);
490 dde
= ddt_alloc(&search
);
491 avl_insert(&ddt
->ddt_tree
, dde
, where
);
494 while (dde
->dde_loading
)
495 cv_wait(&dde
->dde_cv
, &ddt
->ddt_lock
);
500 dde
->dde_loading
= B_TRUE
;
506 for (type
= 0; type
< DDT_TYPES
; type
++) {
507 for (class = 0; class < DDT_CLASSES
; class++) {
508 error
= ddt_object_lookup(ddt
, type
, class, dde
);
509 if (error
!= ENOENT
) {
520 ASSERT(!dde
->dde_loaded
);
521 ASSERT(dde
->dde_loading
);
523 dde
->dde_type
= type
; /* will be DDT_TYPES if no entry found */
524 dde
->dde_class
= class; /* will be DDT_CLASSES if no entry found */
525 dde
->dde_loaded
= B_TRUE
;
526 dde
->dde_loading
= B_FALSE
;
529 ddt_stat_update(ddt
, dde
, -1ULL);
531 cv_broadcast(&dde
->dde_cv
);
537 ddt_prefetch(spa_t
*spa
, const blkptr_t
*bp
)
542 if (!zfs_dedup_prefetch
|| bp
== NULL
|| !BP_GET_DEDUP(bp
))
546 * We only remove the DDT once all tables are empty and only
547 * prefetch dedup blocks when there are entries in the DDT.
548 * Thus no locking is required as the DDT can't disappear on us.
550 ddt
= ddt_select(spa
, bp
);
551 ddt_key_fill(&ddk
, bp
);
553 for (ddt_type_t type
= 0; type
< DDT_TYPES
; type
++) {
554 for (ddt_class_t
class = 0; class < DDT_CLASSES
; class++) {
555 ddt_object_prefetch(ddt
, type
, class, &ddk
);
561 * Key comparison. Any struct wanting to make use of this function must have
562 * the key as the first element.
564 #define DDT_KEY_CMP_LEN (sizeof (ddt_key_t) / sizeof (uint16_t))
566 typedef struct ddt_key_cmp
{
567 uint16_t u16
[DDT_KEY_CMP_LEN
];
571 ddt_key_compare(const void *x1
, const void *x2
)
573 const ddt_key_cmp_t
*k1
= (const ddt_key_cmp_t
*)x1
;
574 const ddt_key_cmp_t
*k2
= (const ddt_key_cmp_t
*)x2
;
577 for (int i
= 0; i
< DDT_KEY_CMP_LEN
; i
++) {
578 cmp
= (int32_t)k1
->u16
[i
] - (int32_t)k2
->u16
[i
];
583 return (TREE_ISIGN(cmp
));
587 ddt_table_alloc(spa_t
*spa
, enum zio_checksum c
)
591 ddt
= kmem_cache_alloc(ddt_cache
, KM_SLEEP
);
592 memset(ddt
, 0, sizeof (ddt_t
));
594 mutex_init(&ddt
->ddt_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
595 avl_create(&ddt
->ddt_tree
, ddt_key_compare
,
596 sizeof (ddt_entry_t
), offsetof(ddt_entry_t
, dde_node
));
597 avl_create(&ddt
->ddt_repair_tree
, ddt_key_compare
,
598 sizeof (ddt_entry_t
), offsetof(ddt_entry_t
, dde_node
));
599 ddt
->ddt_checksum
= c
;
601 ddt
->ddt_os
= spa
->spa_meta_objset
;
607 ddt_table_free(ddt_t
*ddt
)
609 ASSERT0(avl_numnodes(&ddt
->ddt_tree
));
610 ASSERT0(avl_numnodes(&ddt
->ddt_repair_tree
));
611 avl_destroy(&ddt
->ddt_tree
);
612 avl_destroy(&ddt
->ddt_repair_tree
);
613 mutex_destroy(&ddt
->ddt_lock
);
614 kmem_cache_free(ddt_cache
, ddt
);
618 ddt_create(spa_t
*spa
)
620 spa
->spa_dedup_checksum
= ZIO_DEDUPCHECKSUM
;
622 for (enum zio_checksum c
= 0; c
< ZIO_CHECKSUM_FUNCTIONS
; c
++)
623 spa
->spa_ddt
[c
] = ddt_table_alloc(spa
, c
);
633 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
634 DMU_POOL_DDT_STATS
, sizeof (uint64_t), 1,
635 &spa
->spa_ddt_stat_object
);
638 return (error
== ENOENT
? 0 : error
);
640 for (enum zio_checksum c
= 0; c
< ZIO_CHECKSUM_FUNCTIONS
; c
++) {
641 ddt_t
*ddt
= spa
->spa_ddt
[c
];
642 for (ddt_type_t type
= 0; type
< DDT_TYPES
; type
++) {
643 for (ddt_class_t
class = 0; class < DDT_CLASSES
;
645 error
= ddt_object_load(ddt
, type
, class);
646 if (error
!= 0 && error
!= ENOENT
)
652 * Seed the cached histograms.
654 memcpy(&ddt
->ddt_histogram_cache
, ddt
->ddt_histogram
,
655 sizeof (ddt
->ddt_histogram
));
656 spa
->spa_dedup_dspace
= ~0ULL;
663 ddt_unload(spa_t
*spa
)
665 for (enum zio_checksum c
= 0; c
< ZIO_CHECKSUM_FUNCTIONS
; c
++) {
666 if (spa
->spa_ddt
[c
]) {
667 ddt_table_free(spa
->spa_ddt
[c
]);
668 spa
->spa_ddt
[c
] = NULL
;
674 ddt_class_contains(spa_t
*spa
, ddt_class_t max_class
, const blkptr_t
*bp
)
679 if (!BP_GET_DEDUP(bp
))
682 if (max_class
== DDT_CLASS_UNIQUE
)
685 ddt
= spa
->spa_ddt
[BP_GET_CHECKSUM(bp
)];
687 ddt_key_fill(&ddk
, bp
);
689 for (ddt_type_t type
= 0; type
< DDT_TYPES
; type
++) {
690 for (ddt_class_t
class = 0; class <= max_class
; class++) {
691 if (ddt_object_contains(ddt
, type
, class, &ddk
) == 0)
700 ddt_repair_start(ddt_t
*ddt
, const blkptr_t
*bp
)
705 ddt_key_fill(&ddk
, bp
);
707 dde
= ddt_alloc(&ddk
);
709 for (ddt_type_t type
= 0; type
< DDT_TYPES
; type
++) {
710 for (ddt_class_t
class = 0; class < DDT_CLASSES
; class++) {
712 * We can only do repair if there are multiple copies
713 * of the block. For anything in the UNIQUE class,
714 * there's definitely only one copy, so don't even try.
716 if (class != DDT_CLASS_UNIQUE
&&
717 ddt_object_lookup(ddt
, type
, class, dde
) == 0)
722 memset(dde
->dde_phys
, 0, sizeof (dde
->dde_phys
));
728 ddt_repair_done(ddt_t
*ddt
, ddt_entry_t
*dde
)
734 if (dde
->dde_repair_abd
!= NULL
&& spa_writeable(ddt
->ddt_spa
) &&
735 avl_find(&ddt
->ddt_repair_tree
, dde
, &where
) == NULL
)
736 avl_insert(&ddt
->ddt_repair_tree
, dde
, where
);
744 ddt_repair_entry_done(zio_t
*zio
)
746 ddt_entry_t
*rdde
= zio
->io_private
;
752 ddt_repair_entry(ddt_t
*ddt
, ddt_entry_t
*dde
, ddt_entry_t
*rdde
, zio_t
*rio
)
754 ddt_phys_t
*ddp
= dde
->dde_phys
;
755 ddt_phys_t
*rddp
= rdde
->dde_phys
;
756 ddt_key_t
*ddk
= &dde
->dde_key
;
757 ddt_key_t
*rddk
= &rdde
->dde_key
;
761 zio
= zio_null(rio
, rio
->io_spa
, NULL
,
762 ddt_repair_entry_done
, rdde
, rio
->io_flags
);
764 for (int p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++, rddp
++) {
765 if (ddp
->ddp_phys_birth
== 0 ||
766 ddp
->ddp_phys_birth
!= rddp
->ddp_phys_birth
||
767 memcmp(ddp
->ddp_dva
, rddp
->ddp_dva
, sizeof (ddp
->ddp_dva
)))
769 ddt_bp_create(ddt
->ddt_checksum
, ddk
, ddp
, &blk
);
770 zio_nowait(zio_rewrite(zio
, zio
->io_spa
, 0, &blk
,
771 rdde
->dde_repair_abd
, DDK_GET_PSIZE(rddk
), NULL
, NULL
,
772 ZIO_PRIORITY_SYNC_WRITE
, ZIO_DDT_CHILD_FLAGS(zio
), NULL
));
779 ddt_repair_table(ddt_t
*ddt
, zio_t
*rio
)
781 spa_t
*spa
= ddt
->ddt_spa
;
782 ddt_entry_t
*dde
, *rdde_next
, *rdde
;
783 avl_tree_t
*t
= &ddt
->ddt_repair_tree
;
786 if (spa_sync_pass(spa
) > 1)
790 for (rdde
= avl_first(t
); rdde
!= NULL
; rdde
= rdde_next
) {
791 rdde_next
= AVL_NEXT(t
, rdde
);
792 avl_remove(&ddt
->ddt_repair_tree
, rdde
);
794 ddt_bp_create(ddt
->ddt_checksum
, &rdde
->dde_key
, NULL
, &blk
);
795 dde
= ddt_repair_start(ddt
, &blk
);
796 ddt_repair_entry(ddt
, dde
, rdde
, rio
);
797 ddt_repair_done(ddt
, dde
);
804 ddt_sync_entry(ddt_t
*ddt
, ddt_entry_t
*dde
, dmu_tx_t
*tx
, uint64_t txg
)
806 dsl_pool_t
*dp
= ddt
->ddt_spa
->spa_dsl_pool
;
807 ddt_phys_t
*ddp
= dde
->dde_phys
;
808 ddt_key_t
*ddk
= &dde
->dde_key
;
809 ddt_type_t otype
= dde
->dde_type
;
810 ddt_type_t ntype
= DDT_TYPE_DEFAULT
;
811 ddt_class_t oclass
= dde
->dde_class
;
813 uint64_t total_refcnt
= 0;
815 ASSERT(dde
->dde_loaded
);
816 ASSERT(!dde
->dde_loading
);
818 for (int p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
819 ASSERT3P(dde
->dde_lead_zio
[p
], ==, NULL
);
820 if (ddp
->ddp_phys_birth
== 0) {
821 ASSERT0(ddp
->ddp_refcnt
);
824 if (p
== DDT_PHYS_DITTO
) {
826 * Note, we no longer create DDT-DITTO blocks, but we
827 * don't want to leak any written by older software.
829 ddt_phys_free(ddt
, ddk
, ddp
, txg
);
832 if (ddp
->ddp_refcnt
== 0)
833 ddt_phys_free(ddt
, ddk
, ddp
, txg
);
834 total_refcnt
+= ddp
->ddp_refcnt
;
837 /* We do not create new DDT-DITTO blocks. */
838 ASSERT0(dde
->dde_phys
[DDT_PHYS_DITTO
].ddp_phys_birth
);
839 if (total_refcnt
> 1)
840 nclass
= DDT_CLASS_DUPLICATE
;
842 nclass
= DDT_CLASS_UNIQUE
;
844 if (otype
!= DDT_TYPES
&&
845 (otype
!= ntype
|| oclass
!= nclass
|| total_refcnt
== 0)) {
846 VERIFY0(ddt_object_remove(ddt
, otype
, oclass
, ddk
, tx
));
848 ddt_object_contains(ddt
, otype
, oclass
, ddk
), ==, ENOENT
);
851 if (total_refcnt
!= 0) {
852 dde
->dde_type
= ntype
;
853 dde
->dde_class
= nclass
;
854 ddt_stat_update(ddt
, dde
, 0);
855 if (!ddt_object_exists(ddt
, ntype
, nclass
))
856 ddt_object_create(ddt
, ntype
, nclass
, tx
);
857 VERIFY0(ddt_object_update(ddt
, ntype
, nclass
, dde
, tx
));
860 * If the class changes, the order that we scan this bp
861 * changes. If it decreases, we could miss it, so
862 * scan it right now. (This covers both class changing
863 * while we are doing ddt_walk(), and when we are
866 if (nclass
< oclass
) {
867 dsl_scan_ddt_entry(dp
->dp_scan
,
868 ddt
->ddt_checksum
, dde
, tx
);
874 ddt_sync_table(ddt_t
*ddt
, dmu_tx_t
*tx
, uint64_t txg
)
876 spa_t
*spa
= ddt
->ddt_spa
;
880 if (avl_numnodes(&ddt
->ddt_tree
) == 0)
883 ASSERT3U(spa
->spa_uberblock
.ub_version
, >=, SPA_VERSION_DEDUP
);
885 if (spa
->spa_ddt_stat_object
== 0) {
886 spa
->spa_ddt_stat_object
= zap_create_link(ddt
->ddt_os
,
887 DMU_OT_DDT_STATS
, DMU_POOL_DIRECTORY_OBJECT
,
888 DMU_POOL_DDT_STATS
, tx
);
891 while ((dde
= avl_destroy_nodes(&ddt
->ddt_tree
, &cookie
)) != NULL
) {
892 ddt_sync_entry(ddt
, dde
, tx
, txg
);
896 for (ddt_type_t type
= 0; type
< DDT_TYPES
; type
++) {
897 uint64_t add
, count
= 0;
898 for (ddt_class_t
class = 0; class < DDT_CLASSES
; class++) {
899 if (ddt_object_exists(ddt
, type
, class)) {
900 ddt_object_sync(ddt
, type
, class, tx
);
901 VERIFY0(ddt_object_count(ddt
, type
, class,
906 for (ddt_class_t
class = 0; class < DDT_CLASSES
; class++) {
907 if (count
== 0 && ddt_object_exists(ddt
, type
, class))
908 ddt_object_destroy(ddt
, type
, class, tx
);
912 memcpy(&ddt
->ddt_histogram_cache
, ddt
->ddt_histogram
,
913 sizeof (ddt
->ddt_histogram
));
914 spa
->spa_dedup_dspace
= ~0ULL;
918 ddt_sync(spa_t
*spa
, uint64_t txg
)
920 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
924 ASSERT3U(spa_syncing_txg(spa
), ==, txg
);
926 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
928 rio
= zio_root(spa
, NULL
, NULL
,
929 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_SELF_HEAL
);
932 * This function may cause an immediate scan of ddt blocks (see
933 * the comment above dsl_scan_ddt() for details). We set the
934 * scan's root zio here so that we can wait for any scan IOs in
935 * addition to the regular ddt IOs.
937 ASSERT3P(scn
->scn_zio_root
, ==, NULL
);
938 scn
->scn_zio_root
= rio
;
940 for (enum zio_checksum c
= 0; c
< ZIO_CHECKSUM_FUNCTIONS
; c
++) {
941 ddt_t
*ddt
= spa
->spa_ddt
[c
];
944 ddt_sync_table(ddt
, tx
, txg
);
945 ddt_repair_table(ddt
, rio
);
948 (void) zio_wait(rio
);
949 scn
->scn_zio_root
= NULL
;
955 ddt_walk(spa_t
*spa
, ddt_bookmark_t
*ddb
, ddt_entry_t
*dde
)
960 ddt_t
*ddt
= spa
->spa_ddt
[ddb
->ddb_checksum
];
962 if (ddt_object_exists(ddt
, ddb
->ddb_type
,
964 error
= ddt_object_walk(ddt
,
965 ddb
->ddb_type
, ddb
->ddb_class
,
966 &ddb
->ddb_cursor
, dde
);
968 dde
->dde_type
= ddb
->ddb_type
;
969 dde
->dde_class
= ddb
->ddb_class
;
975 } while (++ddb
->ddb_checksum
< ZIO_CHECKSUM_FUNCTIONS
);
976 ddb
->ddb_checksum
= 0;
977 } while (++ddb
->ddb_type
< DDT_TYPES
);
979 } while (++ddb
->ddb_class
< DDT_CLASSES
);
981 return (SET_ERROR(ENOENT
));
985 * This function is used by Block Cloning (brt.c) to increase reference
986 * counter for the DDT entry if the block is already in DDT.
988 * Return false if the block, despite having the D bit set, is not present
989 * in the DDT. Currently this is not possible but might be in the future.
990 * See the comment below.
993 ddt_addref(spa_t
*spa
, const blkptr_t
*bp
)
999 spa_config_enter(spa
, SCL_ZIO
, FTAG
, RW_READER
);
1000 ddt
= ddt_select(spa
, bp
);
1003 dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
1004 ASSERT3P(dde
, !=, NULL
);
1006 if (dde
->dde_type
< DDT_TYPES
) {
1009 ASSERT3S(dde
->dde_class
, <, DDT_CLASSES
);
1011 ddp
= &dde
->dde_phys
[BP_GET_NDVAS(bp
)];
1014 * This entry already existed (dde_type is real), so it must
1015 * have refcnt >0 at the start of this txg. We are called from
1016 * brt_pending_apply(), before frees are issued, so the refcnt
1017 * can't be lowered yet. Therefore, it must be >0. We assert
1018 * this because if the order of BRT and DDT interactions were
1019 * ever to change and the refcnt was ever zero here, then
1020 * likely further action is required to fill out the DDT entry,
1021 * and this is a place that is likely to be missed in testing.
1023 ASSERT3U(ddp
->ddp_refcnt
, >, 0);
1025 ddt_phys_addref(ddp
);
1029 * At the time of implementating this if the block has the
1030 * DEDUP flag set it must exist in the DEDUP table, but
1031 * there are many advocates that want ability to remove
1032 * entries from DDT with refcnt=1. If this will happen,
1033 * we may have a block with the DEDUP set, but which doesn't
1034 * have a corresponding entry in the DDT. Be ready.
1036 ASSERT3S(dde
->dde_class
, ==, DDT_CLASSES
);
1037 ddt_remove(ddt
, dde
);
1042 spa_config_exit(spa
, SCL_ZIO
, FTAG
);
1047 ZFS_MODULE_PARAM(zfs_dedup
, zfs_dedup_
, prefetch
, INT
, ZMOD_RW
,
1048 "Enable prefetching dedup-ed blks");