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