1b24ab67Jeff Bonwick/*
2b24ab67Jeff Bonwick * CDDL HEADER START
3b24ab67Jeff Bonwick *
4b24ab67Jeff Bonwick * The contents of this file are subject to the terms of the
5b24ab67Jeff Bonwick * Common Development and Distribution License (the "License").
6b24ab67Jeff Bonwick * You may not use this file except in compliance with the License.
7b24ab67Jeff Bonwick *
8b24ab67Jeff Bonwick * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9b24ab67Jeff Bonwick * or http://www.opensolaris.org/os/licensing.
10b24ab67Jeff Bonwick * See the License for the specific language governing permissions
11b24ab67Jeff Bonwick * and limitations under the License.
12b24ab67Jeff Bonwick *
13b24ab67Jeff Bonwick * When distributing Covered Code, include this CDDL HEADER in each
14b24ab67Jeff Bonwick * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15b24ab67Jeff Bonwick * If applicable, add the following below this CDDL HEADER, with the
16b24ab67Jeff Bonwick * fields enclosed by brackets "[]" replaced with your own identifying
17b24ab67Jeff Bonwick * information: Portions Copyright [yyyy] [name of copyright owner]
18b24ab67Jeff Bonwick *
19b24ab67Jeff Bonwick * CDDL HEADER END
20b24ab67Jeff Bonwick */
21b24ab67Jeff Bonwick/*
223f9d6adLin Ling * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23770499eDan Kimmel * Copyright (c) 2016 by Delphix. All rights reserved.
24b24ab67Jeff Bonwick */
25b24ab67Jeff Bonwick
26b24ab67Jeff Bonwick#ifndef _SYS_DDT_H
27b24ab67Jeff Bonwick#define	_SYS_DDT_H
28b24ab67Jeff Bonwick
29b24ab67Jeff Bonwick#include <sys/sysmacros.h>
30b24ab67Jeff Bonwick#include <sys/types.h>
31b24ab67Jeff Bonwick#include <sys/fs/zfs.h>
32b24ab67Jeff Bonwick#include <sys/zio.h>
33b24ab67Jeff Bonwick#include <sys/dmu.h>
34b24ab67Jeff Bonwick
35b24ab67Jeff Bonwick#ifdef	__cplusplus
36b24ab67Jeff Bonwickextern "C" {
37b24ab67Jeff Bonwick#endif
38b24ab67Jeff Bonwick
39770499eDan Kimmelstruct abd;
40770499eDan Kimmel
41b24ab67Jeff Bonwick/*
42b24ab67Jeff Bonwick * On-disk DDT formats, in the desired search order (newest version first).
43b24ab67Jeff Bonwick */
44b24ab67Jeff Bonwickenum ddt_type {
45b24ab67Jeff Bonwick	DDT_TYPE_ZAP = 0,
46b24ab67Jeff Bonwick	DDT_TYPES
47b24ab67Jeff Bonwick};
48b24ab67Jeff Bonwick
49b24ab67Jeff Bonwick/*
50b24ab67Jeff Bonwick * DDT classes, in the desired search order (highest replication level first).
51b24ab67Jeff Bonwick */
52b24ab67Jeff Bonwickenum ddt_class {
53b24ab67Jeff Bonwick	DDT_CLASS_DITTO = 0,
54b24ab67Jeff Bonwick	DDT_CLASS_DUPLICATE,
55b24ab67Jeff Bonwick	DDT_CLASS_UNIQUE,
56b24ab67Jeff Bonwick	DDT_CLASSES
57b24ab67Jeff Bonwick};
58b24ab67Jeff Bonwick
59b24ab67Jeff Bonwick#define	DDT_TYPE_CURRENT		0
60b24ab67Jeff Bonwick
61b24ab67Jeff Bonwick#define	DDT_COMPRESS_BYTEORDER_MASK	0x80
62b24ab67Jeff Bonwick#define	DDT_COMPRESS_FUNCTION_MASK	0x7f
63b24ab67Jeff Bonwick
64b24ab67Jeff Bonwick/*
65b24ab67Jeff Bonwick * On-disk ddt entry:  key (name) and physical storage (value).
66b24ab67Jeff Bonwick */
67b24ab67Jeff Bonwicktypedef struct ddt_key {
68b24ab67Jeff Bonwick	zio_cksum_t	ddk_cksum;	/* 256-bit block checksum */
69f717074Will Andrews	/*
70eb63303Tom Caputi	 * Encoded with logical & physical size, encryption, and compression,
71eb63303Tom Caputi	 * as follows:
72f717074Will Andrews	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
73eb63303Tom Caputi	 *   |   0   |   0   |   0   |X| comp|     PSIZE     |     LSIZE     |
74f717074Will Andrews	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
75f717074Will Andrews	 */
76f717074Will Andrews	uint64_t	ddk_prop;
77b24ab67Jeff Bonwick} ddt_key_t;
78b24ab67Jeff Bonwick
79b24ab67Jeff Bonwick#define	DDK_GET_LSIZE(ddk)	\
80b24ab67Jeff Bonwick	BF64_GET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1)
81b24ab67Jeff Bonwick#define	DDK_SET_LSIZE(ddk, x)	\
82b24ab67Jeff Bonwick	BF64_SET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
83b24ab67Jeff Bonwick
84b24ab67Jeff Bonwick#define	DDK_GET_PSIZE(ddk)	\
85b24ab67Jeff Bonwick	BF64_GET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
86b24ab67Jeff Bonwick#define	DDK_SET_PSIZE(ddk, x)	\
87b24ab67Jeff Bonwick	BF64_SET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
88b24ab67Jeff Bonwick
89eb63303Tom Caputi#define	DDK_GET_COMPRESS(ddk)		BF64_GET((ddk)->ddk_prop, 32, 7)
90eb63303Tom Caputi#define	DDK_SET_COMPRESS(ddk, x)	BF64_SET((ddk)->ddk_prop, 32, 7, x)
91eb63303Tom Caputi
92eb63303Tom Caputi#define	DDK_GET_CRYPT(ddk)		BF64_GET((ddk)->ddk_prop, 39, 1)
93eb63303Tom Caputi#define	DDK_SET_CRYPT(ddk, x)	BF64_SET((ddk)->ddk_prop, 39, 1, x)
94b24ab67Jeff Bonwick
95b24ab67Jeff Bonwick#define	DDT_KEY_WORDS	(sizeof (ddt_key_t) / sizeof (uint64_t))
96b24ab67Jeff Bonwick
97eb63303Tom Caputi#define	DDE_GET_NDVAS(dde) (DDK_GET_CRYPT(&dde->dde_key) \
98eb63303Tom Caputi	? SPA_DVAS_PER_BP - 1 : SPA_DVAS_PER_BP)
99eb63303Tom Caputi
100b24ab67Jeff Bonwicktypedef struct ddt_phys {
101b24ab67Jeff Bonwick	dva_t		ddp_dva[SPA_DVAS_PER_BP];
102b24ab67Jeff Bonwick	uint64_t	ddp_refcnt;
103b24ab67Jeff Bonwick	uint64_t	ddp_phys_birth;
104b24ab67Jeff Bonwick} ddt_phys_t;
105b24ab67Jeff Bonwick
106b24ab67Jeff Bonwickenum ddt_phys_type {
107b24ab67Jeff Bonwick	DDT_PHYS_DITTO = 0,
108b24ab67Jeff Bonwick	DDT_PHYS_SINGLE = 1,
109b24ab67Jeff Bonwick	DDT_PHYS_DOUBLE = 2,
110b24ab67Jeff Bonwick	DDT_PHYS_TRIPLE = 3,
111b24ab67Jeff Bonwick	DDT_PHYS_TYPES
11230f608aGeorge Wilson};
113b24ab67Jeff Bonwick
114b24ab67Jeff Bonwick/*
115b24ab67Jeff Bonwick * In-core ddt entry
116b24ab67Jeff Bonwick */
117b24ab67Jeff Bonwickstruct ddt_entry {
118b24ab67Jeff Bonwick	ddt_key_t	dde_key;
119b24ab67Jeff Bonwick	ddt_phys_t	dde_phys[DDT_PHYS_TYPES];
120b24ab67Jeff Bonwick	zio_t		*dde_lead_zio[DDT_PHYS_TYPES];
121770499eDan Kimmel	struct abd	*dde_repair_abd;
122b24ab67Jeff Bonwick	enum ddt_type	dde_type;
123b24ab67Jeff Bonwick	enum ddt_class	dde_class;
124b24ab67Jeff Bonwick	uint8_t		dde_loading;
125b24ab67Jeff Bonwick	uint8_t		dde_loaded;
126b24ab67Jeff Bonwick	kcondvar_t	dde_cv;
127b24ab67Jeff Bonwick	avl_node_t	dde_node;
128b24ab67Jeff Bonwick};
129b24ab67Jeff Bonwick
130b24ab67Jeff Bonwick/*
131b24ab67Jeff Bonwick * In-core ddt
132b24ab67Jeff Bonwick */
133b24ab67Jeff Bonwickstruct ddt {
134b24ab67Jeff Bonwick	kmutex_t	ddt_lock;
135b24ab67Jeff Bonwick	avl_tree_t	ddt_tree;
136b24ab67Jeff Bonwick	avl_tree_t	ddt_repair_tree;
137b24ab67Jeff Bonwick	enum zio_checksum ddt_checksum;
138b24ab67Jeff Bonwick	spa_t		*ddt_spa;
139b24ab67Jeff Bonwick	objset_t	*ddt_os;
140b24ab67Jeff Bonwick	uint64_t	ddt_stat_object;
141b24ab67Jeff Bonwick	uint64_t	ddt_object[DDT_TYPES][DDT_CLASSES];
142b24ab67Jeff Bonwick	ddt_histogram_t	ddt_histogram[DDT_TYPES][DDT_CLASSES];
14330f608aGeorge Wilson	ddt_histogram_t	ddt_histogram_cache[DDT_TYPES][DDT_CLASSES];
14430f608aGeorge Wilson	ddt_object_t	ddt_object_stats[DDT_TYPES][DDT_CLASSES];
145b24ab67Jeff Bonwick	avl_node_t	ddt_node;
146b24ab67Jeff Bonwick};
147b24ab67Jeff Bonwick
148bbfd46cJeff Bonwick/*
149bbfd46cJeff Bonwick * In-core and on-disk bookmark for DDT walks
150bbfd46cJeff Bonwick */
151bbfd46cJeff Bonwicktypedef struct ddt_bookmark {
152bbfd46cJeff Bonwick	uint64_t	ddb_class;
153bbfd46cJeff Bonwick	uint64_t	ddb_type;
154bbfd46cJeff Bonwick	uint64_t	ddb_checksum;
155bbfd46cJeff Bonwick	uint64_t	ddb_cursor;
156bbfd46cJeff Bonwick} ddt_bookmark_t;
157bbfd46cJeff Bonwick
158bbfd46cJeff Bonwick/*
159bbfd46cJeff Bonwick * Ops vector to access a specific DDT object type.
160bbfd46cJeff Bonwick */
161b24ab67Jeff Bonwicktypedef struct ddt_ops {
162b24ab67Jeff Bonwick	char ddt_op_name[32];
163b24ab67Jeff Bonwick	int (*ddt_op_create)(objset_t *os, uint64_t *object, dmu_tx_t *tx,
164b24ab67Jeff Bonwick	    boolean_t prehash);
165b24ab67Jeff Bonwick	int (*ddt_op_destroy)(objset_t *os, uint64_t object, dmu_tx_t *tx);
166b24ab67Jeff Bonwick	int (*ddt_op_lookup)(objset_t *os, uint64_t object, ddt_entry_t *dde);
167c7cd242George Wilson	void (*ddt_op_prefetch)(objset_t *os, uint64_t object,
168c7cd242George Wilson	    ddt_entry_t *dde);
169b24ab67Jeff Bonwick	int (*ddt_op_update)(objset_t *os, uint64_t object, ddt_entry_t *dde,
170b24ab67Jeff Bonwick	    dmu_tx_t *tx);
171b24ab67Jeff Bonwick	int (*ddt_op_remove)(objset_t *os, uint64_t object, ddt_entry_t *dde,
172b24ab67Jeff Bonwick	    dmu_tx_t *tx);
173b24ab67Jeff Bonwick	int (*ddt_op_walk)(objset_t *os, uint64_t object, ddt_entry_t *dde,
174b24ab67Jeff Bonwick	    uint64_t *walk);
175b24ab67Jeff Bonwick	uint64_t (*ddt_op_count)(objset_t *os, uint64_t object);
176b24ab67Jeff Bonwick} ddt_ops_t;
177b24ab67Jeff Bonwick
178b24ab67Jeff Bonwick#define	DDT_NAMELEN	80
179b24ab67Jeff Bonwick
180b24ab67Jeff Bonwickextern void ddt_object_name(ddt_t *ddt, enum ddt_type type,
181b24ab67Jeff Bonwick    enum ddt_class class, char *name);
182b24ab67Jeff Bonwickextern int ddt_object_walk(ddt_t *ddt, enum ddt_type type,
183bbfd46cJeff Bonwick    enum ddt_class class, uint64_t *walk, ddt_entry_t *dde);
184b24ab67Jeff Bonwickextern uint64_t ddt_object_count(ddt_t *ddt, enum ddt_type type,
185b24ab67Jeff Bonwick    enum ddt_class class);
186b24ab67Jeff Bonwickextern int ddt_object_info(ddt_t *ddt, enum ddt_type type,
187b24ab67Jeff Bonwick    enum ddt_class class, dmu_object_info_t *);
188b24ab67Jeff Bonwickextern boolean_t ddt_object_exists(ddt_t *ddt, enum ddt_type type,
189b24ab67Jeff Bonwick    enum ddt_class class);
190b24ab67Jeff Bonwick
191b24ab67Jeff Bonwickextern void ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp,
192b24ab67Jeff Bonwick    uint64_t txg);
193bbfd46cJeff Bonwickextern void ddt_bp_create(enum zio_checksum checksum, const ddt_key_t *ddk,
194b24ab67Jeff Bonwick    const ddt_phys_t *ddp, blkptr_t *bp);
195b24ab67Jeff Bonwick
196b24ab67Jeff Bonwickextern void ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp);
197b24ab67Jeff Bonwick
198b24ab67Jeff Bonwickextern void ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp);
199b24ab67Jeff Bonwickextern void ddt_phys_clear(ddt_phys_t *ddp);
200b24ab67Jeff Bonwickextern void ddt_phys_addref(ddt_phys_t *ddp);
201b24ab67Jeff Bonwickextern void ddt_phys_decref(ddt_phys_t *ddp);
202b24ab67Jeff Bonwickextern void ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp,
203b24ab67Jeff Bonwick    uint64_t txg);
204b24ab67Jeff Bonwickextern ddt_phys_t *ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp);
205b24ab67Jeff Bonwickextern uint64_t ddt_phys_total_refcnt(const ddt_entry_t *dde);
206b24ab67Jeff Bonwick
207b24ab67Jeff Bonwickextern void ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg);
208b24ab67Jeff Bonwick
209b24ab67Jeff Bonwickextern void ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src);
210b24ab67Jeff Bonwickextern void ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh);
211b24ab67Jeff Bonwickextern boolean_t ddt_histogram_empty(const ddt_histogram_t *ddh);
2129eb19f4George Wilsonextern void ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo);
2139eb19f4George Wilsonextern void ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh);
2149eb19f4George Wilsonextern void ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total);
215b24ab67Jeff Bonwick
216485bbbfGeorge Wilsonextern uint64_t ddt_get_dedup_dspace(spa_t *spa);
217b24ab67Jeff Bonwickextern uint64_t ddt_get_pool_dedup_ratio(spa_t *spa);
218b24ab67Jeff Bonwick
219b24ab67Jeff Bonwickextern int ddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde,
220b24ab67Jeff Bonwick    ddt_phys_t *ddp_willref);
221b24ab67Jeff Bonwickextern int ddt_ditto_copies_present(ddt_entry_t *dde);
222b24ab67Jeff Bonwick
223b24ab67Jeff Bonwickextern size_t ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len);
224b24ab67Jeff Bonwickextern void ddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len);
225b24ab67Jeff Bonwick
226b24ab67Jeff Bonwickextern ddt_t *ddt_select(spa_t *spa, const blkptr_t *bp);
227b24ab67Jeff Bonwickextern void ddt_enter(ddt_t *ddt);
228b24ab67Jeff Bonwickextern void ddt_exit(ddt_t *ddt);
229b24ab67Jeff Bonwickextern ddt_entry_t *ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add);
230c7cd242George Wilsonextern void ddt_prefetch(spa_t *spa, const blkptr_t *bp);
231b24ab67Jeff Bonwickextern void ddt_remove(ddt_t *ddt, ddt_entry_t *dde);
232b24ab67Jeff Bonwick
233bbfd46cJeff Bonwickextern boolean_t ddt_class_contains(spa_t *spa, enum ddt_class max_class,
234bbfd46cJeff Bonwick    const blkptr_t *bp);
235bbfd46cJeff Bonwick
236b24ab67Jeff Bonwickextern ddt_entry_t *ddt_repair_start(ddt_t *ddt, const blkptr_t *bp);
237b24ab67Jeff Bonwickextern void ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde);
238b24ab67Jeff Bonwick
239b24ab67Jeff Bonwickextern int ddt_entry_compare(const void *x1, const void *x2);
240b24ab67Jeff Bonwick
241b24ab67Jeff Bonwickextern void ddt_create(spa_t *spa);
242b24ab67Jeff Bonwickextern int ddt_load(spa_t *spa);
243b24ab67Jeff Bonwickextern void ddt_unload(spa_t *spa);
244b24ab67Jeff Bonwickextern void ddt_sync(spa_t *spa, uint64_t txg);
245bbfd46cJeff Bonwickextern int ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde);
2463f9d6adLin Lingextern int ddt_object_update(ddt_t *ddt, enum ddt_type type,
2473f9d6adLin Ling    enum ddt_class class, ddt_entry_t *dde, dmu_tx_t *tx);
248b24ab67Jeff Bonwick
249b24ab67Jeff Bonwickextern const ddt_ops_t ddt_zap_ops;
250b24ab67Jeff Bonwick
251b24ab67Jeff Bonwick#ifdef	__cplusplus
252b24ab67Jeff Bonwick}
253b24ab67Jeff Bonwick#endif
254b24ab67Jeff Bonwick
255b24ab67Jeff Bonwick#endif	/* _SYS_DDT_H */