xref: /freebsd/sys/contrib/openzfs/include/sys/ddt_impl.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
14  * Copyright (c) 2016 by Delphix. All rights reserved.
15  * Copyright (c) 2023, Klara Inc.
16  */
17 
18 #ifndef _SYS_DDT_IMPL_H
19 #define	_SYS_DDT_IMPL_H
20 
21 #include <sys/ddt.h>
22 #include <sys/bitops.h>
23 
24 #ifdef	__cplusplus
25 extern "C" {
26 #endif
27 
28 /* DDT version numbers */
29 #define	DDT_VERSION_LEGACY		(0)
30 #define	DDT_VERSION_FDT			(1)
31 
32 /* Dummy version to signal that configure is still necessary */
33 #define	DDT_VERSION_UNCONFIGURED	(UINT64_MAX)
34 
35 /* Names of interesting objects in the DDT root dir */
36 #define	DDT_DIR_VERSION		"version"
37 #define	DDT_DIR_FLAGS		"flags"
38 
39 /* Fill a lightweight entry from a live entry. */
40 #define	DDT_ENTRY_TO_LIGHTWEIGHT(ddt, dde, ddlwe) do {			\
41 	memset((ddlwe), 0, sizeof (*ddlwe));				\
42 	(ddlwe)->ddlwe_key = (dde)->dde_key;				\
43 	(ddlwe)->ddlwe_type = (dde)->dde_type;				\
44 	(ddlwe)->ddlwe_class = (dde)->dde_class;			\
45 	memcpy(&(ddlwe)->ddlwe_phys, (dde)->dde_phys, DDT_PHYS_SIZE(ddt)); \
46 } while (0)
47 
48 #define	DDT_LOG_ENTRY_TO_LIGHTWEIGHT(ddt, ddle, ddlwe) do {             \
49 	memset((ddlwe), 0, sizeof (*ddlwe));                            \
50 	(ddlwe)->ddlwe_key = (ddle)->ddle_key;                          \
51 	(ddlwe)->ddlwe_type = (ddle)->ddle_type;                        \
52 	(ddlwe)->ddlwe_class = (ddle)->ddle_class;                      \
53 	memcpy(&(ddlwe)->ddlwe_phys, (ddle)->ddle_phys, DDT_PHYS_SIZE(ddt)); \
54 } while (0)
55 
56 /*
57  * An entry on the log tree. These are "frozen", and a record of what's in
58  * the on-disk log. They can't be used in place, but can be "loaded" back into
59  * the live tree.
60  */
61 typedef struct {
62 	ddt_key_t	ddle_key;	/* ddl_tree key */
63 	avl_node_t	ddle_node;	/* ddl_tree node */
64 
65 	ddt_type_t	ddle_type;	/* storage type */
66 	ddt_class_t	ddle_class;	/* storage class */
67 
68 	/* extra allocation for flat/trad phys */
69 	ddt_univ_phys_t	ddle_phys[];
70 } ddt_log_entry_t;
71 
72 /* On-disk log record types. */
73 typedef enum {
74 	DLR_INVALID	= 0,	/* end of block marker */
75 	DLR_ENTRY	= 1,	/* an entry to add or replace in the log tree */
76 } ddt_log_record_type_t;
77 
78 /* On-disk log record header. */
79 typedef struct {
80 	/*
81 	 * dlr_info is a packed u64, use the DLR_GET/DLR_SET macros below to
82 	 * access it.
83 	 *
84 	 * bits 0-7:    record type (ddt_log_record_type_t)
85 	 * bits 8-15:  length of record header+payload
86 	 * bits 16-47:  reserved, all zero
87 	 * bits 48-55:   if type==DLR_ENTRY, storage type (ddt_type)
88 	 *                otherwise all zero
89 	 * bits 56-63:  if type==DLR_ENTRY, storage class (ddt_class)
90 	 *                otherwise all zero
91 	 */
92 	uint64_t	dlr_info;
93 	uint8_t		dlr_payload[];
94 } ddt_log_record_t;
95 
96 #define	DLR_GET_TYPE(dlr)		BF64_GET((dlr)->dlr_info, 0, 8)
97 #define	DLR_SET_TYPE(dlr, v)		BF64_SET((dlr)->dlr_info, 0, 8, v)
98 #define	DLR_GET_RECLEN(dlr)		BF64_GET((dlr)->dlr_info, 8, 16)
99 #define	DLR_SET_RECLEN(dlr, v)		BF64_SET((dlr)->dlr_info, 8, 16, v)
100 #define	DLR_GET_ENTRY_TYPE(dlr)		BF64_GET((dlr)->dlr_info, 48, 8)
101 #define	DLR_SET_ENTRY_TYPE(dlr, v)	BF64_SET((dlr)->dlr_info, 48, 8, v)
102 #define	DLR_GET_ENTRY_CLASS(dlr)	BF64_GET((dlr)->dlr_info, 56, 8)
103 #define	DLR_SET_ENTRY_CLASS(dlr, v)	BF64_SET((dlr)->dlr_info, 56, 8, v)
104 
105 /* Payload for DLR_ENTRY. */
106 typedef struct {
107 	ddt_key_t	dlre_key;
108 	ddt_univ_phys_t	dlre_phys[];
109 } ddt_log_record_entry_t;
110 
111 /* Log flags (ddl_flags, dlh_flags) */
112 #define	DDL_FLAG_FLUSHING	(1 << 0)	/* this log is being flushed */
113 #define	DDL_FLAG_CHECKPOINT	(1 << 1)	/* header has a checkpoint */
114 
115 /* On-disk log header, stored in the bonus buffer. */
116 typedef struct {
117 	/*
118 	 * dlh_info is a packed u64, use the DLH_GET/DLH_SET macros below to
119 	 * access it.
120 	 *
121 	 * bits 0-7:   log version
122 	 * bits 8-15:  log flags
123 	 * bits 16-63: reserved, all zero
124 	 */
125 	uint64_t	dlh_info;
126 
127 	uint64_t	dlh_length;	/* log size in bytes */
128 	uint64_t	dlh_first_txg;	/* txg this log went active */
129 	ddt_key_t	dlh_checkpoint;	/* last checkpoint */
130 } ddt_log_header_t;
131 
132 #define	DLH_GET_VERSION(dlh)	BF64_GET((dlh)->dlh_info, 0, 8)
133 #define	DLH_SET_VERSION(dlh, v)	BF64_SET((dlh)->dlh_info, 0, 8, v)
134 #define	DLH_GET_FLAGS(dlh)	BF64_GET((dlh)->dlh_info, 8, 8)
135 #define	DLH_SET_FLAGS(dlh, v)	BF64_SET((dlh)->dlh_info, 8, 8, v)
136 
137 /* DDT log update state */
138 typedef struct {
139 	dmu_tx_t	*dlu_tx;	/* tx the update is being applied to */
140 	dnode_t		*dlu_dn;	/* log object dnode */
141 	dmu_buf_t	**dlu_dbp;	/* array of block buffer pointers */
142 	int		dlu_ndbp;	/* number of block buffer pointers */
143 	uint16_t	dlu_reclen;	/* cached length of record */
144 	uint64_t	dlu_block;	/* block for next entry */
145 	uint64_t	dlu_offset;	/* offset for next entry */
146 } ddt_log_update_t;
147 
148 /*
149  * Ops vector to access a specific DDT object type.
150  */
151 typedef struct {
152 	char ddt_op_name[32];
153 	int (*ddt_op_create)(objset_t *os, uint64_t *object, dmu_tx_t *tx,
154 	    boolean_t prehash);
155 	int (*ddt_op_destroy)(objset_t *os, uint64_t object, dmu_tx_t *tx);
156 	int (*ddt_op_lookup)(dnode_t *dn, const ddt_key_t *ddk,
157 	    void *phys, size_t psize);
158 	int (*ddt_op_contains)(dnode_t *dn, const ddt_key_t *ddk);
159 	void (*ddt_op_prefetch)(dnode_t *dn, const ddt_key_t *ddk);
160 	void (*ddt_op_prefetch_all)(dnode_t *dn);
161 	int (*ddt_op_update)(dnode_t *dn, const ddt_key_t *ddk,
162 	    const void *phys, size_t psize, dmu_tx_t *tx);
163 	int (*ddt_op_remove)(dnode_t *dn, const ddt_key_t *ddk,
164 	    dmu_tx_t *tx);
165 	int (*ddt_op_walk)(dnode_t *dn, uint64_t *walk, ddt_key_t *ddk,
166 	    void *phys, size_t psize);
167 	int (*ddt_op_count)(dnode_t *dn, uint64_t *count);
168 } ddt_ops_t;
169 
170 extern const ddt_ops_t ddt_zap_ops;
171 
172 extern unsigned int ddt_zap_default_bs;
173 
174 /* Dedup log API */
175 extern void ddt_log_begin(ddt_t *ddt, size_t nentries, dmu_tx_t *tx,
176     ddt_log_update_t *dlu);
177 extern void ddt_log_entry(ddt_t *ddt, ddt_lightweight_entry_t *dde,
178     ddt_log_update_t *dlu);
179 extern void ddt_log_commit(ddt_t *ddt, ddt_log_update_t *dlu);
180 
181 extern boolean_t ddt_log_take_first(ddt_t *ddt, ddt_log_t *ddl,
182     ddt_lightweight_entry_t *ddlwe);
183 
184 extern boolean_t ddt_log_find_key(ddt_t *ddt, const ddt_key_t *ddk,
185     ddt_lightweight_entry_t *ddlwe, boolean_t *from_flushing);
186 extern boolean_t ddt_log_remove_key(ddt_t *ddt, ddt_log_t *ddl,
187     const ddt_key_t *ddk);
188 
189 extern void ddt_log_checkpoint(ddt_t *ddt, ddt_lightweight_entry_t *ddlwe,
190     dmu_tx_t *tx);
191 extern void ddt_log_truncate(ddt_t *ddt, dmu_tx_t *tx);
192 
193 extern boolean_t ddt_log_swap(ddt_t *ddt, dmu_tx_t *tx);
194 
195 extern void ddt_log_destroy(ddt_t *ddt, dmu_tx_t *tx);
196 
197 extern int ddt_log_load(ddt_t *ddt);
198 extern void ddt_log_alloc(ddt_t *ddt);
199 extern void ddt_log_free(ddt_t *ddt);
200 
201 extern void ddt_log_init(void);
202 extern void ddt_log_fini(void);
203 
204 /*
205  * These are only exposed so that zdb can access them. Try not to use them
206  * outside of the DDT implementation proper, and if you do, consider moving
207  * them up.
208  */
209 
210 /*
211  * We use a histogram to convert a percentage request into a
212  * cutoff value where entries older than the cutoff get pruned.
213  *
214  * The histogram bins represent hours in power-of-two increments.
215  * 16 bins covers up to four years.
216  */
217 #define	HIST_BINS 16
218 
219 typedef struct ddt_age_histo {
220 	uint64_t dah_entries;
221 	uint64_t dah_age_histo[HIST_BINS];
222 } ddt_age_histo_t;
223 
224 void ddt_prune_walk(spa_t *spa, uint64_t cutoff, ddt_age_histo_t *histogram);
225 
226 #if defined(_KERNEL) || !defined(ZFS_DEBUG)
227 #define	ddt_dump_age_histogram(histo, cutoff)	((void)0)
228 #else
229 static inline void
ddt_dump_age_histogram(ddt_age_histo_t * histogram,uint64_t cutoff)230 ddt_dump_age_histogram(ddt_age_histo_t *histogram, uint64_t cutoff)
231 {
232 	if (histogram->dah_entries == 0)
233 		return;
234 
235 	(void) printf("DDT prune unique class age, %llu hour cutoff\n",
236 	    (u_longlong_t)(gethrestime_sec() - cutoff)/3600);
237 	(void) printf("%5s  %9s  %4s\n", "age", "blocks", "amnt");
238 	(void) printf("%5s  %9s  %4s\n", "-----", "---------", "----");
239 	for (int i = 0; i < HIST_BINS; i++) {
240 		(void) printf("%5d  %9llu %4d%%\n", 1<<i,
241 		    (u_longlong_t)histogram->dah_age_histo[i],
242 		    (int)((histogram->dah_age_histo[i] * 100) /
243 		    histogram->dah_entries));
244 	}
245 }
246 #endif
247 
248 /*
249  * Enough room to expand DMU_POOL_DDT format for all possible DDT
250  * checksum/class/type combinations.
251  */
252 #define	DDT_NAMELEN	32
253 
254 extern uint64_t ddt_phys_total_refcnt(const ddt_t *ddt,
255     const ddt_univ_phys_t *ddp);
256 
257 extern void ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp);
258 
259 extern void ddt_object_name(ddt_t *ddt, ddt_type_t type, ddt_class_t clazz,
260     char *name);
261 extern int ddt_object_walk(ddt_t *ddt, ddt_type_t type, ddt_class_t clazz,
262     uint64_t *walk, ddt_lightweight_entry_t *ddlwe);
263 extern int ddt_object_count(ddt_t *ddt, ddt_type_t type, ddt_class_t clazz,
264     uint64_t *count);
265 extern int ddt_object_info(ddt_t *ddt, ddt_type_t type, ddt_class_t clazz,
266     dmu_object_info_t *);
267 
268 #ifdef	__cplusplus
269 }
270 #endif
271 
272 #endif	/* _SYS_DDT_H */
273