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.
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.
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]
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/zfs_context.h>
28 #include <sys/spa_impl.h>
32 #include <sys/dmu_tx.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>
40 * Enable/disable prefetching of dedup-ed blocks which are going to be freed.
42 int zfs_dedup_prefetch = 1;
44 static const ddt_ops_t *ddt_ops[DDT_TYPES] = {
48 static const char *ddt_class_name[DDT_CLASSES] = {
55 ddt_object_create(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
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];
64 ddt_object_name(ddt, type, class, name);
66 ASSERT(*objectp == 0);
67 VERIFY(ddt_ops[type]->ddt_op_create(os, objectp, tx, prehash) == 0);
68 ASSERT(*objectp != 0);
70 VERIFY(zap_add(os, DMU_POOL_DIRECTORY_OBJECT, name,
71 sizeof (uint64_t), 1, objectp, tx) == 0);
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);
79 ddt_object_destroy(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
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];
87 ddt_object_name(ddt, type, class, name);
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));
101 ddt_object_load(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
103 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
104 dmu_object_info_t doi;
105 char name[DDT_NAMELEN];
108 ddt_object_name(ddt, type, class, name);
110 error = zap_lookup(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT, name,
111 sizeof (uint64_t), 1, &ddt->ddt_object[type][class]);
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]);
121 * Seed the cached statistics.
123 VERIFY(ddt_object_info(ddt, type, class, &doi) == 0);
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;
134 ddt_object_sync(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
137 ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
138 dmu_object_info_t doi;
139 char name[DDT_NAMELEN];
141 ddt_object_name(ddt, type, class, name);
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);
148 * Cache DDT statistics; this is the only time they'll change.
150 VERIFY(ddt_object_info(ddt, type, class, &doi) == 0);
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;
158 ddt_object_lookup(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
161 if (!ddt_object_exists(ddt, type, class))
164 return (ddt_ops[type]->ddt_op_lookup(ddt->ddt_os,
165 ddt->ddt_object[type][class], dde));
169 ddt_object_prefetch(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
172 if (!ddt_object_exists(ddt, type, class))
175 ddt_ops[type]->ddt_op_prefetch(ddt->ddt_os,
176 ddt->ddt_object[type][class], dde);
180 ddt_object_update(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
181 ddt_entry_t *dde, dmu_tx_t *tx)
183 ASSERT(ddt_object_exists(ddt, type, class));
185 return (ddt_ops[type]->ddt_op_update(ddt->ddt_os,
186 ddt->ddt_object[type][class], dde, tx));
190 ddt_object_remove(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
191 ddt_entry_t *dde, dmu_tx_t *tx)
193 ASSERT(ddt_object_exists(ddt, type, class));
195 return (ddt_ops[type]->ddt_op_remove(ddt->ddt_os,
196 ddt->ddt_object[type][class], dde, tx));
200 ddt_object_walk(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
201 uint64_t *walk, ddt_entry_t *dde)
203 ASSERT(ddt_object_exists(ddt, type, class));
205 return (ddt_ops[type]->ddt_op_walk(ddt->ddt_os,
206 ddt->ddt_object[type][class], dde, walk));
210 ddt_object_count(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
212 ASSERT(ddt_object_exists(ddt, type, class));
214 return (ddt_ops[type]->ddt_op_count(ddt->ddt_os,
215 ddt->ddt_object[type][class]));
219 ddt_object_info(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
220 dmu_object_info_t *doi)
222 if (!ddt_object_exists(ddt, type, class))
225 return (dmu_object_info(ddt->ddt_os, ddt->ddt_object[type][class],
230 ddt_object_exists(ddt_t *ddt, enum ddt_type type, enum ddt_class class)
232 return (!!ddt->ddt_object[type][class]);
236 ddt_object_name(ddt_t *ddt, enum ddt_type type, enum ddt_class class,
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]);
245 ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, uint64_t txg)
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);
256 ddt_bp_create(enum zio_checksum checksum,
257 const ddt_key_t *ddk, const ddt_phys_t *ddp, blkptr_t *bp)
262 ddt_bp_fill(ddp, bp, ddp->ddp_phys_birth);
264 bp->blk_cksum = ddk->ddk_cksum;
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);
274 BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
278 ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp)
280 ddk->ddk_cksum = bp->blk_cksum;
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));
289 ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp)
292 ASSERT(ddp->ddp_phys_birth == 0);
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);
300 ddt_phys_clear(ddt_phys_t *ddp)
302 bzero(ddp, sizeof (*ddp));
306 ddt_phys_addref(ddt_phys_t *ddp)
312 ddt_phys_decref(ddt_phys_t *ddp)
314 ASSERT((int64_t)ddp->ddp_refcnt > 0);
319 ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, uint64_t txg)
323 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
325 zio_free(ddt->ddt_spa, txg, &blk);
329 ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp)
331 ddt_phys_t *ddp = (ddt_phys_t *)dde->dde_phys;
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)
343 ddt_phys_total_refcnt(const ddt_entry_t *dde)
348 for (p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++)
349 refcnt += dde->dde_phys[p].ddp_refcnt;
355 ddt_stat_generate(ddt_t *ddt, ddt_entry_t *dde, ddt_stat_t *dds)
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);
364 bzero(dds, sizeof (*dds));
366 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
368 uint64_t refcnt = ddp->ddp_refcnt;
370 if (ddp->ddp_phys_birth == 0)
373 for (d = 0; d < SPA_DVAS_PER_BP; d++)
374 dsize += dva_get_dsize_sync(spa, &ddp->ddp_dva[d]);
376 dds->dds_blocks += 1;
377 dds->dds_lsize += lsize;
378 dds->dds_psize += psize;
379 dds->dds_dsize += dsize;
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;
389 ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg)
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);
395 ASSERT(neg == 0 || neg == -1ULL); /* add or subtract */
398 *d++ += (*s++ ^ neg) - neg;
402 ddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg)
405 ddt_histogram_t *ddh;
408 ddt_stat_generate(ddt, dde, &dds);
410 bucket = highbit(dds.dds_ref_blocks) - 1;
413 ddh = &ddt->ddt_histogram[dde->dde_type][dde->dde_class];
415 ddt_stat_add(&ddh->ddh_stat[bucket], &dds, neg);
419 ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src)
423 for (h = 0; h < 64; h++)
424 ddt_stat_add(&dst->ddh_stat[h], &src->ddh_stat[h], 0);
428 ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh)
432 bzero(dds, sizeof (*dds));
434 for (h = 0; h < 64; h++)
435 ddt_stat_add(dds, &ddh->ddh_stat[h], 0);
439 ddt_histogram_empty(const ddt_histogram_t *ddh)
441 const uint64_t *s = (const uint64_t *)ddh;
442 const uint64_t *s_end = (const uint64_t *)(ddh + 1);
452 ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total)
456 enum ddt_class class;
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;
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;
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;
481 ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh)
485 enum ddt_class class;
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;
492 ddt_histogram_add(ddh,
493 &ddt->ddt_histogram_cache[type][class]);
500 ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total)
502 ddt_histogram_t *ddh_total;
504 /* XXX: Move to a slab */
505 ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP);
506 ddt_get_dedup_histogram(spa, ddh_total);
507 ddt_histogram_stat(dds_total, ddh_total);
508 kmem_free(ddh_total, sizeof (ddt_histogram_t));
512 ddt_get_dedup_dspace(spa_t *spa)
514 ddt_stat_t dds_total = { 0 };
516 ddt_get_dedup_stats(spa, &dds_total);
517 return (dds_total.dds_ref_dsize - dds_total.dds_dsize);
521 ddt_get_pool_dedup_ratio(spa_t *spa)
523 ddt_stat_t dds_total = { 0 };
525 ddt_get_dedup_stats(spa, &dds_total);
526 if (dds_total.dds_dsize == 0)
529 return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize);
533 ddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde, ddt_phys_t *ddp_willref)
535 spa_t *spa = ddt->ddt_spa;
536 uint64_t total_refcnt = 0;
537 uint64_t ditto = spa->spa_dedup_ditto;
538 int total_copies = 0;
539 int desired_copies = 0;
542 for (p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) {
543 ddt_phys_t *ddp = &dde->dde_phys[p];
544 zio_t *zio = dde->dde_lead_zio[p];
545 uint64_t refcnt = ddp->ddp_refcnt; /* committed refs */
547 refcnt += zio->io_parent_count; /* pending refs */
548 if (ddp == ddp_willref)
549 refcnt++; /* caller's ref */
551 total_refcnt += refcnt;
556 if (ditto == 0 || ditto > UINT32_MAX)
559 if (total_refcnt >= 1)
561 if (total_refcnt >= ditto)
563 if (total_refcnt >= ditto * ditto)
566 return (MAX(desired_copies, total_copies) - total_copies);
570 ddt_ditto_copies_present(ddt_entry_t *dde)
572 ddt_phys_t *ddp = &dde->dde_phys[DDT_PHYS_DITTO];
573 dva_t *dva = ddp->ddp_dva;
574 int copies = 0 - DVA_GET_GANG(dva);
577 for (d = 0; d < SPA_DVAS_PER_BP; d++, dva++)
578 if (DVA_IS_VALID(dva))
581 ASSERT(copies >= 0 && copies < SPA_DVAS_PER_BP);
587 ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len)
589 uchar_t *version = dst++;
590 int cpfunc = ZIO_COMPRESS_ZLE;
591 zio_compress_info_t *ci = &zio_compress_table[cpfunc];
594 ASSERT(d_len >= s_len + 1); /* no compression plus version byte */
596 c_len = ci->ci_compress(src, dst, s_len, d_len - 1, ci->ci_level);
598 if (c_len == s_len) {
599 cpfunc = ZIO_COMPRESS_OFF;
600 bcopy(src, dst, s_len);
603 *version = (ZFS_HOST_BYTEORDER & DDT_COMPRESS_BYTEORDER_MASK) | cpfunc;
609 ddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len)
611 uchar_t version = *src++;
612 int cpfunc = version & DDT_COMPRESS_FUNCTION_MASK;
613 zio_compress_info_t *ci = &zio_compress_table[cpfunc];
615 if (ci->ci_decompress != NULL)
616 (void) ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level);
618 bcopy(src, dst, d_len);
620 if ((version ^ ZFS_HOST_BYTEORDER) & DDT_COMPRESS_BYTEORDER_MASK)
621 byteswap_uint64_array(dst, d_len);
625 ddt_select_by_checksum(spa_t *spa, enum zio_checksum c)
627 return (spa->spa_ddt[c]);
631 ddt_select(spa_t *spa, const blkptr_t *bp)
633 return (spa->spa_ddt[BP_GET_CHECKSUM(bp)]);
637 ddt_enter(ddt_t *ddt)
639 mutex_enter(&ddt->ddt_lock);
645 mutex_exit(&ddt->ddt_lock);
649 ddt_alloc(const ddt_key_t *ddk)
653 /* XXX: Move to a slab */
654 dde = kmem_zalloc(sizeof (ddt_entry_t), KM_SLEEP);
655 cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL);
663 ddt_free(ddt_entry_t *dde)
667 ASSERT(!dde->dde_loading);
669 for (p = 0; p < DDT_PHYS_TYPES; p++)
670 ASSERT(dde->dde_lead_zio[p] == NULL);
672 if (dde->dde_repair_data != NULL)
673 zio_buf_free(dde->dde_repair_data,
674 DDK_GET_PSIZE(&dde->dde_key));
676 cv_destroy(&dde->dde_cv);
677 kmem_free(dde, sizeof (*dde));
681 ddt_remove(ddt_t *ddt, ddt_entry_t *dde)
683 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
685 avl_remove(&ddt->ddt_tree, dde);
690 ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add)
692 ddt_entry_t *dde, dde_search;
694 enum ddt_class class;
698 ASSERT(MUTEX_HELD(&ddt->ddt_lock));
700 ddt_key_fill(&dde_search.dde_key, bp);
702 dde = avl_find(&ddt->ddt_tree, &dde_search, &where);
706 dde = ddt_alloc(&dde_search.dde_key);
707 avl_insert(&ddt->ddt_tree, dde, where);
710 while (dde->dde_loading)
711 cv_wait(&dde->dde_cv, &ddt->ddt_lock);
716 dde->dde_loading = B_TRUE;
722 for (type = 0; type < DDT_TYPES; type++) {
723 for (class = 0; class < DDT_CLASSES; class++) {
724 error = ddt_object_lookup(ddt, type, class, dde);
732 ASSERT(error == 0 || error == ENOENT);
736 ASSERT(dde->dde_loaded == B_FALSE);
737 ASSERT(dde->dde_loading == B_TRUE);
739 dde->dde_type = type; /* will be DDT_TYPES if no entry found */
740 dde->dde_class = class; /* will be DDT_CLASSES if no entry found */
741 dde->dde_loaded = B_TRUE;
742 dde->dde_loading = B_FALSE;
745 ddt_stat_update(ddt, dde, -1ULL);
747 cv_broadcast(&dde->dde_cv);
753 ddt_prefetch(spa_t *spa, const blkptr_t *bp)
758 enum ddt_class class;
760 if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp))
764 * We only remove the DDT once all tables are empty and only
765 * prefetch dedup blocks when there are entries in the DDT.
766 * Thus no locking is required as the DDT can't disappear on us.
768 ddt = ddt_select(spa, bp);
769 ddt_key_fill(&dde.dde_key, bp);
771 for (type = 0; type < DDT_TYPES; type++) {
772 for (class = 0; class < DDT_CLASSES; class++) {
773 ddt_object_prefetch(ddt, type, class, &dde);
779 ddt_entry_compare(const void *x1, const void *x2)
781 const ddt_entry_t *dde1 = x1;
782 const ddt_entry_t *dde2 = x2;
783 const uint64_t *u1 = (const uint64_t *)&dde1->dde_key;
784 const uint64_t *u2 = (const uint64_t *)&dde2->dde_key;
787 for (i = 0; i < DDT_KEY_WORDS; i++) {
798 ddt_table_alloc(spa_t *spa, enum zio_checksum c)
802 /* XXX: Move to a slab */
803 ddt = kmem_zalloc(sizeof (*ddt), KM_SLEEP | KM_NODEBUG);
805 mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL);
806 avl_create(&ddt->ddt_tree, ddt_entry_compare,
807 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
808 avl_create(&ddt->ddt_repair_tree, ddt_entry_compare,
809 sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
810 ddt->ddt_checksum = c;
812 ddt->ddt_os = spa->spa_meta_objset;
818 ddt_table_free(ddt_t *ddt)
820 ASSERT(avl_numnodes(&ddt->ddt_tree) == 0);
821 ASSERT(avl_numnodes(&ddt->ddt_repair_tree) == 0);
822 avl_destroy(&ddt->ddt_tree);
823 avl_destroy(&ddt->ddt_repair_tree);
824 mutex_destroy(&ddt->ddt_lock);
825 kmem_free(ddt, sizeof (*ddt));
829 ddt_create(spa_t *spa)
833 spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM;
835 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++)
836 spa->spa_ddt[c] = ddt_table_alloc(spa, c);
844 enum ddt_class class;
849 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
850 DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
851 &spa->spa_ddt_stat_object);
854 return (error == ENOENT ? 0 : error);
856 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
857 ddt_t *ddt = spa->spa_ddt[c];
858 for (type = 0; type < DDT_TYPES; type++) {
859 for (class = 0; class < DDT_CLASSES;
861 error = ddt_object_load(ddt, type, class);
862 if (error != 0 && error != ENOENT)
868 * Seed the cached histograms.
870 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
871 sizeof (ddt->ddt_histogram));
878 ddt_unload(spa_t *spa)
882 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
883 if (spa->spa_ddt[c]) {
884 ddt_table_free(spa->spa_ddt[c]);
885 spa->spa_ddt[c] = NULL;
891 ddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp)
896 enum ddt_class class;
898 if (!BP_GET_DEDUP(bp))
901 if (max_class == DDT_CLASS_UNIQUE)
904 ddt = spa->spa_ddt[BP_GET_CHECKSUM(bp)];
906 ddt_key_fill(&dde.dde_key, bp);
908 for (type = 0; type < DDT_TYPES; type++)
909 for (class = 0; class <= max_class; class++)
910 if (ddt_object_lookup(ddt, type, class, &dde) == 0)
917 ddt_repair_start(ddt_t *ddt, const blkptr_t *bp)
922 enum ddt_class class;
924 ddt_key_fill(&ddk, bp);
926 dde = ddt_alloc(&ddk);
928 for (type = 0; type < DDT_TYPES; type++) {
929 for (class = 0; class < DDT_CLASSES; class++) {
931 * We can only do repair if there are multiple copies
932 * of the block. For anything in the UNIQUE class,
933 * there's definitely only one copy, so don't even try.
935 if (class != DDT_CLASS_UNIQUE &&
936 ddt_object_lookup(ddt, type, class, dde) == 0)
941 bzero(dde->dde_phys, sizeof (dde->dde_phys));
947 ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde)
953 if (dde->dde_repair_data != NULL && spa_writeable(ddt->ddt_spa) &&
954 avl_find(&ddt->ddt_repair_tree, dde, &where) == NULL)
955 avl_insert(&ddt->ddt_repair_tree, dde, where);
963 ddt_repair_entry_done(zio_t *zio)
965 ddt_entry_t *rdde = zio->io_private;
971 ddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio)
973 ddt_phys_t *ddp = dde->dde_phys;
974 ddt_phys_t *rddp = rdde->dde_phys;
975 ddt_key_t *ddk = &dde->dde_key;
976 ddt_key_t *rddk = &rdde->dde_key;
981 zio = zio_null(rio, rio->io_spa, NULL,
982 ddt_repair_entry_done, rdde, rio->io_flags);
984 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) {
985 if (ddp->ddp_phys_birth == 0 ||
986 ddp->ddp_phys_birth != rddp->ddp_phys_birth ||
987 bcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva)))
989 ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
990 zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk,
991 rdde->dde_repair_data, DDK_GET_PSIZE(rddk), NULL, NULL,
992 ZIO_PRIORITY_SYNC_WRITE, ZIO_DDT_CHILD_FLAGS(zio), NULL));
999 ddt_repair_table(ddt_t *ddt, zio_t *rio)
1001 spa_t *spa = ddt->ddt_spa;
1002 ddt_entry_t *dde, *rdde_next, *rdde;
1003 avl_tree_t *t = &ddt->ddt_repair_tree;
1006 if (spa_sync_pass(spa) > 1)
1010 for (rdde = avl_first(t); rdde != NULL; rdde = rdde_next) {
1011 rdde_next = AVL_NEXT(t, rdde);
1012 avl_remove(&ddt->ddt_repair_tree, rdde);
1014 ddt_bp_create(ddt->ddt_checksum, &rdde->dde_key, NULL, &blk);
1015 dde = ddt_repair_start(ddt, &blk);
1016 ddt_repair_entry(ddt, dde, rdde, rio);
1017 ddt_repair_done(ddt, dde);
1024 ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
1026 dsl_pool_t *dp = ddt->ddt_spa->spa_dsl_pool;
1027 ddt_phys_t *ddp = dde->dde_phys;
1028 ddt_key_t *ddk = &dde->dde_key;
1029 enum ddt_type otype = dde->dde_type;
1030 enum ddt_type ntype = DDT_TYPE_CURRENT;
1031 enum ddt_class oclass = dde->dde_class;
1032 enum ddt_class nclass;
1033 uint64_t total_refcnt = 0;
1036 ASSERT(dde->dde_loaded);
1037 ASSERT(!dde->dde_loading);
1039 for (p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
1040 ASSERT(dde->dde_lead_zio[p] == NULL);
1041 ASSERT((int64_t)ddp->ddp_refcnt >= 0);
1042 if (ddp->ddp_phys_birth == 0) {
1043 ASSERT(ddp->ddp_refcnt == 0);
1046 if (p == DDT_PHYS_DITTO) {
1047 if (ddt_ditto_copies_needed(ddt, dde, NULL) == 0)
1048 ddt_phys_free(ddt, ddk, ddp, txg);
1051 if (ddp->ddp_refcnt == 0)
1052 ddt_phys_free(ddt, ddk, ddp, txg);
1053 total_refcnt += ddp->ddp_refcnt;
1056 if (dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth != 0)
1057 nclass = DDT_CLASS_DITTO;
1058 else if (total_refcnt > 1)
1059 nclass = DDT_CLASS_DUPLICATE;
1061 nclass = DDT_CLASS_UNIQUE;
1063 if (otype != DDT_TYPES &&
1064 (otype != ntype || oclass != nclass || total_refcnt == 0)) {
1065 VERIFY(ddt_object_remove(ddt, otype, oclass, dde, tx) == 0);
1066 ASSERT(ddt_object_lookup(ddt, otype, oclass, dde) == ENOENT);
1069 if (total_refcnt != 0) {
1070 dde->dde_type = ntype;
1071 dde->dde_class = nclass;
1072 ddt_stat_update(ddt, dde, 0);
1073 if (!ddt_object_exists(ddt, ntype, nclass))
1074 ddt_object_create(ddt, ntype, nclass, tx);
1075 VERIFY(ddt_object_update(ddt, ntype, nclass, dde, tx) == 0);
1078 * If the class changes, the order that we scan this bp
1079 * changes. If it decreases, we could miss it, so
1080 * scan it right now. (This covers both class changing
1081 * while we are doing ddt_walk(), and when we are
1084 if (nclass < oclass) {
1085 dsl_scan_ddt_entry(dp->dp_scan,
1086 ddt->ddt_checksum, dde, tx);
1092 ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
1094 spa_t *spa = ddt->ddt_spa;
1096 void *cookie = NULL;
1098 enum ddt_class class;
1100 if (avl_numnodes(&ddt->ddt_tree) == 0)
1103 ASSERT(spa->spa_uberblock.ub_version >= SPA_VERSION_DEDUP);
1105 if (spa->spa_ddt_stat_object == 0) {
1106 spa->spa_ddt_stat_object = zap_create(ddt->ddt_os,
1107 DMU_OT_DDT_STATS, DMU_OT_NONE, 0, tx);
1108 VERIFY(zap_add(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT,
1109 DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
1110 &spa->spa_ddt_stat_object, tx) == 0);
1113 while ((dde = avl_destroy_nodes(&ddt->ddt_tree, &cookie)) != NULL) {
1114 ddt_sync_entry(ddt, dde, tx, txg);
1118 for (type = 0; type < DDT_TYPES; type++) {
1120 for (class = 0; class < DDT_CLASSES; class++) {
1121 if (ddt_object_exists(ddt, type, class)) {
1122 ddt_object_sync(ddt, type, class, tx);
1123 count += ddt_object_count(ddt, type, class);
1126 for (class = 0; class < DDT_CLASSES; class++) {
1127 if (count == 0 && ddt_object_exists(ddt, type, class))
1128 ddt_object_destroy(ddt, type, class, tx);
1132 bcopy(ddt->ddt_histogram, &ddt->ddt_histogram_cache,
1133 sizeof (ddt->ddt_histogram));
1137 ddt_sync(spa_t *spa, uint64_t txg)
1140 zio_t *rio = zio_root(spa, NULL, NULL,
1141 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1142 enum zio_checksum c;
1144 ASSERT(spa_syncing_txg(spa) == txg);
1146 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1148 for (c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1149 ddt_t *ddt = spa->spa_ddt[c];
1152 ddt_sync_table(ddt, tx, txg);
1153 ddt_repair_table(ddt, rio);
1156 (void) zio_wait(rio);
1162 ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde)
1167 ddt_t *ddt = spa->spa_ddt[ddb->ddb_checksum];
1169 if (ddt_object_exists(ddt, ddb->ddb_type,
1171 error = ddt_object_walk(ddt,
1172 ddb->ddb_type, ddb->ddb_class,
1173 &ddb->ddb_cursor, dde);
1175 dde->dde_type = ddb->ddb_type;
1176 dde->dde_class = ddb->ddb_class;
1179 if (error != ENOENT)
1181 ddb->ddb_cursor = 0;
1182 } while (++ddb->ddb_checksum < ZIO_CHECKSUM_FUNCTIONS);
1183 ddb->ddb_checksum = 0;
1184 } while (++ddb->ddb_type < DDT_TYPES);
1186 } while (++ddb->ddb_class < DDT_CLASSES);