Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/module/zfs/ddt_zap.c
108609 views
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
/*
24
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
25
* Copyright (c) 2018 by Delphix. All rights reserved.
26
* Copyright (c) 2023, Klara Inc.
27
*/
28
29
#include <sys/zfs_context.h>
30
#include <sys/spa.h>
31
#include <sys/zio.h>
32
#include <sys/ddt.h>
33
#include <sys/ddt_impl.h>
34
#include <sys/zap.h>
35
#include <sys/dmu_tx.h>
36
#include <sys/dnode.h>
37
#include <sys/zio_compress.h>
38
39
static unsigned int ddt_zap_default_bs = 15;
40
static unsigned int ddt_zap_default_ibs = 15;
41
42
#define DDT_ZAP_COMPRESS_BYTEORDER_MASK 0x80
43
#define DDT_ZAP_COMPRESS_FUNCTION_MASK 0x7f
44
45
#define DDT_KEY_WORDS (sizeof (ddt_key_t) / sizeof (uint64_t))
46
47
static size_t
48
ddt_zap_compress(const void *src, uchar_t *dst, size_t s_len, size_t d_len)
49
{
50
uchar_t *version = dst++;
51
int cpfunc = ZIO_COMPRESS_ZLE;
52
zio_compress_info_t *ci = &zio_compress_table[cpfunc];
53
size_t c_len;
54
55
ASSERT3U(d_len, >=, s_len + 1); /* no compression plus version byte */
56
57
/* Call compress function directly to avoid hole detection. */
58
abd_t sabd, dabd;
59
abd_get_from_buf_struct(&sabd, (void *)src, s_len);
60
abd_get_from_buf_struct(&dabd, dst, d_len - 1);
61
c_len = ci->ci_compress(&sabd, &dabd, s_len, d_len - 1, ci->ci_level);
62
abd_free(&dabd);
63
abd_free(&sabd);
64
65
if (c_len == s_len) {
66
cpfunc = ZIO_COMPRESS_OFF;
67
memcpy(dst, src, s_len);
68
}
69
70
*version = cpfunc;
71
if (ZFS_HOST_BYTEORDER)
72
*version |= DDT_ZAP_COMPRESS_BYTEORDER_MASK;
73
74
return (c_len + 1);
75
}
76
77
static void
78
ddt_zap_decompress(uchar_t *src, void *dst, size_t s_len, size_t d_len)
79
{
80
uchar_t version = *src++;
81
int cpfunc = version & DDT_ZAP_COMPRESS_FUNCTION_MASK;
82
83
if (zio_compress_table[cpfunc].ci_decompress == NULL) {
84
memcpy(dst, src, d_len);
85
return;
86
}
87
88
abd_t sabd, dabd;
89
size_t c_len = s_len - 1;
90
abd_get_from_buf_struct(&sabd, src, c_len);
91
abd_get_from_buf_struct(&dabd, dst, d_len);
92
VERIFY0(zio_decompress_data(cpfunc, &sabd, &dabd, c_len, d_len, NULL));
93
abd_free(&dabd);
94
abd_free(&sabd);
95
96
if (((version & DDT_ZAP_COMPRESS_BYTEORDER_MASK) != 0) !=
97
(ZFS_HOST_BYTEORDER != 0))
98
byteswap_uint64_array(dst, d_len);
99
}
100
101
static int
102
ddt_zap_create(objset_t *os, uint64_t *objectp, dmu_tx_t *tx, boolean_t prehash)
103
{
104
zap_flags_t flags = ZAP_FLAG_HASH64 | ZAP_FLAG_UINT64_KEY;
105
106
if (prehash)
107
flags |= ZAP_FLAG_PRE_HASHED_KEY;
108
109
*objectp = zap_create_flags(os, 0, flags, DMU_OT_DDT_ZAP,
110
ddt_zap_default_bs, ddt_zap_default_ibs,
111
DMU_OT_NONE, 0, tx);
112
if (*objectp == 0)
113
return (SET_ERROR(ENOTSUP));
114
115
return (0);
116
}
117
118
static int
119
ddt_zap_destroy(objset_t *os, uint64_t object, dmu_tx_t *tx)
120
{
121
return (zap_destroy(os, object, tx));
122
}
123
124
static int
125
ddt_zap_lookup(dnode_t *dn, const ddt_key_t *ddk, void *phys, size_t psize)
126
{
127
uchar_t *cbuf;
128
uint64_t csize;
129
int error;
130
131
cbuf = kmem_alloc(psize + 1, KM_SLEEP);
132
133
error = zap_lookup_length_uint64_by_dnode(dn, (uint64_t *)ddk,
134
DDT_KEY_WORDS, 1, psize + 1, cbuf, &csize);
135
if (error == 0) {
136
ASSERT3U(csize, <=, psize + 1);
137
ddt_zap_decompress(cbuf, phys, csize, psize);
138
}
139
140
kmem_free(cbuf, psize + 1);
141
142
return (error);
143
}
144
145
static int
146
ddt_zap_contains(dnode_t *dn, const ddt_key_t *ddk)
147
{
148
return (zap_length_uint64_by_dnode(dn, (uint64_t *)ddk,
149
DDT_KEY_WORDS, NULL, NULL));
150
}
151
152
static void
153
ddt_zap_prefetch(dnode_t *dn, const ddt_key_t *ddk)
154
{
155
(void) zap_prefetch_uint64_by_dnode(dn, (uint64_t *)ddk,
156
DDT_KEY_WORDS);
157
}
158
159
static void
160
ddt_zap_prefetch_all(dnode_t *dn)
161
{
162
(void) zap_prefetch_object(dn->dn_objset, dn->dn_object);
163
}
164
165
static int
166
ddt_zap_update(dnode_t *dn, const ddt_key_t *ddk,
167
const void *phys, size_t psize, dmu_tx_t *tx)
168
{
169
const size_t cbuf_size = psize + 1;
170
171
uchar_t *cbuf = kmem_alloc(cbuf_size, KM_SLEEP);
172
173
uint64_t csize = ddt_zap_compress(phys, cbuf, psize, cbuf_size);
174
175
int error = zap_update_uint64_by_dnode(dn, (uint64_t *)ddk,
176
DDT_KEY_WORDS, 1, csize, cbuf, tx);
177
178
kmem_free(cbuf, cbuf_size);
179
180
return (error);
181
}
182
183
static int
184
ddt_zap_remove(dnode_t *dn, const ddt_key_t *ddk, dmu_tx_t *tx)
185
{
186
return (zap_remove_uint64_by_dnode(dn, (uint64_t *)ddk,
187
DDT_KEY_WORDS, tx));
188
}
189
190
static int
191
ddt_zap_walk(dnode_t *dn, uint64_t *walk, ddt_key_t *ddk,
192
void *phys, size_t psize)
193
{
194
zap_cursor_t zc;
195
zap_attribute_t *za;
196
int error;
197
198
za = zap_attribute_alloc();
199
if (*walk == 0) {
200
/*
201
* We don't want to prefetch the entire ZAP object, because
202
* it can be enormous. Also the primary use of DDT iteration
203
* is for scrubbing, in which case we will be issuing many
204
* scrub I/Os for each ZAP block that we read in, so
205
* reading the ZAP is unlikely to be the bottleneck.
206
*/
207
zap_cursor_init_noprefetch(&zc, dn->dn_objset, dn->dn_object);
208
} else {
209
zap_cursor_init_serialized(&zc, dn->dn_objset, dn->dn_object,
210
*walk);
211
}
212
if ((error = zap_cursor_retrieve(&zc, za)) == 0) {
213
uint64_t csize = za->za_num_integers;
214
215
ASSERT3U(za->za_integer_length, ==, 1);
216
ASSERT3U(csize, <=, psize + 1);
217
218
uchar_t *cbuf = kmem_alloc(csize, KM_SLEEP);
219
220
error = zap_lookup_uint64_by_dnode(dn, (uint64_t *)za->za_name,
221
DDT_KEY_WORDS, 1, csize, cbuf);
222
ASSERT0(error);
223
if (error == 0) {
224
ddt_zap_decompress(cbuf, phys, csize, psize);
225
*ddk = *(ddt_key_t *)za->za_name;
226
}
227
228
kmem_free(cbuf, csize);
229
230
zap_cursor_advance(&zc);
231
*walk = zap_cursor_serialize(&zc);
232
}
233
zap_cursor_fini(&zc);
234
zap_attribute_free(za);
235
return (error);
236
}
237
238
static int
239
ddt_zap_count(dnode_t *dn, uint64_t *count)
240
{
241
return (zap_count_by_dnode(dn, count));
242
}
243
244
const ddt_ops_t ddt_zap_ops = {
245
"zap",
246
ddt_zap_create,
247
ddt_zap_destroy,
248
ddt_zap_lookup,
249
ddt_zap_contains,
250
ddt_zap_prefetch,
251
ddt_zap_prefetch_all,
252
ddt_zap_update,
253
ddt_zap_remove,
254
ddt_zap_walk,
255
ddt_zap_count,
256
};
257
258
ZFS_MODULE_PARAM(zfs_dedup, , ddt_zap_default_bs, UINT, ZMOD_RW,
259
"DDT ZAP leaf blockshift");
260
ZFS_MODULE_PARAM(zfs_dedup, , ddt_zap_default_ibs, UINT, ZMOD_RW,
261
"DDT ZAP indirect blockshift");
262
263