]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/ddt.c
Imported Upstream version 0.6.4.2
[mirror_zfs-debian.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
9 * or http://www.opensolaris.org/os/licensing.
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.
ea04106b 24 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
428870ff
BB
25 */
26
27#include <sys/zfs_context.h>
28#include <sys/spa.h>
29#include <sys/spa_impl.h>
30#include <sys/zio.h>
31#include <sys/ddt.h>
32#include <sys/zap.h>
33#include <sys/dmu_tx.h>
34#include <sys/arc.h>
35#include <sys/dsl_pool.h>
36#include <sys/zio_checksum.h>
37#include <sys/zio_compress.h>
38#include <sys/dsl_scan.h>
39
a08ee875
LG
40static kmem_cache_t *ddt_cache;
41static kmem_cache_t *ddt_entry_cache;
42
572e2857
BB
43/*
44 * Enable/disable prefetching of dedup-ed blocks which are going to be freed.
45 */
ea04106b 46int zfs_dedup_prefetch = 0;
572e2857 47
428870ff
BB
48static const ddt_ops_t *ddt_ops[DDT_TYPES] = {
49 &ddt_zap_ops,
50};
51
52static const char *ddt_class_name[DDT_CLASSES] = {
53 "ditto",
54 "duplicate",
55 "unique",
56};
57
58static void
59ddt_object_create(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
60 dmu_tx_t *tx)
61{
62 spa_t *spa = ddt->ddt_spa;
63 objset_t *os = ddt->ddt_os;
64 uint64_t *objectp = &ddt->ddt_object[type][class];
65 boolean_t prehash = zio_checksum_table[ddt->ddt_checksum].ci_dedup;
66 char name[DDT_NAMELEN];
67
68 ddt_object_name(ddt, type, class, name);
69
70 ASSERT(*objectp == 0);
71 VERIFY(ddt_ops[type]->ddt_op_create(os, objectp, tx, prehash) == 0);
72 ASSERT(*objectp != 0);
73
74 VERIFY(zap_add(os, DMU_POOL_DIRECTORY_OBJECT, name,
75 sizeof (uint64_t), 1, objectp, tx) == 0);
76
77 VERIFY(zap_add(os, spa->spa_ddt_stat_object, name,
78 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
79 &ddt->ddt_histogram[type][class], tx) == 0);
80}
81
82static void
83ddt_object_destroy(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
84 dmu_tx_t *tx)
85{
86 spa_t *spa = ddt->ddt_spa;
87 objset_t *os = ddt->ddt_os;
88 uint64_t *objectp = &ddt->ddt_object[type][class];
e8fd45a0 89 uint64_t count;
428870ff
BB
90 char name[DDT_NAMELEN];
91
92 ddt_object_name(ddt, type, class, name);
93
94 ASSERT(*objectp != 0);
428870ff 95 ASSERT(ddt_histogram_empty(&ddt->ddt_histogram[type][class]));
e8fd45a0 96 VERIFY(ddt_object_count(ddt, type, class, &count) == 0 && count == 0);
428870ff
BB
97 VERIFY(zap_remove(os, DMU_POOL_DIRECTORY_OBJECT, name, tx) == 0);
98 VERIFY(zap_remove(os, spa->spa_ddt_stat_object, name, tx) == 0);
99 VERIFY(ddt_ops[type]->ddt_op_destroy(os, *objectp, tx) == 0);
100 bzero(&ddt->ddt_object_stats[type][class], sizeof (ddt_object_t));
101
102 *objectp = 0;
103}
104
105static int
106ddt_object_load(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
107{
108 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
109 dmu_object_info_t doi;
e8fd45a0 110 uint64_t count;
428870ff
BB
111 char name[DDT_NAMELEN];
112 int error;
113
114 ddt_object_name(ddt, type, class, name);
115
116 error = zap_lookup(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT, name,
117 sizeof (uint64_t), 1, &ddt->ddt_object[type][class]);
118
ea04106b 119 if (error != 0)
428870ff
BB
120 return (error);
121
ea04106b 122 VERIFY0(zap_lookup(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
428870ff 123 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
ea04106b 124 &ddt->ddt_histogram[type][class]));
428870ff
BB
125
126 /*
127 * Seed the cached statistics.
128 */
2a4a9dc2
BB
129 error = ddt_object_info(ddt, type, class, &doi);
130 if (error)
131 return (error);
428870ff 132
e8fd45a0
BB
133 error = ddt_object_count(ddt, type, class, &count);
134 if (error)
135 return (error);
136
137 ddo->ddo_count = count;
428870ff
BB
138 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
139 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
140
ea04106b 141 return (0);
428870ff
BB
142}
143
144static void
145ddt_object_sync(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
146 dmu_tx_t *tx)
147{
148 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
149 dmu_object_info_t doi;
e8fd45a0 150 uint64_t count;
428870ff
BB
151 char name[DDT_NAMELEN];
152
153 ddt_object_name(ddt, type, class, name);
154
155 VERIFY(zap_update(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
156 sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
157 &ddt->ddt_histogram[type][class], tx) == 0);
158
159 /*
160 * Cache DDT statistics; this is the only time they'll change.
161 */
162 VERIFY(ddt_object_info(ddt, type, class, &doi) == 0);
e8fd45a0 163 VERIFY(ddt_object_count(ddt, type, class, &count) == 0);
428870ff 164
e8fd45a0 165 ddo->ddo_count = count;
428870ff
BB
166 ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
167 ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
168}
169
170static int
171ddt_object_lookup(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
172 ddt_entry_t *dde)
173{
174 if (!ddt_object_exists(ddt, type, class))
a08ee875 175 return (SET_ERROR(ENOENT));
428870ff
BB
176
177 return (ddt_ops[type]->ddt_op_lookup(ddt->ddt_os,
178 ddt->ddt_object[type][class], dde));
179}
180
181static void
182ddt_object_prefetch(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))
186 return;
187
188 ddt_ops[type]->ddt_op_prefetch(ddt->ddt_os,
189 ddt->ddt_object[type][class], dde);
190}
191
192int
193ddt_object_update(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
194 ddt_entry_t *dde, dmu_tx_t *tx)
195{
196 ASSERT(ddt_object_exists(ddt, type, class));
197
198 return (ddt_ops[type]->ddt_op_update(ddt->ddt_os,
199 ddt->ddt_object[type][class], dde, tx));
200}
201
202static int
203ddt_object_remove(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
204 ddt_entry_t *dde, dmu_tx_t *tx)
205{
206 ASSERT(ddt_object_exists(ddt, type, class));
207
208 return (ddt_ops[type]->ddt_op_remove(ddt->ddt_os,
209 ddt->ddt_object[type][class], dde, tx));
210}
211
212int
213ddt_object_walk(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
214 uint64_t *walk, ddt_entry_t *dde)
215{
216 ASSERT(ddt_object_exists(ddt, type, class));
217
218 return (ddt_ops[type]->ddt_op_walk(ddt->ddt_os,
219 ddt->ddt_object[type][class], dde, walk));
220}
221
e8fd45a0
BB
222int
223ddt_object_count(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
224 uint64_t *count)
428870ff
BB
225{
226 ASSERT(ddt_object_exists(ddt, type, class));
227
228 return (ddt_ops[type]->ddt_op_count(ddt->ddt_os,
e8fd45a0 229 ddt->ddt_object[type][class], count));
428870ff
BB
230}
231
232int
233ddt_object_info(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
234 dmu_object_info_t *doi)
235{
236 if (!ddt_object_exists(ddt, type, class))
a08ee875 237 return (SET_ERROR(ENOENT));
428870ff
BB
238
239 return (dmu_object_info(ddt->ddt_os, ddt->ddt_object[type][class],
240 doi));
241}
242
243boolean_t
244ddt_object_exists(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
245{
246 return (!!ddt->ddt_object[type][class]);
247}
248
249void
250ddt_object_name(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
251 char *name)
252{
253 (void) sprintf(name, DMU_POOL_DDT,
254 zio_checksum_table[ddt->ddt_checksum].ci_name,
255 ddt_ops[type]->ddt_op_name, ddt_class_name[class]);
256}
257
258void
259ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, uint64_t txg)
260{
d6320ddb 261 int d;
428870ff
BB
262 ASSERT(txg != 0);
263
d6320ddb 264 for (d = 0; d < SPA_DVAS_PER_BP; d++)
428870ff
BB
265 bp->blk_dva[d] = ddp->ddp_dva[d];
266 BP_SET_BIRTH(bp, txg, ddp->ddp_phys_birth);
267}
268
269void
270ddt_bp_create(enum zio_checksum checksum,
271 const ddt_key_t *ddk, const ddt_phys_t *ddp, blkptr_t *bp)
272{
273 BP_ZERO(bp);
274
275 if (ddp != NULL)
276 ddt_bp_fill(ddp, bp, ddp->ddp_phys_birth);
277
278 bp->blk_cksum = ddk->ddk_cksum;
279 bp->blk_fill = 1;
280
281 BP_SET_LSIZE(bp, DDK_GET_LSIZE(ddk));
282 BP_SET_PSIZE(bp, DDK_GET_PSIZE(ddk));
283 BP_SET_COMPRESS(bp, DDK_GET_COMPRESS(ddk));
284 BP_SET_CHECKSUM(bp, checksum);
285 BP_SET_TYPE(bp, DMU_OT_DEDUP);
286 BP_SET_LEVEL(bp, 0);
287 BP_SET_DEDUP(bp, 0);
288 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
289}
290
291void
292ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp)
293{
294 ddk->ddk_cksum = bp->blk_cksum;
295 ddk->ddk_prop = 0;
296
297 DDK_SET_LSIZE(ddk, BP_GET_LSIZE(bp));
298 DDK_SET_PSIZE(ddk, BP_GET_PSIZE(bp));
299 DDK_SET_COMPRESS(ddk, BP_GET_COMPRESS(bp));
300}
301
302void
303ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp)
304{
d6320ddb 305 int d;
428870ff
BB
306 ASSERT(ddp->ddp_phys_birth == 0);
307
d6320ddb 308 for (d = 0; d < SPA_DVAS_PER_BP; d++)
428870ff
BB
309 ddp->ddp_dva[d] = bp->blk_dva[d];
310 ddp->ddp_phys_birth = BP_PHYSICAL_BIRTH(bp);
311}
312
313void
314ddt_phys_clear(ddt_phys_t *ddp)
315{
316 bzero(ddp, sizeof (*ddp));
317}
318
319void
320ddt_phys_addref(ddt_phys_t *ddp)
321{
322 ddp->ddp_refcnt++;
323}
324
325void
326ddt_phys_decref(ddt_phys_t *ddp)
327{
5dc6af0e 328 if (ddp) {
c06d4368 329 ASSERT(ddp->ddp_refcnt > 0);
5dc6af0e
BB
330 ddp->ddp_refcnt--;
331 }
428870ff
BB
332}
333
334void
335ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, uint64_t txg)
336{
337 blkptr_t blk;
338
339 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
340 ddt_phys_clear(ddp);
341 zio_free(ddt->ddt_spa, txg, &blk);
342}
343
344ddt_phys_t *
345ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp)
346{
347 ddt_phys_t *ddp = (ddt_phys_t *)dde->dde_phys;
d6320ddb 348 int p;
428870ff 349
d6320ddb 350 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
428870ff
BB
351 if (DVA_EQUAL(BP_IDENTITY(bp), &ddp->ddp_dva[0]) &&
352 BP_PHYSICAL_BIRTH(bp) == ddp->ddp_phys_birth)
353 return (ddp);
354 }
355 return (NULL);
356}
357
358uint64_t
359ddt_phys_total_refcnt(const ddt_entry_t *dde)
360{
361 uint64_t refcnt = 0;
d6320ddb 362 int p;
428870ff 363
d6320ddb 364 for (p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++)
428870ff
BB
365 refcnt += dde->dde_phys[p].ddp_refcnt;
366
367 return (refcnt);
368}
369
370static void
371ddt_stat_generate(ddt_t *ddt, ddt_entry_t *dde, ddt_stat_t *dds)
372{
373 spa_t *spa = ddt->ddt_spa;
374 ddt_phys_t *ddp = dde->dde_phys;
375 ddt_key_t *ddk = &dde->dde_key;
376 uint64_t lsize = DDK_GET_LSIZE(ddk);
377 uint64_t psize = DDK_GET_PSIZE(ddk);
d6320ddb 378 int p, d;
428870ff
BB
379
380 bzero(dds, sizeof (*dds));
381
d6320ddb 382 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
428870ff
BB
383 uint64_t dsize = 0;
384 uint64_t refcnt = ddp->ddp_refcnt;
385
386 if (ddp->ddp_phys_birth == 0)
387 continue;
388
d6320ddb 389 for (d = 0; d < SPA_DVAS_PER_BP; d++)
428870ff
BB
390 dsize += dva_get_dsize_sync(spa, &ddp->ddp_dva[d]);
391
392 dds->dds_blocks += 1;
393 dds->dds_lsize += lsize;
394 dds->dds_psize += psize;
395 dds->dds_dsize += dsize;
396
397 dds->dds_ref_blocks += refcnt;
398 dds->dds_ref_lsize += lsize * refcnt;
399 dds->dds_ref_psize += psize * refcnt;
400 dds->dds_ref_dsize += dsize * refcnt;
401 }
402}
403
404void
405ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg)
406{
407 const uint64_t *s = (const uint64_t *)src;
408 uint64_t *d = (uint64_t *)dst;
409 uint64_t *d_end = (uint64_t *)(dst + 1);
410
411 ASSERT(neg == 0 || neg == -1ULL); /* add or subtract */
412
413 while (d < d_end)
414 *d++ += (*s++ ^ neg) - neg;
415}
416
417static void
418ddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg)
419{
420 ddt_stat_t dds;
421 ddt_histogram_t *ddh;
422 int bucket;
423
424 ddt_stat_generate(ddt, dde, &dds);
425
ea04106b 426 bucket = highbit64(dds.dds_ref_blocks) - 1;
428870ff
BB
427 ASSERT(bucket >= 0);
428
429 ddh = &ddt->ddt_histogram[dde->dde_type][dde->dde_class];
430
431 ddt_stat_add(&ddh->ddh_stat[bucket], &dds, neg);
432}
433
434void
435ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src)
436{
d6320ddb
BB
437 int h;
438
439 for (h = 0; h < 64; h++)
428870ff
BB
440 ddt_stat_add(&dst->ddh_stat[h], &src->ddh_stat[h], 0);
441}
442
443void
444ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh)
445{
d6320ddb
BB
446 int h;
447
428870ff
BB
448 bzero(dds, sizeof (*dds));
449
d6320ddb 450 for (h = 0; h < 64; h++)
428870ff
BB
451 ddt_stat_add(dds, &ddh->ddh_stat[h], 0);
452}
453
454boolean_t
455ddt_histogram_empty(const ddt_histogram_t *ddh)
456{
457 const uint64_t *s = (const uint64_t *)ddh;
458 const uint64_t *s_end = (const uint64_t *)(ddh + 1);
459
460 while (s < s_end)
461 if (*s++ != 0)
462 return (B_FALSE);
463
464 return (B_TRUE);
465}
466
467void
468ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total)
469{
d6320ddb
BB
470 enum zio_checksum c;
471 enum ddt_type type;
472 enum ddt_class class;
473
428870ff 474 /* Sum the statistics we cached in ddt_object_sync(). */
d6320ddb 475 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
428870ff 476 ddt_t *ddt = spa->spa_ddt[c];
d6320ddb
BB
477 for (type = 0; type < DDT_TYPES; type++) {
478 for (class = 0; class < DDT_CLASSES;
428870ff
BB
479 class++) {
480 ddt_object_t *ddo =
481 &ddt->ddt_object_stats[type][class];
482 ddo_total->ddo_count += ddo->ddo_count;
483 ddo_total->ddo_dspace += ddo->ddo_dspace;
484 ddo_total->ddo_mspace += ddo->ddo_mspace;
485 }
486 }
487 }
488
489 /* ... and compute the averages. */
490 if (ddo_total->ddo_count != 0) {
491 ddo_total->ddo_dspace /= ddo_total->ddo_count;
492 ddo_total->ddo_mspace /= ddo_total->ddo_count;
428870ff
BB
493 }
494}
495
496void
497ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh)
498{
d6320ddb
BB
499 enum zio_checksum c;
500 enum ddt_type type;
501 enum ddt_class class;
502
503 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
428870ff 504 ddt_t *ddt = spa->spa_ddt[c];
d6320ddb
BB
505 for (type = 0; type < DDT_TYPES; type++) {
506 for (class = 0; class < DDT_CLASSES;
428870ff
BB
507 class++) {
508 ddt_histogram_add(ddh,
509 &ddt->ddt_histogram_cache[type][class]);
510 }
511 }
512 }
513}
514
515void
516ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total)
517{
518 ddt_histogram_t *ddh_total;
519
ea04106b 520 ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
428870ff
BB
521 ddt_get_dedup_histogram(spa, ddh_total);
522 ddt_histogram_stat(dds_total, ddh_total);
523 kmem_free(ddh_total, sizeof (ddt_histogram_t));
524}
525
526uint64_t
527ddt_get_dedup_dspace(spa_t *spa)
528{
529 ddt_stat_t dds_total = { 0 };
530
531 ddt_get_dedup_stats(spa, &dds_total);
532 return (dds_total.dds_ref_dsize - dds_total.dds_dsize);
533}
534
535uint64_t
536ddt_get_pool_dedup_ratio(spa_t *spa)
537{
538 ddt_stat_t dds_total = { 0 };
539
540 ddt_get_dedup_stats(spa, &dds_total);
541 if (dds_total.dds_dsize == 0)
542 return (100);
543
544 return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize);
545}
546
547int
548ddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde, ddt_phys_t *ddp_willref)
549{
550 spa_t *spa = ddt->ddt_spa;
551 uint64_t total_refcnt = 0;
552 uint64_t ditto = spa->spa_dedup_ditto;
553 int total_copies = 0;
554 int desired_copies = 0;
d6320ddb 555 int p;
428870ff 556
d6320ddb 557 for (p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
428870ff
BB
558 ddt_phys_t *ddp = &dde->dde_phys[p];
559 zio_t *zio = dde->dde_lead_zio[p];
560 uint64_t refcnt = ddp->ddp_refcnt; /* committed refs */
561 if (zio != NULL)
562 refcnt += zio->io_parent_count; /* pending refs */
563 if (ddp == ddp_willref)
564 refcnt++; /* caller's ref */
565 if (refcnt != 0) {
566 total_refcnt += refcnt;
567 total_copies += p;
568 }
569 }
570
571 if (ditto == 0 || ditto > UINT32_MAX)
572 ditto = UINT32_MAX;
573
574 if (total_refcnt >= 1)
575 desired_copies++;
576 if (total_refcnt >= ditto)
577 desired_copies++;
578 if (total_refcnt >= ditto * ditto)
579 desired_copies++;
580
581 return (MAX(desired_copies, total_copies) - total_copies);
582}
583
584int
585ddt_ditto_copies_present(ddt_entry_t *dde)
586{
587 ddt_phys_t *ddp = &dde->dde_phys[DDT_PHYS_DITTO];
588 dva_t *dva = ddp->ddp_dva;
589 int copies = 0 - DVA_GET_GANG(dva);
d6320ddb 590 int d;
428870ff 591
d6320ddb 592 for (d = 0; d < SPA_DVAS_PER_BP; d++, dva++)
428870ff
BB
593 if (DVA_IS_VALID(dva))
594 copies++;
595
596 ASSERT(copies >= 0 && copies < SPA_DVAS_PER_BP);
597
598 return (copies);
599}
600
601size_t
602ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len)
603{
604 uchar_t *version = dst++;
605 int cpfunc = ZIO_COMPRESS_ZLE;
606 zio_compress_info_t *ci = &zio_compress_table[cpfunc];
607 size_t c_len;
608
609 ASSERT(d_len >= s_len + 1); /* no compression plus version byte */
610
611 c_len = ci->ci_compress(src, dst, s_len, d_len - 1, ci->ci_level);
612
613 if (c_len == s_len) {
614 cpfunc = ZIO_COMPRESS_OFF;
615 bcopy(src, dst, s_len);
616 }
617
ea04106b
AX
618 *version = cpfunc;
619 /* CONSTCOND */
620 if (ZFS_HOST_BYTEORDER)
621 *version |= DDT_COMPRESS_BYTEORDER_MASK;
428870ff
BB
622
623 return (c_len + 1);
624}
625
626void
627ddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len)
628{
629 uchar_t version = *src++;
630 int cpfunc = version & DDT_COMPRESS_FUNCTION_MASK;
631 zio_compress_info_t *ci = &zio_compress_table[cpfunc];
632
633 if (ci->ci_decompress != NULL)
634 (void) ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level);
635 else
636 bcopy(src, dst, d_len);
637
ea04106b
AX
638 if (((version & DDT_COMPRESS_BYTEORDER_MASK) != 0) !=
639 (ZFS_HOST_BYTEORDER != 0))
428870ff
BB
640 byteswap_uint64_array(dst, d_len);
641}
642
643ddt_t *
644ddt_select_by_checksum(spa_t *spa, enum zio_checksum c)
645{
646 return (spa->spa_ddt[c]);
647}
648
649ddt_t *
650ddt_select(spa_t *spa, const blkptr_t *bp)
651{
652 return (spa->spa_ddt[BP_GET_CHECKSUM(bp)]);
653}
654
655void
656ddt_enter(ddt_t *ddt)
657{
658 mutex_enter(&ddt->ddt_lock);
659}
660
661void
662ddt_exit(ddt_t *ddt)
663{
664 mutex_exit(&ddt->ddt_lock);
665}
666
a08ee875
LG
667void
668ddt_init(void)
669{
670 ddt_cache = kmem_cache_create("ddt_cache",
671 sizeof (ddt_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
672 ddt_entry_cache = kmem_cache_create("ddt_entry_cache",
673 sizeof (ddt_entry_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
674}
675
676void
677ddt_fini(void)
678{
679 kmem_cache_destroy(ddt_entry_cache);
680 kmem_cache_destroy(ddt_cache);
681}
682
428870ff
BB
683static ddt_entry_t *
684ddt_alloc(const ddt_key_t *ddk)
685{
686 ddt_entry_t *dde;
687
ea04106b 688 dde = kmem_cache_alloc(ddt_entry_cache, KM_SLEEP);
a08ee875 689 bzero(dde, sizeof (ddt_entry_t));
428870ff
BB
690 cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL);
691
692 dde->dde_key = *ddk;
693
694 return (dde);
695}
696
697static void
698ddt_free(ddt_entry_t *dde)
699{
d6320ddb
BB
700 int p;
701
428870ff
BB
702 ASSERT(!dde->dde_loading);
703
d6320ddb 704 for (p = 0; p < DDT_PHYS_TYPES; p++)
428870ff
BB
705 ASSERT(dde->dde_lead_zio[p] == NULL);
706
707 if (dde->dde_repair_data != NULL)
708 zio_buf_free(dde->dde_repair_data,
709 DDK_GET_PSIZE(&dde->dde_key));
710
711 cv_destroy(&dde->dde_cv);
a08ee875 712 kmem_cache_free(ddt_entry_cache, dde);
428870ff
BB
713}
714
715void
716ddt_remove(ddt_t *ddt, ddt_entry_t *dde)
717{
718 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
719
720 avl_remove(&ddt->ddt_tree, dde);
721 ddt_free(dde);
722}
723
724ddt_entry_t *
725ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add)
726{
727 ddt_entry_t *dde, dde_search;
728 enum ddt_type type;
729 enum ddt_class class;
730 avl_index_t where;
731 int error;
732
733 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
734
735 ddt_key_fill(&dde_search.dde_key, bp);
736
737 dde = avl_find(&ddt->ddt_tree, &dde_search, &where);
738 if (dde == NULL) {
739 if (!add)
740 return (NULL);
741 dde = ddt_alloc(&dde_search.dde_key);
742 avl_insert(&ddt->ddt_tree, dde, where);
743 }
744
745 while (dde->dde_loading)
746 cv_wait(&dde->dde_cv, &ddt->ddt_lock);
747
748 if (dde->dde_loaded)
749 return (dde);
750
751 dde->dde_loading = B_TRUE;
752
753 ddt_exit(ddt);
754
755 error = ENOENT;
756
757 for (type = 0; type < DDT_TYPES; type++) {
758 for (class = 0; class < DDT_CLASSES; class++) {
759 error = ddt_object_lookup(ddt, type, class, dde);
760 if (error != ENOENT)
761 break;
762 }
763 if (error != ENOENT)
764 break;
765 }
766
767 ASSERT(error == 0 || error == ENOENT);
768
769 ddt_enter(ddt);
770
771 ASSERT(dde->dde_loaded == B_FALSE);
772 ASSERT(dde->dde_loading == B_TRUE);
773
774 dde->dde_type = type; /* will be DDT_TYPES if no entry found */
775 dde->dde_class = class; /* will be DDT_CLASSES if no entry found */
776 dde->dde_loaded = B_TRUE;
777 dde->dde_loading = B_FALSE;
778
779 if (error == 0)
780 ddt_stat_update(ddt, dde, -1ULL);
781
782 cv_broadcast(&dde->dde_cv);
783
784 return (dde);
785}
786
787void
788ddt_prefetch(spa_t *spa, const blkptr_t *bp)
789{
790 ddt_t *ddt;
791 ddt_entry_t dde;
d6320ddb
BB
792 enum ddt_type type;
793 enum ddt_class class;
428870ff 794
572e2857 795 if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp))
428870ff
BB
796 return;
797
798 /*
572e2857
BB
799 * We only remove the DDT once all tables are empty and only
800 * prefetch dedup blocks when there are entries in the DDT.
801 * Thus no locking is required as the DDT can't disappear on us.
428870ff
BB
802 */
803 ddt = ddt_select(spa, bp);
804 ddt_key_fill(&dde.dde_key, bp);
805
d6320ddb
BB
806 for (type = 0; type < DDT_TYPES; type++) {
807 for (class = 0; class < DDT_CLASSES; class++) {
428870ff
BB
808 ddt_object_prefetch(ddt, type, class, &dde);
809 }
810 }
811}
812
813int
814ddt_entry_compare(const void *x1, const void *x2)
815{
816 const ddt_entry_t *dde1 = x1;
817 const ddt_entry_t *dde2 = x2;
818 const uint64_t *u1 = (const uint64_t *)&dde1->dde_key;
819 const uint64_t *u2 = (const uint64_t *)&dde2->dde_key;
d6320ddb 820 int i;
428870ff 821
d6320ddb 822 for (i = 0; i < DDT_KEY_WORDS; i++) {
428870ff
BB
823 if (u1[i] < u2[i])
824 return (-1);
825 if (u1[i] > u2[i])
826 return (1);
827 }
828
829 return (0);
830}
831
832static ddt_t *
833ddt_table_alloc(spa_t *spa, enum zio_checksum c)
834{
835 ddt_t *ddt;
836
ea04106b 837 ddt = kmem_cache_alloc(ddt_cache, KM_SLEEP);
a08ee875 838 bzero(ddt, sizeof (ddt_t));
428870ff
BB
839
840 mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL);
841 avl_create(&ddt->ddt_tree, ddt_entry_compare,
842 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
843 avl_create(&ddt->ddt_repair_tree, ddt_entry_compare,
844 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
845 ddt->ddt_checksum = c;
846 ddt->ddt_spa = spa;
847 ddt->ddt_os = spa->spa_meta_objset;
848
849 return (ddt);
850}
851
852static void
853ddt_table_free(ddt_t *ddt)
854{
855 ASSERT(avl_numnodes(&ddt->ddt_tree) == 0);
856 ASSERT(avl_numnodes(&ddt->ddt_repair_tree) == 0);
857 avl_destroy(&ddt->ddt_tree);
858 avl_destroy(&ddt->ddt_repair_tree);
859 mutex_destroy(&ddt->ddt_lock);
a08ee875 860 kmem_cache_free(ddt_cache, ddt);
428870ff
BB
861}
862
863void
864ddt_create(spa_t *spa)
865{
d6320ddb
BB
866 enum zio_checksum c;
867
428870ff
BB
868 spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM;
869
d6320ddb 870 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++)
428870ff
BB
871 spa->spa_ddt[c] = ddt_table_alloc(spa, c);
872}
873
874int
875ddt_load(spa_t *spa)
876{
d6320ddb
BB
877 enum zio_checksum c;
878 enum ddt_type type;
879 enum ddt_class class;
428870ff
BB
880 int error;
881
882 ddt_create(spa);
883
884 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
885 DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
886 &spa->spa_ddt_stat_object);
887
888 if (error)
889 return (error == ENOENT ? 0 : error);
890
d6320ddb 891 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
428870ff 892 ddt_t *ddt = spa->spa_ddt[c];
d6320ddb
BB
893 for (type = 0; type < DDT_TYPES; type++) {
894 for (class = 0; class < DDT_CLASSES;
428870ff
BB
895 class++) {
896 error = ddt_object_load(ddt, type, class);
897 if (error != 0 && error != ENOENT)
898 return (error);
899 }
900 }
901
902 /*
903 * Seed the cached histograms.
904 */
905 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
906 sizeof (ddt->ddt_histogram));
907 }
908
909 return (0);
910}
911
912void
913ddt_unload(spa_t *spa)
914{
d6320ddb
BB
915 enum zio_checksum c;
916
917 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
428870ff
BB
918 if (spa->spa_ddt[c]) {
919 ddt_table_free(spa->spa_ddt[c]);
920 spa->spa_ddt[c] = NULL;
921 }
922 }
923}
924
925boolean_t
926ddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp)
927{
928 ddt_t *ddt;
e95b3bdc 929 ddt_entry_t *dde;
d6320ddb
BB
930 enum ddt_type type;
931 enum ddt_class class;
428870ff
BB
932
933 if (!BP_GET_DEDUP(bp))
934 return (B_FALSE);
935
936 if (max_class == DDT_CLASS_UNIQUE)
937 return (B_TRUE);
938
939 ddt = spa->spa_ddt[BP_GET_CHECKSUM(bp)];
ea04106b 940 dde = kmem_cache_alloc(ddt_entry_cache, KM_SLEEP);
428870ff 941
e95b3bdc 942 ddt_key_fill(&(dde->dde_key), bp);
428870ff 943
e95b3bdc
BB
944 for (type = 0; type < DDT_TYPES; type++) {
945 for (class = 0; class <= max_class; class++) {
946 if (ddt_object_lookup(ddt, type, class, dde) == 0) {
a08ee875 947 kmem_cache_free(ddt_entry_cache, dde);
428870ff 948 return (B_TRUE);
e95b3bdc
BB
949 }
950 }
951 }
428870ff 952
a08ee875 953 kmem_cache_free(ddt_entry_cache, dde);
428870ff
BB
954 return (B_FALSE);
955}
956
957ddt_entry_t *
958ddt_repair_start(ddt_t *ddt, const blkptr_t *bp)
959{
960 ddt_key_t ddk;
961 ddt_entry_t *dde;
d6320ddb
BB
962 enum ddt_type type;
963 enum ddt_class class;
428870ff
BB
964
965 ddt_key_fill(&ddk, bp);
966
967 dde = ddt_alloc(&ddk);
968
d6320ddb
BB
969 for (type = 0; type < DDT_TYPES; type++) {
970 for (class = 0; class < DDT_CLASSES; class++) {
428870ff
BB
971 /*
972 * We can only do repair if there are multiple copies
973 * of the block. For anything in the UNIQUE class,
974 * there's definitely only one copy, so don't even try.
975 */
976 if (class != DDT_CLASS_UNIQUE &&
977 ddt_object_lookup(ddt, type, class, dde) == 0)
978 return (dde);
979 }
980 }
981
982 bzero(dde->dde_phys, sizeof (dde->dde_phys));
983
984 return (dde);
985}
986
987void
988ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde)
989{
990 avl_index_t where;
991
992 ddt_enter(ddt);
993
994 if (dde->dde_repair_data != NULL && spa_writeable(ddt->ddt_spa) &&
995 avl_find(&ddt->ddt_repair_tree, dde, &where) == NULL)
996 avl_insert(&ddt->ddt_repair_tree, dde, where);
997 else
998 ddt_free(dde);
999
1000 ddt_exit(ddt);
1001}
1002
1003static void
1004ddt_repair_entry_done(zio_t *zio)
1005{
1006 ddt_entry_t *rdde = zio->io_private;
1007
1008 ddt_free(rdde);
1009}
1010
1011static void
1012ddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio)
1013{
1014 ddt_phys_t *ddp = dde->dde_phys;
1015 ddt_phys_t *rddp = rdde->dde_phys;
1016 ddt_key_t *ddk = &dde->dde_key;
1017 ddt_key_t *rddk = &rdde->dde_key;
1018 zio_t *zio;
1019 blkptr_t blk;
d6320ddb 1020 int p;
428870ff
BB
1021
1022 zio = zio_null(rio, rio->io_spa, NULL,
1023 ddt_repair_entry_done, rdde, rio->io_flags);
1024
d6320ddb 1025 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) {
428870ff
BB
1026 if (ddp->ddp_phys_birth == 0 ||
1027 ddp->ddp_phys_birth != rddp->ddp_phys_birth ||
1028 bcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva)))
1029 continue;
1030 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
1031 zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk,
1032 rdde->dde_repair_data, DDK_GET_PSIZE(rddk), NULL, NULL,
1033 ZIO_PRIORITY_SYNC_WRITE, ZIO_DDT_CHILD_FLAGS(zio), NULL));
1034 }
1035
1036 zio_nowait(zio);
1037}
1038
1039static void
1040ddt_repair_table(ddt_t *ddt, zio_t *rio)
1041{
1042 spa_t *spa = ddt->ddt_spa;
1043 ddt_entry_t *dde, *rdde_next, *rdde;
1044 avl_tree_t *t = &ddt->ddt_repair_tree;
1045 blkptr_t blk;
1046
1047 if (spa_sync_pass(spa) > 1)
1048 return;
1049
1050 ddt_enter(ddt);
1051 for (rdde = avl_first(t); rdde != NULL; rdde = rdde_next) {
1052 rdde_next = AVL_NEXT(t, rdde);
1053 avl_remove(&ddt->ddt_repair_tree, rdde);
1054 ddt_exit(ddt);
1055 ddt_bp_create(ddt->ddt_checksum, &rdde->dde_key, NULL, &blk);
1056 dde = ddt_repair_start(ddt, &blk);
1057 ddt_repair_entry(ddt, dde, rdde, rio);
1058 ddt_repair_done(ddt, dde);
1059 ddt_enter(ddt);
1060 }
1061 ddt_exit(ddt);
1062}
1063
1064static void
1065ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
1066{
1067 dsl_pool_t *dp = ddt->ddt_spa->spa_dsl_pool;
1068 ddt_phys_t *ddp = dde->dde_phys;
1069 ddt_key_t *ddk = &dde->dde_key;
1070 enum ddt_type otype = dde->dde_type;
1071 enum ddt_type ntype = DDT_TYPE_CURRENT;
1072 enum ddt_class oclass = dde->dde_class;
1073 enum ddt_class nclass;
1074 uint64_t total_refcnt = 0;
d6320ddb 1075 int p;
428870ff
BB
1076
1077 ASSERT(dde->dde_loaded);
1078 ASSERT(!dde->dde_loading);
1079
d6320ddb 1080 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
428870ff 1081 ASSERT(dde->dde_lead_zio[p] == NULL);
428870ff
BB
1082 if (ddp->ddp_phys_birth == 0) {
1083 ASSERT(ddp->ddp_refcnt == 0);
1084 continue;
1085 }
1086 if (p == DDT_PHYS_DITTO) {
1087 if (ddt_ditto_copies_needed(ddt, dde, NULL) == 0)
1088 ddt_phys_free(ddt, ddk, ddp, txg);
1089 continue;
1090 }
1091 if (ddp->ddp_refcnt == 0)
1092 ddt_phys_free(ddt, ddk, ddp, txg);
1093 total_refcnt += ddp->ddp_refcnt;
1094 }
1095
1096 if (dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth != 0)
1097 nclass = DDT_CLASS_DITTO;
1098 else if (total_refcnt > 1)
1099 nclass = DDT_CLASS_DUPLICATE;
1100 else
1101 nclass = DDT_CLASS_UNIQUE;
1102
1103 if (otype != DDT_TYPES &&
1104 (otype != ntype || oclass != nclass || total_refcnt == 0)) {
1105 VERIFY(ddt_object_remove(ddt, otype, oclass, dde, tx) == 0);
1106 ASSERT(ddt_object_lookup(ddt, otype, oclass, dde) == ENOENT);
1107 }
1108
1109 if (total_refcnt != 0) {
1110 dde->dde_type = ntype;
1111 dde->dde_class = nclass;
1112 ddt_stat_update(ddt, dde, 0);
1113 if (!ddt_object_exists(ddt, ntype, nclass))
1114 ddt_object_create(ddt, ntype, nclass, tx);
1115 VERIFY(ddt_object_update(ddt, ntype, nclass, dde, tx) == 0);
1116
1117 /*
1118 * If the class changes, the order that we scan this bp
1119 * changes. If it decreases, we could miss it, so
1120 * scan it right now. (This covers both class changing
1121 * while we are doing ddt_walk(), and when we are
1122 * traversing.)
1123 */
1124 if (nclass < oclass) {
1125 dsl_scan_ddt_entry(dp->dp_scan,
1126 ddt->ddt_checksum, dde, tx);
1127 }
1128 }
1129}
1130
1131static void
1132ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
1133{
1134 spa_t *spa = ddt->ddt_spa;
1135 ddt_entry_t *dde;
1136 void *cookie = NULL;
d6320ddb
BB
1137 enum ddt_type type;
1138 enum ddt_class class;
428870ff
BB
1139
1140 if (avl_numnodes(&ddt->ddt_tree) == 0)
1141 return;
1142
1143 ASSERT(spa->spa_uberblock.ub_version >= SPA_VERSION_DEDUP);
1144
1145 if (spa->spa_ddt_stat_object == 0) {
9ae529ec
CS
1146 spa->spa_ddt_stat_object = zap_create_link(ddt->ddt_os,
1147 DMU_OT_DDT_STATS, DMU_POOL_DIRECTORY_OBJECT,
1148 DMU_POOL_DDT_STATS, tx);
428870ff
BB
1149 }
1150
1151 while ((dde = avl_destroy_nodes(&ddt->ddt_tree, &cookie)) != NULL) {
1152 ddt_sync_entry(ddt, dde, tx, txg);
1153 ddt_free(dde);
1154 }
1155
d6320ddb 1156 for (type = 0; type < DDT_TYPES; type++) {
e8fd45a0 1157 uint64_t add, count = 0;
d6320ddb 1158 for (class = 0; class < DDT_CLASSES; class++) {
572e2857
BB
1159 if (ddt_object_exists(ddt, type, class)) {
1160 ddt_object_sync(ddt, type, class, tx);
e8fd45a0
BB
1161 VERIFY(ddt_object_count(ddt, type, class,
1162 &add) == 0);
1163 count += add;
572e2857
BB
1164 }
1165 }
d6320ddb 1166 for (class = 0; class < DDT_CLASSES; class++) {
572e2857 1167 if (count == 0 && ddt_object_exists(ddt, type, class))
428870ff
BB
1168 ddt_object_destroy(ddt, type, class, tx);
1169 }
1170 }
1171
1172 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
1173 sizeof (ddt->ddt_histogram));
1174}
1175
1176void
1177ddt_sync(spa_t *spa, uint64_t txg)
1178{
1179 dmu_tx_t *tx;
1180 zio_t *rio = zio_root(spa, NULL, NULL,
1181 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
d6320ddb 1182 enum zio_checksum c;
428870ff
BB
1183
1184 ASSERT(spa_syncing_txg(spa) == txg);
1185
1186 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1187
d6320ddb 1188 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
428870ff
BB
1189 ddt_t *ddt = spa->spa_ddt[c];
1190 if (ddt == NULL)
1191 continue;
1192 ddt_sync_table(ddt, tx, txg);
1193 ddt_repair_table(ddt, rio);
1194 }
1195
1196 (void) zio_wait(rio);
1197
1198 dmu_tx_commit(tx);
1199}
1200
1201int
1202ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde)
1203{
1204 do {
1205 do {
1206 do {
1207 ddt_t *ddt = spa->spa_ddt[ddb->ddb_checksum];
1208 int error = ENOENT;
1209 if (ddt_object_exists(ddt, ddb->ddb_type,
1210 ddb->ddb_class)) {
1211 error = ddt_object_walk(ddt,
1212 ddb->ddb_type, ddb->ddb_class,
1213 &ddb->ddb_cursor, dde);
1214 }
1215 dde->dde_type = ddb->ddb_type;
1216 dde->dde_class = ddb->ddb_class;
1217 if (error == 0)
1218 return (0);
1219 if (error != ENOENT)
1220 return (error);
1221 ddb->ddb_cursor = 0;
1222 } while (++ddb->ddb_checksum < ZIO_CHECKSUM_FUNCTIONS);
1223 ddb->ddb_checksum = 0;
1224 } while (++ddb->ddb_type < DDT_TYPES);
1225 ddb->ddb_type = 0;
1226 } while (++ddb->ddb_class < DDT_CLASSES);
1227
a08ee875 1228 return (SET_ERROR(ENOENT));
428870ff 1229}
c409e464
BB
1230
1231#if defined(_KERNEL) && defined(HAVE_SPL)
1232module_param(zfs_dedup_prefetch, int, 0644);
a08ee875 1233MODULE_PARM_DESC(zfs_dedup_prefetch, "Enable prefetching dedup-ed blks");
c409e464 1234#endif