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