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 http://www.opensolaris.org/os/licensing.
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 /*ARGSUSED*/
95 static void
abd_checksum_off(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)96 abd_checksum_off(abd_t *abd, uint64_t size,
97 const void *ctx_template, zio_cksum_t *zcp)
98 {
99 ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
100 }
101
102 /*ARGSUSED*/
103 static void
abd_fletcher_2_native(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)104 abd_fletcher_2_native(abd_t *abd, uint64_t size,
105 const void *ctx_template, zio_cksum_t *zcp)
106 {
107 fletcher_init(zcp);
108 (void) abd_iterate_func(abd, 0, size,
109 fletcher_2_incremental_native, zcp);
110 }
111
112 /*ARGSUSED*/
113 static void
abd_fletcher_2_byteswap(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)114 abd_fletcher_2_byteswap(abd_t *abd, uint64_t size,
115 const void *ctx_template, zio_cksum_t *zcp)
116 {
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 /*ARGSUSED*/
131 void
abd_fletcher_4_native(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)132 abd_fletcher_4_native(abd_t *abd, uint64_t size,
133 const void *ctx_template, zio_cksum_t *zcp)
134 {
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 /*ARGSUSED*/
148 void
abd_fletcher_4_byteswap(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)149 abd_fletcher_4_byteswap(abd_t *abd, uint64_t size,
150 const void *ctx_template, zio_cksum_t *zcp)
151 {
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 zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
164 {{NULL, NULL}, NULL, NULL, 0, "inherit"},
165 {{NULL, NULL}, NULL, NULL, 0, "on"},
166 {{abd_checksum_off, abd_checksum_off},
167 NULL, NULL, 0, "off"},
168 {{abd_checksum_SHA256, abd_checksum_SHA256},
169 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
170 "label"},
171 {{abd_checksum_SHA256, abd_checksum_SHA256},
172 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
173 "gang_header"},
174 {{abd_fletcher_2_native, abd_fletcher_2_byteswap},
175 NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog"},
176 {{abd_fletcher_2_native, abd_fletcher_2_byteswap},
177 NULL, NULL, 0, "fletcher2"},
178 {{abd_fletcher_4_native, abd_fletcher_4_byteswap},
179 NULL, NULL, ZCHECKSUM_FLAG_METADATA, "fletcher4"},
180 {{abd_checksum_SHA256, abd_checksum_SHA256},
181 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
182 ZCHECKSUM_FLAG_NOPWRITE, "sha256"},
183 {{abd_fletcher_4_native, abd_fletcher_4_byteswap},
184 NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog2"},
185 {{abd_checksum_off, abd_checksum_off},
186 NULL, NULL, 0, "noparity"},
187 {{abd_checksum_SHA512_native, abd_checksum_SHA512_byteswap},
188 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
189 ZCHECKSUM_FLAG_NOPWRITE, "sha512"},
190 {{abd_checksum_skein_native, abd_checksum_skein_byteswap},
191 abd_checksum_skein_tmpl_init, abd_checksum_skein_tmpl_free,
192 ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
193 ZCHECKSUM_FLAG_SALTED | ZCHECKSUM_FLAG_NOPWRITE, "skein"},
194 #if !defined(__FreeBSD__)
195 {{abd_checksum_edonr_native, abd_checksum_edonr_byteswap},
196 abd_checksum_edonr_tmpl_init, abd_checksum_edonr_tmpl_free,
197 ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_SALTED |
198 ZCHECKSUM_FLAG_NOPWRITE, "edonr"},
199 #endif
200 };
201
202 /*
203 * The flag corresponding to the "verify" in dedup=[checksum,]verify
204 * must be cleared first, so callers should use ZIO_CHECKSUM_MASK.
205 */
206 spa_feature_t
zio_checksum_to_feature(enum zio_checksum cksum)207 zio_checksum_to_feature(enum zio_checksum cksum)
208 {
209 VERIFY((cksum & ~ZIO_CHECKSUM_MASK) == 0);
210
211 switch (cksum) {
212 case ZIO_CHECKSUM_SHA512:
213 return (SPA_FEATURE_SHA512);
214 case ZIO_CHECKSUM_SKEIN:
215 return (SPA_FEATURE_SKEIN);
216 #if !defined(__FreeBSD__)
217 case ZIO_CHECKSUM_EDONR:
218 return (SPA_FEATURE_EDONR);
219 #endif
220 default:
221 return (SPA_FEATURE_NONE);
222 }
223 }
224
225 enum zio_checksum
zio_checksum_select(enum zio_checksum child,enum zio_checksum parent)226 zio_checksum_select(enum zio_checksum child, enum zio_checksum parent)
227 {
228 ASSERT(child < ZIO_CHECKSUM_FUNCTIONS);
229 ASSERT(parent < ZIO_CHECKSUM_FUNCTIONS);
230 ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
231
232 if (child == ZIO_CHECKSUM_INHERIT)
233 return (parent);
234
235 if (child == ZIO_CHECKSUM_ON)
236 return (ZIO_CHECKSUM_ON_VALUE);
237
238 return (child);
239 }
240
241 enum zio_checksum
zio_checksum_dedup_select(spa_t * spa,enum zio_checksum child,enum zio_checksum parent)242 zio_checksum_dedup_select(spa_t *spa, enum zio_checksum child,
243 enum zio_checksum parent)
244 {
245 ASSERT((child & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
246 ASSERT((parent & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
247 ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
248
249 if (child == ZIO_CHECKSUM_INHERIT)
250 return (parent);
251
252 if (child == ZIO_CHECKSUM_ON)
253 return (spa_dedup_checksum(spa));
254
255 if (child == (ZIO_CHECKSUM_ON | ZIO_CHECKSUM_VERIFY))
256 return (spa_dedup_checksum(spa) | ZIO_CHECKSUM_VERIFY);
257
258 ASSERT((zio_checksum_table[child & ZIO_CHECKSUM_MASK].ci_flags &
259 ZCHECKSUM_FLAG_DEDUP) ||
260 (child & ZIO_CHECKSUM_VERIFY) || child == ZIO_CHECKSUM_OFF);
261
262 return (child);
263 }
264
265 /*
266 * Set the external verifier for a gang block based on <vdev, offset, txg>,
267 * a tuple which is guaranteed to be unique for the life of the pool.
268 */
269 static void
zio_checksum_gang_verifier(zio_cksum_t * zcp,const blkptr_t * bp)270 zio_checksum_gang_verifier(zio_cksum_t *zcp, const blkptr_t *bp)
271 {
272 const dva_t *dva = BP_IDENTITY(bp);
273 uint64_t txg = BP_PHYSICAL_BIRTH(bp);
274
275 ASSERT(BP_IS_GANG(bp));
276
277 ZIO_SET_CHECKSUM(zcp, DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva), txg, 0);
278 }
279
280 /*
281 * Set the external verifier for a label block based on its offset.
282 * The vdev is implicit, and the txg is unknowable at pool open time --
283 * hence the logic in vdev_uberblock_load() to find the most recent copy.
284 */
285 static void
zio_checksum_label_verifier(zio_cksum_t * zcp,uint64_t offset)286 zio_checksum_label_verifier(zio_cksum_t *zcp, uint64_t offset)
287 {
288 ZIO_SET_CHECKSUM(zcp, offset, 0, 0, 0);
289 }
290
291 /*
292 * Calls the template init function of a checksum which supports context
293 * templates and installs the template into the spa_t.
294 */
295 static void
zio_checksum_template_init(enum zio_checksum checksum,spa_t * spa)296 zio_checksum_template_init(enum zio_checksum checksum, spa_t *spa)
297 {
298 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
299
300 if (ci->ci_tmpl_init == NULL)
301 return;
302 if (spa->spa_cksum_tmpls[checksum] != NULL)
303 return;
304
305 VERIFY(ci->ci_tmpl_free != NULL);
306 mutex_enter(&spa->spa_cksum_tmpls_lock);
307 if (spa->spa_cksum_tmpls[checksum] == NULL) {
308 spa->spa_cksum_tmpls[checksum] =
309 ci->ci_tmpl_init(&spa->spa_cksum_salt);
310 VERIFY(spa->spa_cksum_tmpls[checksum] != NULL);
311 }
312 mutex_exit(&spa->spa_cksum_tmpls_lock);
313 }
314
315 /* convenience function to update a checksum to accommodate an encryption MAC */
316 static void
zio_checksum_handle_crypt(zio_cksum_t * cksum,zio_cksum_t * saved,boolean_t xor)317 zio_checksum_handle_crypt(zio_cksum_t *cksum, zio_cksum_t *saved, boolean_t xor)
318 {
319 /*
320 * Weak checksums do not have their entropy spread evenly
321 * across the bits of the checksum. Therefore, when truncating
322 * a weak checksum we XOR the first 2 words with the last 2 so
323 * that we don't "lose" any entropy unnecessarily.
324 */
325 if (xor) {
326 cksum->zc_word[0] ^= cksum->zc_word[2];
327 cksum->zc_word[1] ^= cksum->zc_word[3];
328 }
329
330 cksum->zc_word[2] = saved->zc_word[2];
331 cksum->zc_word[3] = saved->zc_word[3];
332 }
333
334 /*
335 * Generate the checksum.
336 */
337 void
zio_checksum_compute(zio_t * zio,enum zio_checksum checksum,abd_t * abd,uint64_t size)338 zio_checksum_compute(zio_t *zio, enum zio_checksum checksum,
339 abd_t *abd, uint64_t size)
340 {
341 static const uint64_t zec_magic = ZEC_MAGIC;
342 blkptr_t *bp = zio->io_bp;
343 uint64_t offset = zio->io_offset;
344 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
345 zio_cksum_t cksum, saved;
346 spa_t *spa = zio->io_spa;
347 boolean_t insecure = (ci->ci_flags & ZCHECKSUM_FLAG_DEDUP) == 0;
348
349 ASSERT((uint_t)checksum < ZIO_CHECKSUM_FUNCTIONS);
350 ASSERT(ci->ci_func[0] != NULL);
351
352 zio_checksum_template_init(checksum, spa);
353
354 if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
355 zio_eck_t eck;
356 size_t eck_offset;
357
358 bzero(&saved, sizeof (zio_cksum_t));
359
360 if (checksum == ZIO_CHECKSUM_ZILOG2) {
361 zil_chain_t zilc;
362 abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t));
363
364 size = P2ROUNDUP_TYPED(zilc.zc_nused, ZIL_MIN_BLKSZ,
365 uint64_t);
366 eck = zilc.zc_eck;
367 eck_offset = offsetof(zil_chain_t, zc_eck);
368 } else {
369 eck_offset = size - sizeof (zio_eck_t);
370 abd_copy_to_buf_off(&eck, abd, eck_offset,
371 sizeof (zio_eck_t));
372 }
373
374 if (checksum == ZIO_CHECKSUM_GANG_HEADER) {
375 zio_checksum_gang_verifier(&eck.zec_cksum, bp);
376 } else if (checksum == ZIO_CHECKSUM_LABEL) {
377 zio_checksum_label_verifier(&eck.zec_cksum, offset);
378 } else {
379 saved = eck.zec_cksum;
380 eck.zec_cksum = bp->blk_cksum;
381 }
382
383 abd_copy_from_buf_off(abd, &zec_magic,
384 eck_offset + offsetof(zio_eck_t, zec_magic),
385 sizeof (zec_magic));
386 abd_copy_from_buf_off(abd, &eck.zec_cksum,
387 eck_offset + offsetof(zio_eck_t, zec_cksum),
388 sizeof (zio_cksum_t));
389
390 ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum],
391 &cksum);
392 if (bp != NULL && BP_USES_CRYPT(bp) &&
393 BP_GET_TYPE(bp) != DMU_OT_OBJSET)
394 zio_checksum_handle_crypt(&cksum, &saved, insecure);
395
396 abd_copy_from_buf_off(abd, &cksum,
397 eck_offset + offsetof(zio_eck_t, zec_cksum),
398 sizeof (zio_cksum_t));
399 } else {
400 saved = bp->blk_cksum;
401 ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum],
402 &cksum);
403 if (BP_USES_CRYPT(bp) && BP_GET_TYPE(bp) != DMU_OT_OBJSET)
404 zio_checksum_handle_crypt(&cksum, &saved, insecure);
405 bp->blk_cksum = cksum;
406 }
407 }
408
409 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)410 zio_checksum_error_impl(spa_t *spa, const blkptr_t *bp,
411 enum zio_checksum checksum, abd_t *abd, uint64_t size, uint64_t offset,
412 zio_bad_cksum_t *info)
413 {
414 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
415 zio_cksum_t actual_cksum, expected_cksum;
416 zio_eck_t eck;
417 int byteswap;
418
419 if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
420 return (SET_ERROR(EINVAL));
421
422 zio_checksum_template_init(checksum, spa);
423
424 if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
425 zio_cksum_t verifier;
426 size_t eck_offset;
427
428 if (checksum == ZIO_CHECKSUM_ZILOG2) {
429 zil_chain_t zilc;
430 uint64_t nused;
431
432 abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t));
433
434 eck = zilc.zc_eck;
435 eck_offset = offsetof(zil_chain_t, zc_eck) +
436 offsetof(zio_eck_t, zec_cksum);
437
438 if (eck.zec_magic == ZEC_MAGIC) {
439 nused = zilc.zc_nused;
440 } else if (eck.zec_magic == BSWAP_64(ZEC_MAGIC)) {
441 nused = BSWAP_64(zilc.zc_nused);
442 } else {
443 return (SET_ERROR(ECKSUM));
444 }
445
446 if (nused > size) {
447 return (SET_ERROR(ECKSUM));
448 }
449
450 size = P2ROUNDUP_TYPED(nused, ZIL_MIN_BLKSZ, uint64_t);
451 } else {
452 eck_offset = size - sizeof (zio_eck_t);
453 abd_copy_to_buf_off(&eck, abd, eck_offset,
454 sizeof (zio_eck_t));
455 eck_offset += offsetof(zio_eck_t, zec_cksum);
456 }
457
458 if (checksum == ZIO_CHECKSUM_GANG_HEADER)
459 zio_checksum_gang_verifier(&verifier, bp);
460 else if (checksum == ZIO_CHECKSUM_LABEL)
461 zio_checksum_label_verifier(&verifier, offset);
462 else
463 verifier = bp->blk_cksum;
464
465 byteswap = (eck.zec_magic == BSWAP_64(ZEC_MAGIC));
466
467 if (byteswap)
468 byteswap_uint64_array(&verifier, sizeof (zio_cksum_t));
469
470 expected_cksum = eck.zec_cksum;
471
472 abd_copy_from_buf_off(abd, &verifier, eck_offset,
473 sizeof (zio_cksum_t));
474
475 ci->ci_func[byteswap](abd, size,
476 spa->spa_cksum_tmpls[checksum], &actual_cksum);
477
478 abd_copy_from_buf_off(abd, &expected_cksum, eck_offset,
479 sizeof (zio_cksum_t));
480
481 if (byteswap) {
482 byteswap_uint64_array(&expected_cksum,
483 sizeof (zio_cksum_t));
484 }
485 } else {
486 byteswap = BP_SHOULD_BYTESWAP(bp);
487 expected_cksum = bp->blk_cksum;
488 ci->ci_func[byteswap](abd, size,
489 spa->spa_cksum_tmpls[checksum], &actual_cksum);
490 }
491
492 /*
493 * MAC checksums are a special case since half of this checksum will
494 * actually be the encryption MAC. This will be verified by the
495 * decryption process, so we just check the truncated checksum now.
496 * Objset blocks use embedded MACs so we don't truncate the checksum
497 * for them.
498 */
499 if (bp != NULL && BP_USES_CRYPT(bp) &&
500 BP_GET_TYPE(bp) != DMU_OT_OBJSET) {
501 if (!(ci->ci_flags & ZCHECKSUM_FLAG_DEDUP)) {
502 actual_cksum.zc_word[0] ^= actual_cksum.zc_word[2];
503 actual_cksum.zc_word[1] ^= actual_cksum.zc_word[3];
504 }
505
506 actual_cksum.zc_word[2] = 0;
507 actual_cksum.zc_word[3] = 0;
508 expected_cksum.zc_word[2] = 0;
509 expected_cksum.zc_word[3] = 0;
510 }
511
512 if (info != NULL) {
513 info->zbc_expected = expected_cksum;
514 info->zbc_actual = actual_cksum;
515 info->zbc_checksum_name = ci->ci_name;
516 info->zbc_byteswapped = byteswap;
517 info->zbc_injected = 0;
518 info->zbc_has_cksum = 1;
519 }
520
521 if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum))
522 return (SET_ERROR(ECKSUM));
523
524 return (0);
525 }
526
527 int
zio_checksum_error(zio_t * zio,zio_bad_cksum_t * info)528 zio_checksum_error(zio_t *zio, zio_bad_cksum_t *info)
529 {
530 blkptr_t *bp = zio->io_bp;
531 uint_t checksum = (bp == NULL ? zio->io_prop.zp_checksum :
532 (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp)));
533 int error;
534 uint64_t size = (bp == NULL ? zio->io_size :
535 (BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp)));
536 uint64_t offset = zio->io_offset;
537 abd_t *data = zio->io_abd;
538 spa_t *spa = zio->io_spa;
539
540 error = zio_checksum_error_impl(spa, bp, checksum, data, size,
541 offset, info);
542
543 if (zio_injection_enabled && error == 0 && zio->io_error == 0) {
544 error = zio_handle_fault_injection(zio, ECKSUM);
545 if (error != 0)
546 info->zbc_injected = 1;
547 }
548
549 return (error);
550 }
551
552 /*
553 * Called by a spa_t that's about to be deallocated. This steps through
554 * all of the checksum context templates and deallocates any that were
555 * initialized using the algorithm-specific template init function.
556 */
557 void
zio_checksum_templates_free(spa_t * spa)558 zio_checksum_templates_free(spa_t *spa)
559 {
560 for (enum zio_checksum checksum = 0;
561 checksum < ZIO_CHECKSUM_FUNCTIONS; checksum++) {
562 if (spa->spa_cksum_tmpls[checksum] != NULL) {
563 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
564
565 VERIFY(ci->ci_tmpl_free != NULL);
566 ci->ci_tmpl_free(spa->spa_cksum_tmpls[checksum]);
567 spa->spa_cksum_tmpls[checksum] = NULL;
568 }
569 }
570 }
571