]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/ddt.c
ddt: split internal DDT API into separate header
[mirror_zfs.git] / module / zfs / ddt.c
CommitLineData
428870ff
BB
1/*
2 * CDDL HEADER START
3 *
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.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
428870ff
BB
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
a6255b7f 24 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
67a1b037 25 * Copyright (c) 2022 by Pawel Jakub Dawidek
428870ff
BB
26 */
27
28#include <sys/zfs_context.h>
29#include <sys/spa.h>
30#include <sys/spa_impl.h>
31#include <sys/zio.h>
32#include <sys/ddt.h>
8e414fcd 33#include <sys/ddt_impl.h>
428870ff
BB
34#include <sys/zap.h>
35#include <sys/dmu_tx.h>
36#include <sys/arc.h>
37#include <sys/dsl_pool.h>
38#include <sys/zio_checksum.h>
428870ff 39#include <sys/dsl_scan.h>
a6255b7f 40#include <sys/abd.h>
428870ff 41
ecf3d9b8
JL
42static kmem_cache_t *ddt_cache;
43static kmem_cache_t *ddt_entry_cache;
44
572e2857
BB
45/*
46 * Enable/disable prefetching of dedup-ed blocks which are going to be freed.
47 */
0dfc7324 48int zfs_dedup_prefetch = 0;
572e2857 49
861166b0 50static const ddt_ops_t *const ddt_ops[DDT_TYPES] = {
428870ff
BB
51 &ddt_zap_ops,
52};
53
861166b0 54static const char *const ddt_class_name[DDT_CLASSES] = {
428870ff
BB
55 "ditto",
56 "duplicate",
57 "unique",
58};
59
60static void
61ddt_object_create(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
62 dmu_tx_t *tx)
63{
64 spa_t *spa = ddt->ddt_spa;
65 objset_t *os = ddt->ddt_os;
66 uint64_t *objectp = &ddt->ddt_object[type][class];
3c67d83a
TH
67 boolean_t prehash = zio_checksum_table[ddt->ddt_checksum].ci_flags &
68 ZCHECKSUM_FLAG_DEDUP;
428870ff
BB
69 char name[DDT_NAMELEN];
70
71 ddt_object_name(ddt, type, class, name);
72
d3bafe45
RN
73 ASSERT3U(*objectp, ==, 0);
74 VERIFY0(ddt_ops[type]->ddt_op_create(os, objectp, tx, prehash));
75 ASSERT3U(*objectp, !=, 0);
428870ff 76
d3bafe45
RN
77 VERIFY0(zap_add(os, DMU_POOL_DIRECTORY_OBJECT, name,
78 sizeof (uint64_t), 1, objectp, tx));
428870ff 79
d3bafe45 80 VERIFY0(zap_add(os, spa->spa_ddt_stat_object, name,
428870ff 81 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
d3bafe45 82 &ddt->ddt_histogram[type][class], tx));
428870ff
BB
83}
84
85static void
86ddt_object_destroy(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
87 dmu_tx_t *tx)
88{
89 spa_t *spa = ddt->ddt_spa;
90 objset_t *os = ddt->ddt_os;
91 uint64_t *objectp = &ddt->ddt_object[type][class];
e8fd45a0 92 uint64_t count;
428870ff
BB
93 char name[DDT_NAMELEN];
94
95 ddt_object_name(ddt, type, class, name);
96
d3bafe45 97 ASSERT3U(*objectp, !=, 0);
428870ff 98 ASSERT(ddt_histogram_empty(&ddt->ddt_histogram[type][class]));
d3bafe45
RN
99 VERIFY0(ddt_object_count(ddt, type, class, &count));
100 VERIFY0(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));
861166b0 104 memset(&ddt->ddt_object_stats[type][class], 0, sizeof (ddt_object_t));
428870ff
BB
105
106 *objectp = 0;
107}
108
109static int
110ddt_object_load(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
111{
112 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
113 dmu_object_info_t doi;
e8fd45a0 114 uint64_t count;
428870ff
BB
115 char name[DDT_NAMELEN];
116 int error;
117
118 ddt_object_name(ddt, type, class, name);
119
120 error = zap_lookup(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT, name,
121 sizeof (uint64_t), 1, &ddt->ddt_object[type][class]);
b0bc7a84 122 if (error != 0)
428870ff
BB
123 return (error);
124
ff9b1d07 125 error = zap_lookup(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
428870ff 126 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
ff9b1d07
BB
127 &ddt->ddt_histogram[type][class]);
128 if (error != 0)
129 return (error);
428870ff
BB
130
131 /*
132 * Seed the cached statistics.
133 */
2a4a9dc2
BB
134 error = ddt_object_info(ddt, type, class, &doi);
135 if (error)
136 return (error);
428870ff 137
e8fd45a0
BB
138 error = ddt_object_count(ddt, type, class, &count);
139 if (error)
140 return (error);
141
142 ddo->ddo_count = count;
428870ff
BB
143 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
144 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
145
b0bc7a84 146 return (0);
428870ff
BB
147}
148
149static void
150ddt_object_sync(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
151 dmu_tx_t *tx)
152{
153 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
154 dmu_object_info_t doi;
e8fd45a0 155 uint64_t count;
428870ff
BB
156 char name[DDT_NAMELEN];
157
158 ddt_object_name(ddt, type, class, name);
159
d3bafe45 160 VERIFY0(zap_update(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
428870ff 161 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
d3bafe45 162 &ddt->ddt_histogram[type][class], tx));
428870ff
BB
163
164 /*
165 * Cache DDT statistics; this is the only time they'll change.
166 */
d3bafe45
RN
167 VERIFY0(ddt_object_info(ddt, type, class, &doi));
168 VERIFY0(ddt_object_count(ddt, type, class, &count));
428870ff 169
e8fd45a0 170 ddo->ddo_count = count;
428870ff
BB
171 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
172 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
173}
174
8e414fcd
RN
175static boolean_t
176ddt_object_exists(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
177{
178 return (!!ddt->ddt_object[type][class]);
179}
180
428870ff
BB
181static int
182ddt_object_lookup(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
183 ddt_entry_t *dde)
184{
185 if (!ddt_object_exists(ddt, type, class))
2e528b49 186 return (SET_ERROR(ENOENT));
428870ff
BB
187
188 return (ddt_ops[type]->ddt_op_lookup(ddt->ddt_os,
189 ddt->ddt_object[type][class], dde));
190}
191
192static void
193ddt_object_prefetch(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
194 ddt_entry_t *dde)
195{
196 if (!ddt_object_exists(ddt, type, class))
197 return;
198
199 ddt_ops[type]->ddt_op_prefetch(ddt->ddt_os,
200 ddt->ddt_object[type][class], dde);
201}
202
8e414fcd 203static int
428870ff
BB
204ddt_object_update(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
205 ddt_entry_t *dde, dmu_tx_t *tx)
206{
207 ASSERT(ddt_object_exists(ddt, type, class));
208
209 return (ddt_ops[type]->ddt_op_update(ddt->ddt_os,
210 ddt->ddt_object[type][class], dde, tx));
211}
212
213static int
214ddt_object_remove(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
215 ddt_entry_t *dde, dmu_tx_t *tx)
216{
217 ASSERT(ddt_object_exists(ddt, type, class));
218
219 return (ddt_ops[type]->ddt_op_remove(ddt->ddt_os,
220 ddt->ddt_object[type][class], dde, tx));
221}
222
223int
224ddt_object_walk(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
225 uint64_t *walk, ddt_entry_t *dde)
226{
227 ASSERT(ddt_object_exists(ddt, type, class));
228
229 return (ddt_ops[type]->ddt_op_walk(ddt->ddt_os,
230 ddt->ddt_object[type][class], dde, walk));
231}
232
e8fd45a0
BB
233int
234ddt_object_count(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
235 uint64_t *count)
428870ff
BB
236{
237 ASSERT(ddt_object_exists(ddt, type, class));
238
239 return (ddt_ops[type]->ddt_op_count(ddt->ddt_os,
e8fd45a0 240 ddt->ddt_object[type][class], count));
428870ff
BB
241}
242
243int
244ddt_object_info(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
245 dmu_object_info_t *doi)
246{
247 if (!ddt_object_exists(ddt, type, class))
2e528b49 248 return (SET_ERROR(ENOENT));
428870ff
BB
249
250 return (dmu_object_info(ddt->ddt_os, ddt->ddt_object[type][class],
251 doi));
252}
253
428870ff
BB
254void
255ddt_object_name(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
256 char *name)
257{
c9e319fa 258 (void) snprintf(name, DDT_NAMELEN, DMU_POOL_DDT,
428870ff
BB
259 zio_checksum_table[ddt->ddt_checksum].ci_name,
260 ddt_ops[type]->ddt_op_name, ddt_class_name[class]);
261}
262
263void
264ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, uint64_t txg)
265{
d3bafe45 266 ASSERT3U(txg, !=, 0);
428870ff 267
1c27024e 268 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
428870ff
BB
269 bp->blk_dva[d] = ddp->ddp_dva[d];
270 BP_SET_BIRTH(bp, txg, ddp->ddp_phys_birth);
271}
272
b5256303
TC
273/*
274 * The bp created via this function may be used for repairs and scrub, but it
275 * will be missing the salt / IV required to do a full decrypting read.
276 */
428870ff
BB
277void
278ddt_bp_create(enum zio_checksum checksum,
279 const ddt_key_t *ddk, const ddt_phys_t *ddp, blkptr_t *bp)
280{
281 BP_ZERO(bp);
282
283 if (ddp != NULL)
284 ddt_bp_fill(ddp, bp, ddp->ddp_phys_birth);
285
286 bp->blk_cksum = ddk->ddk_cksum;
428870ff
BB
287
288 BP_SET_LSIZE(bp, DDK_GET_LSIZE(ddk));
289 BP_SET_PSIZE(bp, DDK_GET_PSIZE(ddk));
290 BP_SET_COMPRESS(bp, DDK_GET_COMPRESS(ddk));
b5256303
TC
291 BP_SET_CRYPT(bp, DDK_GET_CRYPT(ddk));
292 BP_SET_FILL(bp, 1);
428870ff
BB
293 BP_SET_CHECKSUM(bp, checksum);
294 BP_SET_TYPE(bp, DMU_OT_DEDUP);
295 BP_SET_LEVEL(bp, 0);
52b68423 296 BP_SET_DEDUP(bp, 1);
428870ff
BB
297 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
298}
299
300void
301ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp)
302{
303 ddk->ddk_cksum = bp->blk_cksum;
304 ddk->ddk_prop = 0;
305
b5256303
TC
306 ASSERT(BP_IS_ENCRYPTED(bp) || !BP_USES_CRYPT(bp));
307
428870ff
BB
308 DDK_SET_LSIZE(ddk, BP_GET_LSIZE(bp));
309 DDK_SET_PSIZE(ddk, BP_GET_PSIZE(bp));
310 DDK_SET_COMPRESS(ddk, BP_GET_COMPRESS(bp));
b5256303 311 DDK_SET_CRYPT(ddk, BP_USES_CRYPT(bp));
428870ff
BB
312}
313
314void
315ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp)
316{
d3bafe45 317 ASSERT0(ddp->ddp_phys_birth);
428870ff 318
1c27024e 319 for (int d = 0; d < SPA_DVAS_PER_BP; d++)
428870ff
BB
320 ddp->ddp_dva[d] = bp->blk_dva[d];
321 ddp->ddp_phys_birth = BP_PHYSICAL_BIRTH(bp);
322}
323
324void
325ddt_phys_clear(ddt_phys_t *ddp)
326{
861166b0 327 memset(ddp, 0, sizeof (*ddp));
428870ff
BB
328}
329
330void
331ddt_phys_addref(ddt_phys_t *ddp)
332{
333 ddp->ddp_refcnt++;
334}
335
336void
337ddt_phys_decref(ddt_phys_t *ddp)
338{
5dc6af0e 339 if (ddp) {
d3bafe45 340 ASSERT3U(ddp->ddp_refcnt, >, 0);
5dc6af0e
BB
341 ddp->ddp_refcnt--;
342 }
428870ff
BB
343}
344
8e414fcd 345static void
428870ff
BB
346ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, uint64_t txg)
347{
348 blkptr_t blk;
349
350 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
52b68423
BB
351
352 /*
353 * We clear the dedup bit so that zio_free() will actually free the
354 * space, rather than just decrementing the refcount in the DDT.
355 */
356 BP_SET_DEDUP(&blk, 0);
357
428870ff
BB
358 ddt_phys_clear(ddp);
359 zio_free(ddt->ddt_spa, txg, &blk);
360}
361
362ddt_phys_t *
363ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp)
364{
365 ddt_phys_t *ddp = (ddt_phys_t *)dde->dde_phys;
366
1c27024e 367 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
428870ff
BB
368 if (DVA_EQUAL(BP_IDENTITY(bp), &ddp->ddp_dva[0]) &&
369 BP_PHYSICAL_BIRTH(bp) == ddp->ddp_phys_birth)
370 return (ddp);
371 }
372 return (NULL);
373}
374
375uint64_t
376ddt_phys_total_refcnt(const ddt_entry_t *dde)
377{
378 uint64_t refcnt = 0;
379
1c27024e 380 for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++)
428870ff
BB
381 refcnt += dde->dde_phys[p].ddp_refcnt;
382
383 return (refcnt);
384}
385
428870ff
BB
386ddt_t *
387ddt_select(spa_t *spa, const blkptr_t *bp)
388{
389 return (spa->spa_ddt[BP_GET_CHECKSUM(bp)]);
390}
391
392void
393ddt_enter(ddt_t *ddt)
394{
395 mutex_enter(&ddt->ddt_lock);
396}
397
398void
399ddt_exit(ddt_t *ddt)
400{
401 mutex_exit(&ddt->ddt_lock);
402}
403
ecf3d9b8
JL
404void
405ddt_init(void)
406{
407 ddt_cache = kmem_cache_create("ddt_cache",
408 sizeof (ddt_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
409 ddt_entry_cache = kmem_cache_create("ddt_entry_cache",
410 sizeof (ddt_entry_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
411}
412
413void
414ddt_fini(void)
415{
416 kmem_cache_destroy(ddt_entry_cache);
417 kmem_cache_destroy(ddt_cache);
418}
419
428870ff
BB
420static ddt_entry_t *
421ddt_alloc(const ddt_key_t *ddk)
422{
423 ddt_entry_t *dde;
424
79c76d5b 425 dde = kmem_cache_alloc(ddt_entry_cache, KM_SLEEP);
861166b0 426 memset(dde, 0, sizeof (ddt_entry_t));
428870ff
BB
427 cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL);
428
429 dde->dde_key = *ddk;
430
431 return (dde);
432}
433
434static void
435ddt_free(ddt_entry_t *dde)
436{
437 ASSERT(!dde->dde_loading);
438
1c27024e 439 for (int p = 0; p < DDT_PHYS_TYPES; p++)
d3bafe45 440 ASSERT3P(dde->dde_lead_zio[p], ==, NULL);
428870ff 441
a6255b7f
DQ
442 if (dde->dde_repair_abd != NULL)
443 abd_free(dde->dde_repair_abd);
428870ff
BB
444
445 cv_destroy(&dde->dde_cv);
ecf3d9b8 446 kmem_cache_free(ddt_entry_cache, dde);
428870ff
BB
447}
448
449void
450ddt_remove(ddt_t *ddt, ddt_entry_t *dde)
451{
452 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
453
454 avl_remove(&ddt->ddt_tree, dde);
455 ddt_free(dde);
456}
457
458ddt_entry_t *
459ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add)
460{
0cb1ef60
RN
461 ddt_key_t search;
462 ddt_entry_t *dde;
428870ff
BB
463 enum ddt_type type;
464 enum ddt_class class;
465 avl_index_t where;
466 int error;
467
468 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
469
0cb1ef60 470 ddt_key_fill(&search, bp);
428870ff 471
0cb1ef60 472 dde = avl_find(&ddt->ddt_tree, &search, &where);
428870ff
BB
473 if (dde == NULL) {
474 if (!add)
475 return (NULL);
0cb1ef60 476 dde = ddt_alloc(&search);
428870ff
BB
477 avl_insert(&ddt->ddt_tree, dde, where);
478 }
479
480 while (dde->dde_loading)
481 cv_wait(&dde->dde_cv, &ddt->ddt_lock);
482
483 if (dde->dde_loaded)
484 return (dde);
485
486 dde->dde_loading = B_TRUE;
487
488 ddt_exit(ddt);
489
490 error = ENOENT;
491
492 for (type = 0; type < DDT_TYPES; type++) {
493 for (class = 0; class < DDT_CLASSES; class++) {
494 error = ddt_object_lookup(ddt, type, class, dde);
a1d477c2
MA
495 if (error != ENOENT) {
496 ASSERT0(error);
428870ff 497 break;
a1d477c2 498 }
428870ff
BB
499 }
500 if (error != ENOENT)
501 break;
502 }
503
428870ff
BB
504 ddt_enter(ddt);
505
d3bafe45
RN
506 ASSERT(!dde->dde_loaded);
507 ASSERT(dde->dde_loading);
428870ff
BB
508
509 dde->dde_type = type; /* will be DDT_TYPES if no entry found */
510 dde->dde_class = class; /* will be DDT_CLASSES if no entry found */
511 dde->dde_loaded = B_TRUE;
512 dde->dde_loading = B_FALSE;
513
514 if (error == 0)
515 ddt_stat_update(ddt, dde, -1ULL);
516
517 cv_broadcast(&dde->dde_cv);
518
519 return (dde);
520}
521
522void
523ddt_prefetch(spa_t *spa, const blkptr_t *bp)
524{
525 ddt_t *ddt;
526 ddt_entry_t dde;
527
572e2857 528 if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp))
428870ff
BB
529 return;
530
531 /*
572e2857
BB
532 * We only remove the DDT once all tables are empty and only
533 * prefetch dedup blocks when there are entries in the DDT.
534 * Thus no locking is required as the DDT can't disappear on us.
428870ff
BB
535 */
536 ddt = ddt_select(spa, bp);
537 ddt_key_fill(&dde.dde_key, bp);
538
1c27024e
DB
539 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
540 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
428870ff
BB
541 ddt_object_prefetch(ddt, type, class, &dde);
542 }
543 }
544}
545
ee36c709 546/*
0cb1ef60
RN
547 * Key comparison. Any struct wanting to make use of this function must have
548 * the key as the first element.
ee36c709
GN
549 */
550#define DDT_KEY_CMP_LEN (sizeof (ddt_key_t) / sizeof (uint16_t))
551
552typedef struct ddt_key_cmp {
553 uint16_t u16[DDT_KEY_CMP_LEN];
554} ddt_key_cmp_t;
555
428870ff 556int
0cb1ef60 557ddt_key_compare(const void *x1, const void *x2)
428870ff 558{
0cb1ef60
RN
559 const ddt_key_cmp_t *k1 = (const ddt_key_cmp_t *)x1;
560 const ddt_key_cmp_t *k2 = (const ddt_key_cmp_t *)x2;
ee36c709 561 int32_t cmp = 0;
428870ff 562
1c27024e 563 for (int i = 0; i < DDT_KEY_CMP_LEN; i++) {
ee36c709
GN
564 cmp = (int32_t)k1->u16[i] - (int32_t)k2->u16[i];
565 if (likely(cmp))
566 break;
428870ff
BB
567 }
568
ca577779 569 return (TREE_ISIGN(cmp));
428870ff
BB
570}
571
572static ddt_t *
573ddt_table_alloc(spa_t *spa, enum zio_checksum c)
574{
575 ddt_t *ddt;
576
79c76d5b 577 ddt = kmem_cache_alloc(ddt_cache, KM_SLEEP);
861166b0 578 memset(ddt, 0, sizeof (ddt_t));
428870ff
BB
579
580 mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL);
0cb1ef60 581 avl_create(&ddt->ddt_tree, ddt_key_compare,
428870ff 582 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
0cb1ef60 583 avl_create(&ddt->ddt_repair_tree, ddt_key_compare,
428870ff
BB
584 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
585 ddt->ddt_checksum = c;
586 ddt->ddt_spa = spa;
587 ddt->ddt_os = spa->spa_meta_objset;
588
589 return (ddt);
590}
591
592static void
593ddt_table_free(ddt_t *ddt)
594{
d3bafe45
RN
595 ASSERT0(avl_numnodes(&ddt->ddt_tree));
596 ASSERT0(avl_numnodes(&ddt->ddt_repair_tree));
428870ff
BB
597 avl_destroy(&ddt->ddt_tree);
598 avl_destroy(&ddt->ddt_repair_tree);
599 mutex_destroy(&ddt->ddt_lock);
ecf3d9b8 600 kmem_cache_free(ddt_cache, ddt);
428870ff
BB
601}
602
603void
604ddt_create(spa_t *spa)
605{
606 spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM;
607
1c27024e 608 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++)
428870ff
BB
609 spa->spa_ddt[c] = ddt_table_alloc(spa, c);
610}
611
612int
613ddt_load(spa_t *spa)
614{
615 int error;
616
617 ddt_create(spa);
618
619 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
620 DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
621 &spa->spa_ddt_stat_object);
622
623 if (error)
624 return (error == ENOENT ? 0 : error);
625
1c27024e 626 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
428870ff 627 ddt_t *ddt = spa->spa_ddt[c];
1c27024e
DB
628 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
629 for (enum ddt_class class = 0; class < DDT_CLASSES;
428870ff
BB
630 class++) {
631 error = ddt_object_load(ddt, type, class);
632 if (error != 0 && error != ENOENT)
633 return (error);
634 }
635 }
636
637 /*
638 * Seed the cached histograms.
639 */
861166b0 640 memcpy(&ddt->ddt_histogram_cache, ddt->ddt_histogram,
428870ff 641 sizeof (ddt->ddt_histogram));
e8a20144 642 spa->spa_dedup_dspace = ~0ULL;
428870ff
BB
643 }
644
645 return (0);
646}
647
648void
649ddt_unload(spa_t *spa)
650{
1c27024e 651 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
428870ff
BB
652 if (spa->spa_ddt[c]) {
653 ddt_table_free(spa->spa_ddt[c]);
654 spa->spa_ddt[c] = NULL;
655 }
656 }
657}
658
659boolean_t
660ddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp)
661{
662 ddt_t *ddt;
e95b3bdc 663 ddt_entry_t *dde;
428870ff
BB
664
665 if (!BP_GET_DEDUP(bp))
666 return (B_FALSE);
667
668 if (max_class == DDT_CLASS_UNIQUE)
669 return (B_TRUE);
670
671 ddt = spa->spa_ddt[BP_GET_CHECKSUM(bp)];
79c76d5b 672 dde = kmem_cache_alloc(ddt_entry_cache, KM_SLEEP);
428870ff 673
e95b3bdc 674 ddt_key_fill(&(dde->dde_key), bp);
428870ff 675
1c27024e
DB
676 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
677 for (enum ddt_class class = 0; class <= max_class; class++) {
e95b3bdc 678 if (ddt_object_lookup(ddt, type, class, dde) == 0) {
ecf3d9b8 679 kmem_cache_free(ddt_entry_cache, dde);
428870ff 680 return (B_TRUE);
e95b3bdc
BB
681 }
682 }
683 }
428870ff 684
ecf3d9b8 685 kmem_cache_free(ddt_entry_cache, dde);
428870ff
BB
686 return (B_FALSE);
687}
688
689ddt_entry_t *
690ddt_repair_start(ddt_t *ddt, const blkptr_t *bp)
691{
692 ddt_key_t ddk;
693 ddt_entry_t *dde;
694
695 ddt_key_fill(&ddk, bp);
696
697 dde = ddt_alloc(&ddk);
698
1c27024e
DB
699 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
700 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
428870ff
BB
701 /*
702 * We can only do repair if there are multiple copies
703 * of the block. For anything in the UNIQUE class,
704 * there's definitely only one copy, so don't even try.
705 */
706 if (class != DDT_CLASS_UNIQUE &&
707 ddt_object_lookup(ddt, type, class, dde) == 0)
708 return (dde);
709 }
710 }
711
861166b0 712 memset(dde->dde_phys, 0, sizeof (dde->dde_phys));
428870ff
BB
713
714 return (dde);
715}
716
717void
718ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde)
719{
720 avl_index_t where;
721
722 ddt_enter(ddt);
723
a6255b7f 724 if (dde->dde_repair_abd != NULL && spa_writeable(ddt->ddt_spa) &&
428870ff
BB
725 avl_find(&ddt->ddt_repair_tree, dde, &where) == NULL)
726 avl_insert(&ddt->ddt_repair_tree, dde, where);
727 else
728 ddt_free(dde);
729
730 ddt_exit(ddt);
731}
732
733static void
734ddt_repair_entry_done(zio_t *zio)
735{
736 ddt_entry_t *rdde = zio->io_private;
737
738 ddt_free(rdde);
739}
740
741static void
742ddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio)
743{
744 ddt_phys_t *ddp = dde->dde_phys;
745 ddt_phys_t *rddp = rdde->dde_phys;
746 ddt_key_t *ddk = &dde->dde_key;
747 ddt_key_t *rddk = &rdde->dde_key;
748 zio_t *zio;
749 blkptr_t blk;
750
751 zio = zio_null(rio, rio->io_spa, NULL,
752 ddt_repair_entry_done, rdde, rio->io_flags);
753
1c27024e 754 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) {
428870ff
BB
755 if (ddp->ddp_phys_birth == 0 ||
756 ddp->ddp_phys_birth != rddp->ddp_phys_birth ||
861166b0 757 memcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva)))
428870ff
BB
758 continue;
759 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
760 zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk,
a6255b7f 761 rdde->dde_repair_abd, DDK_GET_PSIZE(rddk), NULL, NULL,
428870ff
BB
762 ZIO_PRIORITY_SYNC_WRITE, ZIO_DDT_CHILD_FLAGS(zio), NULL));
763 }
764
765 zio_nowait(zio);
766}
767
768static void
769ddt_repair_table(ddt_t *ddt, zio_t *rio)
770{
771 spa_t *spa = ddt->ddt_spa;
772 ddt_entry_t *dde, *rdde_next, *rdde;
773 avl_tree_t *t = &ddt->ddt_repair_tree;
774 blkptr_t blk;
775
776 if (spa_sync_pass(spa) > 1)
777 return;
778
779 ddt_enter(ddt);
780 for (rdde = avl_first(t); rdde != NULL; rdde = rdde_next) {
781 rdde_next = AVL_NEXT(t, rdde);
782 avl_remove(&ddt->ddt_repair_tree, rdde);
783 ddt_exit(ddt);
784 ddt_bp_create(ddt->ddt_checksum, &rdde->dde_key, NULL, &blk);
785 dde = ddt_repair_start(ddt, &blk);
786 ddt_repair_entry(ddt, dde, rdde, rio);
787 ddt_repair_done(ddt, dde);
788 ddt_enter(ddt);
789 }
790 ddt_exit(ddt);
791}
792
793static void
794ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
795{
796 dsl_pool_t *dp = ddt->ddt_spa->spa_dsl_pool;
797 ddt_phys_t *ddp = dde->dde_phys;
798 ddt_key_t *ddk = &dde->dde_key;
799 enum ddt_type otype = dde->dde_type;
800 enum ddt_type ntype = DDT_TYPE_CURRENT;
801 enum ddt_class oclass = dde->dde_class;
802 enum ddt_class nclass;
803 uint64_t total_refcnt = 0;
804
805 ASSERT(dde->dde_loaded);
806 ASSERT(!dde->dde_loading);
807
1c27024e 808 for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
d3bafe45 809 ASSERT3P(dde->dde_lead_zio[p], ==, NULL);
428870ff 810 if (ddp->ddp_phys_birth == 0) {
d3bafe45 811 ASSERT0(ddp->ddp_refcnt);
428870ff
BB
812 continue;
813 }
814 if (p == DDT_PHYS_DITTO) {
050d720c
MA
815 /*
816 * Note, we no longer create DDT-DITTO blocks, but we
817 * don't want to leak any written by older software.
818 */
819 ddt_phys_free(ddt, ddk, ddp, txg);
428870ff
BB
820 continue;
821 }
822 if (ddp->ddp_refcnt == 0)
823 ddt_phys_free(ddt, ddk, ddp, txg);
824 total_refcnt += ddp->ddp_refcnt;
825 }
826
050d720c
MA
827 /* We do not create new DDT-DITTO blocks. */
828 ASSERT0(dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth);
829 if (total_refcnt > 1)
428870ff
BB
830 nclass = DDT_CLASS_DUPLICATE;
831 else
832 nclass = DDT_CLASS_UNIQUE;
833
834 if (otype != DDT_TYPES &&
835 (otype != ntype || oclass != nclass || total_refcnt == 0)) {
d3bafe45
RN
836 VERIFY0(ddt_object_remove(ddt, otype, oclass, dde, tx));
837 ASSERT3U(
838 ddt_object_lookup(ddt, otype, oclass, dde), ==, ENOENT);
428870ff
BB
839 }
840
841 if (total_refcnt != 0) {
842 dde->dde_type = ntype;
843 dde->dde_class = nclass;
844 ddt_stat_update(ddt, dde, 0);
845 if (!ddt_object_exists(ddt, ntype, nclass))
846 ddt_object_create(ddt, ntype, nclass, tx);
d3bafe45 847 VERIFY0(ddt_object_update(ddt, ntype, nclass, dde, tx));
428870ff
BB
848
849 /*
850 * If the class changes, the order that we scan this bp
851 * changes. If it decreases, we could miss it, so
852 * scan it right now. (This covers both class changing
853 * while we are doing ddt_walk(), and when we are
854 * traversing.)
855 */
856 if (nclass < oclass) {
857 dsl_scan_ddt_entry(dp->dp_scan,
858 ddt->ddt_checksum, dde, tx);
859 }
860 }
861}
862
863static void
864ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
865{
866 spa_t *spa = ddt->ddt_spa;
867 ddt_entry_t *dde;
868 void *cookie = NULL;
869
870 if (avl_numnodes(&ddt->ddt_tree) == 0)
871 return;
872
d3bafe45 873 ASSERT3U(spa->spa_uberblock.ub_version, >=, SPA_VERSION_DEDUP);
428870ff
BB
874
875 if (spa->spa_ddt_stat_object == 0) {
9ae529ec
CS
876 spa->spa_ddt_stat_object = zap_create_link(ddt->ddt_os,
877 DMU_OT_DDT_STATS, DMU_POOL_DIRECTORY_OBJECT,
878 DMU_POOL_DDT_STATS, tx);
428870ff
BB
879 }
880
881 while ((dde = avl_destroy_nodes(&ddt->ddt_tree, &cookie)) != NULL) {
882 ddt_sync_entry(ddt, dde, tx, txg);
883 ddt_free(dde);
884 }
885
1c27024e 886 for (enum ddt_type type = 0; type < DDT_TYPES; type++) {
e8fd45a0 887 uint64_t add, count = 0;
1c27024e 888 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
572e2857
BB
889 if (ddt_object_exists(ddt, type, class)) {
890 ddt_object_sync(ddt, type, class, tx);
d3bafe45
RN
891 VERIFY0(ddt_object_count(ddt, type, class,
892 &add));
e8fd45a0 893 count += add;
572e2857
BB
894 }
895 }
1c27024e 896 for (enum ddt_class class = 0; class < DDT_CLASSES; class++) {
572e2857 897 if (count == 0 && ddt_object_exists(ddt, type, class))
428870ff
BB
898 ddt_object_destroy(ddt, type, class, tx);
899 }
900 }
901
861166b0 902 memcpy(&ddt->ddt_histogram_cache, ddt->ddt_histogram,
428870ff 903 sizeof (ddt->ddt_histogram));
e8a20144 904 spa->spa_dedup_dspace = ~0ULL;
428870ff
BB
905}
906
907void
908ddt_sync(spa_t *spa, uint64_t txg)
909{
d4a72f23 910 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
428870ff 911 dmu_tx_t *tx;
d4a72f23 912 zio_t *rio;
428870ff 913
d3bafe45 914 ASSERT3U(spa_syncing_txg(spa), ==, txg);
428870ff
BB
915
916 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
917
d4a72f23 918 rio = zio_root(spa, NULL, NULL,
a1d477c2 919 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SELF_HEAL);
d4a72f23
TC
920
921 /*
922 * This function may cause an immediate scan of ddt blocks (see
923 * the comment above dsl_scan_ddt() for details). We set the
924 * scan's root zio here so that we can wait for any scan IOs in
925 * addition to the regular ddt IOs.
926 */
927 ASSERT3P(scn->scn_zio_root, ==, NULL);
928 scn->scn_zio_root = rio;
929
1c27024e 930 for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
428870ff
BB
931 ddt_t *ddt = spa->spa_ddt[c];
932 if (ddt == NULL)
933 continue;
934 ddt_sync_table(ddt, tx, txg);
935 ddt_repair_table(ddt, rio);
936 }
937
938 (void) zio_wait(rio);
d4a72f23 939 scn->scn_zio_root = NULL;
428870ff
BB
940
941 dmu_tx_commit(tx);
942}
943
944int
945ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde)
946{
947 do {
948 do {
949 do {
950 ddt_t *ddt = spa->spa_ddt[ddb->ddb_checksum];
951 int error = ENOENT;
952 if (ddt_object_exists(ddt, ddb->ddb_type,
953 ddb->ddb_class)) {
954 error = ddt_object_walk(ddt,
955 ddb->ddb_type, ddb->ddb_class,
956 &ddb->ddb_cursor, dde);
957 }
958 dde->dde_type = ddb->ddb_type;
959 dde->dde_class = ddb->ddb_class;
960 if (error == 0)
961 return (0);
962 if (error != ENOENT)
963 return (error);
964 ddb->ddb_cursor = 0;
965 } while (++ddb->ddb_checksum < ZIO_CHECKSUM_FUNCTIONS);
966 ddb->ddb_checksum = 0;
967 } while (++ddb->ddb_type < DDT_TYPES);
968 ddb->ddb_type = 0;
969 } while (++ddb->ddb_class < DDT_CLASSES);
970
2e528b49 971 return (SET_ERROR(ENOENT));
428870ff 972}
c409e464 973
67a1b037
PJD
974/*
975 * This function is used by Block Cloning (brt.c) to increase reference
976 * counter for the DDT entry if the block is already in DDT.
977 *
978 * Return false if the block, despite having the D bit set, is not present
979 * in the DDT. Currently this is not possible but might be in the future.
980 * See the comment below.
981 */
982boolean_t
983ddt_addref(spa_t *spa, const blkptr_t *bp)
984{
985 ddt_t *ddt;
986 ddt_entry_t *dde;
987 boolean_t result;
988
989 spa_config_enter(spa, SCL_ZIO, FTAG, RW_READER);
990 ddt = ddt_select(spa, bp);
991 ddt_enter(ddt);
992
993 dde = ddt_lookup(ddt, bp, B_TRUE);
d3bafe45 994 ASSERT3P(dde, !=, NULL);
67a1b037
PJD
995
996 if (dde->dde_type < DDT_TYPES) {
997 ddt_phys_t *ddp;
998
999 ASSERT3S(dde->dde_class, <, DDT_CLASSES);
1000
1001 ddp = &dde->dde_phys[BP_GET_NDVAS(bp)];
61ab05ca
RN
1002
1003 /*
1004 * This entry already existed (dde_type is real), so it must
1005 * have refcnt >0 at the start of this txg. We are called from
1006 * brt_pending_apply(), before frees are issued, so the refcnt
1007 * can't be lowered yet. Therefore, it must be >0. We assert
1008 * this because if the order of BRT and DDT interactions were
1009 * ever to change and the refcnt was ever zero here, then
1010 * likely further action is required to fill out the DDT entry,
1011 * and this is a place that is likely to be missed in testing.
1012 */
1013 ASSERT3U(ddp->ddp_refcnt, >, 0);
1014
67a1b037
PJD
1015 ddt_phys_addref(ddp);
1016 result = B_TRUE;
1017 } else {
1018 /*
1019 * At the time of implementating this if the block has the
1020 * DEDUP flag set it must exist in the DEDUP table, but
1021 * there are many advocates that want ability to remove
1022 * entries from DDT with refcnt=1. If this will happen,
1023 * we may have a block with the DEDUP set, but which doesn't
1024 * have a corresponding entry in the DDT. Be ready.
1025 */
1026 ASSERT3S(dde->dde_class, ==, DDT_CLASSES);
1027 ddt_remove(ddt, dde);
1028 result = B_FALSE;
1029 }
1030
1031 ddt_exit(ddt);
1032 spa_config_exit(spa, SCL_ZIO, FTAG);
1033
1034 return (result);
1035}
1036
a7929f31 1037ZFS_MODULE_PARAM(zfs_dedup, zfs_dedup_, prefetch, INT, ZMOD_RW,
03fdcb9a 1038 "Enable prefetching dedup-ed blks");