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