xref: /freebsd/sys/contrib/openzfs/module/zfs/ddt.c (revision 783d3ff6)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
252a58b312SMartin Matuska  * Copyright (c) 2022 by Pawel Jakub Dawidek
264fefe1b7SMartin Matuska  * Copyright (c) 2023, Klara Inc.
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #include <sys/zfs_context.h>
30eda14cbcSMatt Macy #include <sys/spa.h>
31eda14cbcSMatt Macy #include <sys/spa_impl.h>
32eda14cbcSMatt Macy #include <sys/zio.h>
33eda14cbcSMatt Macy #include <sys/ddt.h>
344fefe1b7SMartin Matuska #include <sys/ddt_impl.h>
35eda14cbcSMatt Macy #include <sys/zap.h>
36eda14cbcSMatt Macy #include <sys/dmu_tx.h>
37eda14cbcSMatt Macy #include <sys/arc.h>
38eda14cbcSMatt Macy #include <sys/dsl_pool.h>
39eda14cbcSMatt Macy #include <sys/zio_checksum.h>
40eda14cbcSMatt Macy #include <sys/dsl_scan.h>
41eda14cbcSMatt Macy #include <sys/abd.h>
42eda14cbcSMatt Macy 
434fefe1b7SMartin Matuska /*
444fefe1b7SMartin Matuska  * # DDT: Deduplication tables
454fefe1b7SMartin Matuska  *
464fefe1b7SMartin Matuska  * The dedup subsystem provides block-level deduplication. When enabled, blocks
474fefe1b7SMartin Matuska  * to be written will have the dedup (D) bit set, which causes them to be
484fefe1b7SMartin Matuska  * tracked in a "dedup table", or DDT. If a block has been seen before (exists
494fefe1b7SMartin Matuska  * in the DDT), instead of being written, it will instead be made to reference
504fefe1b7SMartin Matuska  * the existing on-disk data, and a refcount bumped in the DDT instead.
514fefe1b7SMartin Matuska  *
524fefe1b7SMartin Matuska  * ## Dedup tables and entries
534fefe1b7SMartin Matuska  *
544fefe1b7SMartin Matuska  * Conceptually, a DDT is a dictionary or map. Each entry has a "key"
554fefe1b7SMartin Matuska  * (ddt_key_t) made up a block's checksum and certian properties, and a "value"
564fefe1b7SMartin Matuska  * (one or more ddt_phys_t) containing valid DVAs for the block's data, birth
574fefe1b7SMartin Matuska  * time and refcount. Together these are enough to track references to a
584fefe1b7SMartin Matuska  * specific block, to build a valid block pointer to reference that block (for
594fefe1b7SMartin Matuska  * freeing, scrubbing, etc), and to fill a new block pointer with the missing
604fefe1b7SMartin Matuska  * pieces to make it seem like it was written.
614fefe1b7SMartin Matuska  *
624fefe1b7SMartin Matuska  * There's a single DDT (ddt_t) for each checksum type, held in spa_ddt[].
634fefe1b7SMartin Matuska  * Within each DDT, there can be multiple storage "types" (ddt_type_t, on-disk
644fefe1b7SMartin Matuska  * object data formats, each with their own implementations) and "classes"
654fefe1b7SMartin Matuska  * (ddt_class_t, instance of a storage type object, for entries with a specific
664fefe1b7SMartin Matuska  * characteristic). An entry (key) will only ever exist on one of these objects
674fefe1b7SMartin Matuska  * at any given time, but may be moved from one to another if their type or
684fefe1b7SMartin Matuska  * class changes.
694fefe1b7SMartin Matuska  *
704fefe1b7SMartin Matuska  * The DDT is driven by the write IO pipeline (zio_ddt_write()). When a block
714fefe1b7SMartin Matuska  * is to be written, before DVAs have been allocated, ddt_lookup() is called to
724fefe1b7SMartin Matuska  * see if the block has been seen before. If its not found, the write proceeds
734fefe1b7SMartin Matuska  * as normal, and after it succeeds, a new entry is created. If it is found, we
744fefe1b7SMartin Matuska  * fill the BP with the DVAs from the entry, increment the refcount and cause
754fefe1b7SMartin Matuska  * the write IO to return immediately.
764fefe1b7SMartin Matuska  *
774fefe1b7SMartin Matuska  * Each ddt_phys_t slot in the entry represents a separate dedup block for the
784fefe1b7SMartin Matuska  * same content/checksum. The slot is selected based on the zp_copies parameter
794fefe1b7SMartin Matuska  * the block is written with, that is, the number of DVAs in the block. The
804fefe1b7SMartin Matuska  * "ditto" slot (DDT_PHYS_DITTO) used to be used for now-removed "dedupditto"
814fefe1b7SMartin Matuska  * feature. These are no longer written, and will be freed if encountered on
824fefe1b7SMartin Matuska  * old pools.
834fefe1b7SMartin Matuska  *
844fefe1b7SMartin Matuska  * ## Lifetime of an entry
854fefe1b7SMartin Matuska  *
864fefe1b7SMartin Matuska  * A DDT can be enormous, and typically is not held in memory all at once.
874fefe1b7SMartin Matuska  * Instead, the changes to an entry are tracked in memory, and written down to
884fefe1b7SMartin Matuska  * disk at the end of each txg.
894fefe1b7SMartin Matuska  *
904fefe1b7SMartin Matuska  * A "live" in-memory entry (ddt_entry_t) is a node on the live tree
914fefe1b7SMartin Matuska  * (ddt_tree).  At the start of a txg, ddt_tree is empty. When an entry is
924fefe1b7SMartin Matuska  * required for IO, ddt_lookup() is called. If an entry already exists on
934fefe1b7SMartin Matuska  * ddt_tree, it is returned. Otherwise, a new one is created, and the
944fefe1b7SMartin Matuska  * type/class objects for the DDT are searched for that key. If its found, its
954fefe1b7SMartin Matuska  * value is copied into the live entry. If not, an empty entry is created.
964fefe1b7SMartin Matuska  *
974fefe1b7SMartin Matuska  * The live entry will be modified during the txg, usually by modifying the
984fefe1b7SMartin Matuska  * refcount, but sometimes by adding or updating DVAs. At the end of the txg
994fefe1b7SMartin Matuska  * (during spa_sync()), type and class are recalculated for entry (see
1004fefe1b7SMartin Matuska  * ddt_sync_entry()), and the entry is written to the appropriate storage
1014fefe1b7SMartin Matuska  * object and (if necessary), removed from an old one. ddt_tree is cleared and
1024fefe1b7SMartin Matuska  * the next txg can start.
1034fefe1b7SMartin Matuska  *
1044fefe1b7SMartin Matuska  * ## Repair IO
1054fefe1b7SMartin Matuska  *
1064fefe1b7SMartin Matuska  * If a read on a dedup block fails, but there are other copies of the block in
1074fefe1b7SMartin Matuska  * the other ddt_phys_t slots, reads will be issued for those instead
1084fefe1b7SMartin Matuska  * (zio_ddt_read_start()). If one of those succeeds, the read is returned to
1094fefe1b7SMartin Matuska  * the caller, and a copy is stashed on the entry's dde_repair_abd.
1104fefe1b7SMartin Matuska  *
1114fefe1b7SMartin Matuska  * During the end-of-txg sync, any entries with a dde_repair_abd get a
1124fefe1b7SMartin Matuska  * "rewrite" write issued for the original block pointer, with the data read
1134fefe1b7SMartin Matuska  * from the alternate block. If the block is actually damaged, this will invoke
1144fefe1b7SMartin Matuska  * the pool's "self-healing" mechanism, and repair the block.
1154fefe1b7SMartin Matuska  *
1164fefe1b7SMartin Matuska  * ## Scanning (scrub/resilver)
1174fefe1b7SMartin Matuska  *
1184fefe1b7SMartin Matuska  * If dedup is active, the scrub machinery will walk the dedup table first, and
1194fefe1b7SMartin Matuska  * scrub all blocks with refcnt > 1 first. After that it will move on to the
1204fefe1b7SMartin Matuska  * regular top-down scrub, and exclude the refcnt > 1 blocks when it sees them.
1214fefe1b7SMartin Matuska  * In this way, heavily deduplicated blocks are only scrubbed once. See the
1224fefe1b7SMartin Matuska  * commentary on dsl_scan_ddt() for more details.
1234fefe1b7SMartin Matuska  *
1244fefe1b7SMartin Matuska  * Walking the DDT is done via ddt_walk(). The current position is stored in a
1254fefe1b7SMartin Matuska  * ddt_bookmark_t, which represents a stable position in the storage object.
1264fefe1b7SMartin Matuska  * This bookmark is stored by the scan machinery, and must reference the same
1274fefe1b7SMartin Matuska  * position on the object even if the object changes, the pool is exported, or
1284fefe1b7SMartin Matuska  * OpenZFS is upgraded.
1294fefe1b7SMartin Matuska  *
1304fefe1b7SMartin Matuska  * ## Interaction with block cloning
1314fefe1b7SMartin Matuska  *
1324fefe1b7SMartin Matuska  * If block cloning and dedup are both enabled on a pool, BRT will look for the
1334fefe1b7SMartin Matuska  * dedup bit on an incoming block pointer. If set, it will call into the DDT
1344fefe1b7SMartin Matuska  * (ddt_addref()) to add a reference to the block, instead of adding a
1354fefe1b7SMartin Matuska  * reference to the BRT. See brt_pending_apply().
1364fefe1b7SMartin Matuska  */
1374fefe1b7SMartin Matuska 
1384fefe1b7SMartin Matuska /*
1394fefe1b7SMartin Matuska  * These are the only checksums valid for dedup. They must match the list
1404fefe1b7SMartin Matuska  * from dedup_table in zfs_prop.c
1414fefe1b7SMartin Matuska  */
1424fefe1b7SMartin Matuska #define	DDT_CHECKSUM_VALID(c)	\
1434fefe1b7SMartin Matuska 	(c == ZIO_CHECKSUM_SHA256 || c == ZIO_CHECKSUM_SHA512 || \
1444fefe1b7SMartin Matuska 	c == ZIO_CHECKSUM_SKEIN || c == ZIO_CHECKSUM_EDONR || \
1454fefe1b7SMartin Matuska 	c == ZIO_CHECKSUM_BLAKE3)
1464fefe1b7SMartin Matuska 
147eda14cbcSMatt Macy static kmem_cache_t *ddt_cache;
148eda14cbcSMatt Macy static kmem_cache_t *ddt_entry_cache;
149eda14cbcSMatt Macy 
150eda14cbcSMatt Macy /*
151eda14cbcSMatt Macy  * Enable/disable prefetching of dedup-ed blocks which are going to be freed.
152eda14cbcSMatt Macy  */
153eda14cbcSMatt Macy int zfs_dedup_prefetch = 0;
154eda14cbcSMatt Macy 
155da5137abSMartin Matuska static const ddt_ops_t *const ddt_ops[DDT_TYPES] = {
156eda14cbcSMatt Macy 	&ddt_zap_ops,
157eda14cbcSMatt Macy };
158eda14cbcSMatt Macy 
159da5137abSMartin Matuska static const char *const ddt_class_name[DDT_CLASSES] = {
160eda14cbcSMatt Macy 	"ditto",
161eda14cbcSMatt Macy 	"duplicate",
162eda14cbcSMatt Macy 	"unique",
163eda14cbcSMatt Macy };
164eda14cbcSMatt Macy 
165eda14cbcSMatt Macy static void
ddt_object_create(ddt_t * ddt,ddt_type_t type,ddt_class_t class,dmu_tx_t * tx)1664fefe1b7SMartin Matuska ddt_object_create(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
167eda14cbcSMatt Macy     dmu_tx_t *tx)
168eda14cbcSMatt Macy {
169eda14cbcSMatt Macy 	spa_t *spa = ddt->ddt_spa;
170eda14cbcSMatt Macy 	objset_t *os = ddt->ddt_os;
171eda14cbcSMatt Macy 	uint64_t *objectp = &ddt->ddt_object[type][class];
172eda14cbcSMatt Macy 	boolean_t prehash = zio_checksum_table[ddt->ddt_checksum].ci_flags &
173eda14cbcSMatt Macy 	    ZCHECKSUM_FLAG_DEDUP;
174eda14cbcSMatt Macy 	char name[DDT_NAMELEN];
175eda14cbcSMatt Macy 
176eda14cbcSMatt Macy 	ddt_object_name(ddt, type, class, name);
177eda14cbcSMatt Macy 
1784fefe1b7SMartin Matuska 	ASSERT3U(*objectp, ==, 0);
1794fefe1b7SMartin Matuska 	VERIFY0(ddt_ops[type]->ddt_op_create(os, objectp, tx, prehash));
1804fefe1b7SMartin Matuska 	ASSERT3U(*objectp, !=, 0);
181eda14cbcSMatt Macy 
1824fefe1b7SMartin Matuska 	VERIFY0(zap_add(os, DMU_POOL_DIRECTORY_OBJECT, name,
1834fefe1b7SMartin Matuska 	    sizeof (uint64_t), 1, objectp, tx));
184eda14cbcSMatt Macy 
1854fefe1b7SMartin Matuska 	VERIFY0(zap_add(os, spa->spa_ddt_stat_object, name,
186eda14cbcSMatt Macy 	    sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
1874fefe1b7SMartin Matuska 	    &ddt->ddt_histogram[type][class], tx));
188eda14cbcSMatt Macy }
189eda14cbcSMatt Macy 
190eda14cbcSMatt Macy static void
ddt_object_destroy(ddt_t * ddt,ddt_type_t type,ddt_class_t class,dmu_tx_t * tx)1914fefe1b7SMartin Matuska ddt_object_destroy(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
192eda14cbcSMatt Macy     dmu_tx_t *tx)
193eda14cbcSMatt Macy {
194eda14cbcSMatt Macy 	spa_t *spa = ddt->ddt_spa;
195eda14cbcSMatt Macy 	objset_t *os = ddt->ddt_os;
196eda14cbcSMatt Macy 	uint64_t *objectp = &ddt->ddt_object[type][class];
197eda14cbcSMatt Macy 	uint64_t count;
198eda14cbcSMatt Macy 	char name[DDT_NAMELEN];
199eda14cbcSMatt Macy 
200eda14cbcSMatt Macy 	ddt_object_name(ddt, type, class, name);
201eda14cbcSMatt Macy 
2024fefe1b7SMartin Matuska 	ASSERT3U(*objectp, !=, 0);
203eda14cbcSMatt Macy 	ASSERT(ddt_histogram_empty(&ddt->ddt_histogram[type][class]));
2044fefe1b7SMartin Matuska 	VERIFY0(ddt_object_count(ddt, type, class, &count));
2054fefe1b7SMartin Matuska 	VERIFY0(count);
2064fefe1b7SMartin Matuska 	VERIFY0(zap_remove(os, DMU_POOL_DIRECTORY_OBJECT, name, tx));
2074fefe1b7SMartin Matuska 	VERIFY0(zap_remove(os, spa->spa_ddt_stat_object, name, tx));
2084fefe1b7SMartin Matuska 	VERIFY0(ddt_ops[type]->ddt_op_destroy(os, *objectp, tx));
209da5137abSMartin Matuska 	memset(&ddt->ddt_object_stats[type][class], 0, sizeof (ddt_object_t));
210eda14cbcSMatt Macy 
211eda14cbcSMatt Macy 	*objectp = 0;
212eda14cbcSMatt Macy }
213eda14cbcSMatt Macy 
214eda14cbcSMatt Macy static int
ddt_object_load(ddt_t * ddt,ddt_type_t type,ddt_class_t class)2154fefe1b7SMartin Matuska ddt_object_load(ddt_t *ddt, ddt_type_t type, ddt_class_t class)
216eda14cbcSMatt Macy {
217eda14cbcSMatt Macy 	ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
218eda14cbcSMatt Macy 	dmu_object_info_t doi;
219eda14cbcSMatt Macy 	uint64_t count;
220eda14cbcSMatt Macy 	char name[DDT_NAMELEN];
221eda14cbcSMatt Macy 	int error;
222eda14cbcSMatt Macy 
223eda14cbcSMatt Macy 	ddt_object_name(ddt, type, class, name);
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy 	error = zap_lookup(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT, name,
226eda14cbcSMatt Macy 	    sizeof (uint64_t), 1, &ddt->ddt_object[type][class]);
227eda14cbcSMatt Macy 	if (error != 0)
228eda14cbcSMatt Macy 		return (error);
229eda14cbcSMatt Macy 
230eda14cbcSMatt Macy 	error = zap_lookup(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
231eda14cbcSMatt Macy 	    sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
232eda14cbcSMatt Macy 	    &ddt->ddt_histogram[type][class]);
233eda14cbcSMatt Macy 	if (error != 0)
234eda14cbcSMatt Macy 		return (error);
235eda14cbcSMatt Macy 
236eda14cbcSMatt Macy 	/*
237eda14cbcSMatt Macy 	 * Seed the cached statistics.
238eda14cbcSMatt Macy 	 */
239eda14cbcSMatt Macy 	error = ddt_object_info(ddt, type, class, &doi);
240eda14cbcSMatt Macy 	if (error)
241eda14cbcSMatt Macy 		return (error);
242eda14cbcSMatt Macy 
243eda14cbcSMatt Macy 	error = ddt_object_count(ddt, type, class, &count);
244eda14cbcSMatt Macy 	if (error)
245eda14cbcSMatt Macy 		return (error);
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy 	ddo->ddo_count = count;
248eda14cbcSMatt Macy 	ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
249eda14cbcSMatt Macy 	ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
250eda14cbcSMatt Macy 
251eda14cbcSMatt Macy 	return (0);
252eda14cbcSMatt Macy }
253eda14cbcSMatt Macy 
254eda14cbcSMatt Macy static void
ddt_object_sync(ddt_t * ddt,ddt_type_t type,ddt_class_t class,dmu_tx_t * tx)2554fefe1b7SMartin Matuska ddt_object_sync(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
256eda14cbcSMatt Macy     dmu_tx_t *tx)
257eda14cbcSMatt Macy {
258eda14cbcSMatt Macy 	ddt_object_t *ddo = &ddt->ddt_object_stats[type][class];
259eda14cbcSMatt Macy 	dmu_object_info_t doi;
260eda14cbcSMatt Macy 	uint64_t count;
261eda14cbcSMatt Macy 	char name[DDT_NAMELEN];
262eda14cbcSMatt Macy 
263eda14cbcSMatt Macy 	ddt_object_name(ddt, type, class, name);
264eda14cbcSMatt Macy 
2654fefe1b7SMartin Matuska 	VERIFY0(zap_update(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name,
266eda14cbcSMatt Macy 	    sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t),
2674fefe1b7SMartin Matuska 	    &ddt->ddt_histogram[type][class], tx));
268eda14cbcSMatt Macy 
269eda14cbcSMatt Macy 	/*
270eda14cbcSMatt Macy 	 * Cache DDT statistics; this is the only time they'll change.
271eda14cbcSMatt Macy 	 */
2724fefe1b7SMartin Matuska 	VERIFY0(ddt_object_info(ddt, type, class, &doi));
2734fefe1b7SMartin Matuska 	VERIFY0(ddt_object_count(ddt, type, class, &count));
274eda14cbcSMatt Macy 
275eda14cbcSMatt Macy 	ddo->ddo_count = count;
276eda14cbcSMatt Macy 	ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9;
277eda14cbcSMatt Macy 	ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size;
278eda14cbcSMatt Macy }
279eda14cbcSMatt Macy 
2804fefe1b7SMartin Matuska static boolean_t
ddt_object_exists(ddt_t * ddt,ddt_type_t type,ddt_class_t class)2814fefe1b7SMartin Matuska ddt_object_exists(ddt_t *ddt, ddt_type_t type, ddt_class_t class)
2824fefe1b7SMartin Matuska {
2834fefe1b7SMartin Matuska 	return (!!ddt->ddt_object[type][class]);
2844fefe1b7SMartin Matuska }
2854fefe1b7SMartin Matuska 
286eda14cbcSMatt Macy static int
ddt_object_lookup(ddt_t * ddt,ddt_type_t type,ddt_class_t class,ddt_entry_t * dde)2874fefe1b7SMartin Matuska ddt_object_lookup(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
288eda14cbcSMatt Macy     ddt_entry_t *dde)
289eda14cbcSMatt Macy {
290eda14cbcSMatt Macy 	if (!ddt_object_exists(ddt, type, class))
291eda14cbcSMatt Macy 		return (SET_ERROR(ENOENT));
292eda14cbcSMatt Macy 
293eda14cbcSMatt Macy 	return (ddt_ops[type]->ddt_op_lookup(ddt->ddt_os,
2944fefe1b7SMartin Matuska 	    ddt->ddt_object[type][class], &dde->dde_key,
2954fefe1b7SMartin Matuska 	    dde->dde_phys, sizeof (dde->dde_phys)));
2964fefe1b7SMartin Matuska }
2974fefe1b7SMartin Matuska 
2984fefe1b7SMartin Matuska static int
ddt_object_contains(ddt_t * ddt,ddt_type_t type,ddt_class_t class,const ddt_key_t * ddk)2994fefe1b7SMartin Matuska ddt_object_contains(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
3004fefe1b7SMartin Matuska     const ddt_key_t *ddk)
3014fefe1b7SMartin Matuska {
3024fefe1b7SMartin Matuska 	if (!ddt_object_exists(ddt, type, class))
3034fefe1b7SMartin Matuska 		return (SET_ERROR(ENOENT));
3044fefe1b7SMartin Matuska 
3054fefe1b7SMartin Matuska 	return (ddt_ops[type]->ddt_op_contains(ddt->ddt_os,
3064fefe1b7SMartin Matuska 	    ddt->ddt_object[type][class], ddk));
307eda14cbcSMatt Macy }
308eda14cbcSMatt Macy 
309eda14cbcSMatt Macy static void
ddt_object_prefetch(ddt_t * ddt,ddt_type_t type,ddt_class_t class,const ddt_key_t * ddk)3104fefe1b7SMartin Matuska ddt_object_prefetch(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
3114fefe1b7SMartin Matuska     const ddt_key_t *ddk)
312eda14cbcSMatt Macy {
313eda14cbcSMatt Macy 	if (!ddt_object_exists(ddt, type, class))
314eda14cbcSMatt Macy 		return;
315eda14cbcSMatt Macy 
316eda14cbcSMatt Macy 	ddt_ops[type]->ddt_op_prefetch(ddt->ddt_os,
3174fefe1b7SMartin Matuska 	    ddt->ddt_object[type][class], ddk);
318eda14cbcSMatt Macy }
319eda14cbcSMatt Macy 
3204fefe1b7SMartin Matuska static int
ddt_object_update(ddt_t * ddt,ddt_type_t type,ddt_class_t class,ddt_entry_t * dde,dmu_tx_t * tx)3214fefe1b7SMartin Matuska ddt_object_update(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
322eda14cbcSMatt Macy     ddt_entry_t *dde, dmu_tx_t *tx)
323eda14cbcSMatt Macy {
324eda14cbcSMatt Macy 	ASSERT(ddt_object_exists(ddt, type, class));
325eda14cbcSMatt Macy 
326eda14cbcSMatt Macy 	return (ddt_ops[type]->ddt_op_update(ddt->ddt_os,
3274fefe1b7SMartin Matuska 	    ddt->ddt_object[type][class], &dde->dde_key, dde->dde_phys,
3284fefe1b7SMartin Matuska 	    sizeof (dde->dde_phys), tx));
329eda14cbcSMatt Macy }
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy static int
ddt_object_remove(ddt_t * ddt,ddt_type_t type,ddt_class_t class,const ddt_key_t * ddk,dmu_tx_t * tx)3324fefe1b7SMartin Matuska ddt_object_remove(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
3334fefe1b7SMartin Matuska     const ddt_key_t *ddk, dmu_tx_t *tx)
334eda14cbcSMatt Macy {
335eda14cbcSMatt Macy 	ASSERT(ddt_object_exists(ddt, type, class));
336eda14cbcSMatt Macy 
337eda14cbcSMatt Macy 	return (ddt_ops[type]->ddt_op_remove(ddt->ddt_os,
3384fefe1b7SMartin Matuska 	    ddt->ddt_object[type][class], ddk, tx));
339eda14cbcSMatt Macy }
340eda14cbcSMatt Macy 
341eda14cbcSMatt Macy int
ddt_object_walk(ddt_t * ddt,ddt_type_t type,ddt_class_t class,uint64_t * walk,ddt_entry_t * dde)3424fefe1b7SMartin Matuska ddt_object_walk(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
343eda14cbcSMatt Macy     uint64_t *walk, ddt_entry_t *dde)
344eda14cbcSMatt Macy {
345eda14cbcSMatt Macy 	ASSERT(ddt_object_exists(ddt, type, class));
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy 	return (ddt_ops[type]->ddt_op_walk(ddt->ddt_os,
3484fefe1b7SMartin Matuska 	    ddt->ddt_object[type][class], walk, &dde->dde_key,
3494fefe1b7SMartin Matuska 	    dde->dde_phys, sizeof (dde->dde_phys)));
350eda14cbcSMatt Macy }
351eda14cbcSMatt Macy 
352eda14cbcSMatt Macy int
ddt_object_count(ddt_t * ddt,ddt_type_t type,ddt_class_t class,uint64_t * count)3534fefe1b7SMartin Matuska ddt_object_count(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
354eda14cbcSMatt Macy     uint64_t *count)
355eda14cbcSMatt Macy {
356eda14cbcSMatt Macy 	ASSERT(ddt_object_exists(ddt, type, class));
357eda14cbcSMatt Macy 
358eda14cbcSMatt Macy 	return (ddt_ops[type]->ddt_op_count(ddt->ddt_os,
359eda14cbcSMatt Macy 	    ddt->ddt_object[type][class], count));
360eda14cbcSMatt Macy }
361eda14cbcSMatt Macy 
362eda14cbcSMatt Macy int
ddt_object_info(ddt_t * ddt,ddt_type_t type,ddt_class_t class,dmu_object_info_t * doi)3634fefe1b7SMartin Matuska ddt_object_info(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
364eda14cbcSMatt Macy     dmu_object_info_t *doi)
365eda14cbcSMatt Macy {
366eda14cbcSMatt Macy 	if (!ddt_object_exists(ddt, type, class))
367eda14cbcSMatt Macy 		return (SET_ERROR(ENOENT));
368eda14cbcSMatt Macy 
369eda14cbcSMatt Macy 	return (dmu_object_info(ddt->ddt_os, ddt->ddt_object[type][class],
370eda14cbcSMatt Macy 	    doi));
371eda14cbcSMatt Macy }
372eda14cbcSMatt Macy 
373eda14cbcSMatt Macy void
ddt_object_name(ddt_t * ddt,ddt_type_t type,ddt_class_t class,char * name)3744fefe1b7SMartin Matuska ddt_object_name(ddt_t *ddt, ddt_type_t type, ddt_class_t class,
375eda14cbcSMatt Macy     char *name)
376eda14cbcSMatt Macy {
377eda14cbcSMatt Macy 	(void) snprintf(name, DDT_NAMELEN, DMU_POOL_DDT,
378eda14cbcSMatt Macy 	    zio_checksum_table[ddt->ddt_checksum].ci_name,
379eda14cbcSMatt Macy 	    ddt_ops[type]->ddt_op_name, ddt_class_name[class]);
380eda14cbcSMatt Macy }
381eda14cbcSMatt Macy 
382eda14cbcSMatt Macy void
ddt_bp_fill(const ddt_phys_t * ddp,blkptr_t * bp,uint64_t txg)383eda14cbcSMatt Macy ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, uint64_t txg)
384eda14cbcSMatt Macy {
3854fefe1b7SMartin Matuska 	ASSERT3U(txg, !=, 0);
386eda14cbcSMatt Macy 
387eda14cbcSMatt Macy 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
388eda14cbcSMatt Macy 		bp->blk_dva[d] = ddp->ddp_dva[d];
389eda14cbcSMatt Macy 	BP_SET_BIRTH(bp, txg, ddp->ddp_phys_birth);
390eda14cbcSMatt Macy }
391eda14cbcSMatt Macy 
392eda14cbcSMatt Macy /*
393eda14cbcSMatt Macy  * The bp created via this function may be used for repairs and scrub, but it
394eda14cbcSMatt Macy  * will be missing the salt / IV required to do a full decrypting read.
395eda14cbcSMatt Macy  */
396eda14cbcSMatt Macy void
ddt_bp_create(enum zio_checksum checksum,const ddt_key_t * ddk,const ddt_phys_t * ddp,blkptr_t * bp)397eda14cbcSMatt Macy ddt_bp_create(enum zio_checksum checksum,
398eda14cbcSMatt Macy     const ddt_key_t *ddk, const ddt_phys_t *ddp, blkptr_t *bp)
399eda14cbcSMatt Macy {
400eda14cbcSMatt Macy 	BP_ZERO(bp);
401eda14cbcSMatt Macy 
402eda14cbcSMatt Macy 	if (ddp != NULL)
403eda14cbcSMatt Macy 		ddt_bp_fill(ddp, bp, ddp->ddp_phys_birth);
404eda14cbcSMatt Macy 
405eda14cbcSMatt Macy 	bp->blk_cksum = ddk->ddk_cksum;
406eda14cbcSMatt Macy 
407eda14cbcSMatt Macy 	BP_SET_LSIZE(bp, DDK_GET_LSIZE(ddk));
408eda14cbcSMatt Macy 	BP_SET_PSIZE(bp, DDK_GET_PSIZE(ddk));
409eda14cbcSMatt Macy 	BP_SET_COMPRESS(bp, DDK_GET_COMPRESS(ddk));
410eda14cbcSMatt Macy 	BP_SET_CRYPT(bp, DDK_GET_CRYPT(ddk));
411eda14cbcSMatt Macy 	BP_SET_FILL(bp, 1);
412eda14cbcSMatt Macy 	BP_SET_CHECKSUM(bp, checksum);
413eda14cbcSMatt Macy 	BP_SET_TYPE(bp, DMU_OT_DEDUP);
414eda14cbcSMatt Macy 	BP_SET_LEVEL(bp, 0);
415eda14cbcSMatt Macy 	BP_SET_DEDUP(bp, 1);
416eda14cbcSMatt Macy 	BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER);
417eda14cbcSMatt Macy }
418eda14cbcSMatt Macy 
419eda14cbcSMatt Macy void
ddt_key_fill(ddt_key_t * ddk,const blkptr_t * bp)420eda14cbcSMatt Macy ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp)
421eda14cbcSMatt Macy {
422eda14cbcSMatt Macy 	ddk->ddk_cksum = bp->blk_cksum;
423eda14cbcSMatt Macy 	ddk->ddk_prop = 0;
424eda14cbcSMatt Macy 
425eda14cbcSMatt Macy 	ASSERT(BP_IS_ENCRYPTED(bp) || !BP_USES_CRYPT(bp));
426eda14cbcSMatt Macy 
427eda14cbcSMatt Macy 	DDK_SET_LSIZE(ddk, BP_GET_LSIZE(bp));
428eda14cbcSMatt Macy 	DDK_SET_PSIZE(ddk, BP_GET_PSIZE(bp));
429eda14cbcSMatt Macy 	DDK_SET_COMPRESS(ddk, BP_GET_COMPRESS(bp));
430eda14cbcSMatt Macy 	DDK_SET_CRYPT(ddk, BP_USES_CRYPT(bp));
431eda14cbcSMatt Macy }
432eda14cbcSMatt Macy 
433eda14cbcSMatt Macy void
ddt_phys_fill(ddt_phys_t * ddp,const blkptr_t * bp)434eda14cbcSMatt Macy ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp)
435eda14cbcSMatt Macy {
4364fefe1b7SMartin Matuska 	ASSERT0(ddp->ddp_phys_birth);
437eda14cbcSMatt Macy 
438eda14cbcSMatt Macy 	for (int d = 0; d < SPA_DVAS_PER_BP; d++)
439eda14cbcSMatt Macy 		ddp->ddp_dva[d] = bp->blk_dva[d];
440*783d3ff6SMartin Matuska 	ddp->ddp_phys_birth = BP_GET_BIRTH(bp);
441eda14cbcSMatt Macy }
442eda14cbcSMatt Macy 
443eda14cbcSMatt Macy void
ddt_phys_clear(ddt_phys_t * ddp)444eda14cbcSMatt Macy ddt_phys_clear(ddt_phys_t *ddp)
445eda14cbcSMatt Macy {
446da5137abSMartin Matuska 	memset(ddp, 0, sizeof (*ddp));
447eda14cbcSMatt Macy }
448eda14cbcSMatt Macy 
449eda14cbcSMatt Macy void
ddt_phys_addref(ddt_phys_t * ddp)450eda14cbcSMatt Macy ddt_phys_addref(ddt_phys_t *ddp)
451eda14cbcSMatt Macy {
452eda14cbcSMatt Macy 	ddp->ddp_refcnt++;
453eda14cbcSMatt Macy }
454eda14cbcSMatt Macy 
455eda14cbcSMatt Macy void
ddt_phys_decref(ddt_phys_t * ddp)456eda14cbcSMatt Macy ddt_phys_decref(ddt_phys_t *ddp)
457eda14cbcSMatt Macy {
458eda14cbcSMatt Macy 	if (ddp) {
4594fefe1b7SMartin Matuska 		ASSERT3U(ddp->ddp_refcnt, >, 0);
460eda14cbcSMatt Macy 		ddp->ddp_refcnt--;
461eda14cbcSMatt Macy 	}
462eda14cbcSMatt Macy }
463eda14cbcSMatt Macy 
4644fefe1b7SMartin Matuska static void
ddt_phys_free(ddt_t * ddt,ddt_key_t * ddk,ddt_phys_t * ddp,uint64_t txg)465eda14cbcSMatt Macy ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, uint64_t txg)
466eda14cbcSMatt Macy {
467eda14cbcSMatt Macy 	blkptr_t blk;
468eda14cbcSMatt Macy 
469eda14cbcSMatt Macy 	ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
470eda14cbcSMatt Macy 
471eda14cbcSMatt Macy 	/*
472eda14cbcSMatt Macy 	 * We clear the dedup bit so that zio_free() will actually free the
473eda14cbcSMatt Macy 	 * space, rather than just decrementing the refcount in the DDT.
474eda14cbcSMatt Macy 	 */
475eda14cbcSMatt Macy 	BP_SET_DEDUP(&blk, 0);
476eda14cbcSMatt Macy 
477eda14cbcSMatt Macy 	ddt_phys_clear(ddp);
478eda14cbcSMatt Macy 	zio_free(ddt->ddt_spa, txg, &blk);
479eda14cbcSMatt Macy }
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy ddt_phys_t *
ddt_phys_select(const ddt_entry_t * dde,const blkptr_t * bp)482eda14cbcSMatt Macy ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp)
483eda14cbcSMatt Macy {
484eda14cbcSMatt Macy 	ddt_phys_t *ddp = (ddt_phys_t *)dde->dde_phys;
485eda14cbcSMatt Macy 
486eda14cbcSMatt Macy 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
487eda14cbcSMatt Macy 		if (DVA_EQUAL(BP_IDENTITY(bp), &ddp->ddp_dva[0]) &&
488*783d3ff6SMartin Matuska 		    BP_GET_BIRTH(bp) == ddp->ddp_phys_birth)
489eda14cbcSMatt Macy 			return (ddp);
490eda14cbcSMatt Macy 	}
491eda14cbcSMatt Macy 	return (NULL);
492eda14cbcSMatt Macy }
493eda14cbcSMatt Macy 
494eda14cbcSMatt Macy uint64_t
ddt_phys_total_refcnt(const ddt_entry_t * dde)495eda14cbcSMatt Macy ddt_phys_total_refcnt(const ddt_entry_t *dde)
496eda14cbcSMatt Macy {
497eda14cbcSMatt Macy 	uint64_t refcnt = 0;
498eda14cbcSMatt Macy 
499eda14cbcSMatt Macy 	for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++)
500eda14cbcSMatt Macy 		refcnt += dde->dde_phys[p].ddp_refcnt;
501eda14cbcSMatt Macy 
502eda14cbcSMatt Macy 	return (refcnt);
503eda14cbcSMatt Macy }
504eda14cbcSMatt Macy 
505eda14cbcSMatt Macy ddt_t *
ddt_select(spa_t * spa,const blkptr_t * bp)506eda14cbcSMatt Macy ddt_select(spa_t *spa, const blkptr_t *bp)
507eda14cbcSMatt Macy {
5084fefe1b7SMartin Matuska 	ASSERT(DDT_CHECKSUM_VALID(BP_GET_CHECKSUM(bp)));
509eda14cbcSMatt Macy 	return (spa->spa_ddt[BP_GET_CHECKSUM(bp)]);
510eda14cbcSMatt Macy }
511eda14cbcSMatt Macy 
512eda14cbcSMatt Macy void
ddt_enter(ddt_t * ddt)513eda14cbcSMatt Macy ddt_enter(ddt_t *ddt)
514eda14cbcSMatt Macy {
515eda14cbcSMatt Macy 	mutex_enter(&ddt->ddt_lock);
516eda14cbcSMatt Macy }
517eda14cbcSMatt Macy 
518eda14cbcSMatt Macy void
ddt_exit(ddt_t * ddt)519eda14cbcSMatt Macy ddt_exit(ddt_t *ddt)
520eda14cbcSMatt Macy {
521eda14cbcSMatt Macy 	mutex_exit(&ddt->ddt_lock);
522eda14cbcSMatt Macy }
523eda14cbcSMatt Macy 
524eda14cbcSMatt Macy void
ddt_init(void)525eda14cbcSMatt Macy ddt_init(void)
526eda14cbcSMatt Macy {
527eda14cbcSMatt Macy 	ddt_cache = kmem_cache_create("ddt_cache",
528eda14cbcSMatt Macy 	    sizeof (ddt_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
529eda14cbcSMatt Macy 	ddt_entry_cache = kmem_cache_create("ddt_entry_cache",
530eda14cbcSMatt Macy 	    sizeof (ddt_entry_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
531eda14cbcSMatt Macy }
532eda14cbcSMatt Macy 
533eda14cbcSMatt Macy void
ddt_fini(void)534eda14cbcSMatt Macy ddt_fini(void)
535eda14cbcSMatt Macy {
536eda14cbcSMatt Macy 	kmem_cache_destroy(ddt_entry_cache);
537eda14cbcSMatt Macy 	kmem_cache_destroy(ddt_cache);
538eda14cbcSMatt Macy }
539eda14cbcSMatt Macy 
540eda14cbcSMatt Macy static ddt_entry_t *
ddt_alloc(const ddt_key_t * ddk)541eda14cbcSMatt Macy ddt_alloc(const ddt_key_t *ddk)
542eda14cbcSMatt Macy {
543eda14cbcSMatt Macy 	ddt_entry_t *dde;
544eda14cbcSMatt Macy 
545eda14cbcSMatt Macy 	dde = kmem_cache_alloc(ddt_entry_cache, KM_SLEEP);
546da5137abSMartin Matuska 	memset(dde, 0, sizeof (ddt_entry_t));
547eda14cbcSMatt Macy 	cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL);
548eda14cbcSMatt Macy 
549eda14cbcSMatt Macy 	dde->dde_key = *ddk;
550eda14cbcSMatt Macy 
551eda14cbcSMatt Macy 	return (dde);
552eda14cbcSMatt Macy }
553eda14cbcSMatt Macy 
554eda14cbcSMatt Macy static void
ddt_free(ddt_entry_t * dde)555eda14cbcSMatt Macy ddt_free(ddt_entry_t *dde)
556eda14cbcSMatt Macy {
5574fefe1b7SMartin Matuska 	ASSERT(dde->dde_flags & DDE_FLAG_LOADED);
558eda14cbcSMatt Macy 
559eda14cbcSMatt Macy 	for (int p = 0; p < DDT_PHYS_TYPES; p++)
5604fefe1b7SMartin Matuska 		ASSERT3P(dde->dde_lead_zio[p], ==, NULL);
561eda14cbcSMatt Macy 
562eda14cbcSMatt Macy 	if (dde->dde_repair_abd != NULL)
563eda14cbcSMatt Macy 		abd_free(dde->dde_repair_abd);
564eda14cbcSMatt Macy 
565eda14cbcSMatt Macy 	cv_destroy(&dde->dde_cv);
566eda14cbcSMatt Macy 	kmem_cache_free(ddt_entry_cache, dde);
567eda14cbcSMatt Macy }
568eda14cbcSMatt Macy 
569eda14cbcSMatt Macy void
ddt_remove(ddt_t * ddt,ddt_entry_t * dde)570eda14cbcSMatt Macy ddt_remove(ddt_t *ddt, ddt_entry_t *dde)
571eda14cbcSMatt Macy {
572eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&ddt->ddt_lock));
573eda14cbcSMatt Macy 
574eda14cbcSMatt Macy 	avl_remove(&ddt->ddt_tree, dde);
575eda14cbcSMatt Macy 	ddt_free(dde);
576eda14cbcSMatt Macy }
577eda14cbcSMatt Macy 
578eda14cbcSMatt Macy ddt_entry_t *
ddt_lookup(ddt_t * ddt,const blkptr_t * bp,boolean_t add)579eda14cbcSMatt Macy ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add)
580eda14cbcSMatt Macy {
5814fefe1b7SMartin Matuska 	ddt_key_t search;
5824fefe1b7SMartin Matuska 	ddt_entry_t *dde;
5834fefe1b7SMartin Matuska 	ddt_type_t type;
5844fefe1b7SMartin Matuska 	ddt_class_t class;
585eda14cbcSMatt Macy 	avl_index_t where;
586eda14cbcSMatt Macy 	int error;
587eda14cbcSMatt Macy 
588eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&ddt->ddt_lock));
589eda14cbcSMatt Macy 
5904fefe1b7SMartin Matuska 	ddt_key_fill(&search, bp);
591eda14cbcSMatt Macy 
5924fefe1b7SMartin Matuska 	/* Find an existing live entry */
5934fefe1b7SMartin Matuska 	dde = avl_find(&ddt->ddt_tree, &search, &where);
5944fefe1b7SMartin Matuska 	if (dde != NULL) {
5954fefe1b7SMartin Matuska 		/* Found it. If it's already loaded, we can just return it. */
5964fefe1b7SMartin Matuska 		if (dde->dde_flags & DDE_FLAG_LOADED)
597eda14cbcSMatt Macy 			return (dde);
598eda14cbcSMatt Macy 
5994fefe1b7SMartin Matuska 		/* Someone else is loading it, wait for it. */
6004fefe1b7SMartin Matuska 		while (!(dde->dde_flags & DDE_FLAG_LOADED))
6014fefe1b7SMartin Matuska 			cv_wait(&dde->dde_cv, &ddt->ddt_lock);
602eda14cbcSMatt Macy 
6034fefe1b7SMartin Matuska 		return (dde);
6044fefe1b7SMartin Matuska 	}
6054fefe1b7SMartin Matuska 
6064fefe1b7SMartin Matuska 	/* Not found. */
6074fefe1b7SMartin Matuska 	if (!add)
6084fefe1b7SMartin Matuska 		return (NULL);
6094fefe1b7SMartin Matuska 
6104fefe1b7SMartin Matuska 	/* Time to make a new entry. */
6114fefe1b7SMartin Matuska 	dde = ddt_alloc(&search);
6124fefe1b7SMartin Matuska 	avl_insert(&ddt->ddt_tree, dde, where);
6134fefe1b7SMartin Matuska 
6144fefe1b7SMartin Matuska 	/*
6154fefe1b7SMartin Matuska 	 * ddt_tree is now stable, so unlock and let everyone else keep moving.
6164fefe1b7SMartin Matuska 	 * Anyone landing on this entry will find it without DDE_FLAG_LOADED,
6174fefe1b7SMartin Matuska 	 * and go to sleep waiting for it above.
6184fefe1b7SMartin Matuska 	 */
619eda14cbcSMatt Macy 	ddt_exit(ddt);
620eda14cbcSMatt Macy 
6214fefe1b7SMartin Matuska 	/* Search all store objects for the entry. */
622eda14cbcSMatt Macy 	error = ENOENT;
623eda14cbcSMatt Macy 	for (type = 0; type < DDT_TYPES; type++) {
624eda14cbcSMatt Macy 		for (class = 0; class < DDT_CLASSES; class++) {
625eda14cbcSMatt Macy 			error = ddt_object_lookup(ddt, type, class, dde);
626eda14cbcSMatt Macy 			if (error != ENOENT) {
627eda14cbcSMatt Macy 				ASSERT0(error);
628eda14cbcSMatt Macy 				break;
629eda14cbcSMatt Macy 			}
630eda14cbcSMatt Macy 		}
631eda14cbcSMatt Macy 		if (error != ENOENT)
632eda14cbcSMatt Macy 			break;
633eda14cbcSMatt Macy 	}
634eda14cbcSMatt Macy 
635eda14cbcSMatt Macy 	ddt_enter(ddt);
636eda14cbcSMatt Macy 
6374fefe1b7SMartin Matuska 	ASSERT(!(dde->dde_flags & DDE_FLAG_LOADED));
638eda14cbcSMatt Macy 
639eda14cbcSMatt Macy 	dde->dde_type = type;	/* will be DDT_TYPES if no entry found */
640eda14cbcSMatt Macy 	dde->dde_class = class;	/* will be DDT_CLASSES if no entry found */
641eda14cbcSMatt Macy 
642eda14cbcSMatt Macy 	if (error == 0)
643eda14cbcSMatt Macy 		ddt_stat_update(ddt, dde, -1ULL);
644eda14cbcSMatt Macy 
6454fefe1b7SMartin Matuska 	/* Entry loaded, everyone can proceed now */
6464fefe1b7SMartin Matuska 	dde->dde_flags |= DDE_FLAG_LOADED;
647eda14cbcSMatt Macy 	cv_broadcast(&dde->dde_cv);
648eda14cbcSMatt Macy 
649eda14cbcSMatt Macy 	return (dde);
650eda14cbcSMatt Macy }
651eda14cbcSMatt Macy 
652eda14cbcSMatt Macy void
ddt_prefetch(spa_t * spa,const blkptr_t * bp)653eda14cbcSMatt Macy ddt_prefetch(spa_t *spa, const blkptr_t *bp)
654eda14cbcSMatt Macy {
655eda14cbcSMatt Macy 	ddt_t *ddt;
6564fefe1b7SMartin Matuska 	ddt_key_t ddk;
657eda14cbcSMatt Macy 
658eda14cbcSMatt Macy 	if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp))
659eda14cbcSMatt Macy 		return;
660eda14cbcSMatt Macy 
661eda14cbcSMatt Macy 	/*
662eda14cbcSMatt Macy 	 * We only remove the DDT once all tables are empty and only
663eda14cbcSMatt Macy 	 * prefetch dedup blocks when there are entries in the DDT.
664eda14cbcSMatt Macy 	 * Thus no locking is required as the DDT can't disappear on us.
665eda14cbcSMatt Macy 	 */
666eda14cbcSMatt Macy 	ddt = ddt_select(spa, bp);
6674fefe1b7SMartin Matuska 	ddt_key_fill(&ddk, bp);
668eda14cbcSMatt Macy 
6694fefe1b7SMartin Matuska 	for (ddt_type_t type = 0; type < DDT_TYPES; type++) {
6704fefe1b7SMartin Matuska 		for (ddt_class_t class = 0; class < DDT_CLASSES; class++) {
6714fefe1b7SMartin Matuska 			ddt_object_prefetch(ddt, type, class, &ddk);
672eda14cbcSMatt Macy 		}
673eda14cbcSMatt Macy 	}
674eda14cbcSMatt Macy }
675eda14cbcSMatt Macy 
676eda14cbcSMatt Macy /*
6774fefe1b7SMartin Matuska  * Key comparison. Any struct wanting to make use of this function must have
6784fefe1b7SMartin Matuska  * the key as the first element.
679eda14cbcSMatt Macy  */
680eda14cbcSMatt Macy #define	DDT_KEY_CMP_LEN	(sizeof (ddt_key_t) / sizeof (uint16_t))
681eda14cbcSMatt Macy 
682eda14cbcSMatt Macy typedef struct ddt_key_cmp {
683eda14cbcSMatt Macy 	uint16_t	u16[DDT_KEY_CMP_LEN];
684eda14cbcSMatt Macy } ddt_key_cmp_t;
685eda14cbcSMatt Macy 
686eda14cbcSMatt Macy int
ddt_key_compare(const void * x1,const void * x2)6874fefe1b7SMartin Matuska ddt_key_compare(const void *x1, const void *x2)
688eda14cbcSMatt Macy {
6894fefe1b7SMartin Matuska 	const ddt_key_cmp_t *k1 = (const ddt_key_cmp_t *)x1;
6904fefe1b7SMartin Matuska 	const ddt_key_cmp_t *k2 = (const ddt_key_cmp_t *)x2;
691eda14cbcSMatt Macy 	int32_t cmp = 0;
692eda14cbcSMatt Macy 
693eda14cbcSMatt Macy 	for (int i = 0; i < DDT_KEY_CMP_LEN; i++) {
694eda14cbcSMatt Macy 		cmp = (int32_t)k1->u16[i] - (int32_t)k2->u16[i];
695eda14cbcSMatt Macy 		if (likely(cmp))
696eda14cbcSMatt Macy 			break;
697eda14cbcSMatt Macy 	}
698eda14cbcSMatt Macy 
699eda14cbcSMatt Macy 	return (TREE_ISIGN(cmp));
700eda14cbcSMatt Macy }
701eda14cbcSMatt Macy 
702eda14cbcSMatt Macy static ddt_t *
ddt_table_alloc(spa_t * spa,enum zio_checksum c)703eda14cbcSMatt Macy ddt_table_alloc(spa_t *spa, enum zio_checksum c)
704eda14cbcSMatt Macy {
705eda14cbcSMatt Macy 	ddt_t *ddt;
706eda14cbcSMatt Macy 
707eda14cbcSMatt Macy 	ddt = kmem_cache_alloc(ddt_cache, KM_SLEEP);
708da5137abSMartin Matuska 	memset(ddt, 0, sizeof (ddt_t));
709eda14cbcSMatt Macy 
710eda14cbcSMatt Macy 	mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL);
7114fefe1b7SMartin Matuska 	avl_create(&ddt->ddt_tree, ddt_key_compare,
712eda14cbcSMatt Macy 	    sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
7134fefe1b7SMartin Matuska 	avl_create(&ddt->ddt_repair_tree, ddt_key_compare,
714eda14cbcSMatt Macy 	    sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node));
715eda14cbcSMatt Macy 	ddt->ddt_checksum = c;
716eda14cbcSMatt Macy 	ddt->ddt_spa = spa;
717eda14cbcSMatt Macy 	ddt->ddt_os = spa->spa_meta_objset;
718eda14cbcSMatt Macy 
719eda14cbcSMatt Macy 	return (ddt);
720eda14cbcSMatt Macy }
721eda14cbcSMatt Macy 
722eda14cbcSMatt Macy static void
ddt_table_free(ddt_t * ddt)723eda14cbcSMatt Macy ddt_table_free(ddt_t *ddt)
724eda14cbcSMatt Macy {
7254fefe1b7SMartin Matuska 	ASSERT0(avl_numnodes(&ddt->ddt_tree));
7264fefe1b7SMartin Matuska 	ASSERT0(avl_numnodes(&ddt->ddt_repair_tree));
727eda14cbcSMatt Macy 	avl_destroy(&ddt->ddt_tree);
728eda14cbcSMatt Macy 	avl_destroy(&ddt->ddt_repair_tree);
729eda14cbcSMatt Macy 	mutex_destroy(&ddt->ddt_lock);
730eda14cbcSMatt Macy 	kmem_cache_free(ddt_cache, ddt);
731eda14cbcSMatt Macy }
732eda14cbcSMatt Macy 
733eda14cbcSMatt Macy void
ddt_create(spa_t * spa)734eda14cbcSMatt Macy ddt_create(spa_t *spa)
735eda14cbcSMatt Macy {
736eda14cbcSMatt Macy 	spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM;
737eda14cbcSMatt Macy 
7384fefe1b7SMartin Matuska 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
7394fefe1b7SMartin Matuska 		if (DDT_CHECKSUM_VALID(c))
740eda14cbcSMatt Macy 			spa->spa_ddt[c] = ddt_table_alloc(spa, c);
741eda14cbcSMatt Macy 	}
7424fefe1b7SMartin Matuska }
743eda14cbcSMatt Macy 
744eda14cbcSMatt Macy int
ddt_load(spa_t * spa)745eda14cbcSMatt Macy ddt_load(spa_t *spa)
746eda14cbcSMatt Macy {
747eda14cbcSMatt Macy 	int error;
748eda14cbcSMatt Macy 
749eda14cbcSMatt Macy 	ddt_create(spa);
750eda14cbcSMatt Macy 
751eda14cbcSMatt Macy 	error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
752eda14cbcSMatt Macy 	    DMU_POOL_DDT_STATS, sizeof (uint64_t), 1,
753eda14cbcSMatt Macy 	    &spa->spa_ddt_stat_object);
754eda14cbcSMatt Macy 
755eda14cbcSMatt Macy 	if (error)
756eda14cbcSMatt Macy 		return (error == ENOENT ? 0 : error);
757eda14cbcSMatt Macy 
758eda14cbcSMatt Macy 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
7594fefe1b7SMartin Matuska 		if (!DDT_CHECKSUM_VALID(c))
7604fefe1b7SMartin Matuska 			continue;
7614fefe1b7SMartin Matuska 
762eda14cbcSMatt Macy 		ddt_t *ddt = spa->spa_ddt[c];
7634fefe1b7SMartin Matuska 		for (ddt_type_t type = 0; type < DDT_TYPES; type++) {
7644fefe1b7SMartin Matuska 			for (ddt_class_t class = 0; class < DDT_CLASSES;
765eda14cbcSMatt Macy 			    class++) {
766eda14cbcSMatt Macy 				error = ddt_object_load(ddt, type, class);
767eda14cbcSMatt Macy 				if (error != 0 && error != ENOENT)
768eda14cbcSMatt Macy 					return (error);
769eda14cbcSMatt Macy 			}
770eda14cbcSMatt Macy 		}
771eda14cbcSMatt Macy 
772eda14cbcSMatt Macy 		/*
773eda14cbcSMatt Macy 		 * Seed the cached histograms.
774eda14cbcSMatt Macy 		 */
775da5137abSMartin Matuska 		memcpy(&ddt->ddt_histogram_cache, ddt->ddt_histogram,
776eda14cbcSMatt Macy 		    sizeof (ddt->ddt_histogram));
777eda14cbcSMatt Macy 		spa->spa_dedup_dspace = ~0ULL;
778eda14cbcSMatt Macy 	}
779eda14cbcSMatt Macy 
780eda14cbcSMatt Macy 	return (0);
781eda14cbcSMatt Macy }
782eda14cbcSMatt Macy 
783eda14cbcSMatt Macy void
ddt_unload(spa_t * spa)784eda14cbcSMatt Macy ddt_unload(spa_t *spa)
785eda14cbcSMatt Macy {
786eda14cbcSMatt Macy 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
787eda14cbcSMatt Macy 		if (spa->spa_ddt[c]) {
788eda14cbcSMatt Macy 			ddt_table_free(spa->spa_ddt[c]);
789eda14cbcSMatt Macy 			spa->spa_ddt[c] = NULL;
790eda14cbcSMatt Macy 		}
791eda14cbcSMatt Macy 	}
792eda14cbcSMatt Macy }
793eda14cbcSMatt Macy 
794eda14cbcSMatt Macy boolean_t
ddt_class_contains(spa_t * spa,ddt_class_t max_class,const blkptr_t * bp)7954fefe1b7SMartin Matuska ddt_class_contains(spa_t *spa, ddt_class_t max_class, const blkptr_t *bp)
796eda14cbcSMatt Macy {
797eda14cbcSMatt Macy 	ddt_t *ddt;
7984fefe1b7SMartin Matuska 	ddt_key_t ddk;
799eda14cbcSMatt Macy 
800eda14cbcSMatt Macy 	if (!BP_GET_DEDUP(bp))
801eda14cbcSMatt Macy 		return (B_FALSE);
802eda14cbcSMatt Macy 
803eda14cbcSMatt Macy 	if (max_class == DDT_CLASS_UNIQUE)
804eda14cbcSMatt Macy 		return (B_TRUE);
805eda14cbcSMatt Macy 
806eda14cbcSMatt Macy 	ddt = spa->spa_ddt[BP_GET_CHECKSUM(bp)];
807eda14cbcSMatt Macy 
8084fefe1b7SMartin Matuska 	ddt_key_fill(&ddk, bp);
809eda14cbcSMatt Macy 
8104fefe1b7SMartin Matuska 	for (ddt_type_t type = 0; type < DDT_TYPES; type++) {
8114fefe1b7SMartin Matuska 		for (ddt_class_t class = 0; class <= max_class; class++) {
8124fefe1b7SMartin Matuska 			if (ddt_object_contains(ddt, type, class, &ddk) == 0)
813eda14cbcSMatt Macy 				return (B_TRUE);
814eda14cbcSMatt Macy 		}
815eda14cbcSMatt Macy 	}
816eda14cbcSMatt Macy 
817eda14cbcSMatt Macy 	return (B_FALSE);
818eda14cbcSMatt Macy }
819eda14cbcSMatt Macy 
820eda14cbcSMatt Macy ddt_entry_t *
ddt_repair_start(ddt_t * ddt,const blkptr_t * bp)821eda14cbcSMatt Macy ddt_repair_start(ddt_t *ddt, const blkptr_t *bp)
822eda14cbcSMatt Macy {
823eda14cbcSMatt Macy 	ddt_key_t ddk;
824eda14cbcSMatt Macy 	ddt_entry_t *dde;
825eda14cbcSMatt Macy 
826eda14cbcSMatt Macy 	ddt_key_fill(&ddk, bp);
827eda14cbcSMatt Macy 
828eda14cbcSMatt Macy 	dde = ddt_alloc(&ddk);
829eda14cbcSMatt Macy 
8304fefe1b7SMartin Matuska 	for (ddt_type_t type = 0; type < DDT_TYPES; type++) {
8314fefe1b7SMartin Matuska 		for (ddt_class_t class = 0; class < DDT_CLASSES; class++) {
832eda14cbcSMatt Macy 			/*
833eda14cbcSMatt Macy 			 * We can only do repair if there are multiple copies
834eda14cbcSMatt Macy 			 * of the block.  For anything in the UNIQUE class,
835eda14cbcSMatt Macy 			 * there's definitely only one copy, so don't even try.
836eda14cbcSMatt Macy 			 */
837eda14cbcSMatt Macy 			if (class != DDT_CLASS_UNIQUE &&
838eda14cbcSMatt Macy 			    ddt_object_lookup(ddt, type, class, dde) == 0)
839eda14cbcSMatt Macy 				return (dde);
840eda14cbcSMatt Macy 		}
841eda14cbcSMatt Macy 	}
842eda14cbcSMatt Macy 
843da5137abSMartin Matuska 	memset(dde->dde_phys, 0, sizeof (dde->dde_phys));
844eda14cbcSMatt Macy 
845eda14cbcSMatt Macy 	return (dde);
846eda14cbcSMatt Macy }
847eda14cbcSMatt Macy 
848eda14cbcSMatt Macy void
ddt_repair_done(ddt_t * ddt,ddt_entry_t * dde)849eda14cbcSMatt Macy ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde)
850eda14cbcSMatt Macy {
851eda14cbcSMatt Macy 	avl_index_t where;
852eda14cbcSMatt Macy 
853eda14cbcSMatt Macy 	ddt_enter(ddt);
854eda14cbcSMatt Macy 
855eda14cbcSMatt Macy 	if (dde->dde_repair_abd != NULL && spa_writeable(ddt->ddt_spa) &&
856eda14cbcSMatt Macy 	    avl_find(&ddt->ddt_repair_tree, dde, &where) == NULL)
857eda14cbcSMatt Macy 		avl_insert(&ddt->ddt_repair_tree, dde, where);
858eda14cbcSMatt Macy 	else
859eda14cbcSMatt Macy 		ddt_free(dde);
860eda14cbcSMatt Macy 
861eda14cbcSMatt Macy 	ddt_exit(ddt);
862eda14cbcSMatt Macy }
863eda14cbcSMatt Macy 
864eda14cbcSMatt Macy static void
ddt_repair_entry_done(zio_t * zio)865eda14cbcSMatt Macy ddt_repair_entry_done(zio_t *zio)
866eda14cbcSMatt Macy {
867eda14cbcSMatt Macy 	ddt_entry_t *rdde = zio->io_private;
868eda14cbcSMatt Macy 
869eda14cbcSMatt Macy 	ddt_free(rdde);
870eda14cbcSMatt Macy }
871eda14cbcSMatt Macy 
872eda14cbcSMatt Macy static void
ddt_repair_entry(ddt_t * ddt,ddt_entry_t * dde,ddt_entry_t * rdde,zio_t * rio)873eda14cbcSMatt Macy ddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio)
874eda14cbcSMatt Macy {
875eda14cbcSMatt Macy 	ddt_phys_t *ddp = dde->dde_phys;
876eda14cbcSMatt Macy 	ddt_phys_t *rddp = rdde->dde_phys;
877eda14cbcSMatt Macy 	ddt_key_t *ddk = &dde->dde_key;
878eda14cbcSMatt Macy 	ddt_key_t *rddk = &rdde->dde_key;
879eda14cbcSMatt Macy 	zio_t *zio;
880eda14cbcSMatt Macy 	blkptr_t blk;
881eda14cbcSMatt Macy 
882eda14cbcSMatt Macy 	zio = zio_null(rio, rio->io_spa, NULL,
883eda14cbcSMatt Macy 	    ddt_repair_entry_done, rdde, rio->io_flags);
884eda14cbcSMatt Macy 
885eda14cbcSMatt Macy 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) {
886eda14cbcSMatt Macy 		if (ddp->ddp_phys_birth == 0 ||
887eda14cbcSMatt Macy 		    ddp->ddp_phys_birth != rddp->ddp_phys_birth ||
888da5137abSMartin Matuska 		    memcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva)))
889eda14cbcSMatt Macy 			continue;
890eda14cbcSMatt Macy 		ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk);
891eda14cbcSMatt Macy 		zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk,
892eda14cbcSMatt Macy 		    rdde->dde_repair_abd, DDK_GET_PSIZE(rddk), NULL, NULL,
893eda14cbcSMatt Macy 		    ZIO_PRIORITY_SYNC_WRITE, ZIO_DDT_CHILD_FLAGS(zio), NULL));
894eda14cbcSMatt Macy 	}
895eda14cbcSMatt Macy 
896eda14cbcSMatt Macy 	zio_nowait(zio);
897eda14cbcSMatt Macy }
898eda14cbcSMatt Macy 
899eda14cbcSMatt Macy static void
ddt_repair_table(ddt_t * ddt,zio_t * rio)900eda14cbcSMatt Macy ddt_repair_table(ddt_t *ddt, zio_t *rio)
901eda14cbcSMatt Macy {
902eda14cbcSMatt Macy 	spa_t *spa = ddt->ddt_spa;
903eda14cbcSMatt Macy 	ddt_entry_t *dde, *rdde_next, *rdde;
904eda14cbcSMatt Macy 	avl_tree_t *t = &ddt->ddt_repair_tree;
905eda14cbcSMatt Macy 	blkptr_t blk;
906eda14cbcSMatt Macy 
907eda14cbcSMatt Macy 	if (spa_sync_pass(spa) > 1)
908eda14cbcSMatt Macy 		return;
909eda14cbcSMatt Macy 
910eda14cbcSMatt Macy 	ddt_enter(ddt);
911eda14cbcSMatt Macy 	for (rdde = avl_first(t); rdde != NULL; rdde = rdde_next) {
912eda14cbcSMatt Macy 		rdde_next = AVL_NEXT(t, rdde);
913eda14cbcSMatt Macy 		avl_remove(&ddt->ddt_repair_tree, rdde);
914eda14cbcSMatt Macy 		ddt_exit(ddt);
915eda14cbcSMatt Macy 		ddt_bp_create(ddt->ddt_checksum, &rdde->dde_key, NULL, &blk);
916eda14cbcSMatt Macy 		dde = ddt_repair_start(ddt, &blk);
917eda14cbcSMatt Macy 		ddt_repair_entry(ddt, dde, rdde, rio);
918eda14cbcSMatt Macy 		ddt_repair_done(ddt, dde);
919eda14cbcSMatt Macy 		ddt_enter(ddt);
920eda14cbcSMatt Macy 	}
921eda14cbcSMatt Macy 	ddt_exit(ddt);
922eda14cbcSMatt Macy }
923eda14cbcSMatt Macy 
924eda14cbcSMatt Macy static void
ddt_sync_entry(ddt_t * ddt,ddt_entry_t * dde,dmu_tx_t * tx,uint64_t txg)925eda14cbcSMatt Macy ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg)
926eda14cbcSMatt Macy {
927eda14cbcSMatt Macy 	dsl_pool_t *dp = ddt->ddt_spa->spa_dsl_pool;
928eda14cbcSMatt Macy 	ddt_phys_t *ddp = dde->dde_phys;
929eda14cbcSMatt Macy 	ddt_key_t *ddk = &dde->dde_key;
9304fefe1b7SMartin Matuska 	ddt_type_t otype = dde->dde_type;
9314fefe1b7SMartin Matuska 	ddt_type_t ntype = DDT_TYPE_DEFAULT;
9324fefe1b7SMartin Matuska 	ddt_class_t oclass = dde->dde_class;
9334fefe1b7SMartin Matuska 	ddt_class_t nclass;
934eda14cbcSMatt Macy 	uint64_t total_refcnt = 0;
935eda14cbcSMatt Macy 
9364fefe1b7SMartin Matuska 	ASSERT(dde->dde_flags & DDE_FLAG_LOADED);
937eda14cbcSMatt Macy 
938eda14cbcSMatt Macy 	for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) {
9394fefe1b7SMartin Matuska 		ASSERT3P(dde->dde_lead_zio[p], ==, NULL);
940eda14cbcSMatt Macy 		if (ddp->ddp_phys_birth == 0) {
9414fefe1b7SMartin Matuska 			ASSERT0(ddp->ddp_refcnt);
942eda14cbcSMatt Macy 			continue;
943eda14cbcSMatt Macy 		}
944eda14cbcSMatt Macy 		if (p == DDT_PHYS_DITTO) {
945eda14cbcSMatt Macy 			/*
946eda14cbcSMatt Macy 			 * Note, we no longer create DDT-DITTO blocks, but we
947eda14cbcSMatt Macy 			 * don't want to leak any written by older software.
948eda14cbcSMatt Macy 			 */
949eda14cbcSMatt Macy 			ddt_phys_free(ddt, ddk, ddp, txg);
950eda14cbcSMatt Macy 			continue;
951eda14cbcSMatt Macy 		}
952eda14cbcSMatt Macy 		if (ddp->ddp_refcnt == 0)
953eda14cbcSMatt Macy 			ddt_phys_free(ddt, ddk, ddp, txg);
954eda14cbcSMatt Macy 		total_refcnt += ddp->ddp_refcnt;
955eda14cbcSMatt Macy 	}
956eda14cbcSMatt Macy 
957eda14cbcSMatt Macy 	/* We do not create new DDT-DITTO blocks. */
958eda14cbcSMatt Macy 	ASSERT0(dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth);
959eda14cbcSMatt Macy 	if (total_refcnt > 1)
960eda14cbcSMatt Macy 		nclass = DDT_CLASS_DUPLICATE;
961eda14cbcSMatt Macy 	else
962eda14cbcSMatt Macy 		nclass = DDT_CLASS_UNIQUE;
963eda14cbcSMatt Macy 
964eda14cbcSMatt Macy 	if (otype != DDT_TYPES &&
965eda14cbcSMatt Macy 	    (otype != ntype || oclass != nclass || total_refcnt == 0)) {
9664fefe1b7SMartin Matuska 		VERIFY0(ddt_object_remove(ddt, otype, oclass, ddk, tx));
9674fefe1b7SMartin Matuska 		ASSERT3U(
9684fefe1b7SMartin Matuska 		    ddt_object_contains(ddt, otype, oclass, ddk), ==, ENOENT);
969eda14cbcSMatt Macy 	}
970eda14cbcSMatt Macy 
971eda14cbcSMatt Macy 	if (total_refcnt != 0) {
972eda14cbcSMatt Macy 		dde->dde_type = ntype;
973eda14cbcSMatt Macy 		dde->dde_class = nclass;
974eda14cbcSMatt Macy 		ddt_stat_update(ddt, dde, 0);
975eda14cbcSMatt Macy 		if (!ddt_object_exists(ddt, ntype, nclass))
976eda14cbcSMatt Macy 			ddt_object_create(ddt, ntype, nclass, tx);
9774fefe1b7SMartin Matuska 		VERIFY0(ddt_object_update(ddt, ntype, nclass, dde, tx));
978eda14cbcSMatt Macy 
979eda14cbcSMatt Macy 		/*
980eda14cbcSMatt Macy 		 * If the class changes, the order that we scan this bp
981eda14cbcSMatt Macy 		 * changes.  If it decreases, we could miss it, so
982eda14cbcSMatt Macy 		 * scan it right now.  (This covers both class changing
983eda14cbcSMatt Macy 		 * while we are doing ddt_walk(), and when we are
984eda14cbcSMatt Macy 		 * traversing.)
985eda14cbcSMatt Macy 		 */
986eda14cbcSMatt Macy 		if (nclass < oclass) {
987eda14cbcSMatt Macy 			dsl_scan_ddt_entry(dp->dp_scan,
988eda14cbcSMatt Macy 			    ddt->ddt_checksum, dde, tx);
989eda14cbcSMatt Macy 		}
990eda14cbcSMatt Macy 	}
991eda14cbcSMatt Macy }
992eda14cbcSMatt Macy 
993eda14cbcSMatt Macy static void
ddt_sync_table(ddt_t * ddt,dmu_tx_t * tx,uint64_t txg)994eda14cbcSMatt Macy ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg)
995eda14cbcSMatt Macy {
996eda14cbcSMatt Macy 	spa_t *spa = ddt->ddt_spa;
997eda14cbcSMatt Macy 	ddt_entry_t *dde;
998eda14cbcSMatt Macy 	void *cookie = NULL;
999eda14cbcSMatt Macy 
1000eda14cbcSMatt Macy 	if (avl_numnodes(&ddt->ddt_tree) == 0)
1001eda14cbcSMatt Macy 		return;
1002eda14cbcSMatt Macy 
10034fefe1b7SMartin Matuska 	ASSERT3U(spa->spa_uberblock.ub_version, >=, SPA_VERSION_DEDUP);
1004eda14cbcSMatt Macy 
1005eda14cbcSMatt Macy 	if (spa->spa_ddt_stat_object == 0) {
1006eda14cbcSMatt Macy 		spa->spa_ddt_stat_object = zap_create_link(ddt->ddt_os,
1007eda14cbcSMatt Macy 		    DMU_OT_DDT_STATS, DMU_POOL_DIRECTORY_OBJECT,
1008eda14cbcSMatt Macy 		    DMU_POOL_DDT_STATS, tx);
1009eda14cbcSMatt Macy 	}
1010eda14cbcSMatt Macy 
1011eda14cbcSMatt Macy 	while ((dde = avl_destroy_nodes(&ddt->ddt_tree, &cookie)) != NULL) {
1012eda14cbcSMatt Macy 		ddt_sync_entry(ddt, dde, tx, txg);
1013eda14cbcSMatt Macy 		ddt_free(dde);
1014eda14cbcSMatt Macy 	}
1015eda14cbcSMatt Macy 
10164fefe1b7SMartin Matuska 	for (ddt_type_t type = 0; type < DDT_TYPES; type++) {
1017eda14cbcSMatt Macy 		uint64_t add, count = 0;
10184fefe1b7SMartin Matuska 		for (ddt_class_t class = 0; class < DDT_CLASSES; class++) {
1019eda14cbcSMatt Macy 			if (ddt_object_exists(ddt, type, class)) {
1020eda14cbcSMatt Macy 				ddt_object_sync(ddt, type, class, tx);
10214fefe1b7SMartin Matuska 				VERIFY0(ddt_object_count(ddt, type, class,
10224fefe1b7SMartin Matuska 				    &add));
1023eda14cbcSMatt Macy 				count += add;
1024eda14cbcSMatt Macy 			}
1025eda14cbcSMatt Macy 		}
10264fefe1b7SMartin Matuska 		for (ddt_class_t class = 0; class < DDT_CLASSES; class++) {
1027eda14cbcSMatt Macy 			if (count == 0 && ddt_object_exists(ddt, type, class))
1028eda14cbcSMatt Macy 				ddt_object_destroy(ddt, type, class, tx);
1029eda14cbcSMatt Macy 		}
1030eda14cbcSMatt Macy 	}
1031eda14cbcSMatt Macy 
1032da5137abSMartin Matuska 	memcpy(&ddt->ddt_histogram_cache, ddt->ddt_histogram,
1033eda14cbcSMatt Macy 	    sizeof (ddt->ddt_histogram));
1034eda14cbcSMatt Macy 	spa->spa_dedup_dspace = ~0ULL;
1035eda14cbcSMatt Macy }
1036eda14cbcSMatt Macy 
1037eda14cbcSMatt Macy void
ddt_sync(spa_t * spa,uint64_t txg)1038eda14cbcSMatt Macy ddt_sync(spa_t *spa, uint64_t txg)
1039eda14cbcSMatt Macy {
1040eda14cbcSMatt Macy 	dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1041eda14cbcSMatt Macy 	dmu_tx_t *tx;
1042eda14cbcSMatt Macy 	zio_t *rio;
1043eda14cbcSMatt Macy 
10444fefe1b7SMartin Matuska 	ASSERT3U(spa_syncing_txg(spa), ==, txg);
1045eda14cbcSMatt Macy 
1046eda14cbcSMatt Macy 	tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1047eda14cbcSMatt Macy 
1048eda14cbcSMatt Macy 	rio = zio_root(spa, NULL, NULL,
1049eda14cbcSMatt Macy 	    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SELF_HEAL);
1050eda14cbcSMatt Macy 
1051eda14cbcSMatt Macy 	/*
1052eda14cbcSMatt Macy 	 * This function may cause an immediate scan of ddt blocks (see
1053eda14cbcSMatt Macy 	 * the comment above dsl_scan_ddt() for details). We set the
1054eda14cbcSMatt Macy 	 * scan's root zio here so that we can wait for any scan IOs in
1055eda14cbcSMatt Macy 	 * addition to the regular ddt IOs.
1056eda14cbcSMatt Macy 	 */
1057eda14cbcSMatt Macy 	ASSERT3P(scn->scn_zio_root, ==, NULL);
1058eda14cbcSMatt Macy 	scn->scn_zio_root = rio;
1059eda14cbcSMatt Macy 
1060eda14cbcSMatt Macy 	for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) {
1061eda14cbcSMatt Macy 		ddt_t *ddt = spa->spa_ddt[c];
1062eda14cbcSMatt Macy 		if (ddt == NULL)
1063eda14cbcSMatt Macy 			continue;
1064eda14cbcSMatt Macy 		ddt_sync_table(ddt, tx, txg);
1065eda14cbcSMatt Macy 		ddt_repair_table(ddt, rio);
1066eda14cbcSMatt Macy 	}
1067eda14cbcSMatt Macy 
1068eda14cbcSMatt Macy 	(void) zio_wait(rio);
1069eda14cbcSMatt Macy 	scn->scn_zio_root = NULL;
1070eda14cbcSMatt Macy 
1071eda14cbcSMatt Macy 	dmu_tx_commit(tx);
1072eda14cbcSMatt Macy }
1073eda14cbcSMatt Macy 
1074eda14cbcSMatt Macy int
ddt_walk(spa_t * spa,ddt_bookmark_t * ddb,ddt_entry_t * dde)1075eda14cbcSMatt Macy ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde)
1076eda14cbcSMatt Macy {
1077eda14cbcSMatt Macy 	do {
1078eda14cbcSMatt Macy 		do {
1079eda14cbcSMatt Macy 			do {
1080eda14cbcSMatt Macy 				ddt_t *ddt = spa->spa_ddt[ddb->ddb_checksum];
10814fefe1b7SMartin Matuska 				if (ddt == NULL)
10824fefe1b7SMartin Matuska 					continue;
1083eda14cbcSMatt Macy 				int error = ENOENT;
1084eda14cbcSMatt Macy 				if (ddt_object_exists(ddt, ddb->ddb_type,
1085eda14cbcSMatt Macy 				    ddb->ddb_class)) {
1086eda14cbcSMatt Macy 					error = ddt_object_walk(ddt,
1087eda14cbcSMatt Macy 					    ddb->ddb_type, ddb->ddb_class,
1088eda14cbcSMatt Macy 					    &ddb->ddb_cursor, dde);
1089eda14cbcSMatt Macy 				}
1090eda14cbcSMatt Macy 				dde->dde_type = ddb->ddb_type;
1091eda14cbcSMatt Macy 				dde->dde_class = ddb->ddb_class;
1092eda14cbcSMatt Macy 				if (error == 0)
1093eda14cbcSMatt Macy 					return (0);
1094eda14cbcSMatt Macy 				if (error != ENOENT)
1095eda14cbcSMatt Macy 					return (error);
1096eda14cbcSMatt Macy 				ddb->ddb_cursor = 0;
1097eda14cbcSMatt Macy 			} while (++ddb->ddb_checksum < ZIO_CHECKSUM_FUNCTIONS);
1098eda14cbcSMatt Macy 			ddb->ddb_checksum = 0;
1099eda14cbcSMatt Macy 		} while (++ddb->ddb_type < DDT_TYPES);
1100eda14cbcSMatt Macy 		ddb->ddb_type = 0;
1101eda14cbcSMatt Macy 	} while (++ddb->ddb_class < DDT_CLASSES);
1102eda14cbcSMatt Macy 
1103eda14cbcSMatt Macy 	return (SET_ERROR(ENOENT));
1104eda14cbcSMatt Macy }
1105eda14cbcSMatt Macy 
11062a58b312SMartin Matuska /*
11072a58b312SMartin Matuska  * This function is used by Block Cloning (brt.c) to increase reference
11082a58b312SMartin Matuska  * counter for the DDT entry if the block is already in DDT.
11092a58b312SMartin Matuska  *
11102a58b312SMartin Matuska  * Return false if the block, despite having the D bit set, is not present
11112a58b312SMartin Matuska  * in the DDT. Currently this is not possible but might be in the future.
11122a58b312SMartin Matuska  * See the comment below.
11132a58b312SMartin Matuska  */
11142a58b312SMartin Matuska boolean_t
ddt_addref(spa_t * spa,const blkptr_t * bp)11152a58b312SMartin Matuska ddt_addref(spa_t *spa, const blkptr_t *bp)
11162a58b312SMartin Matuska {
11172a58b312SMartin Matuska 	ddt_t *ddt;
11182a58b312SMartin Matuska 	ddt_entry_t *dde;
11192a58b312SMartin Matuska 	boolean_t result;
11202a58b312SMartin Matuska 
11212a58b312SMartin Matuska 	spa_config_enter(spa, SCL_ZIO, FTAG, RW_READER);
11222a58b312SMartin Matuska 	ddt = ddt_select(spa, bp);
11232a58b312SMartin Matuska 	ddt_enter(ddt);
11242a58b312SMartin Matuska 
11252a58b312SMartin Matuska 	dde = ddt_lookup(ddt, bp, B_TRUE);
11264fefe1b7SMartin Matuska 	ASSERT3P(dde, !=, NULL);
11272a58b312SMartin Matuska 
11282a58b312SMartin Matuska 	if (dde->dde_type < DDT_TYPES) {
11292a58b312SMartin Matuska 		ddt_phys_t *ddp;
11302a58b312SMartin Matuska 
11312a58b312SMartin Matuska 		ASSERT3S(dde->dde_class, <, DDT_CLASSES);
11322a58b312SMartin Matuska 
11332a58b312SMartin Matuska 		ddp = &dde->dde_phys[BP_GET_NDVAS(bp)];
11340a97523dSMartin Matuska 
11350a97523dSMartin Matuska 		/*
11360a97523dSMartin Matuska 		 * This entry already existed (dde_type is real), so it must
11370a97523dSMartin Matuska 		 * have refcnt >0 at the start of this txg. We are called from
11380a97523dSMartin Matuska 		 * brt_pending_apply(), before frees are issued, so the refcnt
11390a97523dSMartin Matuska 		 * can't be lowered yet. Therefore, it must be >0. We assert
11400a97523dSMartin Matuska 		 * this because if the order of BRT and DDT interactions were
11410a97523dSMartin Matuska 		 * ever to change and the refcnt was ever zero here, then
11420a97523dSMartin Matuska 		 * likely further action is required to fill out the DDT entry,
11430a97523dSMartin Matuska 		 * and this is a place that is likely to be missed in testing.
11440a97523dSMartin Matuska 		 */
11450a97523dSMartin Matuska 		ASSERT3U(ddp->ddp_refcnt, >, 0);
11460a97523dSMartin Matuska 
11472a58b312SMartin Matuska 		ddt_phys_addref(ddp);
11482a58b312SMartin Matuska 		result = B_TRUE;
11492a58b312SMartin Matuska 	} else {
11502a58b312SMartin Matuska 		/*
11512a58b312SMartin Matuska 		 * At the time of implementating this if the block has the
11522a58b312SMartin Matuska 		 * DEDUP flag set it must exist in the DEDUP table, but
11532a58b312SMartin Matuska 		 * there are many advocates that want ability to remove
11542a58b312SMartin Matuska 		 * entries from DDT with refcnt=1. If this will happen,
11552a58b312SMartin Matuska 		 * we may have a block with the DEDUP set, but which doesn't
11562a58b312SMartin Matuska 		 * have a corresponding entry in the DDT. Be ready.
11572a58b312SMartin Matuska 		 */
11582a58b312SMartin Matuska 		ASSERT3S(dde->dde_class, ==, DDT_CLASSES);
11592a58b312SMartin Matuska 		ddt_remove(ddt, dde);
11602a58b312SMartin Matuska 		result = B_FALSE;
11612a58b312SMartin Matuska 	}
11622a58b312SMartin Matuska 
11632a58b312SMartin Matuska 	ddt_exit(ddt);
11642a58b312SMartin Matuska 	spa_config_exit(spa, SCL_ZIO, FTAG);
11652a58b312SMartin Matuska 
11662a58b312SMartin Matuska 	return (result);
11672a58b312SMartin Matuska }
11682a58b312SMartin Matuska 
1169eda14cbcSMatt Macy ZFS_MODULE_PARAM(zfs_dedup, zfs_dedup_, prefetch, INT, ZMOD_RW,
1170eda14cbcSMatt Macy 	"Enable prefetching dedup-ed blks");
1171