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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
24 * Copyright 2013 Saso Kiselkov. All rights reserved.
25 */
26
27 #include <sys/zfs_context.h>
28 #include <sys/spa.h>
29 #include <sys/spa_impl.h>
30 #include <sys/zio.h>
31 #include <sys/zio_checksum.h>
32 #include <sys/zil.h>
33 #include <sys/abd.h>
34 #include <zfs_fletcher.h>
35
36 /*
37 * Checksum vectors.
38 *
39 * In the SPA, everything is checksummed. We support checksum vectors
40 * for three distinct reasons:
41 *
42 * 1. Different kinds of data need different levels of protection.
43 * For SPA metadata, we always want a very strong checksum.
44 * For user data, we let users make the trade-off between speed
45 * and checksum strength.
46 *
47 * 2. Cryptographic hash and MAC algorithms are an area of active research.
48 * It is likely that in future hash functions will be at least as strong
49 * as current best-of-breed, and may be substantially faster as well.
50 * We want the ability to take advantage of these new hashes as soon as
51 * they become available.
52 *
53 * 3. If someone develops hardware that can compute a strong hash quickly,
54 * we want the ability to take advantage of that hardware.
55 *
56 * Of course, we don't want a checksum upgrade to invalidate existing
57 * data, so we store the checksum *function* in eight bits of the bp.
58 * This gives us room for up to 256 different checksum functions.
59 *
60 * When writing a block, we always checksum it with the latest-and-greatest
61 * checksum function of the appropriate strength. When reading a block,
62 * we compare the expected checksum against the actual checksum, which we
63 * compute via the checksum function specified by BP_GET_CHECKSUM(bp).
64 *
65 * SALTED CHECKSUMS
66 *
67 * To enable the use of less secure hash algorithms with dedup, we
68 * introduce the notion of salted checksums (MACs, really). A salted
69 * checksum is fed both a random 256-bit value (the salt) and the data
70 * to be checksummed. This salt is kept secret (stored on the pool, but
71 * never shown to the user). Thus even if an attacker knew of collision
72 * weaknesses in the hash algorithm, they won't be able to mount a known
73 * plaintext attack on the DDT, since the actual hash value cannot be
74 * known ahead of time. How the salt is used is algorithm-specific
75 * (some might simply prefix it to the data block, others might need to
76 * utilize a full-blown HMAC). On disk the salt is stored in a ZAP
77 * object in the MOS (DMU_POOL_CHECKSUM_SALT).
78 *
79 * CONTEXT TEMPLATES
80 *
81 * Some hashing algorithms need to perform a substantial amount of
82 * initialization work (e.g. salted checksums above may need to pre-hash
83 * the salt) before being able to process data. Performing this
84 * redundant work for each block would be wasteful, so we instead allow
85 * a checksum algorithm to do the work once (the first time it's used)
86 * and then keep this pre-initialized context as a template inside the
87 * spa_t (spa_cksum_tmpls). If the zio_checksum_info_t contains
88 * non-NULL ci_tmpl_init and ci_tmpl_free callbacks, they are used to
89 * construct and destruct the pre-initialized checksum context. The
90 * pre-initialized context is then reused during each checksum
91 * invocation and passed to the checksum function.
92 */
93
94 static void
abd_checksum_off(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)95 abd_checksum_off(abd_t *abd, uint64_t size,
96 const void *ctx_template, zio_cksum_t *zcp)
97 {
98 (void) abd, (void) size, (void) ctx_template;
99 ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
100 }
101
102 static void
abd_fletcher_2_native(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)103 abd_fletcher_2_native(abd_t *abd, uint64_t size,
104 const void *ctx_template, zio_cksum_t *zcp)
105 {
106 (void) ctx_template;
107 fletcher_init(zcp);
108 (void) abd_iterate_func(abd, 0, size,
109 fletcher_2_incremental_native, zcp);
110 }
111
112 static void
abd_fletcher_2_byteswap(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)113 abd_fletcher_2_byteswap(abd_t *abd, uint64_t size,
114 const void *ctx_template, zio_cksum_t *zcp)
115 {
116 (void) ctx_template;
117 fletcher_init(zcp);
118 (void) abd_iterate_func(abd, 0, size,
119 fletcher_2_incremental_byteswap, zcp);
120 }
121
122 static inline void
abd_fletcher_4_impl(abd_t * abd,uint64_t size,zio_abd_checksum_data_t * acdp)123 abd_fletcher_4_impl(abd_t *abd, uint64_t size, zio_abd_checksum_data_t *acdp)
124 {
125 fletcher_4_abd_ops.acf_init(acdp);
126 abd_iterate_func(abd, 0, size, fletcher_4_abd_ops.acf_iter, acdp);
127 fletcher_4_abd_ops.acf_fini(acdp);
128 }
129
130 void
abd_fletcher_4_native(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)131 abd_fletcher_4_native(abd_t *abd, uint64_t size,
132 const void *ctx_template, zio_cksum_t *zcp)
133 {
134 (void) ctx_template;
135 fletcher_4_ctx_t ctx;
136
137 zio_abd_checksum_data_t acd = {
138 .acd_byteorder = ZIO_CHECKSUM_NATIVE,
139 .acd_zcp = zcp,
140 .acd_ctx = &ctx
141 };
142
143 abd_fletcher_4_impl(abd, size, &acd);
144
145 }
146
147 void
abd_fletcher_4_byteswap(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)148 abd_fletcher_4_byteswap(abd_t *abd, uint64_t size,
149 const void *ctx_template, zio_cksum_t *zcp)
150 {
151 (void) ctx_template;
152 fletcher_4_ctx_t ctx;
153
154 zio_abd_checksum_data_t acd = {
155 .acd_byteorder = ZIO_CHECKSUM_BYTESWAP,
156 .acd_zcp = zcp,
157 .acd_ctx = &ctx
158 };
159
160 abd_fletcher_4_impl(abd, size, &acd);
161 }
162
163 /*
164 * Checksum vectors.
165 *
166 * Note: you cannot change the name string for these functions, as they are
167 * embedded in on-disk data in some places (eg dedup table names).
168 */
169 zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
170 {{NULL, NULL}, NULL, NULL, 0, "inherit"},
171 {{NULL, NULL}, NULL, NULL, 0, "on"},
172 {{abd_checksum_off, abd_checksum_off},
173 NULL, NULL, 0, "off"},
174 {{abd_checksum_sha256, abd_checksum_sha256},
175 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
176 "label"},
177 {{abd_checksum_sha256, abd_checksum_sha256},
178 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
179 "gang_header"},
180 {{abd_fletcher_2_native, abd_fletcher_2_byteswap},
181 NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog"},
182 {{abd_fletcher_2_native, abd_fletcher_2_byteswap},
183 NULL, NULL, 0, "fletcher2"},
184 {{abd_fletcher_4_native, abd_fletcher_4_byteswap},
185 NULL, NULL, ZCHECKSUM_FLAG_METADATA, "fletcher4"},
186 {{abd_checksum_sha256, abd_checksum_sha256},
187 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
188 ZCHECKSUM_FLAG_NOPWRITE, "sha256"},
189 {{abd_fletcher_4_native, abd_fletcher_4_byteswap},
190 NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog2"},
191 {{abd_checksum_off, abd_checksum_off},
192 NULL, NULL, 0, "noparity"},
193 {{abd_checksum_sha512_native, abd_checksum_sha512_byteswap},
194 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
195 ZCHECKSUM_FLAG_NOPWRITE, "sha512"},
196 {{abd_checksum_skein_native, abd_checksum_skein_byteswap},
197 abd_checksum_skein_tmpl_init, abd_checksum_skein_tmpl_free,
198 ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
199 ZCHECKSUM_FLAG_SALTED | ZCHECKSUM_FLAG_NOPWRITE, "skein"},
200 {{abd_checksum_edonr_native, abd_checksum_edonr_byteswap},
201 abd_checksum_edonr_tmpl_init, abd_checksum_edonr_tmpl_free,
202 ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_SALTED |
203 ZCHECKSUM_FLAG_NOPWRITE, "edonr"},
204 {{abd_checksum_blake3_native, abd_checksum_blake3_byteswap},
205 abd_checksum_blake3_tmpl_init, abd_checksum_blake3_tmpl_free,
206 ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
207 ZCHECKSUM_FLAG_SALTED | ZCHECKSUM_FLAG_NOPWRITE, "blake3"},
208 };
209
210 /*
211 * The flag corresponding to the "verify" in dedup=[checksum,]verify
212 * must be cleared first, so callers should use ZIO_CHECKSUM_MASK.
213 */
214 spa_feature_t
zio_checksum_to_feature(enum zio_checksum cksum)215 zio_checksum_to_feature(enum zio_checksum cksum)
216 {
217 VERIFY((cksum & ~ZIO_CHECKSUM_MASK) == 0);
218
219 switch (cksum) {
220 case ZIO_CHECKSUM_BLAKE3:
221 return (SPA_FEATURE_BLAKE3);
222 case ZIO_CHECKSUM_SHA512:
223 return (SPA_FEATURE_SHA512);
224 case ZIO_CHECKSUM_SKEIN:
225 return (SPA_FEATURE_SKEIN);
226 case ZIO_CHECKSUM_EDONR:
227 return (SPA_FEATURE_EDONR);
228 default:
229 return (SPA_FEATURE_NONE);
230 }
231 }
232
233 enum zio_checksum
zio_checksum_select(enum zio_checksum child,enum zio_checksum parent)234 zio_checksum_select(enum zio_checksum child, enum zio_checksum parent)
235 {
236 ASSERT(child < ZIO_CHECKSUM_FUNCTIONS);
237 ASSERT(parent < ZIO_CHECKSUM_FUNCTIONS);
238 ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
239
240 if (child == ZIO_CHECKSUM_INHERIT)
241 return (parent);
242
243 if (child == ZIO_CHECKSUM_ON)
244 return (ZIO_CHECKSUM_ON_VALUE);
245
246 return (child);
247 }
248
249 enum zio_checksum
zio_checksum_dedup_select(spa_t * spa,enum zio_checksum child,enum zio_checksum parent)250 zio_checksum_dedup_select(spa_t *spa, enum zio_checksum child,
251 enum zio_checksum parent)
252 {
253 ASSERT((child & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
254 ASSERT((parent & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
255 ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
256
257 if (child == ZIO_CHECKSUM_INHERIT)
258 return (parent);
259
260 if (child == ZIO_CHECKSUM_ON)
261 return (spa_dedup_checksum(spa));
262
263 if (child == (ZIO_CHECKSUM_ON | ZIO_CHECKSUM_VERIFY))
264 return (spa_dedup_checksum(spa) | ZIO_CHECKSUM_VERIFY);
265
266 ASSERT((zio_checksum_table[child & ZIO_CHECKSUM_MASK].ci_flags &
267 ZCHECKSUM_FLAG_DEDUP) ||
268 (child & ZIO_CHECKSUM_VERIFY) || child == ZIO_CHECKSUM_OFF);
269
270 return (child);
271 }
272
273 /*
274 * Set the external verifier for a gang block based on <vdev, offset, txg>,
275 * a tuple which is guaranteed to be unique for the life of the pool.
276 */
277 static void
zio_checksum_gang_verifier(zio_cksum_t * zcp,const blkptr_t * bp)278 zio_checksum_gang_verifier(zio_cksum_t *zcp, const blkptr_t *bp)
279 {
280 const dva_t *dva = BP_IDENTITY(bp);
281 uint64_t txg = BP_GET_BIRTH(bp);
282
283 ASSERT(BP_IS_GANG(bp));
284
285 ZIO_SET_CHECKSUM(zcp, DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva), txg, 0);
286 }
287
288 /*
289 * Set the external verifier for a label block based on its offset.
290 * The vdev is implicit, and the txg is unknowable at pool open time --
291 * hence the logic in vdev_uberblock_load() to find the most recent copy.
292 */
293 static void
zio_checksum_label_verifier(zio_cksum_t * zcp,uint64_t offset)294 zio_checksum_label_verifier(zio_cksum_t *zcp, uint64_t offset)
295 {
296 ZIO_SET_CHECKSUM(zcp, offset, 0, 0, 0);
297 }
298
299 /*
300 * Calls the template init function of a checksum which supports context
301 * templates and installs the template into the spa_t.
302 */
303 static void
zio_checksum_template_init(enum zio_checksum checksum,spa_t * spa)304 zio_checksum_template_init(enum zio_checksum checksum, spa_t *spa)
305 {
306 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
307
308 if (ci->ci_tmpl_init == NULL)
309 return;
310 if (spa->spa_cksum_tmpls[checksum] != NULL)
311 return;
312
313 VERIFY(ci->ci_tmpl_free != NULL);
314 mutex_enter(&spa->spa_cksum_tmpls_lock);
315 if (spa->spa_cksum_tmpls[checksum] == NULL) {
316 spa->spa_cksum_tmpls[checksum] =
317 ci->ci_tmpl_init(&spa->spa_cksum_salt);
318 VERIFY(spa->spa_cksum_tmpls[checksum] != NULL);
319 }
320 mutex_exit(&spa->spa_cksum_tmpls_lock);
321 }
322
323 /* convenience function to update a checksum to accommodate an encryption MAC */
324 static void
zio_checksum_handle_crypt(zio_cksum_t * cksum,zio_cksum_t * saved,boolean_t xor)325 zio_checksum_handle_crypt(zio_cksum_t *cksum, zio_cksum_t *saved, boolean_t xor)
326 {
327 /*
328 * Weak checksums do not have their entropy spread evenly
329 * across the bits of the checksum. Therefore, when truncating
330 * a weak checksum we XOR the first 2 words with the last 2 so
331 * that we don't "lose" any entropy unnecessarily.
332 */
333 if (xor) {
334 cksum->zc_word[0] ^= cksum->zc_word[2];
335 cksum->zc_word[1] ^= cksum->zc_word[3];
336 }
337
338 cksum->zc_word[2] = saved->zc_word[2];
339 cksum->zc_word[3] = saved->zc_word[3];
340 }
341
342 /*
343 * Generate the checksum.
344 */
345 void
zio_checksum_compute(zio_t * zio,enum zio_checksum checksum,abd_t * abd,uint64_t size)346 zio_checksum_compute(zio_t *zio, enum zio_checksum checksum,
347 abd_t *abd, uint64_t size)
348 {
349 static const uint64_t zec_magic = ZEC_MAGIC;
350 blkptr_t *bp = zio->io_bp;
351 uint64_t offset = zio->io_offset;
352 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
353 zio_cksum_t cksum, saved;
354 spa_t *spa = zio->io_spa;
355 boolean_t insecure = (ci->ci_flags & ZCHECKSUM_FLAG_DEDUP) == 0;
356
357 ASSERT((uint_t)checksum < ZIO_CHECKSUM_FUNCTIONS);
358 ASSERT(ci->ci_func[0] != NULL);
359
360 zio_checksum_template_init(checksum, spa);
361
362 if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
363 zio_eck_t eck;
364 size_t eck_offset;
365
366 memset(&saved, 0, sizeof (zio_cksum_t));
367
368 if (checksum == ZIO_CHECKSUM_ZILOG2) {
369 zil_chain_t zilc;
370 abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t));
371
372 uint64_t nused = P2ROUNDUP_TYPED(zilc.zc_nused,
373 ZIL_MIN_BLKSZ, uint64_t);
374 ASSERT3U(size, >=, nused);
375 size = nused;
376 eck = zilc.zc_eck;
377 eck_offset = offsetof(zil_chain_t, zc_eck);
378 } else {
379 ASSERT3U(size, >=, sizeof (zio_eck_t));
380 eck_offset = size - sizeof (zio_eck_t);
381 abd_copy_to_buf_off(&eck, abd, eck_offset,
382 sizeof (zio_eck_t));
383 }
384
385 if (checksum == ZIO_CHECKSUM_GANG_HEADER) {
386 zio_checksum_gang_verifier(&eck.zec_cksum, bp);
387 } else if (checksum == ZIO_CHECKSUM_LABEL) {
388 zio_checksum_label_verifier(&eck.zec_cksum, offset);
389 } else {
390 saved = eck.zec_cksum;
391 eck.zec_cksum = bp->blk_cksum;
392 }
393
394 abd_copy_from_buf_off(abd, &zec_magic,
395 eck_offset + offsetof(zio_eck_t, zec_magic),
396 sizeof (zec_magic));
397 abd_copy_from_buf_off(abd, &eck.zec_cksum,
398 eck_offset + offsetof(zio_eck_t, zec_cksum),
399 sizeof (zio_cksum_t));
400
401 ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum],
402 &cksum);
403 if (bp != NULL && BP_USES_CRYPT(bp) &&
404 BP_GET_TYPE(bp) != DMU_OT_OBJSET)
405 zio_checksum_handle_crypt(&cksum, &saved, insecure);
406
407 abd_copy_from_buf_off(abd, &cksum,
408 eck_offset + offsetof(zio_eck_t, zec_cksum),
409 sizeof (zio_cksum_t));
410 } else {
411 saved = bp->blk_cksum;
412 ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum],
413 &cksum);
414 if (BP_USES_CRYPT(bp) && BP_GET_TYPE(bp) != DMU_OT_OBJSET)
415 zio_checksum_handle_crypt(&cksum, &saved, insecure);
416 bp->blk_cksum = cksum;
417 }
418 }
419
420 int
zio_checksum_error_impl(spa_t * spa,const blkptr_t * bp,enum zio_checksum checksum,abd_t * abd,uint64_t size,uint64_t offset,zio_bad_cksum_t * info)421 zio_checksum_error_impl(spa_t *spa, const blkptr_t *bp,
422 enum zio_checksum checksum, abd_t *abd, uint64_t size, uint64_t offset,
423 zio_bad_cksum_t *info)
424 {
425 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
426 zio_cksum_t actual_cksum, expected_cksum;
427 zio_eck_t eck;
428 int byteswap;
429
430 if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
431 return (SET_ERROR(EINVAL));
432
433 zio_checksum_template_init(checksum, spa);
434
435 IMPLY(bp == NULL, ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED);
436 IMPLY(bp == NULL, checksum == ZIO_CHECKSUM_LABEL);
437
438 if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
439 zio_cksum_t verifier;
440 size_t eck_offset;
441
442 if (checksum == ZIO_CHECKSUM_ZILOG2) {
443 zil_chain_t zilc;
444 uint64_t nused;
445
446 abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t));
447
448 eck = zilc.zc_eck;
449 eck_offset = offsetof(zil_chain_t, zc_eck) +
450 offsetof(zio_eck_t, zec_cksum);
451
452 if (eck.zec_magic == ZEC_MAGIC) {
453 nused = zilc.zc_nused;
454 } else if (eck.zec_magic == BSWAP_64(ZEC_MAGIC)) {
455 nused = BSWAP_64(zilc.zc_nused);
456 } else {
457 return (SET_ERROR(ECKSUM));
458 }
459
460 nused = P2ROUNDUP_TYPED(nused, ZIL_MIN_BLKSZ, uint64_t);
461 if (size < nused)
462 return (SET_ERROR(ECKSUM));
463 size = nused;
464 } else {
465 if (size < sizeof (zio_eck_t))
466 return (SET_ERROR(ECKSUM));
467 eck_offset = size - sizeof (zio_eck_t);
468 abd_copy_to_buf_off(&eck, abd, eck_offset,
469 sizeof (zio_eck_t));
470 eck_offset += offsetof(zio_eck_t, zec_cksum);
471 }
472
473 if (checksum == ZIO_CHECKSUM_GANG_HEADER)
474 zio_checksum_gang_verifier(&verifier, bp);
475 else if (checksum == ZIO_CHECKSUM_LABEL)
476 zio_checksum_label_verifier(&verifier, offset);
477 else
478 verifier = bp->blk_cksum;
479
480 byteswap = (eck.zec_magic == BSWAP_64(ZEC_MAGIC));
481
482 if (byteswap)
483 byteswap_uint64_array(&verifier, sizeof (zio_cksum_t));
484
485 expected_cksum = eck.zec_cksum;
486
487 abd_copy_from_buf_off(abd, &verifier, eck_offset,
488 sizeof (zio_cksum_t));
489
490 ci->ci_func[byteswap](abd, size,
491 spa->spa_cksum_tmpls[checksum], &actual_cksum);
492
493 abd_copy_from_buf_off(abd, &expected_cksum, eck_offset,
494 sizeof (zio_cksum_t));
495
496 if (byteswap) {
497 byteswap_uint64_array(&expected_cksum,
498 sizeof (zio_cksum_t));
499 }
500 } else {
501 byteswap = BP_SHOULD_BYTESWAP(bp);
502 expected_cksum = bp->blk_cksum;
503 ci->ci_func[byteswap](abd, size,
504 spa->spa_cksum_tmpls[checksum], &actual_cksum);
505 }
506
507 /*
508 * MAC checksums are a special case since half of this checksum will
509 * actually be the encryption MAC. This will be verified by the
510 * decryption process, so we just check the truncated checksum now.
511 * Objset blocks use embedded MACs so we don't truncate the checksum
512 * for them.
513 */
514 if (bp != NULL && BP_USES_CRYPT(bp) &&
515 BP_GET_TYPE(bp) != DMU_OT_OBJSET) {
516 if (!(ci->ci_flags & ZCHECKSUM_FLAG_DEDUP)) {
517 actual_cksum.zc_word[0] ^= actual_cksum.zc_word[2];
518 actual_cksum.zc_word[1] ^= actual_cksum.zc_word[3];
519 }
520
521 actual_cksum.zc_word[2] = 0;
522 actual_cksum.zc_word[3] = 0;
523 expected_cksum.zc_word[2] = 0;
524 expected_cksum.zc_word[3] = 0;
525 }
526
527 if (info != NULL) {
528 info->zbc_checksum_name = ci->ci_name;
529 info->zbc_byteswapped = byteswap;
530 info->zbc_injected = 0;
531 info->zbc_has_cksum = 1;
532 }
533
534 if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum))
535 return (SET_ERROR(ECKSUM));
536
537 return (0);
538 }
539
540 int
zio_checksum_error(zio_t * zio,zio_bad_cksum_t * info)541 zio_checksum_error(zio_t *zio, zio_bad_cksum_t *info)
542 {
543 blkptr_t *bp = zio->io_bp;
544 uint_t checksum = (bp == NULL ? zio->io_prop.zp_checksum :
545 (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp)));
546 int error;
547 uint64_t size = (bp == NULL ? zio->io_size :
548 (BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp)));
549 uint64_t offset = zio->io_offset;
550 abd_t *data = zio->io_abd;
551 spa_t *spa = zio->io_spa;
552
553 error = zio_checksum_error_impl(spa, bp, checksum, data, size,
554 offset, info);
555
556 if (zio_injection_enabled && error == 0 && zio->io_error == 0) {
557 error = zio_handle_fault_injection(zio, ECKSUM);
558 if (error != 0)
559 info->zbc_injected = 1;
560 }
561
562 return (error);
563 }
564
565 /*
566 * Called by a spa_t that's about to be deallocated. This steps through
567 * all of the checksum context templates and deallocates any that were
568 * initialized using the algorithm-specific template init function.
569 */
570 void
zio_checksum_templates_free(spa_t * spa)571 zio_checksum_templates_free(spa_t *spa)
572 {
573 for (enum zio_checksum checksum = 0;
574 checksum < ZIO_CHECKSUM_FUNCTIONS; checksum++) {
575 if (spa->spa_cksum_tmpls[checksum] != NULL) {
576 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
577
578 VERIFY(ci->ci_tmpl_free != NULL);
579 ci->ci_tmpl_free(spa->spa_cksum_tmpls[checksum]);
580 spa->spa_cksum_tmpls[checksum] = NULL;
581 }
582 }
583 }
584