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>
34 #include <sys/dmu_tx.h>
36 #include <sys/dsl_pool.h>
37 #include <sys/zio_checksum.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
*const ddt_ops
[DDT_TYPES
] = {
53 static const char *const 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 ASSERT3U(*objectp
, ==, 0);
73 VERIFY0(ddt_ops
[type
]->ddt_op_create(os
, objectp
, tx
, prehash
));
74 ASSERT3U(*objectp
, !=, 0);
76 VERIFY0(zap_add(os
, DMU_POOL_DIRECTORY_OBJECT
, name
,
77 sizeof (uint64_t), 1, objectp
, tx
));
79 VERIFY0(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
));
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 ASSERT3U(*objectp
, !=, 0);
97 ASSERT(ddt_histogram_empty(&ddt
->ddt_histogram
[type
][class]));
98 VERIFY0(ddt_object_count(ddt
, type
, class, &count
));
100 VERIFY0(zap_remove(os
, DMU_POOL_DIRECTORY_OBJECT
, name
, tx
));
101 VERIFY0(zap_remove(os
, spa
->spa_ddt_stat_object
, name
, tx
));
102 VERIFY0(ddt_ops
[type
]->ddt_op_destroy(os
, *objectp
, tx
));
103 memset(&ddt
->ddt_object_stats
[type
][class], 0, sizeof (ddt_object_t
));
109 ddt_object_load(ddt_t
*ddt
, enum ddt_type type
, enum ddt_class
class)
111 ddt_object_t
*ddo
= &ddt
->ddt_object_stats
[type
][class];
112 dmu_object_info_t doi
;
114 char name
[DDT_NAMELEN
];
117 ddt_object_name(ddt
, type
, class, name
);
119 error
= zap_lookup(ddt
->ddt_os
, DMU_POOL_DIRECTORY_OBJECT
, name
,
120 sizeof (uint64_t), 1, &ddt
->ddt_object
[type
][class]);
124 error
= 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]);
131 * Seed the cached statistics.
133 error
= ddt_object_info(ddt
, type
, class, &doi
);
137 error
= ddt_object_count(ddt
, type
, class, &count
);
141 ddo
->ddo_count
= count
;
142 ddo
->ddo_dspace
= doi
.doi_physical_blocks_512
<< 9;
143 ddo
->ddo_mspace
= doi
.doi_fill_count
* doi
.doi_data_block_size
;
149 ddt_object_sync(ddt_t
*ddt
, enum ddt_type type
, enum ddt_class
class,
152 ddt_object_t
*ddo
= &ddt
->ddt_object_stats
[type
][class];
153 dmu_object_info_t doi
;
155 char name
[DDT_NAMELEN
];
157 ddt_object_name(ddt
, type
, class, name
);
159 VERIFY0(zap_update(ddt
->ddt_os
, ddt
->ddt_spa
->spa_ddt_stat_object
, name
,
160 sizeof (uint64_t), sizeof (ddt_histogram_t
) / sizeof (uint64_t),
161 &ddt
->ddt_histogram
[type
][class], tx
));
164 * Cache DDT statistics; this is the only time they'll change.
166 VERIFY0(ddt_object_info(ddt
, type
, class, &doi
));
167 VERIFY0(ddt_object_count(ddt
, type
, class, &count
));
169 ddo
->ddo_count
= count
;
170 ddo
->ddo_dspace
= doi
.doi_physical_blocks_512
<< 9;
171 ddo
->ddo_mspace
= doi
.doi_fill_count
* doi
.doi_data_block_size
;
175 ddt_object_lookup(ddt_t
*ddt
, enum ddt_type type
, enum ddt_class
class,
178 if (!ddt_object_exists(ddt
, type
, class))
179 return (SET_ERROR(ENOENT
));
181 return (ddt_ops
[type
]->ddt_op_lookup(ddt
->ddt_os
,
182 ddt
->ddt_object
[type
][class], dde
));
186 ddt_object_prefetch(ddt_t
*ddt
, enum ddt_type type
, enum ddt_class
class,
189 if (!ddt_object_exists(ddt
, type
, class))
192 ddt_ops
[type
]->ddt_op_prefetch(ddt
->ddt_os
,
193 ddt
->ddt_object
[type
][class], dde
);
197 ddt_object_update(ddt_t
*ddt
, enum ddt_type type
, enum ddt_class
class,
198 ddt_entry_t
*dde
, dmu_tx_t
*tx
)
200 ASSERT(ddt_object_exists(ddt
, type
, class));
202 return (ddt_ops
[type
]->ddt_op_update(ddt
->ddt_os
,
203 ddt
->ddt_object
[type
][class], dde
, tx
));
207 ddt_object_remove(ddt_t
*ddt
, enum ddt_type type
, enum ddt_class
class,
208 ddt_entry_t
*dde
, dmu_tx_t
*tx
)
210 ASSERT(ddt_object_exists(ddt
, type
, class));
212 return (ddt_ops
[type
]->ddt_op_remove(ddt
->ddt_os
,
213 ddt
->ddt_object
[type
][class], dde
, tx
));
217 ddt_object_walk(ddt_t
*ddt
, enum ddt_type type
, enum ddt_class
class,
218 uint64_t *walk
, ddt_entry_t
*dde
)
220 ASSERT(ddt_object_exists(ddt
, type
, class));
222 return (ddt_ops
[type
]->ddt_op_walk(ddt
->ddt_os
,
223 ddt
->ddt_object
[type
][class], dde
, walk
));
227 ddt_object_count(ddt_t
*ddt
, enum ddt_type type
, enum ddt_class
class,
230 ASSERT(ddt_object_exists(ddt
, type
, class));
232 return (ddt_ops
[type
]->ddt_op_count(ddt
->ddt_os
,
233 ddt
->ddt_object
[type
][class], count
));
237 ddt_object_info(ddt_t
*ddt
, enum ddt_type type
, enum ddt_class
class,
238 dmu_object_info_t
*doi
)
240 if (!ddt_object_exists(ddt
, type
, class))
241 return (SET_ERROR(ENOENT
));
243 return (dmu_object_info(ddt
->ddt_os
, ddt
->ddt_object
[type
][class],
248 ddt_object_exists(ddt_t
*ddt
, enum ddt_type type
, enum ddt_class
class)
250 return (!!ddt
->ddt_object
[type
][class]);
254 ddt_object_name(ddt_t
*ddt
, enum ddt_type type
, enum ddt_class
class,
257 (void) snprintf(name
, DDT_NAMELEN
, DMU_POOL_DDT
,
258 zio_checksum_table
[ddt
->ddt_checksum
].ci_name
,
259 ddt_ops
[type
]->ddt_op_name
, ddt_class_name
[class]);
263 ddt_bp_fill(const ddt_phys_t
*ddp
, blkptr_t
*bp
, uint64_t txg
)
265 ASSERT3U(txg
, !=, 0);
267 for (int d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
268 bp
->blk_dva
[d
] = ddp
->ddp_dva
[d
];
269 BP_SET_BIRTH(bp
, txg
, ddp
->ddp_phys_birth
);
273 * The bp created via this function may be used for repairs and scrub, but it
274 * will be missing the salt / IV required to do a full decrypting read.
277 ddt_bp_create(enum zio_checksum checksum
,
278 const ddt_key_t
*ddk
, const ddt_phys_t
*ddp
, blkptr_t
*bp
)
283 ddt_bp_fill(ddp
, bp
, ddp
->ddp_phys_birth
);
285 bp
->blk_cksum
= ddk
->ddk_cksum
;
287 BP_SET_LSIZE(bp
, DDK_GET_LSIZE(ddk
));
288 BP_SET_PSIZE(bp
, DDK_GET_PSIZE(ddk
));
289 BP_SET_COMPRESS(bp
, DDK_GET_COMPRESS(ddk
));
290 BP_SET_CRYPT(bp
, DDK_GET_CRYPT(ddk
));
292 BP_SET_CHECKSUM(bp
, checksum
);
293 BP_SET_TYPE(bp
, DMU_OT_DEDUP
);
296 BP_SET_BYTEORDER(bp
, ZFS_HOST_BYTEORDER
);
300 ddt_key_fill(ddt_key_t
*ddk
, const blkptr_t
*bp
)
302 ddk
->ddk_cksum
= bp
->blk_cksum
;
305 ASSERT(BP_IS_ENCRYPTED(bp
) || !BP_USES_CRYPT(bp
));
307 DDK_SET_LSIZE(ddk
, BP_GET_LSIZE(bp
));
308 DDK_SET_PSIZE(ddk
, BP_GET_PSIZE(bp
));
309 DDK_SET_COMPRESS(ddk
, BP_GET_COMPRESS(bp
));
310 DDK_SET_CRYPT(ddk
, BP_USES_CRYPT(bp
));
314 ddt_phys_fill(ddt_phys_t
*ddp
, const blkptr_t
*bp
)
316 ASSERT0(ddp
->ddp_phys_birth
);
318 for (int d
= 0; d
< SPA_DVAS_PER_BP
; d
++)
319 ddp
->ddp_dva
[d
] = bp
->blk_dva
[d
];
320 ddp
->ddp_phys_birth
= BP_PHYSICAL_BIRTH(bp
);
324 ddt_phys_clear(ddt_phys_t
*ddp
)
326 memset(ddp
, 0, sizeof (*ddp
));
330 ddt_phys_addref(ddt_phys_t
*ddp
)
336 ddt_phys_decref(ddt_phys_t
*ddp
)
339 ASSERT3U(ddp
->ddp_refcnt
, >, 0);
345 ddt_phys_free(ddt_t
*ddt
, ddt_key_t
*ddk
, ddt_phys_t
*ddp
, uint64_t txg
)
349 ddt_bp_create(ddt
->ddt_checksum
, ddk
, ddp
, &blk
);
352 * We clear the dedup bit so that zio_free() will actually free the
353 * space, rather than just decrementing the refcount in the DDT.
355 BP_SET_DEDUP(&blk
, 0);
358 zio_free(ddt
->ddt_spa
, txg
, &blk
);
362 ddt_phys_select(const ddt_entry_t
*dde
, const blkptr_t
*bp
)
364 ddt_phys_t
*ddp
= (ddt_phys_t
*)dde
->dde_phys
;
366 for (int p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
367 if (DVA_EQUAL(BP_IDENTITY(bp
), &ddp
->ddp_dva
[0]) &&
368 BP_PHYSICAL_BIRTH(bp
) == ddp
->ddp_phys_birth
)
375 ddt_phys_total_refcnt(const ddt_entry_t
*dde
)
379 for (int p
= DDT_PHYS_SINGLE
; p
<= DDT_PHYS_TRIPLE
; p
++)
380 refcnt
+= dde
->dde_phys
[p
].ddp_refcnt
;
386 ddt_stat_generate(ddt_t
*ddt
, ddt_entry_t
*dde
, ddt_stat_t
*dds
)
388 spa_t
*spa
= ddt
->ddt_spa
;
389 ddt_phys_t
*ddp
= dde
->dde_phys
;
390 ddt_key_t
*ddk
= &dde
->dde_key
;
391 uint64_t lsize
= DDK_GET_LSIZE(ddk
);
392 uint64_t psize
= DDK_GET_PSIZE(ddk
);
394 memset(dds
, 0, sizeof (*dds
));
396 for (int p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
398 uint64_t refcnt
= ddp
->ddp_refcnt
;
400 if (ddp
->ddp_phys_birth
== 0)
403 for (int d
= 0; d
< DDE_GET_NDVAS(dde
); d
++)
404 dsize
+= dva_get_dsize_sync(spa
, &ddp
->ddp_dva
[d
]);
406 dds
->dds_blocks
+= 1;
407 dds
->dds_lsize
+= lsize
;
408 dds
->dds_psize
+= psize
;
409 dds
->dds_dsize
+= dsize
;
411 dds
->dds_ref_blocks
+= refcnt
;
412 dds
->dds_ref_lsize
+= lsize
* refcnt
;
413 dds
->dds_ref_psize
+= psize
* refcnt
;
414 dds
->dds_ref_dsize
+= dsize
* refcnt
;
419 ddt_stat_add(ddt_stat_t
*dst
, const ddt_stat_t
*src
, uint64_t neg
)
421 const uint64_t *s
= (const uint64_t *)src
;
422 uint64_t *d
= (uint64_t *)dst
;
423 uint64_t *d_end
= (uint64_t *)(dst
+ 1);
425 ASSERT(neg
== 0 || neg
== -1ULL); /* add or subtract */
427 for (int i
= 0; i
< d_end
- d
; i
++)
428 d
[i
] += (s
[i
] ^ neg
) - neg
;
432 ddt_stat_update(ddt_t
*ddt
, ddt_entry_t
*dde
, uint64_t neg
)
435 ddt_histogram_t
*ddh
;
438 ddt_stat_generate(ddt
, dde
, &dds
);
440 bucket
= highbit64(dds
.dds_ref_blocks
) - 1;
441 ASSERT3U(bucket
, >=, 0);
443 ddh
= &ddt
->ddt_histogram
[dde
->dde_type
][dde
->dde_class
];
445 ddt_stat_add(&ddh
->ddh_stat
[bucket
], &dds
, neg
);
449 ddt_histogram_add(ddt_histogram_t
*dst
, const ddt_histogram_t
*src
)
451 for (int h
= 0; h
< 64; h
++)
452 ddt_stat_add(&dst
->ddh_stat
[h
], &src
->ddh_stat
[h
], 0);
456 ddt_histogram_stat(ddt_stat_t
*dds
, const ddt_histogram_t
*ddh
)
458 memset(dds
, 0, sizeof (*dds
));
460 for (int h
= 0; h
< 64; h
++)
461 ddt_stat_add(dds
, &ddh
->ddh_stat
[h
], 0);
465 ddt_histogram_empty(const ddt_histogram_t
*ddh
)
467 const uint64_t *s
= (const uint64_t *)ddh
;
468 const uint64_t *s_end
= (const uint64_t *)(ddh
+ 1);
478 ddt_get_dedup_object_stats(spa_t
*spa
, ddt_object_t
*ddo_total
)
480 /* Sum the statistics we cached in ddt_object_sync(). */
481 for (enum zio_checksum c
= 0; c
< ZIO_CHECKSUM_FUNCTIONS
; c
++) {
482 ddt_t
*ddt
= spa
->spa_ddt
[c
];
483 for (enum ddt_type type
= 0; type
< DDT_TYPES
; type
++) {
484 for (enum ddt_class
class = 0; class < DDT_CLASSES
;
487 &ddt
->ddt_object_stats
[type
][class];
488 ddo_total
->ddo_count
+= ddo
->ddo_count
;
489 ddo_total
->ddo_dspace
+= ddo
->ddo_dspace
;
490 ddo_total
->ddo_mspace
+= ddo
->ddo_mspace
;
495 /* ... and compute the averages. */
496 if (ddo_total
->ddo_count
!= 0) {
497 ddo_total
->ddo_dspace
/= ddo_total
->ddo_count
;
498 ddo_total
->ddo_mspace
/= ddo_total
->ddo_count
;
503 ddt_get_dedup_histogram(spa_t
*spa
, ddt_histogram_t
*ddh
)
505 for (enum zio_checksum c
= 0; c
< ZIO_CHECKSUM_FUNCTIONS
; c
++) {
506 ddt_t
*ddt
= spa
->spa_ddt
[c
];
507 for (enum ddt_type type
= 0; type
< DDT_TYPES
&& ddt
; type
++) {
508 for (enum ddt_class
class = 0; class < DDT_CLASSES
;
510 ddt_histogram_add(ddh
,
511 &ddt
->ddt_histogram_cache
[type
][class]);
518 ddt_get_dedup_stats(spa_t
*spa
, ddt_stat_t
*dds_total
)
520 ddt_histogram_t
*ddh_total
;
522 ddh_total
= kmem_zalloc(sizeof (ddt_histogram_t
), KM_SLEEP
);
523 ddt_get_dedup_histogram(spa
, ddh_total
);
524 ddt_histogram_stat(dds_total
, ddh_total
);
525 kmem_free(ddh_total
, sizeof (ddt_histogram_t
));
529 ddt_get_dedup_dspace(spa_t
*spa
)
531 ddt_stat_t dds_total
;
533 if (spa
->spa_dedup_dspace
!= ~0ULL)
534 return (spa
->spa_dedup_dspace
);
536 memset(&dds_total
, 0, sizeof (ddt_stat_t
));
538 /* Calculate and cache the stats */
539 ddt_get_dedup_stats(spa
, &dds_total
);
540 spa
->spa_dedup_dspace
= dds_total
.dds_ref_dsize
- dds_total
.dds_dsize
;
541 return (spa
->spa_dedup_dspace
);
545 ddt_get_pool_dedup_ratio(spa_t
*spa
)
547 ddt_stat_t dds_total
= { 0 };
549 ddt_get_dedup_stats(spa
, &dds_total
);
550 if (dds_total
.dds_dsize
== 0)
553 return (dds_total
.dds_ref_dsize
* 100 / dds_total
.dds_dsize
);
557 ddt_select(spa_t
*spa
, const blkptr_t
*bp
)
559 return (spa
->spa_ddt
[BP_GET_CHECKSUM(bp
)]);
563 ddt_enter(ddt_t
*ddt
)
565 mutex_enter(&ddt
->ddt_lock
);
571 mutex_exit(&ddt
->ddt_lock
);
577 ddt_cache
= kmem_cache_create("ddt_cache",
578 sizeof (ddt_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
579 ddt_entry_cache
= kmem_cache_create("ddt_entry_cache",
580 sizeof (ddt_entry_t
), 0, NULL
, NULL
, NULL
, NULL
, NULL
, 0);
586 kmem_cache_destroy(ddt_entry_cache
);
587 kmem_cache_destroy(ddt_cache
);
591 ddt_alloc(const ddt_key_t
*ddk
)
595 dde
= kmem_cache_alloc(ddt_entry_cache
, KM_SLEEP
);
596 memset(dde
, 0, sizeof (ddt_entry_t
));
597 cv_init(&dde
->dde_cv
, NULL
, CV_DEFAULT
, NULL
);
605 ddt_free(ddt_entry_t
*dde
)
607 ASSERT(!dde
->dde_loading
);
609 for (int p
= 0; p
< DDT_PHYS_TYPES
; p
++)
610 ASSERT3P(dde
->dde_lead_zio
[p
], ==, NULL
);
612 if (dde
->dde_repair_abd
!= NULL
)
613 abd_free(dde
->dde_repair_abd
);
615 cv_destroy(&dde
->dde_cv
);
616 kmem_cache_free(ddt_entry_cache
, dde
);
620 ddt_remove(ddt_t
*ddt
, ddt_entry_t
*dde
)
622 ASSERT(MUTEX_HELD(&ddt
->ddt_lock
));
624 avl_remove(&ddt
->ddt_tree
, dde
);
629 ddt_lookup(ddt_t
*ddt
, const blkptr_t
*bp
, boolean_t add
)
631 ddt_entry_t
*dde
, dde_search
;
633 enum ddt_class
class;
637 ASSERT(MUTEX_HELD(&ddt
->ddt_lock
));
639 ddt_key_fill(&dde_search
.dde_key
, bp
);
641 dde
= avl_find(&ddt
->ddt_tree
, &dde_search
, &where
);
645 dde
= ddt_alloc(&dde_search
.dde_key
);
646 avl_insert(&ddt
->ddt_tree
, dde
, where
);
649 while (dde
->dde_loading
)
650 cv_wait(&dde
->dde_cv
, &ddt
->ddt_lock
);
655 dde
->dde_loading
= B_TRUE
;
661 for (type
= 0; type
< DDT_TYPES
; type
++) {
662 for (class = 0; class < DDT_CLASSES
; class++) {
663 error
= ddt_object_lookup(ddt
, type
, class, dde
);
664 if (error
!= ENOENT
) {
675 ASSERT(!dde
->dde_loaded
);
676 ASSERT(dde
->dde_loading
);
678 dde
->dde_type
= type
; /* will be DDT_TYPES if no entry found */
679 dde
->dde_class
= class; /* will be DDT_CLASSES if no entry found */
680 dde
->dde_loaded
= B_TRUE
;
681 dde
->dde_loading
= B_FALSE
;
684 ddt_stat_update(ddt
, dde
, -1ULL);
686 cv_broadcast(&dde
->dde_cv
);
692 ddt_prefetch(spa_t
*spa
, const blkptr_t
*bp
)
697 if (!zfs_dedup_prefetch
|| bp
== NULL
|| !BP_GET_DEDUP(bp
))
701 * We only remove the DDT once all tables are empty and only
702 * prefetch dedup blocks when there are entries in the DDT.
703 * Thus no locking is required as the DDT can't disappear on us.
705 ddt
= ddt_select(spa
, bp
);
706 ddt_key_fill(&dde
.dde_key
, bp
);
708 for (enum ddt_type type
= 0; type
< DDT_TYPES
; type
++) {
709 for (enum ddt_class
class = 0; class < DDT_CLASSES
; class++) {
710 ddt_object_prefetch(ddt
, type
, class, &dde
);
716 * Opaque struct used for ddt_key comparison
718 #define DDT_KEY_CMP_LEN (sizeof (ddt_key_t) / sizeof (uint16_t))
720 typedef struct ddt_key_cmp
{
721 uint16_t u16
[DDT_KEY_CMP_LEN
];
725 ddt_entry_compare(const void *x1
, const void *x2
)
727 const ddt_entry_t
*dde1
= x1
;
728 const ddt_entry_t
*dde2
= x2
;
729 const ddt_key_cmp_t
*k1
= (const ddt_key_cmp_t
*)&dde1
->dde_key
;
730 const ddt_key_cmp_t
*k2
= (const ddt_key_cmp_t
*)&dde2
->dde_key
;
733 for (int i
= 0; i
< DDT_KEY_CMP_LEN
; i
++) {
734 cmp
= (int32_t)k1
->u16
[i
] - (int32_t)k2
->u16
[i
];
739 return (TREE_ISIGN(cmp
));
743 ddt_table_alloc(spa_t
*spa
, enum zio_checksum c
)
747 ddt
= kmem_cache_alloc(ddt_cache
, KM_SLEEP
);
748 memset(ddt
, 0, sizeof (ddt_t
));
750 mutex_init(&ddt
->ddt_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
751 avl_create(&ddt
->ddt_tree
, ddt_entry_compare
,
752 sizeof (ddt_entry_t
), offsetof(ddt_entry_t
, dde_node
));
753 avl_create(&ddt
->ddt_repair_tree
, ddt_entry_compare
,
754 sizeof (ddt_entry_t
), offsetof(ddt_entry_t
, dde_node
));
755 ddt
->ddt_checksum
= c
;
757 ddt
->ddt_os
= spa
->spa_meta_objset
;
763 ddt_table_free(ddt_t
*ddt
)
765 ASSERT0(avl_numnodes(&ddt
->ddt_tree
));
766 ASSERT0(avl_numnodes(&ddt
->ddt_repair_tree
));
767 avl_destroy(&ddt
->ddt_tree
);
768 avl_destroy(&ddt
->ddt_repair_tree
);
769 mutex_destroy(&ddt
->ddt_lock
);
770 kmem_cache_free(ddt_cache
, ddt
);
774 ddt_create(spa_t
*spa
)
776 spa
->spa_dedup_checksum
= ZIO_DEDUPCHECKSUM
;
778 for (enum zio_checksum c
= 0; c
< ZIO_CHECKSUM_FUNCTIONS
; c
++)
779 spa
->spa_ddt
[c
] = ddt_table_alloc(spa
, c
);
789 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
790 DMU_POOL_DDT_STATS
, sizeof (uint64_t), 1,
791 &spa
->spa_ddt_stat_object
);
794 return (error
== ENOENT
? 0 : error
);
796 for (enum zio_checksum c
= 0; c
< ZIO_CHECKSUM_FUNCTIONS
; c
++) {
797 ddt_t
*ddt
= spa
->spa_ddt
[c
];
798 for (enum ddt_type type
= 0; type
< DDT_TYPES
; type
++) {
799 for (enum ddt_class
class = 0; class < DDT_CLASSES
;
801 error
= ddt_object_load(ddt
, type
, class);
802 if (error
!= 0 && error
!= ENOENT
)
808 * Seed the cached histograms.
810 memcpy(&ddt
->ddt_histogram_cache
, ddt
->ddt_histogram
,
811 sizeof (ddt
->ddt_histogram
));
812 spa
->spa_dedup_dspace
= ~0ULL;
819 ddt_unload(spa_t
*spa
)
821 for (enum zio_checksum c
= 0; c
< ZIO_CHECKSUM_FUNCTIONS
; c
++) {
822 if (spa
->spa_ddt
[c
]) {
823 ddt_table_free(spa
->spa_ddt
[c
]);
824 spa
->spa_ddt
[c
] = NULL
;
830 ddt_class_contains(spa_t
*spa
, enum ddt_class max_class
, const blkptr_t
*bp
)
835 if (!BP_GET_DEDUP(bp
))
838 if (max_class
== DDT_CLASS_UNIQUE
)
841 ddt
= spa
->spa_ddt
[BP_GET_CHECKSUM(bp
)];
842 dde
= kmem_cache_alloc(ddt_entry_cache
, KM_SLEEP
);
844 ddt_key_fill(&(dde
->dde_key
), bp
);
846 for (enum ddt_type type
= 0; type
< DDT_TYPES
; type
++) {
847 for (enum ddt_class
class = 0; class <= max_class
; class++) {
848 if (ddt_object_lookup(ddt
, type
, class, dde
) == 0) {
849 kmem_cache_free(ddt_entry_cache
, dde
);
855 kmem_cache_free(ddt_entry_cache
, dde
);
860 ddt_repair_start(ddt_t
*ddt
, const blkptr_t
*bp
)
865 ddt_key_fill(&ddk
, bp
);
867 dde
= ddt_alloc(&ddk
);
869 for (enum ddt_type type
= 0; type
< DDT_TYPES
; type
++) {
870 for (enum ddt_class
class = 0; class < DDT_CLASSES
; class++) {
872 * We can only do repair if there are multiple copies
873 * of the block. For anything in the UNIQUE class,
874 * there's definitely only one copy, so don't even try.
876 if (class != DDT_CLASS_UNIQUE
&&
877 ddt_object_lookup(ddt
, type
, class, dde
) == 0)
882 memset(dde
->dde_phys
, 0, sizeof (dde
->dde_phys
));
888 ddt_repair_done(ddt_t
*ddt
, ddt_entry_t
*dde
)
894 if (dde
->dde_repair_abd
!= NULL
&& spa_writeable(ddt
->ddt_spa
) &&
895 avl_find(&ddt
->ddt_repair_tree
, dde
, &where
) == NULL
)
896 avl_insert(&ddt
->ddt_repair_tree
, dde
, where
);
904 ddt_repair_entry_done(zio_t
*zio
)
906 ddt_entry_t
*rdde
= zio
->io_private
;
912 ddt_repair_entry(ddt_t
*ddt
, ddt_entry_t
*dde
, ddt_entry_t
*rdde
, zio_t
*rio
)
914 ddt_phys_t
*ddp
= dde
->dde_phys
;
915 ddt_phys_t
*rddp
= rdde
->dde_phys
;
916 ddt_key_t
*ddk
= &dde
->dde_key
;
917 ddt_key_t
*rddk
= &rdde
->dde_key
;
921 zio
= zio_null(rio
, rio
->io_spa
, NULL
,
922 ddt_repair_entry_done
, rdde
, rio
->io_flags
);
924 for (int p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++, rddp
++) {
925 if (ddp
->ddp_phys_birth
== 0 ||
926 ddp
->ddp_phys_birth
!= rddp
->ddp_phys_birth
||
927 memcmp(ddp
->ddp_dva
, rddp
->ddp_dva
, sizeof (ddp
->ddp_dva
)))
929 ddt_bp_create(ddt
->ddt_checksum
, ddk
, ddp
, &blk
);
930 zio_nowait(zio_rewrite(zio
, zio
->io_spa
, 0, &blk
,
931 rdde
->dde_repair_abd
, DDK_GET_PSIZE(rddk
), NULL
, NULL
,
932 ZIO_PRIORITY_SYNC_WRITE
, ZIO_DDT_CHILD_FLAGS(zio
), NULL
));
939 ddt_repair_table(ddt_t
*ddt
, zio_t
*rio
)
941 spa_t
*spa
= ddt
->ddt_spa
;
942 ddt_entry_t
*dde
, *rdde_next
, *rdde
;
943 avl_tree_t
*t
= &ddt
->ddt_repair_tree
;
946 if (spa_sync_pass(spa
) > 1)
950 for (rdde
= avl_first(t
); rdde
!= NULL
; rdde
= rdde_next
) {
951 rdde_next
= AVL_NEXT(t
, rdde
);
952 avl_remove(&ddt
->ddt_repair_tree
, rdde
);
954 ddt_bp_create(ddt
->ddt_checksum
, &rdde
->dde_key
, NULL
, &blk
);
955 dde
= ddt_repair_start(ddt
, &blk
);
956 ddt_repair_entry(ddt
, dde
, rdde
, rio
);
957 ddt_repair_done(ddt
, dde
);
964 ddt_sync_entry(ddt_t
*ddt
, ddt_entry_t
*dde
, dmu_tx_t
*tx
, uint64_t txg
)
966 dsl_pool_t
*dp
= ddt
->ddt_spa
->spa_dsl_pool
;
967 ddt_phys_t
*ddp
= dde
->dde_phys
;
968 ddt_key_t
*ddk
= &dde
->dde_key
;
969 enum ddt_type otype
= dde
->dde_type
;
970 enum ddt_type ntype
= DDT_TYPE_CURRENT
;
971 enum ddt_class oclass
= dde
->dde_class
;
972 enum ddt_class nclass
;
973 uint64_t total_refcnt
= 0;
975 ASSERT(dde
->dde_loaded
);
976 ASSERT(!dde
->dde_loading
);
978 for (int p
= 0; p
< DDT_PHYS_TYPES
; p
++, ddp
++) {
979 ASSERT3P(dde
->dde_lead_zio
[p
], ==, NULL
);
980 if (ddp
->ddp_phys_birth
== 0) {
981 ASSERT0(ddp
->ddp_refcnt
);
984 if (p
== DDT_PHYS_DITTO
) {
986 * Note, we no longer create DDT-DITTO blocks, but we
987 * don't want to leak any written by older software.
989 ddt_phys_free(ddt
, ddk
, ddp
, txg
);
992 if (ddp
->ddp_refcnt
== 0)
993 ddt_phys_free(ddt
, ddk
, ddp
, txg
);
994 total_refcnt
+= ddp
->ddp_refcnt
;
997 /* We do not create new DDT-DITTO blocks. */
998 ASSERT0(dde
->dde_phys
[DDT_PHYS_DITTO
].ddp_phys_birth
);
999 if (total_refcnt
> 1)
1000 nclass
= DDT_CLASS_DUPLICATE
;
1002 nclass
= DDT_CLASS_UNIQUE
;
1004 if (otype
!= DDT_TYPES
&&
1005 (otype
!= ntype
|| oclass
!= nclass
|| total_refcnt
== 0)) {
1006 VERIFY0(ddt_object_remove(ddt
, otype
, oclass
, dde
, tx
));
1008 ddt_object_lookup(ddt
, otype
, oclass
, dde
), ==, ENOENT
);
1011 if (total_refcnt
!= 0) {
1012 dde
->dde_type
= ntype
;
1013 dde
->dde_class
= nclass
;
1014 ddt_stat_update(ddt
, dde
, 0);
1015 if (!ddt_object_exists(ddt
, ntype
, nclass
))
1016 ddt_object_create(ddt
, ntype
, nclass
, tx
);
1017 VERIFY0(ddt_object_update(ddt
, ntype
, nclass
, dde
, tx
));
1020 * If the class changes, the order that we scan this bp
1021 * changes. If it decreases, we could miss it, so
1022 * scan it right now. (This covers both class changing
1023 * while we are doing ddt_walk(), and when we are
1026 if (nclass
< oclass
) {
1027 dsl_scan_ddt_entry(dp
->dp_scan
,
1028 ddt
->ddt_checksum
, dde
, tx
);
1034 ddt_sync_table(ddt_t
*ddt
, dmu_tx_t
*tx
, uint64_t txg
)
1036 spa_t
*spa
= ddt
->ddt_spa
;
1038 void *cookie
= NULL
;
1040 if (avl_numnodes(&ddt
->ddt_tree
) == 0)
1043 ASSERT3U(spa
->spa_uberblock
.ub_version
, >=, SPA_VERSION_DEDUP
);
1045 if (spa
->spa_ddt_stat_object
== 0) {
1046 spa
->spa_ddt_stat_object
= zap_create_link(ddt
->ddt_os
,
1047 DMU_OT_DDT_STATS
, DMU_POOL_DIRECTORY_OBJECT
,
1048 DMU_POOL_DDT_STATS
, tx
);
1051 while ((dde
= avl_destroy_nodes(&ddt
->ddt_tree
, &cookie
)) != NULL
) {
1052 ddt_sync_entry(ddt
, dde
, tx
, txg
);
1056 for (enum ddt_type type
= 0; type
< DDT_TYPES
; type
++) {
1057 uint64_t add
, count
= 0;
1058 for (enum ddt_class
class = 0; class < DDT_CLASSES
; class++) {
1059 if (ddt_object_exists(ddt
, type
, class)) {
1060 ddt_object_sync(ddt
, type
, class, tx
);
1061 VERIFY0(ddt_object_count(ddt
, type
, class,
1066 for (enum ddt_class
class = 0; class < DDT_CLASSES
; class++) {
1067 if (count
== 0 && ddt_object_exists(ddt
, type
, class))
1068 ddt_object_destroy(ddt
, type
, class, tx
);
1072 memcpy(&ddt
->ddt_histogram_cache
, ddt
->ddt_histogram
,
1073 sizeof (ddt
->ddt_histogram
));
1074 spa
->spa_dedup_dspace
= ~0ULL;
1078 ddt_sync(spa_t
*spa
, uint64_t txg
)
1080 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
1084 ASSERT3U(spa_syncing_txg(spa
), ==, txg
);
1086 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
1088 rio
= zio_root(spa
, NULL
, NULL
,
1089 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_SELF_HEAL
);
1092 * This function may cause an immediate scan of ddt blocks (see
1093 * the comment above dsl_scan_ddt() for details). We set the
1094 * scan's root zio here so that we can wait for any scan IOs in
1095 * addition to the regular ddt IOs.
1097 ASSERT3P(scn
->scn_zio_root
, ==, NULL
);
1098 scn
->scn_zio_root
= rio
;
1100 for (enum zio_checksum c
= 0; c
< ZIO_CHECKSUM_FUNCTIONS
; c
++) {
1101 ddt_t
*ddt
= spa
->spa_ddt
[c
];
1104 ddt_sync_table(ddt
, tx
, txg
);
1105 ddt_repair_table(ddt
, rio
);
1108 (void) zio_wait(rio
);
1109 scn
->scn_zio_root
= NULL
;
1115 ddt_walk(spa_t
*spa
, ddt_bookmark_t
*ddb
, ddt_entry_t
*dde
)
1120 ddt_t
*ddt
= spa
->spa_ddt
[ddb
->ddb_checksum
];
1122 if (ddt_object_exists(ddt
, ddb
->ddb_type
,
1124 error
= ddt_object_walk(ddt
,
1125 ddb
->ddb_type
, ddb
->ddb_class
,
1126 &ddb
->ddb_cursor
, dde
);
1128 dde
->dde_type
= ddb
->ddb_type
;
1129 dde
->dde_class
= ddb
->ddb_class
;
1132 if (error
!= ENOENT
)
1134 ddb
->ddb_cursor
= 0;
1135 } while (++ddb
->ddb_checksum
< ZIO_CHECKSUM_FUNCTIONS
);
1136 ddb
->ddb_checksum
= 0;
1137 } while (++ddb
->ddb_type
< DDT_TYPES
);
1139 } while (++ddb
->ddb_class
< DDT_CLASSES
);
1141 return (SET_ERROR(ENOENT
));
1145 * This function is used by Block Cloning (brt.c) to increase reference
1146 * counter for the DDT entry if the block is already in DDT.
1148 * Return false if the block, despite having the D bit set, is not present
1149 * in the DDT. Currently this is not possible but might be in the future.
1150 * See the comment below.
1153 ddt_addref(spa_t
*spa
, const blkptr_t
*bp
)
1159 spa_config_enter(spa
, SCL_ZIO
, FTAG
, RW_READER
);
1160 ddt
= ddt_select(spa
, bp
);
1163 dde
= ddt_lookup(ddt
, bp
, B_TRUE
);
1164 ASSERT3P(dde
, !=, NULL
);
1166 if (dde
->dde_type
< DDT_TYPES
) {
1169 ASSERT3S(dde
->dde_class
, <, DDT_CLASSES
);
1171 ddp
= &dde
->dde_phys
[BP_GET_NDVAS(bp
)];
1174 * This entry already existed (dde_type is real), so it must
1175 * have refcnt >0 at the start of this txg. We are called from
1176 * brt_pending_apply(), before frees are issued, so the refcnt
1177 * can't be lowered yet. Therefore, it must be >0. We assert
1178 * this because if the order of BRT and DDT interactions were
1179 * ever to change and the refcnt was ever zero here, then
1180 * likely further action is required to fill out the DDT entry,
1181 * and this is a place that is likely to be missed in testing.
1183 ASSERT3U(ddp
->ddp_refcnt
, >, 0);
1185 ddt_phys_addref(ddp
);
1189 * At the time of implementating this if the block has the
1190 * DEDUP flag set it must exist in the DEDUP table, but
1191 * there are many advocates that want ability to remove
1192 * entries from DDT with refcnt=1. If this will happen,
1193 * we may have a block with the DEDUP set, but which doesn't
1194 * have a corresponding entry in the DDT. Be ready.
1196 ASSERT3S(dde
->dde_class
, ==, DDT_CLASSES
);
1197 ddt_remove(ddt
, dde
);
1202 spa_config_exit(spa
, SCL_ZIO
, FTAG
);
1207 ZFS_MODULE_PARAM(zfs_dedup
, zfs_dedup_
, prefetch
, INT
, ZMOD_RW
,
1208 "Enable prefetching dedup-ed blks");