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