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