xref: /onnv-gate/usr/src/uts/common/fs/zfs/sys/ddt.h (revision 10922)
1*10922SJeff.Bonwick@Sun.COM /*
2*10922SJeff.Bonwick@Sun.COM  * CDDL HEADER START
3*10922SJeff.Bonwick@Sun.COM  *
4*10922SJeff.Bonwick@Sun.COM  * The contents of this file are subject to the terms of the
5*10922SJeff.Bonwick@Sun.COM  * Common Development and Distribution License (the "License").
6*10922SJeff.Bonwick@Sun.COM  * You may not use this file except in compliance with the License.
7*10922SJeff.Bonwick@Sun.COM  *
8*10922SJeff.Bonwick@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10922SJeff.Bonwick@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*10922SJeff.Bonwick@Sun.COM  * See the License for the specific language governing permissions
11*10922SJeff.Bonwick@Sun.COM  * and limitations under the License.
12*10922SJeff.Bonwick@Sun.COM  *
13*10922SJeff.Bonwick@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*10922SJeff.Bonwick@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10922SJeff.Bonwick@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*10922SJeff.Bonwick@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*10922SJeff.Bonwick@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*10922SJeff.Bonwick@Sun.COM  *
19*10922SJeff.Bonwick@Sun.COM  * CDDL HEADER END
20*10922SJeff.Bonwick@Sun.COM  */
21*10922SJeff.Bonwick@Sun.COM /*
22*10922SJeff.Bonwick@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*10922SJeff.Bonwick@Sun.COM  * Use is subject to license terms.
24*10922SJeff.Bonwick@Sun.COM  */
25*10922SJeff.Bonwick@Sun.COM 
26*10922SJeff.Bonwick@Sun.COM #ifndef _SYS_DDT_H
27*10922SJeff.Bonwick@Sun.COM #define	_SYS_DDT_H
28*10922SJeff.Bonwick@Sun.COM 
29*10922SJeff.Bonwick@Sun.COM #include <sys/sysmacros.h>
30*10922SJeff.Bonwick@Sun.COM #include <sys/types.h>
31*10922SJeff.Bonwick@Sun.COM #include <sys/fs/zfs.h>
32*10922SJeff.Bonwick@Sun.COM #include <sys/zio.h>
33*10922SJeff.Bonwick@Sun.COM #include <sys/dmu.h>
34*10922SJeff.Bonwick@Sun.COM 
35*10922SJeff.Bonwick@Sun.COM #ifdef	__cplusplus
36*10922SJeff.Bonwick@Sun.COM extern "C" {
37*10922SJeff.Bonwick@Sun.COM #endif
38*10922SJeff.Bonwick@Sun.COM 
39*10922SJeff.Bonwick@Sun.COM /*
40*10922SJeff.Bonwick@Sun.COM  * On-disk DDT formats, in the desired search order (newest version first).
41*10922SJeff.Bonwick@Sun.COM  */
42*10922SJeff.Bonwick@Sun.COM enum ddt_type {
43*10922SJeff.Bonwick@Sun.COM 	DDT_TYPE_ZAP = 0,
44*10922SJeff.Bonwick@Sun.COM 	DDT_TYPES
45*10922SJeff.Bonwick@Sun.COM };
46*10922SJeff.Bonwick@Sun.COM 
47*10922SJeff.Bonwick@Sun.COM /*
48*10922SJeff.Bonwick@Sun.COM  * DDT classes, in the desired search order (highest replication level first).
49*10922SJeff.Bonwick@Sun.COM  */
50*10922SJeff.Bonwick@Sun.COM enum ddt_class {
51*10922SJeff.Bonwick@Sun.COM 	DDT_CLASS_DITTO = 0,
52*10922SJeff.Bonwick@Sun.COM 	DDT_CLASS_DUPLICATE,
53*10922SJeff.Bonwick@Sun.COM 	DDT_CLASS_UNIQUE,
54*10922SJeff.Bonwick@Sun.COM 	DDT_CLASSES
55*10922SJeff.Bonwick@Sun.COM };
56*10922SJeff.Bonwick@Sun.COM 
57*10922SJeff.Bonwick@Sun.COM #define	DDT_TYPE_CURRENT		0
58*10922SJeff.Bonwick@Sun.COM 
59*10922SJeff.Bonwick@Sun.COM #define	DDT_COMPRESS_BYTEORDER_MASK	0x80
60*10922SJeff.Bonwick@Sun.COM #define	DDT_COMPRESS_FUNCTION_MASK	0x7f
61*10922SJeff.Bonwick@Sun.COM 
62*10922SJeff.Bonwick@Sun.COM /*
63*10922SJeff.Bonwick@Sun.COM  * DDT statistics.
64*10922SJeff.Bonwick@Sun.COM  */
65*10922SJeff.Bonwick@Sun.COM typedef struct ddt_stat {
66*10922SJeff.Bonwick@Sun.COM 	uint64_t	dds_blocks;	/* blocks			*/
67*10922SJeff.Bonwick@Sun.COM 	uint64_t	dds_lsize;	/* logical size			*/
68*10922SJeff.Bonwick@Sun.COM 	uint64_t	dds_psize;	/* physical size		*/
69*10922SJeff.Bonwick@Sun.COM 	uint64_t	dds_dsize;	/* deflated allocated size	*/
70*10922SJeff.Bonwick@Sun.COM 	uint64_t	dds_ref_blocks;	/* referenced blocks		*/
71*10922SJeff.Bonwick@Sun.COM 	uint64_t	dds_ref_lsize;	/* referenced lsize * refcnt	*/
72*10922SJeff.Bonwick@Sun.COM 	uint64_t	dds_ref_psize;	/* referenced psize * refcnt	*/
73*10922SJeff.Bonwick@Sun.COM 	uint64_t	dds_ref_dsize;	/* referenced dsize * refcnt	*/
74*10922SJeff.Bonwick@Sun.COM } ddt_stat_t;
75*10922SJeff.Bonwick@Sun.COM 
76*10922SJeff.Bonwick@Sun.COM typedef struct ddt_histogram {
77*10922SJeff.Bonwick@Sun.COM 	ddt_stat_t	ddh_stat[64];	/* power-of-two histogram buckets */
78*10922SJeff.Bonwick@Sun.COM } ddt_histogram_t;
79*10922SJeff.Bonwick@Sun.COM 
80*10922SJeff.Bonwick@Sun.COM /*
81*10922SJeff.Bonwick@Sun.COM  * On-disk ddt entry:  key (name) and physical storage (value).
82*10922SJeff.Bonwick@Sun.COM  */
83*10922SJeff.Bonwick@Sun.COM typedef struct ddt_key {
84*10922SJeff.Bonwick@Sun.COM 	zio_cksum_t	ddk_cksum;	/* 256-bit block checksum */
85*10922SJeff.Bonwick@Sun.COM 	uint64_t	ddk_prop;	/* LSIZE, PSIZE, compression */
86*10922SJeff.Bonwick@Sun.COM } ddt_key_t;
87*10922SJeff.Bonwick@Sun.COM 
88*10922SJeff.Bonwick@Sun.COM /*
89*10922SJeff.Bonwick@Sun.COM  * ddk_prop layout:
90*10922SJeff.Bonwick@Sun.COM  *
91*10922SJeff.Bonwick@Sun.COM  *	+-------+-------+-------+-------+-------+-------+-------+-------+
92*10922SJeff.Bonwick@Sun.COM  *	|   0	|   0	|   0	| comp	|     PSIZE	|     LSIZE	|
93*10922SJeff.Bonwick@Sun.COM  *	+-------+-------+-------+-------+-------+-------+-------+-------+
94*10922SJeff.Bonwick@Sun.COM  */
95*10922SJeff.Bonwick@Sun.COM #define	DDK_GET_LSIZE(ddk)	\
96*10922SJeff.Bonwick@Sun.COM 	BF64_GET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1)
97*10922SJeff.Bonwick@Sun.COM #define	DDK_SET_LSIZE(ddk, x)	\
98*10922SJeff.Bonwick@Sun.COM 	BF64_SET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
99*10922SJeff.Bonwick@Sun.COM 
100*10922SJeff.Bonwick@Sun.COM #define	DDK_GET_PSIZE(ddk)	\
101*10922SJeff.Bonwick@Sun.COM 	BF64_GET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
102*10922SJeff.Bonwick@Sun.COM #define	DDK_SET_PSIZE(ddk, x)	\
103*10922SJeff.Bonwick@Sun.COM 	BF64_SET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
104*10922SJeff.Bonwick@Sun.COM 
105*10922SJeff.Bonwick@Sun.COM #define	DDK_GET_COMPRESS(ddk)		BF64_GET((ddk)->ddk_prop, 32, 8)
106*10922SJeff.Bonwick@Sun.COM #define	DDK_SET_COMPRESS(ddk, x)	BF64_SET((ddk)->ddk_prop, 32, 8, x)
107*10922SJeff.Bonwick@Sun.COM 
108*10922SJeff.Bonwick@Sun.COM #define	DDT_KEY_WORDS	(sizeof (ddt_key_t) / sizeof (uint64_t))
109*10922SJeff.Bonwick@Sun.COM 
110*10922SJeff.Bonwick@Sun.COM typedef struct ddt_phys {
111*10922SJeff.Bonwick@Sun.COM 	dva_t		ddp_dva[SPA_DVAS_PER_BP];
112*10922SJeff.Bonwick@Sun.COM 	uint64_t	ddp_refcnt;
113*10922SJeff.Bonwick@Sun.COM 	uint64_t	ddp_phys_birth;
114*10922SJeff.Bonwick@Sun.COM } ddt_phys_t;
115*10922SJeff.Bonwick@Sun.COM 
116*10922SJeff.Bonwick@Sun.COM enum ddt_phys_type {
117*10922SJeff.Bonwick@Sun.COM 	DDT_PHYS_DITTO = 0,
118*10922SJeff.Bonwick@Sun.COM 	DDT_PHYS_SINGLE = 1,
119*10922SJeff.Bonwick@Sun.COM 	DDT_PHYS_DOUBLE = 2,
120*10922SJeff.Bonwick@Sun.COM 	DDT_PHYS_TRIPLE = 3,
121*10922SJeff.Bonwick@Sun.COM 	DDT_PHYS_TYPES
122*10922SJeff.Bonwick@Sun.COM } ddt_phys_type_t;
123*10922SJeff.Bonwick@Sun.COM 
124*10922SJeff.Bonwick@Sun.COM /*
125*10922SJeff.Bonwick@Sun.COM  * In-core ddt entry
126*10922SJeff.Bonwick@Sun.COM  */
127*10922SJeff.Bonwick@Sun.COM struct ddt_entry {
128*10922SJeff.Bonwick@Sun.COM 	ddt_key_t	dde_key;
129*10922SJeff.Bonwick@Sun.COM 	ddt_phys_t	dde_phys[DDT_PHYS_TYPES];
130*10922SJeff.Bonwick@Sun.COM 	zio_t		*dde_lead_zio[DDT_PHYS_TYPES];
131*10922SJeff.Bonwick@Sun.COM 	void		*dde_repair_data;
132*10922SJeff.Bonwick@Sun.COM 	enum ddt_type	dde_type;
133*10922SJeff.Bonwick@Sun.COM 	enum ddt_class	dde_class;
134*10922SJeff.Bonwick@Sun.COM 	uint8_t		dde_loading;
135*10922SJeff.Bonwick@Sun.COM 	uint8_t		dde_loaded;
136*10922SJeff.Bonwick@Sun.COM 	kcondvar_t	dde_cv;
137*10922SJeff.Bonwick@Sun.COM 	avl_node_t	dde_node;
138*10922SJeff.Bonwick@Sun.COM };
139*10922SJeff.Bonwick@Sun.COM 
140*10922SJeff.Bonwick@Sun.COM /*
141*10922SJeff.Bonwick@Sun.COM  * In-core ddt
142*10922SJeff.Bonwick@Sun.COM  */
143*10922SJeff.Bonwick@Sun.COM struct ddt {
144*10922SJeff.Bonwick@Sun.COM 	kmutex_t	ddt_lock;
145*10922SJeff.Bonwick@Sun.COM 	avl_tree_t	ddt_tree;
146*10922SJeff.Bonwick@Sun.COM 	avl_tree_t	ddt_repair_tree;
147*10922SJeff.Bonwick@Sun.COM 	enum zio_checksum ddt_checksum;
148*10922SJeff.Bonwick@Sun.COM 	spa_t		*ddt_spa;
149*10922SJeff.Bonwick@Sun.COM 	objset_t	*ddt_os;
150*10922SJeff.Bonwick@Sun.COM 	uint64_t	ddt_stat_object;
151*10922SJeff.Bonwick@Sun.COM 	uint64_t	ddt_object[DDT_TYPES][DDT_CLASSES];
152*10922SJeff.Bonwick@Sun.COM 	ddt_histogram_t	ddt_histogram[DDT_TYPES][DDT_CLASSES];
153*10922SJeff.Bonwick@Sun.COM 	avl_node_t	ddt_node;
154*10922SJeff.Bonwick@Sun.COM };
155*10922SJeff.Bonwick@Sun.COM 
156*10922SJeff.Bonwick@Sun.COM typedef struct ddt_ops {
157*10922SJeff.Bonwick@Sun.COM 	char ddt_op_name[32];
158*10922SJeff.Bonwick@Sun.COM 	int (*ddt_op_create)(objset_t *os, uint64_t *object, dmu_tx_t *tx,
159*10922SJeff.Bonwick@Sun.COM 	    boolean_t prehash);
160*10922SJeff.Bonwick@Sun.COM 	int (*ddt_op_destroy)(objset_t *os, uint64_t object, dmu_tx_t *tx);
161*10922SJeff.Bonwick@Sun.COM 	int (*ddt_op_lookup)(objset_t *os, uint64_t object, ddt_entry_t *dde);
162*10922SJeff.Bonwick@Sun.COM 	int (*ddt_op_update)(objset_t *os, uint64_t object, ddt_entry_t *dde,
163*10922SJeff.Bonwick@Sun.COM 	    dmu_tx_t *tx);
164*10922SJeff.Bonwick@Sun.COM 	int (*ddt_op_remove)(objset_t *os, uint64_t object, ddt_entry_t *dde,
165*10922SJeff.Bonwick@Sun.COM 	    dmu_tx_t *tx);
166*10922SJeff.Bonwick@Sun.COM 	int (*ddt_op_walk)(objset_t *os, uint64_t object, ddt_entry_t *dde,
167*10922SJeff.Bonwick@Sun.COM 	    uint64_t *walk);
168*10922SJeff.Bonwick@Sun.COM 	uint64_t (*ddt_op_count)(objset_t *os, uint64_t object);
169*10922SJeff.Bonwick@Sun.COM } ddt_ops_t;
170*10922SJeff.Bonwick@Sun.COM 
171*10922SJeff.Bonwick@Sun.COM #define	DDT_NAMELEN	80
172*10922SJeff.Bonwick@Sun.COM 
173*10922SJeff.Bonwick@Sun.COM extern void ddt_object_name(ddt_t *ddt, enum ddt_type type,
174*10922SJeff.Bonwick@Sun.COM     enum ddt_class class, char *name);
175*10922SJeff.Bonwick@Sun.COM extern int ddt_object_walk(ddt_t *ddt, enum ddt_type type,
176*10922SJeff.Bonwick@Sun.COM     enum ddt_class class, ddt_entry_t *dde, uint64_t *walk);
177*10922SJeff.Bonwick@Sun.COM extern uint64_t ddt_object_count(ddt_t *ddt, enum ddt_type type,
178*10922SJeff.Bonwick@Sun.COM     enum ddt_class class);
179*10922SJeff.Bonwick@Sun.COM extern int ddt_object_info(ddt_t *ddt, enum ddt_type type,
180*10922SJeff.Bonwick@Sun.COM     enum ddt_class class, dmu_object_info_t *);
181*10922SJeff.Bonwick@Sun.COM extern boolean_t ddt_object_exists(ddt_t *ddt, enum ddt_type type,
182*10922SJeff.Bonwick@Sun.COM     enum ddt_class class);
183*10922SJeff.Bonwick@Sun.COM 
184*10922SJeff.Bonwick@Sun.COM extern void ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp,
185*10922SJeff.Bonwick@Sun.COM     uint64_t txg);
186*10922SJeff.Bonwick@Sun.COM extern void ddt_bp_create(const ddt_t *ddt, const ddt_key_t *ddk,
187*10922SJeff.Bonwick@Sun.COM     const ddt_phys_t *ddp, blkptr_t *bp);
188*10922SJeff.Bonwick@Sun.COM 
189*10922SJeff.Bonwick@Sun.COM extern void ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp);
190*10922SJeff.Bonwick@Sun.COM 
191*10922SJeff.Bonwick@Sun.COM extern void ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp);
192*10922SJeff.Bonwick@Sun.COM extern void ddt_phys_clear(ddt_phys_t *ddp);
193*10922SJeff.Bonwick@Sun.COM extern void ddt_phys_addref(ddt_phys_t *ddp);
194*10922SJeff.Bonwick@Sun.COM extern void ddt_phys_decref(ddt_phys_t *ddp);
195*10922SJeff.Bonwick@Sun.COM extern void ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp,
196*10922SJeff.Bonwick@Sun.COM     uint64_t txg);
197*10922SJeff.Bonwick@Sun.COM extern ddt_phys_t *ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp);
198*10922SJeff.Bonwick@Sun.COM extern uint64_t ddt_phys_total_refcnt(const ddt_entry_t *dde);
199*10922SJeff.Bonwick@Sun.COM 
200*10922SJeff.Bonwick@Sun.COM extern void ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg);
201*10922SJeff.Bonwick@Sun.COM 
202*10922SJeff.Bonwick@Sun.COM extern void ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src);
203*10922SJeff.Bonwick@Sun.COM extern void ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh);
204*10922SJeff.Bonwick@Sun.COM extern boolean_t ddt_histogram_empty(const ddt_histogram_t *ddh);
205*10922SJeff.Bonwick@Sun.COM 
206*10922SJeff.Bonwick@Sun.COM extern uint64_t ddt_get_pool_dedup_ratio(spa_t *spa);
207*10922SJeff.Bonwick@Sun.COM 
208*10922SJeff.Bonwick@Sun.COM extern int ddt_ditto_copies_needed(ddt_t *ddt, ddt_entry_t *dde,
209*10922SJeff.Bonwick@Sun.COM     ddt_phys_t *ddp_willref);
210*10922SJeff.Bonwick@Sun.COM extern int ddt_ditto_copies_present(ddt_entry_t *dde);
211*10922SJeff.Bonwick@Sun.COM 
212*10922SJeff.Bonwick@Sun.COM extern size_t ddt_compress(void *src, uchar_t *dst, size_t s_len, size_t d_len);
213*10922SJeff.Bonwick@Sun.COM extern void ddt_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len);
214*10922SJeff.Bonwick@Sun.COM 
215*10922SJeff.Bonwick@Sun.COM extern ddt_t *ddt_select(spa_t *spa, const blkptr_t *bp);
216*10922SJeff.Bonwick@Sun.COM extern ddt_t *ddt_select_by_checksum(spa_t *spa, enum zio_checksum c);
217*10922SJeff.Bonwick@Sun.COM 
218*10922SJeff.Bonwick@Sun.COM extern void ddt_enter(ddt_t *ddt);
219*10922SJeff.Bonwick@Sun.COM extern void ddt_exit(ddt_t *ddt);
220*10922SJeff.Bonwick@Sun.COM extern ddt_entry_t *ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add);
221*10922SJeff.Bonwick@Sun.COM extern void ddt_remove(ddt_t *ddt, ddt_entry_t *dde);
222*10922SJeff.Bonwick@Sun.COM 
223*10922SJeff.Bonwick@Sun.COM extern ddt_entry_t *ddt_repair_start(ddt_t *ddt, const blkptr_t *bp);
224*10922SJeff.Bonwick@Sun.COM extern void ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde);
225*10922SJeff.Bonwick@Sun.COM 
226*10922SJeff.Bonwick@Sun.COM extern int ddt_entry_compare(const void *x1, const void *x2);
227*10922SJeff.Bonwick@Sun.COM 
228*10922SJeff.Bonwick@Sun.COM extern void ddt_create(spa_t *spa);
229*10922SJeff.Bonwick@Sun.COM extern int ddt_load(spa_t *spa);
230*10922SJeff.Bonwick@Sun.COM extern void ddt_unload(spa_t *spa);
231*10922SJeff.Bonwick@Sun.COM extern void ddt_sync(spa_t *spa, uint64_t txg);
232*10922SJeff.Bonwick@Sun.COM 
233*10922SJeff.Bonwick@Sun.COM extern const ddt_ops_t ddt_zap_ops;
234*10922SJeff.Bonwick@Sun.COM 
235*10922SJeff.Bonwick@Sun.COM #ifdef	__cplusplus
236*10922SJeff.Bonwick@Sun.COM }
237*10922SJeff.Bonwick@Sun.COM #endif
238*10922SJeff.Bonwick@Sun.COM 
239*10922SJeff.Bonwick@Sun.COM #endif	/* _SYS_DDT_H */
240