]> git.proxmox.com Git - mirror_zfs.git/blob - include/sys/ddt.h
b29667e5abbc849701e67ad6f140a1060d37bf7a
[mirror_zfs.git] / include / sys / ddt.h
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 https://opensource.org/licenses/CDDL-1.0.
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 (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2016 by Delphix. All rights reserved.
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 struct abd;
40
41 /*
42 * On-disk DDT formats, in the desired search order (newest version first).
43 */
44 typedef enum {
45 DDT_TYPE_ZAP = 0,
46 DDT_TYPES
47 } ddt_type_t;
48
49 _Static_assert(DDT_TYPES <= UINT8_MAX,
50 "ddt_type_t must fit in a uint8_t");
51
52 /* New and updated entries recieve this type, see ddt_sync_entry() */
53 #define DDT_TYPE_DEFAULT (DDT_TYPE_ZAP)
54
55 /*
56 * DDT classes, in the desired search order (highest replication level first).
57 */
58 typedef enum {
59 DDT_CLASS_DITTO = 0,
60 DDT_CLASS_DUPLICATE,
61 DDT_CLASS_UNIQUE,
62 DDT_CLASSES
63 } ddt_class_t;
64
65 _Static_assert(DDT_CLASSES < UINT8_MAX,
66 "ddt_class_t must fit in a uint8_t");
67
68 /*
69 * On-disk ddt entry: key (name) and physical storage (value).
70 */
71 typedef struct {
72 zio_cksum_t ddk_cksum; /* 256-bit block checksum */
73 /*
74 * Encoded with logical & physical size, encryption, and compression,
75 * as follows:
76 * +-------+-------+-------+-------+-------+-------+-------+-------+
77 * | 0 | 0 | 0 |X| comp| PSIZE | LSIZE |
78 * +-------+-------+-------+-------+-------+-------+-------+-------+
79 */
80 uint64_t ddk_prop;
81 } ddt_key_t;
82
83 #define DDK_GET_LSIZE(ddk) \
84 BF64_GET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1)
85 #define DDK_SET_LSIZE(ddk, x) \
86 BF64_SET_SB((ddk)->ddk_prop, 0, 16, SPA_MINBLOCKSHIFT, 1, x)
87
88 #define DDK_GET_PSIZE(ddk) \
89 BF64_GET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1)
90 #define DDK_SET_PSIZE(ddk, x) \
91 BF64_SET_SB((ddk)->ddk_prop, 16, 16, SPA_MINBLOCKSHIFT, 1, x)
92
93 #define DDK_GET_COMPRESS(ddk) BF64_GET((ddk)->ddk_prop, 32, 7)
94 #define DDK_SET_COMPRESS(ddk, x) BF64_SET((ddk)->ddk_prop, 32, 7, x)
95
96 #define DDK_GET_CRYPT(ddk) BF64_GET((ddk)->ddk_prop, 39, 1)
97 #define DDK_SET_CRYPT(ddk, x) BF64_SET((ddk)->ddk_prop, 39, 1, x)
98
99 typedef struct {
100 dva_t ddp_dva[SPA_DVAS_PER_BP];
101 uint64_t ddp_refcnt;
102 uint64_t ddp_phys_birth;
103 } ddt_phys_t;
104
105 /*
106 * Note, we no longer generate new DDT_PHYS_DITTO-type blocks. However,
107 * we maintain the ability to free existing dedup-ditto blocks.
108 */
109 enum ddt_phys_type {
110 DDT_PHYS_DITTO = 0,
111 DDT_PHYS_SINGLE = 1,
112 DDT_PHYS_DOUBLE = 2,
113 DDT_PHYS_TRIPLE = 3,
114 DDT_PHYS_TYPES
115 };
116
117 /*
118 * In-core ddt entry
119 */
120
121 /* State flags for dde_flags */
122 #define DDE_FLAG_LOADED (1 << 0) /* entry ready for use */
123
124 typedef struct {
125 /* key must be first for ddt_key_compare */
126 ddt_key_t dde_key;
127 ddt_phys_t dde_phys[DDT_PHYS_TYPES];
128 zio_t *dde_lead_zio[DDT_PHYS_TYPES];
129 struct abd *dde_repair_abd;
130 ddt_type_t dde_type;
131 ddt_class_t dde_class;
132 uint8_t dde_flags;
133 kcondvar_t dde_cv;
134 avl_node_t dde_node;
135 } ddt_entry_t;
136
137 /*
138 * In-core ddt
139 */
140 typedef struct {
141 kmutex_t ddt_lock;
142 avl_tree_t ddt_tree;
143 avl_tree_t ddt_repair_tree;
144 enum zio_checksum ddt_checksum;
145 spa_t *ddt_spa;
146 objset_t *ddt_os;
147 uint64_t ddt_stat_object;
148 uint64_t ddt_object[DDT_TYPES][DDT_CLASSES];
149 ddt_histogram_t ddt_histogram[DDT_TYPES][DDT_CLASSES];
150 ddt_histogram_t ddt_histogram_cache[DDT_TYPES][DDT_CLASSES];
151 ddt_object_t ddt_object_stats[DDT_TYPES][DDT_CLASSES];
152 } ddt_t;
153
154 /*
155 * In-core and on-disk bookmark for DDT walks
156 */
157 typedef struct {
158 uint64_t ddb_class;
159 uint64_t ddb_type;
160 uint64_t ddb_checksum;
161 uint64_t ddb_cursor;
162 } ddt_bookmark_t;
163
164 extern void ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp,
165 uint64_t txg);
166 extern void ddt_bp_create(enum zio_checksum checksum, const ddt_key_t *ddk,
167 const ddt_phys_t *ddp, blkptr_t *bp);
168
169 extern void ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp);
170 extern void ddt_phys_clear(ddt_phys_t *ddp);
171 extern void ddt_phys_addref(ddt_phys_t *ddp);
172 extern void ddt_phys_decref(ddt_phys_t *ddp);
173 extern ddt_phys_t *ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp);
174
175 extern void ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src);
176 extern void ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh);
177 extern boolean_t ddt_histogram_empty(const ddt_histogram_t *ddh);
178 extern void ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo);
179 extern void ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh);
180 extern void ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total);
181
182 extern uint64_t ddt_get_dedup_dspace(spa_t *spa);
183 extern uint64_t ddt_get_pool_dedup_ratio(spa_t *spa);
184
185 extern ddt_t *ddt_select(spa_t *spa, const blkptr_t *bp);
186 extern void ddt_enter(ddt_t *ddt);
187 extern void ddt_exit(ddt_t *ddt);
188 extern void ddt_init(void);
189 extern void ddt_fini(void);
190 extern ddt_entry_t *ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add);
191 extern void ddt_prefetch(spa_t *spa, const blkptr_t *bp);
192 extern void ddt_remove(ddt_t *ddt, ddt_entry_t *dde);
193
194 extern boolean_t ddt_class_contains(spa_t *spa, ddt_class_t max_class,
195 const blkptr_t *bp);
196
197 extern ddt_entry_t *ddt_repair_start(ddt_t *ddt, const blkptr_t *bp);
198 extern void ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde);
199
200 extern int ddt_key_compare(const void *x1, const void *x2);
201
202 extern void ddt_create(spa_t *spa);
203 extern int ddt_load(spa_t *spa);
204 extern void ddt_unload(spa_t *spa);
205 extern void ddt_sync(spa_t *spa, uint64_t txg);
206 extern int ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde);
207
208 extern boolean_t ddt_addref(spa_t *spa, const blkptr_t *bp);
209
210 #ifdef __cplusplus
211 }
212 #endif
213
214 #endif /* _SYS_DDT_H */