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