]>
Commit | Line | Data |
---|---|---|
428870ff BB |
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 | |
1d3ba0bf | 9 | * or https://opensource.org/licenses/CDDL-1.0. |
428870ff BB |
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 | /* | |
23 | * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. | |
a6255b7f | 24 | * Copyright (c) 2012, 2016 by Delphix. All rights reserved. |
67a1b037 | 25 | * Copyright (c) 2022 by Pawel Jakub Dawidek |
428870ff BB |
26 | */ |
27 | ||
28 | #include <sys/zfs_context.h> | |
29 | #include <sys/spa.h> | |
30 | #include <sys/spa_impl.h> | |
31 | #include <sys/zio.h> | |
32 | #include <sys/ddt.h> | |
33 | #include <sys/zap.h> | |
34 | #include <sys/dmu_tx.h> | |
35 | #include <sys/arc.h> | |
36 | #include <sys/dsl_pool.h> | |
37 | #include <sys/zio_checksum.h> | |
428870ff | 38 | #include <sys/dsl_scan.h> |
a6255b7f | 39 | #include <sys/abd.h> |
428870ff | 40 | |
ecf3d9b8 JL |
41 | static kmem_cache_t *ddt_cache; |
42 | static kmem_cache_t *ddt_entry_cache; | |
43 | ||
572e2857 BB |
44 | /* |
45 | * Enable/disable prefetching of dedup-ed blocks which are going to be freed. | |
46 | */ | |
0dfc7324 | 47 | int zfs_dedup_prefetch = 0; |
572e2857 | 48 | |
861166b0 | 49 | static const ddt_ops_t *const ddt_ops[DDT_TYPES] = { |
428870ff BB |
50 | &ddt_zap_ops, |
51 | }; | |
52 | ||
861166b0 | 53 | static const char *const ddt_class_name[DDT_CLASSES] = { |
428870ff BB |
54 | "ditto", |
55 | "duplicate", | |
56 | "unique", | |
57 | }; | |
58 | ||
59 | static void | |
60 | ddt_object_create(ddt_t *ddt, enum ddt_type type, enum ddt_class class, | |
61 | dmu_tx_t *tx) | |
62 | { | |
63 | spa_t *spa = ddt->ddt_spa; | |
64 | objset_t *os = ddt->ddt_os; | |
65 | uint64_t *objectp = &ddt->ddt_object[type][class]; | |
3c67d83a TH |
66 | boolean_t prehash = zio_checksum_table[ddt->ddt_checksum].ci_flags & |
67 | ZCHECKSUM_FLAG_DEDUP; | |
428870ff BB |
68 | char name[DDT_NAMELEN]; |
69 | ||
70 | ddt_object_name(ddt, type, class, name); | |
71 | ||
d3bafe45 RN |
72 | ASSERT3U(*objectp, ==, 0); |
73 | VERIFY0(ddt_ops[type]->ddt_op_create(os, objectp, tx, prehash)); | |
74 | ASSERT3U(*objectp, !=, 0); | |
428870ff | 75 | |
d3bafe45 RN |
76 | VERIFY0(zap_add(os, DMU_POOL_DIRECTORY_OBJECT, name, |
77 | sizeof (uint64_t), 1, objectp, tx)); | |
428870ff | 78 | |
d3bafe45 | 79 | VERIFY0(zap_add(os, spa->spa_ddt_stat_object, name, |
428870ff | 80 | sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t), |
d3bafe45 | 81 | &ddt->ddt_histogram[type][class], tx)); |
428870ff BB |
82 | } |
83 | ||
84 | static void | |
85 | ddt_object_destroy(ddt_t *ddt, enum ddt_type type, enum ddt_class class, | |
86 | dmu_tx_t *tx) | |
87 | { | |
88 | spa_t *spa = ddt->ddt_spa; | |
89 | objset_t *os = ddt->ddt_os; | |
90 | uint64_t *objectp = &ddt->ddt_object[type][class]; | |
e8fd45a0 | 91 | uint64_t count; |
428870ff BB |
92 | char name[DDT_NAMELEN]; |
93 | ||
94 | ddt_object_name(ddt, type, class, name); | |
95 | ||
d3bafe45 | 96 | ASSERT3U(*objectp, !=, 0); |
428870ff | 97 | ASSERT(ddt_histogram_empty(&ddt->ddt_histogram[type][class])); |
d3bafe45 RN |
98 | VERIFY0(ddt_object_count(ddt, type, class, &count)); |
99 | VERIFY0(count); | |
100 | VERIFY0(zap_remove(os, DMU_POOL_DIRECTORY_OBJECT, name, tx)); | |
101 | VERIFY0(zap_remove(os, spa->spa_ddt_stat_object, name, tx)); | |
102 | VERIFY0(ddt_ops[type]->ddt_op_destroy(os, *objectp, tx)); | |
861166b0 | 103 | memset(&ddt->ddt_object_stats[type][class], 0, sizeof (ddt_object_t)); |
428870ff BB |
104 | |
105 | *objectp = 0; | |
106 | } | |
107 | ||
108 | static int | |
109 | ddt_object_load(ddt_t *ddt, enum ddt_type type, enum ddt_class class) | |
110 | { | |
111 | ddt_object_t *ddo = &ddt->ddt_object_stats[type][class]; | |
112 | dmu_object_info_t doi; | |
e8fd45a0 | 113 | uint64_t count; |
428870ff BB |
114 | char name[DDT_NAMELEN]; |
115 | int error; | |
116 | ||
117 | ddt_object_name(ddt, type, class, name); | |
118 | ||
119 | error = zap_lookup(ddt->ddt_os, DMU_POOL_DIRECTORY_OBJECT, name, | |
120 | sizeof (uint64_t), 1, &ddt->ddt_object[type][class]); | |
b0bc7a84 | 121 | if (error != 0) |
428870ff BB |
122 | return (error); |
123 | ||
ff9b1d07 | 124 | error = zap_lookup(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name, |
428870ff | 125 | sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t), |
ff9b1d07 BB |
126 | &ddt->ddt_histogram[type][class]); |
127 | if (error != 0) | |
128 | return (error); | |
428870ff BB |
129 | |
130 | /* | |
131 | * Seed the cached statistics. | |
132 | */ | |
2a4a9dc2 BB |
133 | error = ddt_object_info(ddt, type, class, &doi); |
134 | if (error) | |
135 | return (error); | |
428870ff | 136 | |
e8fd45a0 BB |
137 | error = ddt_object_count(ddt, type, class, &count); |
138 | if (error) | |
139 | return (error); | |
140 | ||
141 | ddo->ddo_count = count; | |
428870ff BB |
142 | ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9; |
143 | ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size; | |
144 | ||
b0bc7a84 | 145 | return (0); |
428870ff BB |
146 | } |
147 | ||
148 | static void | |
149 | ddt_object_sync(ddt_t *ddt, enum ddt_type type, enum ddt_class class, | |
150 | dmu_tx_t *tx) | |
151 | { | |
152 | ddt_object_t *ddo = &ddt->ddt_object_stats[type][class]; | |
153 | dmu_object_info_t doi; | |
e8fd45a0 | 154 | uint64_t count; |
428870ff BB |
155 | char name[DDT_NAMELEN]; |
156 | ||
157 | ddt_object_name(ddt, type, class, name); | |
158 | ||
d3bafe45 | 159 | VERIFY0(zap_update(ddt->ddt_os, ddt->ddt_spa->spa_ddt_stat_object, name, |
428870ff | 160 | sizeof (uint64_t), sizeof (ddt_histogram_t) / sizeof (uint64_t), |
d3bafe45 | 161 | &ddt->ddt_histogram[type][class], tx)); |
428870ff BB |
162 | |
163 | /* | |
164 | * Cache DDT statistics; this is the only time they'll change. | |
165 | */ | |
d3bafe45 RN |
166 | VERIFY0(ddt_object_info(ddt, type, class, &doi)); |
167 | VERIFY0(ddt_object_count(ddt, type, class, &count)); | |
428870ff | 168 | |
e8fd45a0 | 169 | ddo->ddo_count = count; |
428870ff BB |
170 | ddo->ddo_dspace = doi.doi_physical_blocks_512 << 9; |
171 | ddo->ddo_mspace = doi.doi_fill_count * doi.doi_data_block_size; | |
172 | } | |
173 | ||
174 | static int | |
175 | ddt_object_lookup(ddt_t *ddt, enum ddt_type type, enum ddt_class class, | |
176 | ddt_entry_t *dde) | |
177 | { | |
178 | if (!ddt_object_exists(ddt, type, class)) | |
2e528b49 | 179 | return (SET_ERROR(ENOENT)); |
428870ff BB |
180 | |
181 | return (ddt_ops[type]->ddt_op_lookup(ddt->ddt_os, | |
182 | ddt->ddt_object[type][class], dde)); | |
183 | } | |
184 | ||
185 | static void | |
186 | ddt_object_prefetch(ddt_t *ddt, enum ddt_type type, enum ddt_class class, | |
187 | ddt_entry_t *dde) | |
188 | { | |
189 | if (!ddt_object_exists(ddt, type, class)) | |
190 | return; | |
191 | ||
192 | ddt_ops[type]->ddt_op_prefetch(ddt->ddt_os, | |
193 | ddt->ddt_object[type][class], dde); | |
194 | } | |
195 | ||
196 | int | |
197 | ddt_object_update(ddt_t *ddt, enum ddt_type type, enum ddt_class class, | |
198 | ddt_entry_t *dde, dmu_tx_t *tx) | |
199 | { | |
200 | ASSERT(ddt_object_exists(ddt, type, class)); | |
201 | ||
202 | return (ddt_ops[type]->ddt_op_update(ddt->ddt_os, | |
203 | ddt->ddt_object[type][class], dde, tx)); | |
204 | } | |
205 | ||
206 | static int | |
207 | ddt_object_remove(ddt_t *ddt, enum ddt_type type, enum ddt_class class, | |
208 | ddt_entry_t *dde, dmu_tx_t *tx) | |
209 | { | |
210 | ASSERT(ddt_object_exists(ddt, type, class)); | |
211 | ||
212 | return (ddt_ops[type]->ddt_op_remove(ddt->ddt_os, | |
213 | ddt->ddt_object[type][class], dde, tx)); | |
214 | } | |
215 | ||
216 | int | |
217 | ddt_object_walk(ddt_t *ddt, enum ddt_type type, enum ddt_class class, | |
218 | uint64_t *walk, ddt_entry_t *dde) | |
219 | { | |
220 | ASSERT(ddt_object_exists(ddt, type, class)); | |
221 | ||
222 | return (ddt_ops[type]->ddt_op_walk(ddt->ddt_os, | |
223 | ddt->ddt_object[type][class], dde, walk)); | |
224 | } | |
225 | ||
e8fd45a0 BB |
226 | int |
227 | ddt_object_count(ddt_t *ddt, enum ddt_type type, enum ddt_class class, | |
228 | uint64_t *count) | |
428870ff BB |
229 | { |
230 | ASSERT(ddt_object_exists(ddt, type, class)); | |
231 | ||
232 | return (ddt_ops[type]->ddt_op_count(ddt->ddt_os, | |
e8fd45a0 | 233 | ddt->ddt_object[type][class], count)); |
428870ff BB |
234 | } |
235 | ||
236 | int | |
237 | ddt_object_info(ddt_t *ddt, enum ddt_type type, enum ddt_class class, | |
238 | dmu_object_info_t *doi) | |
239 | { | |
240 | if (!ddt_object_exists(ddt, type, class)) | |
2e528b49 | 241 | return (SET_ERROR(ENOENT)); |
428870ff BB |
242 | |
243 | return (dmu_object_info(ddt->ddt_os, ddt->ddt_object[type][class], | |
244 | doi)); | |
245 | } | |
246 | ||
247 | boolean_t | |
248 | ddt_object_exists(ddt_t *ddt, enum ddt_type type, enum ddt_class class) | |
249 | { | |
250 | return (!!ddt->ddt_object[type][class]); | |
251 | } | |
252 | ||
253 | void | |
254 | ddt_object_name(ddt_t *ddt, enum ddt_type type, enum ddt_class class, | |
255 | char *name) | |
256 | { | |
c9e319fa | 257 | (void) snprintf(name, DDT_NAMELEN, DMU_POOL_DDT, |
428870ff BB |
258 | zio_checksum_table[ddt->ddt_checksum].ci_name, |
259 | ddt_ops[type]->ddt_op_name, ddt_class_name[class]); | |
260 | } | |
261 | ||
262 | void | |
263 | ddt_bp_fill(const ddt_phys_t *ddp, blkptr_t *bp, uint64_t txg) | |
264 | { | |
d3bafe45 | 265 | ASSERT3U(txg, !=, 0); |
428870ff | 266 | |
1c27024e | 267 | for (int d = 0; d < SPA_DVAS_PER_BP; d++) |
428870ff BB |
268 | bp->blk_dva[d] = ddp->ddp_dva[d]; |
269 | BP_SET_BIRTH(bp, txg, ddp->ddp_phys_birth); | |
270 | } | |
271 | ||
b5256303 TC |
272 | /* |
273 | * The bp created via this function may be used for repairs and scrub, but it | |
274 | * will be missing the salt / IV required to do a full decrypting read. | |
275 | */ | |
428870ff BB |
276 | void |
277 | ddt_bp_create(enum zio_checksum checksum, | |
278 | const ddt_key_t *ddk, const ddt_phys_t *ddp, blkptr_t *bp) | |
279 | { | |
280 | BP_ZERO(bp); | |
281 | ||
282 | if (ddp != NULL) | |
283 | ddt_bp_fill(ddp, bp, ddp->ddp_phys_birth); | |
284 | ||
285 | bp->blk_cksum = ddk->ddk_cksum; | |
428870ff BB |
286 | |
287 | BP_SET_LSIZE(bp, DDK_GET_LSIZE(ddk)); | |
288 | BP_SET_PSIZE(bp, DDK_GET_PSIZE(ddk)); | |
289 | BP_SET_COMPRESS(bp, DDK_GET_COMPRESS(ddk)); | |
b5256303 TC |
290 | BP_SET_CRYPT(bp, DDK_GET_CRYPT(ddk)); |
291 | BP_SET_FILL(bp, 1); | |
428870ff BB |
292 | BP_SET_CHECKSUM(bp, checksum); |
293 | BP_SET_TYPE(bp, DMU_OT_DEDUP); | |
294 | BP_SET_LEVEL(bp, 0); | |
52b68423 | 295 | BP_SET_DEDUP(bp, 1); |
428870ff BB |
296 | BP_SET_BYTEORDER(bp, ZFS_HOST_BYTEORDER); |
297 | } | |
298 | ||
299 | void | |
300 | ddt_key_fill(ddt_key_t *ddk, const blkptr_t *bp) | |
301 | { | |
302 | ddk->ddk_cksum = bp->blk_cksum; | |
303 | ddk->ddk_prop = 0; | |
304 | ||
b5256303 TC |
305 | ASSERT(BP_IS_ENCRYPTED(bp) || !BP_USES_CRYPT(bp)); |
306 | ||
428870ff BB |
307 | DDK_SET_LSIZE(ddk, BP_GET_LSIZE(bp)); |
308 | DDK_SET_PSIZE(ddk, BP_GET_PSIZE(bp)); | |
309 | DDK_SET_COMPRESS(ddk, BP_GET_COMPRESS(bp)); | |
b5256303 | 310 | DDK_SET_CRYPT(ddk, BP_USES_CRYPT(bp)); |
428870ff BB |
311 | } |
312 | ||
313 | void | |
314 | ddt_phys_fill(ddt_phys_t *ddp, const blkptr_t *bp) | |
315 | { | |
d3bafe45 | 316 | ASSERT0(ddp->ddp_phys_birth); |
428870ff | 317 | |
1c27024e | 318 | for (int d = 0; d < SPA_DVAS_PER_BP; d++) |
428870ff BB |
319 | ddp->ddp_dva[d] = bp->blk_dva[d]; |
320 | ddp->ddp_phys_birth = BP_PHYSICAL_BIRTH(bp); | |
321 | } | |
322 | ||
323 | void | |
324 | ddt_phys_clear(ddt_phys_t *ddp) | |
325 | { | |
861166b0 | 326 | memset(ddp, 0, sizeof (*ddp)); |
428870ff BB |
327 | } |
328 | ||
329 | void | |
330 | ddt_phys_addref(ddt_phys_t *ddp) | |
331 | { | |
332 | ddp->ddp_refcnt++; | |
333 | } | |
334 | ||
335 | void | |
336 | ddt_phys_decref(ddt_phys_t *ddp) | |
337 | { | |
5dc6af0e | 338 | if (ddp) { |
d3bafe45 | 339 | ASSERT3U(ddp->ddp_refcnt, >, 0); |
5dc6af0e BB |
340 | ddp->ddp_refcnt--; |
341 | } | |
428870ff BB |
342 | } |
343 | ||
344 | void | |
345 | ddt_phys_free(ddt_t *ddt, ddt_key_t *ddk, ddt_phys_t *ddp, uint64_t txg) | |
346 | { | |
347 | blkptr_t blk; | |
348 | ||
349 | ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk); | |
52b68423 BB |
350 | |
351 | /* | |
352 | * We clear the dedup bit so that zio_free() will actually free the | |
353 | * space, rather than just decrementing the refcount in the DDT. | |
354 | */ | |
355 | BP_SET_DEDUP(&blk, 0); | |
356 | ||
428870ff BB |
357 | ddt_phys_clear(ddp); |
358 | zio_free(ddt->ddt_spa, txg, &blk); | |
359 | } | |
360 | ||
361 | ddt_phys_t * | |
362 | ddt_phys_select(const ddt_entry_t *dde, const blkptr_t *bp) | |
363 | { | |
364 | ddt_phys_t *ddp = (ddt_phys_t *)dde->dde_phys; | |
365 | ||
1c27024e | 366 | for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { |
428870ff BB |
367 | if (DVA_EQUAL(BP_IDENTITY(bp), &ddp->ddp_dva[0]) && |
368 | BP_PHYSICAL_BIRTH(bp) == ddp->ddp_phys_birth) | |
369 | return (ddp); | |
370 | } | |
371 | return (NULL); | |
372 | } | |
373 | ||
374 | uint64_t | |
375 | ddt_phys_total_refcnt(const ddt_entry_t *dde) | |
376 | { | |
377 | uint64_t refcnt = 0; | |
378 | ||
1c27024e | 379 | for (int p = DDT_PHYS_SINGLE; p <= DDT_PHYS_TRIPLE; p++) |
428870ff BB |
380 | refcnt += dde->dde_phys[p].ddp_refcnt; |
381 | ||
382 | return (refcnt); | |
383 | } | |
384 | ||
385 | static void | |
386 | ddt_stat_generate(ddt_t *ddt, ddt_entry_t *dde, ddt_stat_t *dds) | |
387 | { | |
388 | spa_t *spa = ddt->ddt_spa; | |
389 | ddt_phys_t *ddp = dde->dde_phys; | |
390 | ddt_key_t *ddk = &dde->dde_key; | |
391 | uint64_t lsize = DDK_GET_LSIZE(ddk); | |
392 | uint64_t psize = DDK_GET_PSIZE(ddk); | |
393 | ||
861166b0 | 394 | memset(dds, 0, sizeof (*dds)); |
428870ff | 395 | |
1c27024e | 396 | for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { |
428870ff BB |
397 | uint64_t dsize = 0; |
398 | uint64_t refcnt = ddp->ddp_refcnt; | |
399 | ||
400 | if (ddp->ddp_phys_birth == 0) | |
401 | continue; | |
402 | ||
1c27024e | 403 | for (int d = 0; d < DDE_GET_NDVAS(dde); d++) |
428870ff BB |
404 | dsize += dva_get_dsize_sync(spa, &ddp->ddp_dva[d]); |
405 | ||
406 | dds->dds_blocks += 1; | |
407 | dds->dds_lsize += lsize; | |
408 | dds->dds_psize += psize; | |
409 | dds->dds_dsize += dsize; | |
410 | ||
411 | dds->dds_ref_blocks += refcnt; | |
412 | dds->dds_ref_lsize += lsize * refcnt; | |
413 | dds->dds_ref_psize += psize * refcnt; | |
414 | dds->dds_ref_dsize += dsize * refcnt; | |
415 | } | |
416 | } | |
417 | ||
418 | void | |
419 | ddt_stat_add(ddt_stat_t *dst, const ddt_stat_t *src, uint64_t neg) | |
420 | { | |
421 | const uint64_t *s = (const uint64_t *)src; | |
422 | uint64_t *d = (uint64_t *)dst; | |
423 | uint64_t *d_end = (uint64_t *)(dst + 1); | |
424 | ||
425 | ASSERT(neg == 0 || neg == -1ULL); /* add or subtract */ | |
426 | ||
6a19dea7 AL |
427 | for (int i = 0; i < d_end - d; i++) |
428 | d[i] += (s[i] ^ neg) - neg; | |
428870ff BB |
429 | } |
430 | ||
431 | static void | |
432 | ddt_stat_update(ddt_t *ddt, ddt_entry_t *dde, uint64_t neg) | |
433 | { | |
434 | ddt_stat_t dds; | |
435 | ddt_histogram_t *ddh; | |
436 | int bucket; | |
437 | ||
438 | ddt_stat_generate(ddt, dde, &dds); | |
439 | ||
9bd274dd | 440 | bucket = highbit64(dds.dds_ref_blocks) - 1; |
d3bafe45 | 441 | ASSERT3U(bucket, >=, 0); |
428870ff BB |
442 | |
443 | ddh = &ddt->ddt_histogram[dde->dde_type][dde->dde_class]; | |
444 | ||
445 | ddt_stat_add(&ddh->ddh_stat[bucket], &dds, neg); | |
446 | } | |
447 | ||
448 | void | |
449 | ddt_histogram_add(ddt_histogram_t *dst, const ddt_histogram_t *src) | |
450 | { | |
1c27024e | 451 | for (int h = 0; h < 64; h++) |
428870ff BB |
452 | ddt_stat_add(&dst->ddh_stat[h], &src->ddh_stat[h], 0); |
453 | } | |
454 | ||
455 | void | |
456 | ddt_histogram_stat(ddt_stat_t *dds, const ddt_histogram_t *ddh) | |
457 | { | |
861166b0 | 458 | memset(dds, 0, sizeof (*dds)); |
428870ff | 459 | |
1c27024e | 460 | for (int h = 0; h < 64; h++) |
428870ff BB |
461 | ddt_stat_add(dds, &ddh->ddh_stat[h], 0); |
462 | } | |
463 | ||
464 | boolean_t | |
465 | ddt_histogram_empty(const ddt_histogram_t *ddh) | |
466 | { | |
467 | const uint64_t *s = (const uint64_t *)ddh; | |
468 | const uint64_t *s_end = (const uint64_t *)(ddh + 1); | |
469 | ||
470 | while (s < s_end) | |
471 | if (*s++ != 0) | |
472 | return (B_FALSE); | |
473 | ||
474 | return (B_TRUE); | |
475 | } | |
476 | ||
477 | void | |
478 | ddt_get_dedup_object_stats(spa_t *spa, ddt_object_t *ddo_total) | |
479 | { | |
480 | /* Sum the statistics we cached in ddt_object_sync(). */ | |
1c27024e | 481 | for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { |
428870ff | 482 | ddt_t *ddt = spa->spa_ddt[c]; |
1c27024e DB |
483 | for (enum ddt_type type = 0; type < DDT_TYPES; type++) { |
484 | for (enum ddt_class class = 0; class < DDT_CLASSES; | |
428870ff BB |
485 | class++) { |
486 | ddt_object_t *ddo = | |
487 | &ddt->ddt_object_stats[type][class]; | |
488 | ddo_total->ddo_count += ddo->ddo_count; | |
489 | ddo_total->ddo_dspace += ddo->ddo_dspace; | |
490 | ddo_total->ddo_mspace += ddo->ddo_mspace; | |
491 | } | |
492 | } | |
493 | } | |
494 | ||
495 | /* ... and compute the averages. */ | |
496 | if (ddo_total->ddo_count != 0) { | |
497 | ddo_total->ddo_dspace /= ddo_total->ddo_count; | |
498 | ddo_total->ddo_mspace /= ddo_total->ddo_count; | |
428870ff BB |
499 | } |
500 | } | |
501 | ||
502 | void | |
503 | ddt_get_dedup_histogram(spa_t *spa, ddt_histogram_t *ddh) | |
504 | { | |
1c27024e | 505 | for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { |
428870ff | 506 | ddt_t *ddt = spa->spa_ddt[c]; |
1325434b | 507 | for (enum ddt_type type = 0; type < DDT_TYPES && ddt; type++) { |
1c27024e | 508 | for (enum ddt_class class = 0; class < DDT_CLASSES; |
428870ff BB |
509 | class++) { |
510 | ddt_histogram_add(ddh, | |
511 | &ddt->ddt_histogram_cache[type][class]); | |
512 | } | |
513 | } | |
514 | } | |
515 | } | |
516 | ||
517 | void | |
518 | ddt_get_dedup_stats(spa_t *spa, ddt_stat_t *dds_total) | |
519 | { | |
520 | ddt_histogram_t *ddh_total; | |
521 | ||
79c76d5b | 522 | ddh_total = kmem_zalloc(sizeof (ddt_histogram_t), KM_SLEEP); |
428870ff BB |
523 | ddt_get_dedup_histogram(spa, ddh_total); |
524 | ddt_histogram_stat(dds_total, ddh_total); | |
525 | kmem_free(ddh_total, sizeof (ddt_histogram_t)); | |
526 | } | |
527 | ||
528 | uint64_t | |
529 | ddt_get_dedup_dspace(spa_t *spa) | |
530 | { | |
e8a20144 GN |
531 | ddt_stat_t dds_total; |
532 | ||
533 | if (spa->spa_dedup_dspace != ~0ULL) | |
534 | return (spa->spa_dedup_dspace); | |
535 | ||
861166b0 | 536 | memset(&dds_total, 0, sizeof (ddt_stat_t)); |
428870ff | 537 | |
e8a20144 | 538 | /* Calculate and cache the stats */ |
428870ff | 539 | ddt_get_dedup_stats(spa, &dds_total); |
e8a20144 GN |
540 | spa->spa_dedup_dspace = dds_total.dds_ref_dsize - dds_total.dds_dsize; |
541 | return (spa->spa_dedup_dspace); | |
428870ff BB |
542 | } |
543 | ||
544 | uint64_t | |
545 | ddt_get_pool_dedup_ratio(spa_t *spa) | |
546 | { | |
547 | ddt_stat_t dds_total = { 0 }; | |
548 | ||
549 | ddt_get_dedup_stats(spa, &dds_total); | |
550 | if (dds_total.dds_dsize == 0) | |
551 | return (100); | |
552 | ||
553 | return (dds_total.dds_ref_dsize * 100 / dds_total.dds_dsize); | |
554 | } | |
555 | ||
428870ff BB |
556 | ddt_t * |
557 | ddt_select(spa_t *spa, const blkptr_t *bp) | |
558 | { | |
559 | return (spa->spa_ddt[BP_GET_CHECKSUM(bp)]); | |
560 | } | |
561 | ||
562 | void | |
563 | ddt_enter(ddt_t *ddt) | |
564 | { | |
565 | mutex_enter(&ddt->ddt_lock); | |
566 | } | |
567 | ||
568 | void | |
569 | ddt_exit(ddt_t *ddt) | |
570 | { | |
571 | mutex_exit(&ddt->ddt_lock); | |
572 | } | |
573 | ||
ecf3d9b8 JL |
574 | void |
575 | ddt_init(void) | |
576 | { | |
577 | ddt_cache = kmem_cache_create("ddt_cache", | |
578 | sizeof (ddt_t), 0, NULL, NULL, NULL, NULL, NULL, 0); | |
579 | ddt_entry_cache = kmem_cache_create("ddt_entry_cache", | |
580 | sizeof (ddt_entry_t), 0, NULL, NULL, NULL, NULL, NULL, 0); | |
581 | } | |
582 | ||
583 | void | |
584 | ddt_fini(void) | |
585 | { | |
586 | kmem_cache_destroy(ddt_entry_cache); | |
587 | kmem_cache_destroy(ddt_cache); | |
588 | } | |
589 | ||
428870ff BB |
590 | static ddt_entry_t * |
591 | ddt_alloc(const ddt_key_t *ddk) | |
592 | { | |
593 | ddt_entry_t *dde; | |
594 | ||
79c76d5b | 595 | dde = kmem_cache_alloc(ddt_entry_cache, KM_SLEEP); |
861166b0 | 596 | memset(dde, 0, sizeof (ddt_entry_t)); |
428870ff BB |
597 | cv_init(&dde->dde_cv, NULL, CV_DEFAULT, NULL); |
598 | ||
599 | dde->dde_key = *ddk; | |
600 | ||
601 | return (dde); | |
602 | } | |
603 | ||
604 | static void | |
605 | ddt_free(ddt_entry_t *dde) | |
606 | { | |
607 | ASSERT(!dde->dde_loading); | |
608 | ||
1c27024e | 609 | for (int p = 0; p < DDT_PHYS_TYPES; p++) |
d3bafe45 | 610 | ASSERT3P(dde->dde_lead_zio[p], ==, NULL); |
428870ff | 611 | |
a6255b7f DQ |
612 | if (dde->dde_repair_abd != NULL) |
613 | abd_free(dde->dde_repair_abd); | |
428870ff BB |
614 | |
615 | cv_destroy(&dde->dde_cv); | |
ecf3d9b8 | 616 | kmem_cache_free(ddt_entry_cache, dde); |
428870ff BB |
617 | } |
618 | ||
619 | void | |
620 | ddt_remove(ddt_t *ddt, ddt_entry_t *dde) | |
621 | { | |
622 | ASSERT(MUTEX_HELD(&ddt->ddt_lock)); | |
623 | ||
624 | avl_remove(&ddt->ddt_tree, dde); | |
625 | ddt_free(dde); | |
626 | } | |
627 | ||
628 | ddt_entry_t * | |
629 | ddt_lookup(ddt_t *ddt, const blkptr_t *bp, boolean_t add) | |
630 | { | |
631 | ddt_entry_t *dde, dde_search; | |
632 | enum ddt_type type; | |
633 | enum ddt_class class; | |
634 | avl_index_t where; | |
635 | int error; | |
636 | ||
637 | ASSERT(MUTEX_HELD(&ddt->ddt_lock)); | |
638 | ||
639 | ddt_key_fill(&dde_search.dde_key, bp); | |
640 | ||
641 | dde = avl_find(&ddt->ddt_tree, &dde_search, &where); | |
642 | if (dde == NULL) { | |
643 | if (!add) | |
644 | return (NULL); | |
645 | dde = ddt_alloc(&dde_search.dde_key); | |
646 | avl_insert(&ddt->ddt_tree, dde, where); | |
647 | } | |
648 | ||
649 | while (dde->dde_loading) | |
650 | cv_wait(&dde->dde_cv, &ddt->ddt_lock); | |
651 | ||
652 | if (dde->dde_loaded) | |
653 | return (dde); | |
654 | ||
655 | dde->dde_loading = B_TRUE; | |
656 | ||
657 | ddt_exit(ddt); | |
658 | ||
659 | error = ENOENT; | |
660 | ||
661 | for (type = 0; type < DDT_TYPES; type++) { | |
662 | for (class = 0; class < DDT_CLASSES; class++) { | |
663 | error = ddt_object_lookup(ddt, type, class, dde); | |
a1d477c2 MA |
664 | if (error != ENOENT) { |
665 | ASSERT0(error); | |
428870ff | 666 | break; |
a1d477c2 | 667 | } |
428870ff BB |
668 | } |
669 | if (error != ENOENT) | |
670 | break; | |
671 | } | |
672 | ||
428870ff BB |
673 | ddt_enter(ddt); |
674 | ||
d3bafe45 RN |
675 | ASSERT(!dde->dde_loaded); |
676 | ASSERT(dde->dde_loading); | |
428870ff BB |
677 | |
678 | dde->dde_type = type; /* will be DDT_TYPES if no entry found */ | |
679 | dde->dde_class = class; /* will be DDT_CLASSES if no entry found */ | |
680 | dde->dde_loaded = B_TRUE; | |
681 | dde->dde_loading = B_FALSE; | |
682 | ||
683 | if (error == 0) | |
684 | ddt_stat_update(ddt, dde, -1ULL); | |
685 | ||
686 | cv_broadcast(&dde->dde_cv); | |
687 | ||
688 | return (dde); | |
689 | } | |
690 | ||
691 | void | |
692 | ddt_prefetch(spa_t *spa, const blkptr_t *bp) | |
693 | { | |
694 | ddt_t *ddt; | |
695 | ddt_entry_t dde; | |
696 | ||
572e2857 | 697 | if (!zfs_dedup_prefetch || bp == NULL || !BP_GET_DEDUP(bp)) |
428870ff BB |
698 | return; |
699 | ||
700 | /* | |
572e2857 BB |
701 | * We only remove the DDT once all tables are empty and only |
702 | * prefetch dedup blocks when there are entries in the DDT. | |
703 | * Thus no locking is required as the DDT can't disappear on us. | |
428870ff BB |
704 | */ |
705 | ddt = ddt_select(spa, bp); | |
706 | ddt_key_fill(&dde.dde_key, bp); | |
707 | ||
1c27024e DB |
708 | for (enum ddt_type type = 0; type < DDT_TYPES; type++) { |
709 | for (enum ddt_class class = 0; class < DDT_CLASSES; class++) { | |
428870ff BB |
710 | ddt_object_prefetch(ddt, type, class, &dde); |
711 | } | |
712 | } | |
713 | } | |
714 | ||
ee36c709 GN |
715 | /* |
716 | * Opaque struct used for ddt_key comparison | |
717 | */ | |
718 | #define DDT_KEY_CMP_LEN (sizeof (ddt_key_t) / sizeof (uint16_t)) | |
719 | ||
720 | typedef struct ddt_key_cmp { | |
721 | uint16_t u16[DDT_KEY_CMP_LEN]; | |
722 | } ddt_key_cmp_t; | |
723 | ||
428870ff BB |
724 | int |
725 | ddt_entry_compare(const void *x1, const void *x2) | |
726 | { | |
727 | const ddt_entry_t *dde1 = x1; | |
728 | const ddt_entry_t *dde2 = x2; | |
ee36c709 GN |
729 | const ddt_key_cmp_t *k1 = (const ddt_key_cmp_t *)&dde1->dde_key; |
730 | const ddt_key_cmp_t *k2 = (const ddt_key_cmp_t *)&dde2->dde_key; | |
731 | int32_t cmp = 0; | |
428870ff | 732 | |
1c27024e | 733 | for (int i = 0; i < DDT_KEY_CMP_LEN; i++) { |
ee36c709 GN |
734 | cmp = (int32_t)k1->u16[i] - (int32_t)k2->u16[i]; |
735 | if (likely(cmp)) | |
736 | break; | |
428870ff BB |
737 | } |
738 | ||
ca577779 | 739 | return (TREE_ISIGN(cmp)); |
428870ff BB |
740 | } |
741 | ||
742 | static ddt_t * | |
743 | ddt_table_alloc(spa_t *spa, enum zio_checksum c) | |
744 | { | |
745 | ddt_t *ddt; | |
746 | ||
79c76d5b | 747 | ddt = kmem_cache_alloc(ddt_cache, KM_SLEEP); |
861166b0 | 748 | memset(ddt, 0, sizeof (ddt_t)); |
428870ff BB |
749 | |
750 | mutex_init(&ddt->ddt_lock, NULL, MUTEX_DEFAULT, NULL); | |
751 | avl_create(&ddt->ddt_tree, ddt_entry_compare, | |
752 | sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node)); | |
753 | avl_create(&ddt->ddt_repair_tree, ddt_entry_compare, | |
754 | sizeof (ddt_entry_t), offsetof(ddt_entry_t, dde_node)); | |
755 | ddt->ddt_checksum = c; | |
756 | ddt->ddt_spa = spa; | |
757 | ddt->ddt_os = spa->spa_meta_objset; | |
758 | ||
759 | return (ddt); | |
760 | } | |
761 | ||
762 | static void | |
763 | ddt_table_free(ddt_t *ddt) | |
764 | { | |
d3bafe45 RN |
765 | ASSERT0(avl_numnodes(&ddt->ddt_tree)); |
766 | ASSERT0(avl_numnodes(&ddt->ddt_repair_tree)); | |
428870ff BB |
767 | avl_destroy(&ddt->ddt_tree); |
768 | avl_destroy(&ddt->ddt_repair_tree); | |
769 | mutex_destroy(&ddt->ddt_lock); | |
ecf3d9b8 | 770 | kmem_cache_free(ddt_cache, ddt); |
428870ff BB |
771 | } |
772 | ||
773 | void | |
774 | ddt_create(spa_t *spa) | |
775 | { | |
776 | spa->spa_dedup_checksum = ZIO_DEDUPCHECKSUM; | |
777 | ||
1c27024e | 778 | for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) |
428870ff BB |
779 | spa->spa_ddt[c] = ddt_table_alloc(spa, c); |
780 | } | |
781 | ||
782 | int | |
783 | ddt_load(spa_t *spa) | |
784 | { | |
785 | int error; | |
786 | ||
787 | ddt_create(spa); | |
788 | ||
789 | error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
790 | DMU_POOL_DDT_STATS, sizeof (uint64_t), 1, | |
791 | &spa->spa_ddt_stat_object); | |
792 | ||
793 | if (error) | |
794 | return (error == ENOENT ? 0 : error); | |
795 | ||
1c27024e | 796 | for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { |
428870ff | 797 | ddt_t *ddt = spa->spa_ddt[c]; |
1c27024e DB |
798 | for (enum ddt_type type = 0; type < DDT_TYPES; type++) { |
799 | for (enum ddt_class class = 0; class < DDT_CLASSES; | |
428870ff BB |
800 | class++) { |
801 | error = ddt_object_load(ddt, type, class); | |
802 | if (error != 0 && error != ENOENT) | |
803 | return (error); | |
804 | } | |
805 | } | |
806 | ||
807 | /* | |
808 | * Seed the cached histograms. | |
809 | */ | |
861166b0 | 810 | memcpy(&ddt->ddt_histogram_cache, ddt->ddt_histogram, |
428870ff | 811 | sizeof (ddt->ddt_histogram)); |
e8a20144 | 812 | spa->spa_dedup_dspace = ~0ULL; |
428870ff BB |
813 | } |
814 | ||
815 | return (0); | |
816 | } | |
817 | ||
818 | void | |
819 | ddt_unload(spa_t *spa) | |
820 | { | |
1c27024e | 821 | for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { |
428870ff BB |
822 | if (spa->spa_ddt[c]) { |
823 | ddt_table_free(spa->spa_ddt[c]); | |
824 | spa->spa_ddt[c] = NULL; | |
825 | } | |
826 | } | |
827 | } | |
828 | ||
829 | boolean_t | |
830 | ddt_class_contains(spa_t *spa, enum ddt_class max_class, const blkptr_t *bp) | |
831 | { | |
832 | ddt_t *ddt; | |
e95b3bdc | 833 | ddt_entry_t *dde; |
428870ff BB |
834 | |
835 | if (!BP_GET_DEDUP(bp)) | |
836 | return (B_FALSE); | |
837 | ||
838 | if (max_class == DDT_CLASS_UNIQUE) | |
839 | return (B_TRUE); | |
840 | ||
841 | ddt = spa->spa_ddt[BP_GET_CHECKSUM(bp)]; | |
79c76d5b | 842 | dde = kmem_cache_alloc(ddt_entry_cache, KM_SLEEP); |
428870ff | 843 | |
e95b3bdc | 844 | ddt_key_fill(&(dde->dde_key), bp); |
428870ff | 845 | |
1c27024e DB |
846 | for (enum ddt_type type = 0; type < DDT_TYPES; type++) { |
847 | for (enum ddt_class class = 0; class <= max_class; class++) { | |
e95b3bdc | 848 | if (ddt_object_lookup(ddt, type, class, dde) == 0) { |
ecf3d9b8 | 849 | kmem_cache_free(ddt_entry_cache, dde); |
428870ff | 850 | return (B_TRUE); |
e95b3bdc BB |
851 | } |
852 | } | |
853 | } | |
428870ff | 854 | |
ecf3d9b8 | 855 | kmem_cache_free(ddt_entry_cache, dde); |
428870ff BB |
856 | return (B_FALSE); |
857 | } | |
858 | ||
859 | ddt_entry_t * | |
860 | ddt_repair_start(ddt_t *ddt, const blkptr_t *bp) | |
861 | { | |
862 | ddt_key_t ddk; | |
863 | ddt_entry_t *dde; | |
864 | ||
865 | ddt_key_fill(&ddk, bp); | |
866 | ||
867 | dde = ddt_alloc(&ddk); | |
868 | ||
1c27024e DB |
869 | for (enum ddt_type type = 0; type < DDT_TYPES; type++) { |
870 | for (enum ddt_class class = 0; class < DDT_CLASSES; class++) { | |
428870ff BB |
871 | /* |
872 | * We can only do repair if there are multiple copies | |
873 | * of the block. For anything in the UNIQUE class, | |
874 | * there's definitely only one copy, so don't even try. | |
875 | */ | |
876 | if (class != DDT_CLASS_UNIQUE && | |
877 | ddt_object_lookup(ddt, type, class, dde) == 0) | |
878 | return (dde); | |
879 | } | |
880 | } | |
881 | ||
861166b0 | 882 | memset(dde->dde_phys, 0, sizeof (dde->dde_phys)); |
428870ff BB |
883 | |
884 | return (dde); | |
885 | } | |
886 | ||
887 | void | |
888 | ddt_repair_done(ddt_t *ddt, ddt_entry_t *dde) | |
889 | { | |
890 | avl_index_t where; | |
891 | ||
892 | ddt_enter(ddt); | |
893 | ||
a6255b7f | 894 | if (dde->dde_repair_abd != NULL && spa_writeable(ddt->ddt_spa) && |
428870ff BB |
895 | avl_find(&ddt->ddt_repair_tree, dde, &where) == NULL) |
896 | avl_insert(&ddt->ddt_repair_tree, dde, where); | |
897 | else | |
898 | ddt_free(dde); | |
899 | ||
900 | ddt_exit(ddt); | |
901 | } | |
902 | ||
903 | static void | |
904 | ddt_repair_entry_done(zio_t *zio) | |
905 | { | |
906 | ddt_entry_t *rdde = zio->io_private; | |
907 | ||
908 | ddt_free(rdde); | |
909 | } | |
910 | ||
911 | static void | |
912 | ddt_repair_entry(ddt_t *ddt, ddt_entry_t *dde, ddt_entry_t *rdde, zio_t *rio) | |
913 | { | |
914 | ddt_phys_t *ddp = dde->dde_phys; | |
915 | ddt_phys_t *rddp = rdde->dde_phys; | |
916 | ddt_key_t *ddk = &dde->dde_key; | |
917 | ddt_key_t *rddk = &rdde->dde_key; | |
918 | zio_t *zio; | |
919 | blkptr_t blk; | |
920 | ||
921 | zio = zio_null(rio, rio->io_spa, NULL, | |
922 | ddt_repair_entry_done, rdde, rio->io_flags); | |
923 | ||
1c27024e | 924 | for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++, rddp++) { |
428870ff BB |
925 | if (ddp->ddp_phys_birth == 0 || |
926 | ddp->ddp_phys_birth != rddp->ddp_phys_birth || | |
861166b0 | 927 | memcmp(ddp->ddp_dva, rddp->ddp_dva, sizeof (ddp->ddp_dva))) |
428870ff BB |
928 | continue; |
929 | ddt_bp_create(ddt->ddt_checksum, ddk, ddp, &blk); | |
930 | zio_nowait(zio_rewrite(zio, zio->io_spa, 0, &blk, | |
a6255b7f | 931 | rdde->dde_repair_abd, DDK_GET_PSIZE(rddk), NULL, NULL, |
428870ff BB |
932 | ZIO_PRIORITY_SYNC_WRITE, ZIO_DDT_CHILD_FLAGS(zio), NULL)); |
933 | } | |
934 | ||
935 | zio_nowait(zio); | |
936 | } | |
937 | ||
938 | static void | |
939 | ddt_repair_table(ddt_t *ddt, zio_t *rio) | |
940 | { | |
941 | spa_t *spa = ddt->ddt_spa; | |
942 | ddt_entry_t *dde, *rdde_next, *rdde; | |
943 | avl_tree_t *t = &ddt->ddt_repair_tree; | |
944 | blkptr_t blk; | |
945 | ||
946 | if (spa_sync_pass(spa) > 1) | |
947 | return; | |
948 | ||
949 | ddt_enter(ddt); | |
950 | for (rdde = avl_first(t); rdde != NULL; rdde = rdde_next) { | |
951 | rdde_next = AVL_NEXT(t, rdde); | |
952 | avl_remove(&ddt->ddt_repair_tree, rdde); | |
953 | ddt_exit(ddt); | |
954 | ddt_bp_create(ddt->ddt_checksum, &rdde->dde_key, NULL, &blk); | |
955 | dde = ddt_repair_start(ddt, &blk); | |
956 | ddt_repair_entry(ddt, dde, rdde, rio); | |
957 | ddt_repair_done(ddt, dde); | |
958 | ddt_enter(ddt); | |
959 | } | |
960 | ddt_exit(ddt); | |
961 | } | |
962 | ||
963 | static void | |
964 | ddt_sync_entry(ddt_t *ddt, ddt_entry_t *dde, dmu_tx_t *tx, uint64_t txg) | |
965 | { | |
966 | dsl_pool_t *dp = ddt->ddt_spa->spa_dsl_pool; | |
967 | ddt_phys_t *ddp = dde->dde_phys; | |
968 | ddt_key_t *ddk = &dde->dde_key; | |
969 | enum ddt_type otype = dde->dde_type; | |
970 | enum ddt_type ntype = DDT_TYPE_CURRENT; | |
971 | enum ddt_class oclass = dde->dde_class; | |
972 | enum ddt_class nclass; | |
973 | uint64_t total_refcnt = 0; | |
974 | ||
975 | ASSERT(dde->dde_loaded); | |
976 | ASSERT(!dde->dde_loading); | |
977 | ||
1c27024e | 978 | for (int p = 0; p < DDT_PHYS_TYPES; p++, ddp++) { |
d3bafe45 | 979 | ASSERT3P(dde->dde_lead_zio[p], ==, NULL); |
428870ff | 980 | if (ddp->ddp_phys_birth == 0) { |
d3bafe45 | 981 | ASSERT0(ddp->ddp_refcnt); |
428870ff BB |
982 | continue; |
983 | } | |
984 | if (p == DDT_PHYS_DITTO) { | |
050d720c MA |
985 | /* |
986 | * Note, we no longer create DDT-DITTO blocks, but we | |
987 | * don't want to leak any written by older software. | |
988 | */ | |
989 | ddt_phys_free(ddt, ddk, ddp, txg); | |
428870ff BB |
990 | continue; |
991 | } | |
992 | if (ddp->ddp_refcnt == 0) | |
993 | ddt_phys_free(ddt, ddk, ddp, txg); | |
994 | total_refcnt += ddp->ddp_refcnt; | |
995 | } | |
996 | ||
050d720c MA |
997 | /* We do not create new DDT-DITTO blocks. */ |
998 | ASSERT0(dde->dde_phys[DDT_PHYS_DITTO].ddp_phys_birth); | |
999 | if (total_refcnt > 1) | |
428870ff BB |
1000 | nclass = DDT_CLASS_DUPLICATE; |
1001 | else | |
1002 | nclass = DDT_CLASS_UNIQUE; | |
1003 | ||
1004 | if (otype != DDT_TYPES && | |
1005 | (otype != ntype || oclass != nclass || total_refcnt == 0)) { | |
d3bafe45 RN |
1006 | VERIFY0(ddt_object_remove(ddt, otype, oclass, dde, tx)); |
1007 | ASSERT3U( | |
1008 | ddt_object_lookup(ddt, otype, oclass, dde), ==, ENOENT); | |
428870ff BB |
1009 | } |
1010 | ||
1011 | if (total_refcnt != 0) { | |
1012 | dde->dde_type = ntype; | |
1013 | dde->dde_class = nclass; | |
1014 | ddt_stat_update(ddt, dde, 0); | |
1015 | if (!ddt_object_exists(ddt, ntype, nclass)) | |
1016 | ddt_object_create(ddt, ntype, nclass, tx); | |
d3bafe45 | 1017 | VERIFY0(ddt_object_update(ddt, ntype, nclass, dde, tx)); |
428870ff BB |
1018 | |
1019 | /* | |
1020 | * If the class changes, the order that we scan this bp | |
1021 | * changes. If it decreases, we could miss it, so | |
1022 | * scan it right now. (This covers both class changing | |
1023 | * while we are doing ddt_walk(), and when we are | |
1024 | * traversing.) | |
1025 | */ | |
1026 | if (nclass < oclass) { | |
1027 | dsl_scan_ddt_entry(dp->dp_scan, | |
1028 | ddt->ddt_checksum, dde, tx); | |
1029 | } | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | static void | |
1034 | ddt_sync_table(ddt_t *ddt, dmu_tx_t *tx, uint64_t txg) | |
1035 | { | |
1036 | spa_t *spa = ddt->ddt_spa; | |
1037 | ddt_entry_t *dde; | |
1038 | void *cookie = NULL; | |
1039 | ||
1040 | if (avl_numnodes(&ddt->ddt_tree) == 0) | |
1041 | return; | |
1042 | ||
d3bafe45 | 1043 | ASSERT3U(spa->spa_uberblock.ub_version, >=, SPA_VERSION_DEDUP); |
428870ff BB |
1044 | |
1045 | if (spa->spa_ddt_stat_object == 0) { | |
9ae529ec CS |
1046 | spa->spa_ddt_stat_object = zap_create_link(ddt->ddt_os, |
1047 | DMU_OT_DDT_STATS, DMU_POOL_DIRECTORY_OBJECT, | |
1048 | DMU_POOL_DDT_STATS, tx); | |
428870ff BB |
1049 | } |
1050 | ||
1051 | while ((dde = avl_destroy_nodes(&ddt->ddt_tree, &cookie)) != NULL) { | |
1052 | ddt_sync_entry(ddt, dde, tx, txg); | |
1053 | ddt_free(dde); | |
1054 | } | |
1055 | ||
1c27024e | 1056 | for (enum ddt_type type = 0; type < DDT_TYPES; type++) { |
e8fd45a0 | 1057 | uint64_t add, count = 0; |
1c27024e | 1058 | for (enum ddt_class class = 0; class < DDT_CLASSES; class++) { |
572e2857 BB |
1059 | if (ddt_object_exists(ddt, type, class)) { |
1060 | ddt_object_sync(ddt, type, class, tx); | |
d3bafe45 RN |
1061 | VERIFY0(ddt_object_count(ddt, type, class, |
1062 | &add)); | |
e8fd45a0 | 1063 | count += add; |
572e2857 BB |
1064 | } |
1065 | } | |
1c27024e | 1066 | for (enum ddt_class class = 0; class < DDT_CLASSES; class++) { |
572e2857 | 1067 | if (count == 0 && ddt_object_exists(ddt, type, class)) |
428870ff BB |
1068 | ddt_object_destroy(ddt, type, class, tx); |
1069 | } | |
1070 | } | |
1071 | ||
861166b0 | 1072 | memcpy(&ddt->ddt_histogram_cache, ddt->ddt_histogram, |
428870ff | 1073 | sizeof (ddt->ddt_histogram)); |
e8a20144 | 1074 | spa->spa_dedup_dspace = ~0ULL; |
428870ff BB |
1075 | } |
1076 | ||
1077 | void | |
1078 | ddt_sync(spa_t *spa, uint64_t txg) | |
1079 | { | |
d4a72f23 | 1080 | dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan; |
428870ff | 1081 | dmu_tx_t *tx; |
d4a72f23 | 1082 | zio_t *rio; |
428870ff | 1083 | |
d3bafe45 | 1084 | ASSERT3U(spa_syncing_txg(spa), ==, txg); |
428870ff BB |
1085 | |
1086 | tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg); | |
1087 | ||
d4a72f23 | 1088 | rio = zio_root(spa, NULL, NULL, |
a1d477c2 | 1089 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_SELF_HEAL); |
d4a72f23 TC |
1090 | |
1091 | /* | |
1092 | * This function may cause an immediate scan of ddt blocks (see | |
1093 | * the comment above dsl_scan_ddt() for details). We set the | |
1094 | * scan's root zio here so that we can wait for any scan IOs in | |
1095 | * addition to the regular ddt IOs. | |
1096 | */ | |
1097 | ASSERT3P(scn->scn_zio_root, ==, NULL); | |
1098 | scn->scn_zio_root = rio; | |
1099 | ||
1c27024e | 1100 | for (enum zio_checksum c = 0; c < ZIO_CHECKSUM_FUNCTIONS; c++) { |
428870ff BB |
1101 | ddt_t *ddt = spa->spa_ddt[c]; |
1102 | if (ddt == NULL) | |
1103 | continue; | |
1104 | ddt_sync_table(ddt, tx, txg); | |
1105 | ddt_repair_table(ddt, rio); | |
1106 | } | |
1107 | ||
1108 | (void) zio_wait(rio); | |
d4a72f23 | 1109 | scn->scn_zio_root = NULL; |
428870ff BB |
1110 | |
1111 | dmu_tx_commit(tx); | |
1112 | } | |
1113 | ||
1114 | int | |
1115 | ddt_walk(spa_t *spa, ddt_bookmark_t *ddb, ddt_entry_t *dde) | |
1116 | { | |
1117 | do { | |
1118 | do { | |
1119 | do { | |
1120 | ddt_t *ddt = spa->spa_ddt[ddb->ddb_checksum]; | |
1121 | int error = ENOENT; | |
1122 | if (ddt_object_exists(ddt, ddb->ddb_type, | |
1123 | ddb->ddb_class)) { | |
1124 | error = ddt_object_walk(ddt, | |
1125 | ddb->ddb_type, ddb->ddb_class, | |
1126 | &ddb->ddb_cursor, dde); | |
1127 | } | |
1128 | dde->dde_type = ddb->ddb_type; | |
1129 | dde->dde_class = ddb->ddb_class; | |
1130 | if (error == 0) | |
1131 | return (0); | |
1132 | if (error != ENOENT) | |
1133 | return (error); | |
1134 | ddb->ddb_cursor = 0; | |
1135 | } while (++ddb->ddb_checksum < ZIO_CHECKSUM_FUNCTIONS); | |
1136 | ddb->ddb_checksum = 0; | |
1137 | } while (++ddb->ddb_type < DDT_TYPES); | |
1138 | ddb->ddb_type = 0; | |
1139 | } while (++ddb->ddb_class < DDT_CLASSES); | |
1140 | ||
2e528b49 | 1141 | return (SET_ERROR(ENOENT)); |
428870ff | 1142 | } |
c409e464 | 1143 | |
67a1b037 PJD |
1144 | /* |
1145 | * This function is used by Block Cloning (brt.c) to increase reference | |
1146 | * counter for the DDT entry if the block is already in DDT. | |
1147 | * | |
1148 | * Return false if the block, despite having the D bit set, is not present | |
1149 | * in the DDT. Currently this is not possible but might be in the future. | |
1150 | * See the comment below. | |
1151 | */ | |
1152 | boolean_t | |
1153 | ddt_addref(spa_t *spa, const blkptr_t *bp) | |
1154 | { | |
1155 | ddt_t *ddt; | |
1156 | ddt_entry_t *dde; | |
1157 | boolean_t result; | |
1158 | ||
1159 | spa_config_enter(spa, SCL_ZIO, FTAG, RW_READER); | |
1160 | ddt = ddt_select(spa, bp); | |
1161 | ddt_enter(ddt); | |
1162 | ||
1163 | dde = ddt_lookup(ddt, bp, B_TRUE); | |
d3bafe45 | 1164 | ASSERT3P(dde, !=, NULL); |
67a1b037 PJD |
1165 | |
1166 | if (dde->dde_type < DDT_TYPES) { | |
1167 | ddt_phys_t *ddp; | |
1168 | ||
1169 | ASSERT3S(dde->dde_class, <, DDT_CLASSES); | |
1170 | ||
1171 | ddp = &dde->dde_phys[BP_GET_NDVAS(bp)]; | |
61ab05ca RN |
1172 | |
1173 | /* | |
1174 | * This entry already existed (dde_type is real), so it must | |
1175 | * have refcnt >0 at the start of this txg. We are called from | |
1176 | * brt_pending_apply(), before frees are issued, so the refcnt | |
1177 | * can't be lowered yet. Therefore, it must be >0. We assert | |
1178 | * this because if the order of BRT and DDT interactions were | |
1179 | * ever to change and the refcnt was ever zero here, then | |
1180 | * likely further action is required to fill out the DDT entry, | |
1181 | * and this is a place that is likely to be missed in testing. | |
1182 | */ | |
1183 | ASSERT3U(ddp->ddp_refcnt, >, 0); | |
1184 | ||
67a1b037 PJD |
1185 | ddt_phys_addref(ddp); |
1186 | result = B_TRUE; | |
1187 | } else { | |
1188 | /* | |
1189 | * At the time of implementating this if the block has the | |
1190 | * DEDUP flag set it must exist in the DEDUP table, but | |
1191 | * there are many advocates that want ability to remove | |
1192 | * entries from DDT with refcnt=1. If this will happen, | |
1193 | * we may have a block with the DEDUP set, but which doesn't | |
1194 | * have a corresponding entry in the DDT. Be ready. | |
1195 | */ | |
1196 | ASSERT3S(dde->dde_class, ==, DDT_CLASSES); | |
1197 | ddt_remove(ddt, dde); | |
1198 | result = B_FALSE; | |
1199 | } | |
1200 | ||
1201 | ddt_exit(ddt); | |
1202 | spa_config_exit(spa, SCL_ZIO, FTAG); | |
1203 | ||
1204 | return (result); | |
1205 | } | |
1206 | ||
a7929f31 | 1207 | ZFS_MODULE_PARAM(zfs_dedup, zfs_dedup_, prefetch, INT, ZMOD_RW, |
03fdcb9a | 1208 | "Enable prefetching dedup-ed blks"); |