xref: /freebsd/sys/contrib/openzfs/include/sys/ddt.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2016 by Delphix. All rights reserved.
25  * Copyright (c) 2023, Klara Inc.
26  */
27 
28 #ifndef _SYS_DDT_H
29 #define	_SYS_DDT_H
30 
31 #include <sys/sysmacros.h>
32 #include <sys/types.h>
33 #include <sys/fs/zfs.h>
34 #include <sys/zio.h>
35 #include <sys/dmu.h>
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 struct abd;
42 
43 /*
44  * DDT-wide feature flags. These are set in ddt_flags by ddt_configure().
45  */
46 #define	DDT_FLAG_FLAT	(1 << 0)	/* single extensible phys */
47 #define	DDT_FLAG_LOG	(1 << 1)	/* dedup log (journal) */
48 #define	DDT_FLAG_MASK	(DDT_FLAG_FLAT|DDT_FLAG_LOG)
49 
50 /*
51  * DDT on-disk storage object types. Each one corresponds to specific
52  * implementation, see ddt_ops_t. The value itself is not stored on disk.
53  *
54  * When searching for an entry, objects types will be searched in this order.
55  *
56  * Note that DDT_TYPES is used as the "no type" for new entries that have not
57  * yet been written to a storage object.
58  */
59 typedef enum {
60 	DDT_TYPE_ZAP = 0,	/* ZAP storage object, ddt_zap */
61 	DDT_TYPES
62 } ddt_type_t;
63 
64 _Static_assert(DDT_TYPES <= UINT8_MAX,
65 	"ddt_type_t must fit in a uint8_t");
66 
67 /* New and updated entries recieve this type, see ddt_sync_entry() */
68 #define	DDT_TYPE_DEFAULT	(DDT_TYPE_ZAP)
69 
70 /*
71  * DDT storage classes. Each class has a separate storage object for each type.
72  * The value itself is not stored on disk.
73  *
74  * When search for an entry, object classes will be searched in this order.
75  *
76  * Note that DDT_CLASSES is used as the "no class" for new entries that have not
77  * yet been written to a storage object.
78  */
79 typedef enum {
80 	DDT_CLASS_DITTO = 0,	/* entry has ditto blocks (obsolete) */
81 	DDT_CLASS_DUPLICATE,	/* entry has multiple references */
82 	DDT_CLASS_UNIQUE,	/* entry has a single reference */
83 	DDT_CLASSES
84 } ddt_class_t;
85 
86 _Static_assert(DDT_CLASSES < UINT8_MAX,
87 	"ddt_class_t must fit in a uint8_t");
88 
89 /*
90  * The "key" part of an on-disk entry. This is the unique "name" for a block,
91  * that is, that parts of the block pointer that will always be the same for
92  * the same data.
93  */
94 typedef struct {
95 	zio_cksum_t	ddk_cksum;	/* 256-bit block checksum */
96 	/*
97 	 * Encoded with logical & physical size, encryption, and compression,
98 	 * as follows:
99 	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
100 	 *   |   0   |   0   |   0   |X| comp|     PSIZE     |     LSIZE     |
101 	 *   +-------+-------+-------+-------+-------+-------+-------+-------+
102 	 */
103 	uint64_t	ddk_prop;
104 } ddt_key_t;
105 
106 /*
107  * Macros for accessing parts of a ddt_key_t. These are similar to their BP_*
108  * counterparts.
109  */
110 #define	DDK_GET_LSIZE(ddk)	\
111 	BF64_GET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1)
112 #define	DDK_SET_LSIZE(ddk, x)	\
113 	BF64_SET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
114 
115 #define	DDK_GET_PSIZE(ddk)	\
116 	BF64_GET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
117 #define	DDK_SET_PSIZE(ddk, x)	\
118 	BF64_SET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
119 
120 #define	DDK_GET_COMPRESS(ddk)		BF64_GET((ddk)->ddk_prop, 32, 7)
121 #define	DDK_SET_COMPRESS(ddk, x)	BF64_SET((ddk)->ddk_prop, 32, 7, x)
122 
123 #define	DDK_GET_CRYPT(ddk)		BF64_GET((ddk)->ddk_prop, 39, 1)
124 #define	DDK_SET_CRYPT(ddk, x)	BF64_SET((ddk)->ddk_prop, 39, 1, x)
125 
126 /*
127  * The "value" part for an on-disk entry. These are the "physical"
128  * characteristics of the stored block, such as its location on disk (DVAs),
129  * birth txg and ref count.
130  *
131  * The "traditional" entry has an array of four, one for each number of DVAs
132  * (copies= property) and another for additional "ditto" copies. Users of the
133  * traditional struct will specify the variant (index) of the one they want.
134  *
135  * The newer "flat" entry has only a single form that is specified using the
136  * DDT_PHYS_FLAT variant.
137  *
138  * Since the value size varies, use one of the size macros when interfacing
139  * with the ddt zap.
140  */
141 
142 #define	DDT_PHYS_MAX	(4)
143 
144 /*
145  * Note - this can be used in a flexible array and allocated for
146  * a specific size (ddp_trad or ddp_flat). So be careful not to
147  * copy using "=" assignment but instead use ddt_phys_copy().
148  */
149 typedef union {
150 	/*
151 	 * Traditional physical payload value for DDT zap (256 bytes)
152 	 */
153 	struct {
154 		dva_t		ddp_dva[SPA_DVAS_PER_BP];
155 		uint64_t	ddp_refcnt;
156 		uint64_t	ddp_phys_birth;
157 	} ddp_trad[DDT_PHYS_MAX];
158 
159 	/*
160 	 * Flat physical payload value for DDT zap (72 bytes)
161 	 */
162 	struct {
163 		dva_t		ddp_dva[SPA_DVAS_PER_BP];
164 		uint64_t	ddp_refcnt;
165 		uint64_t	ddp_phys_birth; /* txg based from BP */
166 		uint64_t	ddp_class_start; /* in realtime seconds */
167 	} ddp_flat;
168 } ddt_univ_phys_t;
169 
170 /*
171  * This enum denotes which variant of a ddt_univ_phys_t to target. For
172  * a traditional DDT entry, it represents the indexes into the ddp_trad
173  * array. Any consumer of a ddt_univ_phys_t needs to know which variant
174  * is being targeted.
175  *
176  * Note, we no longer generate new DDT_PHYS_DITTO-type blocks.  However,
177  * we maintain the ability to free existing dedup-ditto blocks.
178  */
179 
180 typedef enum {
181 	DDT_PHYS_DITTO = 0,
182 	DDT_PHYS_SINGLE = 1,
183 	DDT_PHYS_DOUBLE = 2,
184 	DDT_PHYS_TRIPLE = 3,
185 	DDT_PHYS_FLAT = 4,
186 	DDT_PHYS_NONE = 5
187 } ddt_phys_variant_t;
188 
189 #define	DDT_PHYS_VARIANT(ddt, p)	\
190 	(ASSERT((p) < DDT_PHYS_NONE),	\
191 	((ddt)->ddt_flags & DDT_FLAG_FLAT ? DDT_PHYS_FLAT : (p)))
192 
193 #define	DDT_TRAD_PHYS_SIZE	sizeof (((ddt_univ_phys_t *)0)->ddp_trad)
194 #define	DDT_FLAT_PHYS_SIZE	sizeof (((ddt_univ_phys_t *)0)->ddp_flat)
195 
196 #define	_DDT_PHYS_SWITCH(ddt, flat, trad)	\
197 	(((ddt)->ddt_flags & DDT_FLAG_FLAT) ? (flat) : (trad))
198 
199 #define	DDT_PHYS_SIZE(ddt)		_DDT_PHYS_SWITCH(ddt,	\
200 	DDT_FLAT_PHYS_SIZE, DDT_TRAD_PHYS_SIZE)
201 
202 #define	DDT_NPHYS(ddt)			_DDT_PHYS_SWITCH(ddt, 1, DDT_PHYS_MAX)
203 #define	DDT_PHYS_FOR_COPIES(ddt, p)	_DDT_PHYS_SWITCH(ddt, 0, p)
204 #define	DDT_PHYS_IS_DITTO(ddt, p)	_DDT_PHYS_SWITCH(ddt, 0, (p == 0))
205 
206 /*
207  * A "live" entry, holding changes to an entry made this txg, and other data to
208  * support loading, updating and repairing the entry.
209  */
210 
211 /* State flags for dde_flags */
212 #define	DDE_FLAG_LOADED		(1 << 0)	/* entry ready for use */
213 #define	DDE_FLAG_OVERQUOTA	(1 << 1)	/* entry unusable, no space */
214 #define	DDE_FLAG_LOGGED		(1 << 2)	/* loaded from log */
215 
216 /*
217  * Additional data to support entry update or repair. This is fixed size
218  * because its relatively rarely used.
219  */
220 typedef struct {
221 	/* copy of data after a repair read, to be rewritten */
222 	abd_t		*dde_repair_abd;
223 
224 	/* original phys contents before update, for error handling */
225 	ddt_univ_phys_t	dde_orig_phys;
226 
227 	/* in-flight update IOs */
228 	zio_t		*dde_lead_zio[DDT_PHYS_MAX];
229 } ddt_entry_io_t;
230 
231 typedef struct {
232 	/* key must be first for ddt_key_compare */
233 	ddt_key_t	dde_key;	/* ddt_tree key */
234 	avl_node_t	dde_node;	/* ddt_tree_node */
235 
236 	/* storage type and class the entry was loaded from */
237 	ddt_type_t	dde_type;
238 	ddt_class_t	dde_class;
239 
240 	uint8_t		dde_flags;	/* load state flags */
241 	kcondvar_t	dde_cv;		/* signaled when load completes */
242 	uint64_t	dde_waiters;	/* count of waiters on dde_cv */
243 
244 	ddt_entry_io_t	*dde_io;	/* IO support, when required */
245 
246 	ddt_univ_phys_t	dde_phys[];	/* flexible -- allocated size varies */
247 } ddt_entry_t;
248 
249 /*
250  * A lightweight entry is for short-lived or transient uses, like iterating or
251  * inspecting, when you don't care where it came from.
252  */
253 typedef struct {
254 	ddt_key_t	ddlwe_key;
255 	ddt_type_t	ddlwe_type;
256 	ddt_class_t	ddlwe_class;
257 	ddt_univ_phys_t	ddlwe_phys;
258 } ddt_lightweight_entry_t;
259 
260 /*
261  * In-core DDT log. A separate struct to make it easier to switch between the
262  * appending and flushing logs.
263  */
264 typedef struct {
265 	avl_tree_t	ddl_tree;	/* logged entries */
266 	uint32_t	ddl_flags;	/* flags for this log */
267 	uint64_t	ddl_object;	/* log object id */
268 	uint64_t	ddl_length;	/* on-disk log size */
269 	uint64_t	ddl_first_txg;	/* txg log became active */
270 	ddt_key_t	ddl_checkpoint;	/* last checkpoint */
271 } ddt_log_t;
272 
273 /*
274  * In-core DDT object. This covers all entries and stats for a the whole pool
275  * for a given checksum type.
276  */
277 typedef struct {
278 	kmutex_t	ddt_lock;	/* protects changes to all fields */
279 
280 	avl_tree_t	ddt_tree;	/* "live" (changed) entries this txg */
281 	avl_tree_t	ddt_log_tree;	/* logged entries */
282 
283 	avl_tree_t	ddt_repair_tree;	/* entries being repaired */
284 
285 	ddt_log_t	ddt_log[2];		/* active/flushing logs */
286 	ddt_log_t	*ddt_log_active;	/* pointers into ddt_log */
287 	ddt_log_t	*ddt_log_flushing;	/* swapped when flush starts */
288 
289 	int32_t		ddt_log_ingest_rate;	/* rolling log ingest rate */
290 	int32_t		ddt_log_flush_rate;	/* rolling log flush rate */
291 	int32_t		ddt_log_flush_time_rate; /* avg time spent flushing */
292 	uint32_t	ddt_log_flush_pressure;	/* pressure to apply for cap */
293 	uint32_t	ddt_log_flush_prev_backlog; /* prev backlog size */
294 
295 	uint64_t	ddt_flush_force_txg;	/* flush hard before this txg */
296 
297 	kstat_t		*ddt_ksp;	/* kstats context */
298 
299 	enum zio_checksum ddt_checksum;	/* checksum algorithm in use */
300 	spa_t		*ddt_spa;	/* pool this ddt is on */
301 	objset_t	*ddt_os;	/* ddt objset (always MOS) */
302 
303 	uint64_t	ddt_dir_object;	/* MOS dir holding ddt objects */
304 	uint64_t	ddt_version;	/* DDT version */
305 	uint64_t	ddt_flags;	/* FDT option flags */
306 
307 	/* per-type/per-class entry store objects */
308 	uint64_t	ddt_object[DDT_TYPES][DDT_CLASSES];
309 
310 	/* object ids for stored, logged and per-type/per-class stats */
311 	uint64_t	ddt_stat_object;
312 	ddt_object_t	ddt_log_stats;
313 	ddt_object_t	ddt_object_stats[DDT_TYPES][DDT_CLASSES];
314 
315 	/* type/class stats by power-2-sized referenced blocks */
316 	ddt_histogram_t	ddt_histogram[DDT_TYPES][DDT_CLASSES];
317 	ddt_histogram_t	ddt_histogram_cache[DDT_TYPES][DDT_CLASSES];
318 
319 	/* log stats power-2-sized referenced blocks */
320 	ddt_histogram_t	ddt_log_histogram;
321 } ddt_t;
322 
323 /*
324  * In-core and on-disk bookmark for DDT walks. This is a cursor for ddt_walk(),
325  * and is stable across calls, even if the DDT is updated, the pool is
326  * restarted or loaded on another system, or OpenZFS is upgraded.
327  */
328 typedef struct {
329 	uint64_t	ddb_class;
330 	uint64_t	ddb_type;
331 	uint64_t	ddb_checksum;
332 	uint64_t	ddb_cursor;
333 } ddt_bookmark_t;
334 
335 extern void ddt_bp_fill(const ddt_univ_phys_t *ddp, ddt_phys_variant_t v,
336     blkptr_t *bp, uint64_t txg);
337 extern void ddt_bp_create(enum zio_checksum checksum, const ddt_key_t *ddk,
338     const ddt_univ_phys_t *ddp, ddt_phys_variant_t v, blkptr_t *bp);
339 
340 extern void ddt_phys_extend(ddt_univ_phys_t *ddp, ddt_phys_variant_t v,
341     const blkptr_t *bp);
342 extern void ddt_phys_copy(ddt_univ_phys_t *dst, const ddt_univ_phys_t *src,
343     ddt_phys_variant_t v);
344 extern void ddt_phys_clear(ddt_univ_phys_t *ddp, ddt_phys_variant_t v);
345 extern void ddt_phys_addref(ddt_univ_phys_t *ddp, ddt_phys_variant_t v);
346 extern uint64_t ddt_phys_decref(ddt_univ_phys_t *ddp, ddt_phys_variant_t v);
347 extern uint64_t ddt_phys_refcnt(const ddt_univ_phys_t *ddp,
348     ddt_phys_variant_t v);
349 extern ddt_phys_variant_t ddt_phys_select(const ddt_t *ddt,
350     const ddt_entry_t *dde, const blkptr_t *bp);
351 extern uint64_t ddt_phys_birth(const ddt_univ_phys_t *ddp,
352     ddt_phys_variant_t v);
353 extern int ddt_phys_dva_count(const ddt_univ_phys_t *ddp, ddt_phys_variant_t v,
354     boolean_t encrypted);
355 
356 extern void ddt_histogram_add_entry(ddt_t *ddt, ddt_histogram_t *ddh,
357     const ddt_lightweight_entry_t *ddlwe);
358 extern void ddt_histogram_sub_entry(ddt_t *ddt, ddt_histogram_t *ddh,
359     const ddt_lightweight_entry_t *ddlwe);
360 
361 extern void ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src);
362 extern void ddt_histogram_total(ddt_stat_t *dds, const ddt_histogram_t *ddh);
363 extern boolean_t ddt_histogram_empty(const ddt_histogram_t *ddh);
364 
365 extern void ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo);
366 extern uint64_t ddt_get_ddt_dsize(spa_t *spa);
367 extern void ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh);
368 extern void ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total);
369 
370 extern uint64_t ddt_get_dedup_dspace(spa_t *spa);
371 extern uint64_t ddt_get_pool_dedup_ratio(spa_t *spa);
372 extern int ddt_get_pool_dedup_cached(spa_t *spa, uint64_t *psize);
373 
374 extern ddt_t *ddt_select(spa_t *spa, const blkptr_t *bp);
375 extern void ddt_enter(ddt_t *ddt);
376 extern void ddt_exit(ddt_t *ddt);
377 extern void ddt_init(void);
378 extern void ddt_fini(void);
379 extern ddt_entry_t *ddt_lookup(ddt_t *ddt, const blkptr_t *bp,
380     boolean_t verify);
381 extern void ddt_remove(ddt_t *ddt, ddt_entry_t *dde);
382 extern void ddt_prefetch(spa_t *spa, const blkptr_t *bp);
383 extern void ddt_prefetch_all(spa_t *spa);
384 
385 extern boolean_t ddt_class_contains(spa_t *spa, ddt_class_t max_class,
386     const blkptr_t *bp);
387 
388 extern void ddt_alloc_entry_io(ddt_entry_t *dde);
389 
390 extern ddt_entry_t *ddt_repair_start(ddt_t *ddt, const blkptr_t *bp);
391 extern void ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde);
392 
393 extern int ddt_key_compare(const void *x1, const void *x2);
394 
395 extern void ddt_create(spa_t *spa);
396 extern int ddt_load(spa_t *spa);
397 extern void ddt_unload(spa_t *spa);
398 extern void ddt_sync(spa_t *spa, uint64_t txg);
399 
400 extern void ddt_walk_init(spa_t *spa, uint64_t txg);
401 extern boolean_t ddt_walk_ready(spa_t *spa);
402 extern int ddt_walk(spa_t *spa, ddt_bookmark_t *ddb,
403     ddt_lightweight_entry_t *ddlwe);
404 
405 extern boolean_t ddt_addref(spa_t *spa, const blkptr_t *bp);
406 
407 extern int ddt_prune_unique_entries(spa_t *spa, zpool_ddt_prune_unit_t unit,
408     uint64_t amount);
409 
410 #ifdef	__cplusplus
411 }
412 #endif
413 
414 #endif	/* _SYS_DDT_H */
415