1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5ea8dc4beschrock * Common Development and Distribution License (the "License").
6ea8dc4beschrock * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21fa9e406ahrens/*
22cde58dbMatthew Ahrens * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
236cedfc3Pavel Zakharov * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
24810e43bBill Pijewski * Copyright (c) 2013, Joyent, Inc. All rights reserved.
2545818eeMatthew Ahrens * Copyright 2013 Saso Kiselkov. All rights reserved.
26fa9e406ahrens */
27fa9e406ahrens
28fa9e406ahrens#include <sys/zfs_context.h>
29fa9e406ahrens#include <sys/spa.h>
3045818eeMatthew Ahrens#include <sys/spa_impl.h>
31fa9e406ahrens#include <sys/zio.h>
32fa9e406ahrens#include <sys/zio_checksum.h>
336e1f5caNeil Perrin#include <sys/zil.h>
34770499eDan Kimmel#include <sys/abd.h>
35cde58dbMatthew Ahrens#include <zfs_fletcher.h>
36fa9e406ahrens
37fa9e406ahrens/*
38fa9e406ahrens * Checksum vectors.
39fa9e406ahrens *
40fa9e406ahrens * In the SPA, everything is checksummed.  We support checksum vectors
41fa9e406ahrens * for three distinct reasons:
42fa9e406ahrens *
43fa9e406ahrens *   1. Different kinds of data need different levels of protection.
44fa9e406ahrens *	For SPA metadata, we always want a very strong checksum.
45fa9e406ahrens *	For user data, we let users make the trade-off between speed
46fa9e406ahrens *	and checksum strength.
47fa9e406ahrens *
48fa9e406ahrens *   2. Cryptographic hash and MAC algorithms are an area of active research.
49fa9e406ahrens *	It is likely that in future hash functions will be at least as strong
50fa9e406ahrens *	as current best-of-breed, and may be substantially faster as well.
51fa9e406ahrens *	We want the ability to take advantage of these new hashes as soon as
52fa9e406ahrens *	they become available.
53fa9e406ahrens *
54fa9e406ahrens *   3. If someone develops hardware that can compute a strong hash quickly,
55fa9e406ahrens *	we want the ability to take advantage of that hardware.
56fa9e406ahrens *
57fa9e406ahrens * Of course, we don't want a checksum upgrade to invalidate existing
58b24ab67Jeff Bonwick * data, so we store the checksum *function* in eight bits of the bp.
59b24ab67Jeff Bonwick * This gives us room for up to 256 different checksum functions.
60fa9e406ahrens *
61fa9e406ahrens * When writing a block, we always checksum it with the latest-and-greatest
62fa9e406ahrens * checksum function of the appropriate strength.  When reading a block,
63fa9e406ahrens * we compare the expected checksum against the actual checksum, which we
64b24ab67Jeff Bonwick * compute via the checksum function specified by BP_GET_CHECKSUM(bp).
6545818eeMatthew Ahrens *
6645818eeMatthew Ahrens * SALTED CHECKSUMS
6745818eeMatthew Ahrens *
6845818eeMatthew Ahrens * To enable the use of less secure hash algorithms with dedup, we
6945818eeMatthew Ahrens * introduce the notion of salted checksums (MACs, really).  A salted
7045818eeMatthew Ahrens * checksum is fed both a random 256-bit value (the salt) and the data
7145818eeMatthew Ahrens * to be checksummed.  This salt is kept secret (stored on the pool, but
7245818eeMatthew Ahrens * never shown to the user).  Thus even if an attacker knew of collision
7345818eeMatthew Ahrens * weaknesses in the hash algorithm, they won't be able to mount a known
7445818eeMatthew Ahrens * plaintext attack on the DDT, since the actual hash value cannot be
7545818eeMatthew Ahrens * known ahead of time.  How the salt is used is algorithm-specific
7645818eeMatthew Ahrens * (some might simply prefix it to the data block, others might need to
7745818eeMatthew Ahrens * utilize a full-blown HMAC).  On disk the salt is stored in a ZAP
7845818eeMatthew Ahrens * object in the MOS (DMU_POOL_CHECKSUM_SALT).
7945818eeMatthew Ahrens *
8045818eeMatthew Ahrens * CONTEXT TEMPLATES
8145818eeMatthew Ahrens *
8245818eeMatthew Ahrens * Some hashing algorithms need to perform a substantial amount of
8345818eeMatthew Ahrens * initialization work (e.g. salted checksums above may need to pre-hash
8445818eeMatthew Ahrens * the salt) before being able to process data.  Performing this
8545818eeMatthew Ahrens * redundant work for each block would be wasteful, so we instead allow
8645818eeMatthew Ahrens * a checksum algorithm to do the work once (the first time it's used)
8745818eeMatthew Ahrens * and then keep this pre-initialized context as a template inside the
8845818eeMatthew Ahrens * spa_t (spa_cksum_tmpls).  If the zio_checksum_info_t contains
8945818eeMatthew Ahrens * non-NULL ci_tmpl_init and ci_tmpl_free callbacks, they are used to
9045818eeMatthew Ahrens * construct and destruct the pre-initialized checksum context.  The
9145818eeMatthew Ahrens * pre-initialized context is then reused during each checksum
9245818eeMatthew Ahrens * invocation and passed to the checksum function.
93fa9e406ahrens */
94fa9e406ahrens
95fa9e406ahrens/*ARGSUSED*/
96fa9e406ahrensstatic void
97770499eDan Kimmelabd_checksum_off(abd_t *abd, uint64_t size,
9845818eeMatthew Ahrens    const void *ctx_template, zio_cksum_t *zcp)
99fa9e406ahrens{
100fa9e406ahrens	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
101fa9e406ahrens}
102fa9e406ahrens
103770499eDan Kimmel/*ARGSUSED*/
104770499eDan Kimmelvoid
105770499eDan Kimmelabd_fletcher_2_native(abd_t *abd, uint64_t size,
106770499eDan Kimmel    const void *ctx_template, zio_cksum_t *zcp)
107770499eDan Kimmel{
108770499eDan Kimmel	fletcher_init(zcp);
109770499eDan Kimmel	(void) abd_iterate_func(abd, 0, size,
110770499eDan Kimmel	    fletcher_2_incremental_native, zcp);
111770499eDan Kimmel}
112770499eDan Kimmel
113770499eDan Kimmel/*ARGSUSED*/
114770499eDan Kimmelvoid
115770499eDan Kimmelabd_fletcher_2_byteswap(abd_t *abd, uint64_t size,
116770499eDan Kimmel    const void *ctx_template, zio_cksum_t *zcp)
117770499eDan Kimmel{
118770499eDan Kimmel	fletcher_init(zcp);
119770499eDan Kimmel	(void) abd_iterate_func(abd, 0, size,
120770499eDan Kimmel	    fletcher_2_incremental_byteswap, zcp);
121770499eDan Kimmel}
122770499eDan Kimmel
123770499eDan Kimmel/*ARGSUSED*/
124770499eDan Kimmelvoid
125770499eDan Kimmelabd_fletcher_4_native(abd_t *abd, uint64_t size,
126770499eDan Kimmel    const void *ctx_template, zio_cksum_t *zcp)
127770499eDan Kimmel{
128770499eDan Kimmel	fletcher_init(zcp);
129770499eDan Kimmel	(void) abd_iterate_func(abd, 0, size,
130770499eDan Kimmel	    fletcher_4_incremental_native, zcp);
131770499eDan Kimmel}
132770499eDan Kimmel
133770499eDan Kimmel/*ARGSUSED*/
134770499eDan Kimmelvoid
135770499eDan Kimmelabd_fletcher_4_byteswap(abd_t *abd, uint64_t size,
136770499eDan Kimmel    const void *ctx_template, zio_cksum_t *zcp)
137770499eDan Kimmel{
138770499eDan Kimmel	fletcher_init(zcp);
139770499eDan Kimmel	(void) abd_iterate_func(abd, 0, size,
140770499eDan Kimmel	    fletcher_4_incremental_byteswap, zcp);
141770499eDan Kimmel}
142770499eDan Kimmel
143fa9e406ahrenszio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
14445818eeMatthew Ahrens	{{NULL, NULL}, NULL, NULL, 0, "inherit"},
14545818eeMatthew Ahrens	{{NULL, NULL}, NULL, NULL, 0, "on"},
146770499eDan Kimmel	{{abd_checksum_off,		abd_checksum_off},
14745818eeMatthew Ahrens	    NULL, NULL, 0, "off"},
148770499eDan Kimmel	{{abd_checksum_SHA256,		abd_checksum_SHA256},
14945818eeMatthew Ahrens	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
15045818eeMatthew Ahrens	    "label"},
151770499eDan Kimmel	{{abd_checksum_SHA256,		abd_checksum_SHA256},
15245818eeMatthew Ahrens	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
15345818eeMatthew Ahrens	    "gang_header"},
154770499eDan Kimmel	{{abd_fletcher_2_native,	abd_fletcher_2_byteswap},
15545818eeMatthew Ahrens	    NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog"},
156770499eDan Kimmel	{{abd_fletcher_2_native,	abd_fletcher_2_byteswap},
15745818eeMatthew Ahrens	    NULL, NULL, 0, "fletcher2"},
158770499eDan Kimmel	{{abd_fletcher_4_native,	abd_fletcher_4_byteswap},
15945818eeMatthew Ahrens	    NULL, NULL, ZCHECKSUM_FLAG_METADATA, "fletcher4"},
160770499eDan Kimmel	{{abd_checksum_SHA256,		abd_checksum_SHA256},
16145818eeMatthew Ahrens	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
16245818eeMatthew Ahrens	    ZCHECKSUM_FLAG_NOPWRITE, "sha256"},
163770499eDan Kimmel	{{abd_fletcher_4_native,	abd_fletcher_4_byteswap},
16445818eeMatthew Ahrens	    NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog2"},
165770499eDan Kimmel	{{abd_checksum_off,		abd_checksum_off},
16645818eeMatthew Ahrens	    NULL, NULL, 0, "noparity"},
167770499eDan Kimmel	{{abd_checksum_SHA512_native,	abd_checksum_SHA512_byteswap},
16845818eeMatthew Ahrens	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
16945818eeMatthew Ahrens	    ZCHECKSUM_FLAG_NOPWRITE, "sha512"},
170770499eDan Kimmel	{{abd_checksum_skein_native,	abd_checksum_skein_byteswap},
171770499eDan Kimmel	    abd_checksum_skein_tmpl_init, abd_checksum_skein_tmpl_free,
17245818eeMatthew Ahrens	    ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
17345818eeMatthew Ahrens	    ZCHECKSUM_FLAG_SALTED | ZCHECKSUM_FLAG_NOPWRITE, "skein"},
174770499eDan Kimmel	{{abd_checksum_edonr_native,	abd_checksum_edonr_byteswap},
175770499eDan Kimmel	    abd_checksum_edonr_tmpl_init, abd_checksum_edonr_tmpl_free,
17645818eeMatthew Ahrens	    ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_SALTED |
17745818eeMatthew Ahrens	    ZCHECKSUM_FLAG_NOPWRITE, "edonr"},
178fa9e406ahrens};
179fa9e406ahrens
180971640eilovezfs/*
181971640eilovezfs * The flag corresponding to the "verify" in dedup=[checksum,]verify
182971640eilovezfs * must be cleared first, so callers should use ZIO_CHECKSUM_MASK.
183971640eilovezfs */
18445818eeMatthew Ahrensspa_feature_t
18545818eeMatthew Ahrenszio_checksum_to_feature(enum zio_checksum cksum)
18645818eeMatthew Ahrens{
187971640eilovezfs	VERIFY((cksum & ~ZIO_CHECKSUM_MASK) == 0);
188971640eilovezfs
18945818eeMatthew Ahrens	switch (cksum) {
19045818eeMatthew Ahrens	case ZIO_CHECKSUM_SHA512:
19145818eeMatthew Ahrens		return (SPA_FEATURE_SHA512);
19245818eeMatthew Ahrens	case ZIO_CHECKSUM_SKEIN:
19345818eeMatthew Ahrens		return (SPA_FEATURE_SKEIN);
19445818eeMatthew Ahrens	case ZIO_CHECKSUM_EDONR:
19545818eeMatthew Ahrens		return (SPA_FEATURE_EDONR);
19645818eeMatthew Ahrens	}
19745818eeMatthew Ahrens	return (SPA_FEATURE_NONE);
19845818eeMatthew Ahrens}
19945818eeMatthew Ahrens
200b24ab67Jeff Bonwickenum zio_checksum
201b24ab67Jeff Bonwickzio_checksum_select(enum zio_checksum child, enum zio_checksum parent)
202fa9e406ahrens{
203fa9e406ahrens	ASSERT(child < ZIO_CHECKSUM_FUNCTIONS);
204fa9e406ahrens	ASSERT(parent < ZIO_CHECKSUM_FUNCTIONS);
205fa9e406ahrens	ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
206fa9e406ahrens
207fa9e406ahrens	if (child == ZIO_CHECKSUM_INHERIT)
208fa9e406ahrens		return (parent);
209fa9e406ahrens
210fa9e406ahrens	if (child == ZIO_CHECKSUM_ON)
211fa9e406ahrens		return (ZIO_CHECKSUM_ON_VALUE);
212fa9e406ahrens
213fa9e406ahrens	return (child);
214fa9e406ahrens}
215fa9e406ahrens
216b24ab67Jeff Bonwickenum zio_checksum
217b24ab67Jeff Bonwickzio_checksum_dedup_select(spa_t *spa, enum zio_checksum child,
218b24ab67Jeff Bonwick    enum zio_checksum parent)
219b24ab67Jeff Bonwick{
220b24ab67Jeff Bonwick	ASSERT((child & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
221b24ab67Jeff Bonwick	ASSERT((parent & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
222b24ab67Jeff Bonwick	ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
223b24ab67Jeff Bonwick
224b24ab67Jeff Bonwick	if (child == ZIO_CHECKSUM_INHERIT)
225b24ab67Jeff Bonwick		return (parent);
226b24ab67Jeff Bonwick
227b24ab67Jeff Bonwick	if (child == ZIO_CHECKSUM_ON)
228b24ab67Jeff Bonwick		return (spa_dedup_checksum(spa));
229b24ab67Jeff Bonwick
230b24ab67Jeff Bonwick	if (child == (ZIO_CHECKSUM_ON | ZIO_CHECKSUM_VERIFY))
231b24ab67Jeff Bonwick		return (spa_dedup_checksum(spa) | ZIO_CHECKSUM_VERIFY);
232b24ab67Jeff Bonwick
23345818eeMatthew Ahrens	ASSERT((zio_checksum_table[child & ZIO_CHECKSUM_MASK].ci_flags &
23445818eeMatthew Ahrens	    ZCHECKSUM_FLAG_DEDUP) ||
235b24ab67Jeff Bonwick	    (child & ZIO_CHECKSUM_VERIFY) || child == ZIO_CHECKSUM_OFF);
236b24ab67Jeff Bonwick
237b24ab67Jeff Bonwick	return (child);
238b24ab67Jeff Bonwick}
239b24ab67Jeff Bonwick
240fa9e406ahrens/*
241e14bb32Jeff Bonwick * Set the external verifier for a gang block based on <vdev, offset, txg>,
242e14bb32Jeff Bonwick * a tuple which is guaranteed to be unique for the life of the pool.
243e14bb32Jeff Bonwick */
244e14bb32Jeff Bonwickstatic void
245eb63303Tom Caputizio_checksum_gang_verifier(zio_cksum_t *zcp, const blkptr_t *bp)
246e14bb32Jeff Bonwick{
247eb63303Tom Caputi	const dva_t *dva = BP_IDENTITY(bp);
248b24ab67Jeff Bonwick	uint64_t txg = BP_PHYSICAL_BIRTH(bp);
249e14bb32Jeff Bonwick
250e14bb32Jeff Bonwick	ASSERT(BP_IS_GANG(bp));
251e14bb32Jeff Bonwick
252e14bb32Jeff Bonwick	ZIO_SET_CHECKSUM(zcp, DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva), txg, 0);
253e14bb32Jeff Bonwick}
254e14bb32Jeff Bonwick
255e14bb32Jeff Bonwick/*
256e14bb32Jeff Bonwick * Set the external verifier for a label block based on its offset.
257e14bb32Jeff Bonwick * The vdev is implicit, and the txg is unknowable at pool open time --
258e14bb32Jeff Bonwick * hence the logic in vdev_uberblock_load() to find the most recent copy.
259e14bb32Jeff Bonwick */
260e14bb32Jeff Bonwickstatic void
261e14bb32Jeff Bonwickzio_checksum_label_verifier(zio_cksum_t *zcp, uint64_t offset)
262e14bb32Jeff Bonwick{
263e14bb32Jeff Bonwick	ZIO_SET_CHECKSUM(zcp, offset, 0, 0, 0);
264e14bb32Jeff Bonwick}
265e14bb32Jeff Bonwick
266e14bb32Jeff Bonwick/*
26745818eeMatthew Ahrens * Calls the template init function of a checksum which supports context
26845818eeMatthew Ahrens * templates and installs the template into the spa_t.
26945818eeMatthew Ahrens */
27045818eeMatthew Ahrensstatic void
27145818eeMatthew Ahrenszio_checksum_template_init(enum zio_checksum checksum, spa_t *spa)
27245818eeMatthew Ahrens{
27345818eeMatthew Ahrens	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
27445818eeMatthew Ahrens
27545818eeMatthew Ahrens	if (ci->ci_tmpl_init == NULL)
27645818eeMatthew Ahrens		return;
27745818eeMatthew Ahrens	if (spa->spa_cksum_tmpls[checksum] != NULL)
27845818eeMatthew Ahrens		return;
27945818eeMatthew Ahrens
28045818eeMatthew Ahrens	VERIFY(ci->ci_tmpl_free != NULL);
28145818eeMatthew Ahrens	mutex_enter(&spa->spa_cksum_tmpls_lock);
28245818eeMatthew Ahrens	if (spa->spa_cksum_tmpls[checksum] == NULL) {
28345818eeMatthew Ahrens		spa->spa_cksum_tmpls[checksum] =
28445818eeMatthew Ahrens		    ci->ci_tmpl_init(&spa->spa_cksum_salt);
28545818eeMatthew Ahrens		VERIFY(spa->spa_cksum_tmpls[checksum] != NULL);
28645818eeMatthew Ahrens	}
28745818eeMatthew Ahrens	mutex_exit(&spa->spa_cksum_tmpls_lock);
28845818eeMatthew Ahrens}
28945818eeMatthew Ahrens
290eb63303Tom Caputi/* convenience function to update a checksum to accomodate an encryption MAC */
291eb63303Tom Caputistatic void
292eb63303Tom Caputizio_checksum_handle_crypt(zio_cksum_t *cksum, zio_cksum_t *saved, boolean_t xor)
293eb63303Tom Caputi{
294eb63303Tom Caputi	/*
295eb63303Tom Caputi	 * Weak checksums do not have their entropy spread evenly
296eb63303Tom Caputi	 * across the bits of the checksum. Therefore, when truncating
297eb63303Tom Caputi	 * a weak checksum we XOR the first 2 words with the last 2 so
298eb63303Tom Caputi	 * that we don't "lose" any entropy unnecessarily.
299eb63303Tom Caputi	 */
300eb63303Tom Caputi	if (xor) {
301eb63303Tom Caputi		cksum->zc_word[0] ^= cksum->zc_word[2];
302eb63303Tom Caputi		cksum->zc_word[1] ^= cksum->zc_word[3];
303eb63303Tom Caputi	}
304eb63303Tom Caputi
305eb63303Tom Caputi	cksum->zc_word[2] = saved->zc_word[2];
306eb63303Tom Caputi	cksum->zc_word[3] = saved->zc_word[3];
307eb63303Tom Caputi}
308eb63303Tom Caputi
30945818eeMatthew Ahrens/*
310fa9e406ahrens * Generate the checksum.
311fa9e406ahrens */
312fa9e406ahrensvoid
313e14bb32Jeff Bonwickzio_checksum_compute(zio_t *zio, enum zio_checksum checksum,
314770499eDan Kimmel    abd_t *abd, uint64_t size)
315fa9e406ahrens{
316eb63303Tom Caputi	static const uint64_t zec_magic = ZEC_MAGIC;
317e14bb32Jeff Bonwick	blkptr_t *bp = zio->io_bp;
318e14bb32Jeff Bonwick	uint64_t offset = zio->io_offset;
31929de914Toomas Soome	zio_checksum_info_t *ci;
320eb63303Tom Caputi	zio_cksum_t cksum, saved;
32145818eeMatthew Ahrens	spa_t *spa = zio->io_spa;
32229de914Toomas Soome	boolean_t insecure;
323fa9e406ahrens
324e14bb32Jeff Bonwick	ASSERT((uint_t)checksum < ZIO_CHECKSUM_FUNCTIONS);
32529de914Toomas Soome	ci = &zio_checksum_table[checksum];
326fa9e406ahrens	ASSERT(ci->ci_func[0] != NULL);
32729de914Toomas Soome	insecure = (ci->ci_flags & ZCHECKSUM_FLAG_DEDUP) == 0;
328fa9e406ahrens
32945818eeMatthew Ahrens	zio_checksum_template_init(checksum, spa);
33045818eeMatthew Ahrens
33145818eeMatthew Ahrens	if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
332eb63303Tom Caputi		zio_eck_t eck;
333eb63303Tom Caputi		size_t eck_offset;
334eb63303Tom Caputi
335eb63303Tom Caputi		bzero(&saved, sizeof (zio_cksum_t));
3366e1f5caNeil Perrin
3376e1f5caNeil Perrin		if (checksum == ZIO_CHECKSUM_ZILOG2) {
338eb63303Tom Caputi			zil_chain_t zilc;
339eb63303Tom Caputi			abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t));
3406e1f5caNeil Perrin
341eb63303Tom Caputi			size = P2ROUNDUP_TYPED(zilc.zc_nused, ZIL_MIN_BLKSZ,
3426e1f5caNeil Perrin			    uint64_t);
343eb63303Tom Caputi			eck = zilc.zc_eck;
344eb63303Tom Caputi			eck_offset = offsetof(zil_chain_t, zc_eck);
3456e1f5caNeil Perrin		} else {
346eb63303Tom Caputi			eck_offset = size - sizeof (zio_eck_t);
347eb63303Tom Caputi			abd_copy_to_buf_off(&eck, abd, eck_offset,
348eb63303Tom Caputi			    sizeof (zio_eck_t));
3496e1f5caNeil Perrin		}
350eb63303Tom Caputi
351eb63303Tom Caputi		if (checksum == ZIO_CHECKSUM_GANG_HEADER) {
352eb63303Tom Caputi			zio_checksum_gang_verifier(&eck.zec_cksum, bp);
353eb63303Tom Caputi		} else if (checksum == ZIO_CHECKSUM_LABEL) {
354eb63303Tom Caputi			zio_checksum_label_verifier(&eck.zec_cksum, offset);
355eb63303Tom Caputi		} else {
356eb63303Tom Caputi			saved = eck.zec_cksum;
357eb63303Tom Caputi			eck.zec_cksum = bp->blk_cksum;
358eb63303Tom Caputi		}
359eb63303Tom Caputi
360eb63303Tom Caputi		abd_copy_from_buf_off(abd, &zec_magic,
361eb63303Tom Caputi		    eck_offset + offsetof(zio_eck_t, zec_magic),
362eb63303Tom Caputi		    sizeof (zec_magic));
363eb63303Tom Caputi		abd_copy_from_buf_off(abd, &eck.zec_cksum,
364eb63303Tom Caputi		    eck_offset + offsetof(zio_eck_t, zec_cksum),
365eb63303Tom Caputi		    sizeof (zio_cksum_t));
366eb63303Tom Caputi
367770499eDan Kimmel		ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum],
36845818eeMatthew Ahrens		    &cksum);
369eb63303Tom Caputi		if (bp != NULL && BP_USES_CRYPT(bp) &&
370eb63303Tom Caputi		    BP_GET_TYPE(bp) != DMU_OT_OBJSET)
371eb63303Tom Caputi			zio_checksum_handle_crypt(&cksum, &saved, insecure);
372eb63303Tom Caputi
373eb63303Tom Caputi		abd_copy_from_buf_off(abd, &cksum,
374eb63303Tom Caputi		    eck_offset + offsetof(zio_eck_t, zec_cksum),
375eb63303Tom Caputi		    sizeof (zio_cksum_t));
376fa9e406ahrens	} else {
377eb63303Tom Caputi		saved = bp->blk_cksum;
378770499eDan Kimmel		ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum],
379eb63303Tom Caputi		    &cksum);
380eb63303Tom Caputi		if (BP_USES_CRYPT(bp) && BP_GET_TYPE(bp) != DMU_OT_OBJSET)
381eb63303Tom Caputi			zio_checksum_handle_crypt(&cksum, &saved, insecure);
382eb63303Tom Caputi		bp->blk_cksum = cksum;
383fa9e406ahrens	}
384fa9e406ahrens}
385fa9e406ahrens
386fa9e406ahrensint
387eb63303Tom Caputizio_checksum_error_impl(spa_t *spa, const blkptr_t *bp,
388eb63303Tom Caputi    enum zio_checksum checksum, abd_t *abd, uint64_t size,
389eb63303Tom Caputi    uint64_t offset, zio_bad_cksum_t *info)
390fa9e406ahrens{
39129de914Toomas Soome	zio_checksum_info_t *ci;
392dcbf3bdGeorge Wilson	zio_cksum_t actual_cksum, expected_cksum;
393eb63303Tom Caputi	zio_eck_t eck;
394dcbf3bdGeorge Wilson	int byteswap;
395fa9e406ahrens
39629de914Toomas Soome	if (checksum >= ZIO_CHECKSUM_FUNCTIONS)
39729de914Toomas Soome		return (SET_ERROR(EINVAL));
39829de914Toomas Soome
39929de914Toomas Soome	ci = &zio_checksum_table[checksum];
40029de914Toomas Soome
40129de914Toomas Soome	if (ci->ci_func[0] == NULL)
402be6fd75Matthew Ahrens		return (SET_ERROR(EINVAL));
403fa9e406ahrens
40445818eeMatthew Ahrens	zio_checksum_template_init(checksum, spa);
40545818eeMatthew Ahrens
40645818eeMatthew Ahrens	if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
407dcbf3bdGeorge Wilson		zio_cksum_t verifier;
408eb63303Tom Caputi		size_t eck_offset;
4096e1f5caNeil Perrin
4106e1f5caNeil Perrin		if (checksum == ZIO_CHECKSUM_ZILOG2) {
411eb63303Tom Caputi			zil_chain_t zilc;
4126e1f5caNeil Perrin			uint64_t nused;
4136e1f5caNeil Perrin
414eb63303Tom Caputi			abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t));
415eb63303Tom Caputi
416eb63303Tom Caputi			eck = zilc.zc_eck;
417eb63303Tom Caputi			eck_offset = offsetof(zil_chain_t, zc_eck) +
418eb63303Tom Caputi			    offsetof(zio_eck_t, zec_cksum);
419eb63303Tom Caputi
420eb63303Tom Caputi			if (eck.zec_magic == ZEC_MAGIC) {
421eb63303Tom Caputi				nused = zilc.zc_nused;
422eb63303Tom Caputi			} else if (eck.zec_magic == BSWAP_64(ZEC_MAGIC)) {
423eb63303Tom Caputi				nused = BSWAP_64(zilc.zc_nused);
424770499eDan Kimmel			} else {
425be6fd75Matthew Ahrens				return (SET_ERROR(ECKSUM));
426770499eDan Kimmel			}
4276e1f5caNeil Perrin
428eb63303Tom Caputi			if (nused > size) {
429be6fd75Matthew Ahrens				return (SET_ERROR(ECKSUM));
430770499eDan Kimmel			}
4316e1f5caNeil Perrin
4326e1f5caNeil Perrin			size = P2ROUNDUP_TYPED(nused, ZIL_MIN_BLKSZ, uint64_t);
4336e1f5caNeil Perrin		} else {
434eb63303Tom Caputi			eck_offset = size - sizeof (zio_eck_t);
435eb63303Tom Caputi			abd_copy_to_buf_off(&eck, abd, eck_offset,
436eb63303Tom Caputi			    sizeof (zio_eck_t));
437eb63303Tom Caputi			eck_offset += offsetof(zio_eck_t, zec_cksum);
4386e1f5caNeil Perrin		}
4396e1f5caNeil Perrin
440fa9e406ahrens		if (checksum == ZIO_CHECKSUM_GANG_HEADER)
441e14bb32Jeff Bonwick			zio_checksum_gang_verifier(&verifier, bp);
442e14bb32Jeff Bonwick		else if (checksum == ZIO_CHECKSUM_LABEL)
443e14bb32Jeff Bonwick			zio_checksum_label_verifier(&verifier, offset);
444e14bb32Jeff Bonwick		else
445e14bb32Jeff Bonwick			verifier = bp->blk_cksum;
446e14bb32Jeff Bonwick
447eb63303Tom Caputi		byteswap = (eck.zec_magic == BSWAP_64(ZEC_MAGIC));
448fa9e406ahrens
449e14bb32Jeff Bonwick		if (byteswap)
450e14bb32Jeff Bonwick			byteswap_uint64_array(&verifier, sizeof (zio_cksum_t));
451e14bb32Jeff Bonwick
452eb63303Tom Caputi		expected_cksum = eck.zec_cksum;
453eb63303Tom Caputi
454eb63303Tom Caputi		abd_copy_from_buf_off(abd, &verifier, eck_offset,
455eb63303Tom Caputi		    sizeof (zio_cksum_t));
456770499eDan Kimmel
457770499eDan Kimmel		ci->ci_func[byteswap](abd, size,
45845818eeMatthew Ahrens		    spa->spa_cksum_tmpls[checksum], &actual_cksum);
459eb63303Tom Caputi
460eb63303Tom Caputi		abd_copy_from_buf_off(abd, &expected_cksum, eck_offset,
461eb63303Tom Caputi		    sizeof (zio_cksum_t));
462e14bb32Jeff Bonwick
463dcbf3bdGeorge Wilson		if (byteswap) {
464fa9e406ahrens			byteswap_uint64_array(&expected_cksum,
465fa9e406ahrens			    sizeof (zio_cksum_t));
466dcbf3bdGeorge Wilson		}
467fa9e406ahrens	} else {
468e14bb32Jeff Bonwick		byteswap = BP_SHOULD_BYTESWAP(bp);
469e14bb32Jeff Bonwick		expected_cksum = bp->blk_cksum;
470770499eDan Kimmel		ci->ci_func[byteswap](abd, size,
47145818eeMatthew Ahrens		    spa->spa_cksum_tmpls[checksum], &actual_cksum);
472fa9e406ahrens	}
473fa9e406ahrens
474eb63303Tom Caputi	/*
475eb63303Tom Caputi	 * MAC checksums are a special case since half of this checksum will
476eb63303Tom Caputi	 * actually be the encryption MAC. This will be verified by the
477eb63303Tom Caputi	 * decryption process, so we just check the truncated checksum now.
478eb63303Tom Caputi	 * Objset blocks use embedded MACs so we don't truncate the checksum
479eb63303Tom Caputi	 * for them.
480eb63303Tom Caputi	 */
481eb63303Tom Caputi	if (bp != NULL && BP_USES_CRYPT(bp) &&
482eb63303Tom Caputi	    BP_GET_TYPE(bp) != DMU_OT_OBJSET) {
483eb63303Tom Caputi		if (!(ci->ci_flags & ZCHECKSUM_FLAG_DEDUP)) {
484eb63303Tom Caputi			actual_cksum.zc_word[0] ^= actual_cksum.zc_word[2];
485eb63303Tom Caputi			actual_cksum.zc_word[1] ^= actual_cksum.zc_word[3];
486eb63303Tom Caputi		}
487eb63303Tom Caputi
488eb63303Tom Caputi		actual_cksum.zc_word[2] = 0;
489eb63303Tom Caputi		actual_cksum.zc_word[3] = 0;
490eb63303Tom Caputi		expected_cksum.zc_word[2] = 0;
491eb63303Tom Caputi		expected_cksum.zc_word[3] = 0;
492eb63303Tom Caputi	}
493eb63303Tom Caputi
494dcbf3bdGeorge Wilson	if (info != NULL) {
495dcbf3bdGeorge Wilson		info->zbc_expected = expected_cksum;
496dcbf3bdGeorge Wilson		info->zbc_actual = actual_cksum;
497dcbf3bdGeorge Wilson		info->zbc_checksum_name = ci->ci_name;
498dcbf3bdGeorge Wilson		info->zbc_byteswapped = byteswap;
499dcbf3bdGeorge Wilson		info->zbc_injected = 0;
500dcbf3bdGeorge Wilson		info->zbc_has_cksum = 1;
501dcbf3bdGeorge Wilson	}
502e14bb32Jeff Bonwick	if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum))
503be6fd75Matthew Ahrens		return (SET_ERROR(ECKSUM));
504fa9e406ahrens
505dcbf3bdGeorge Wilson	return (0);
506dcbf3bdGeorge Wilson}
507dcbf3bdGeorge Wilson
508dcbf3bdGeorge Wilsonint
509dcbf3bdGeorge Wilsonzio_checksum_error(zio_t *zio, zio_bad_cksum_t *info)
510dcbf3bdGeorge Wilson{
511dcbf3bdGeorge Wilson	blkptr_t *bp = zio->io_bp;
512dcbf3bdGeorge Wilson	uint_t checksum = (bp == NULL ? zio->io_prop.zp_checksum :
513dcbf3bdGeorge Wilson	    (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp)));
514dcbf3bdGeorge Wilson	int error;
515dcbf3bdGeorge Wilson	uint64_t size = (bp == NULL ? zio->io_size :
516dcbf3bdGeorge Wilson	    (BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp)));
517dcbf3bdGeorge Wilson	uint64_t offset = zio->io_offset;
518770499eDan Kimmel	abd_t *data = zio->io_abd;
519dcbf3bdGeorge Wilson	spa_t *spa = zio->io_spa;
520dcbf3bdGeorge Wilson
521dcbf3bdGeorge Wilson	error = zio_checksum_error_impl(spa, bp, checksum, data, size,
522dcbf3bdGeorge Wilson	    offset, info);
52322fe2c8Jonathan Adams
5246cedfc3Pavel Zakharov	if (zio_injection_enabled && error == 0 && zio->io_error == 0) {
5256cedfc3Pavel Zakharov		error = zio_handle_fault_injection(zio, ECKSUM);
5266cedfc3Pavel Zakharov		if (error != 0)
5276cedfc3Pavel Zakharov			info->zbc_injected = 1;
52822fe2c8Jonathan Adams	}
5296cedfc3Pavel Zakharov
530dcbf3bdGeorge Wilson	return (error);
531fa9e406ahrens}
53245818eeMatthew Ahrens
53345818eeMatthew Ahrens/*
53445818eeMatthew Ahrens * Called by a spa_t that's about to be deallocated. This steps through
53545818eeMatthew Ahrens * all of the checksum context templates and deallocates any that were
53645818eeMatthew Ahrens * initialized using the algorithm-specific template init function.
53745818eeMatthew Ahrens */
53845818eeMatthew Ahrensvoid
53945818eeMatthew Ahrenszio_checksum_templates_free(spa_t *spa)
54045818eeMatthew Ahrens{
54145818eeMatthew Ahrens	for (enum zio_checksum checksum = 0;
54245818eeMatthew Ahrens	    checksum < ZIO_CHECKSUM_FUNCTIONS; checksum++) {
54345818eeMatthew Ahrens		if (spa->spa_cksum_tmpls[checksum] != NULL) {
54445818eeMatthew Ahrens			zio_checksum_info_t *ci = &zio_checksum_table[checksum];
54545818eeMatthew Ahrens
54645818eeMatthew Ahrens			VERIFY(ci->ci_tmpl_free != NULL);
54745818eeMatthew Ahrens			ci->ci_tmpl_free(spa->spa_cksum_tmpls[checksum]);
54845818eeMatthew Ahrens			spa->spa_cksum_tmpls[checksum] = NULL;
54945818eeMatthew Ahrens		}
55045818eeMatthew Ahrens	}
55145818eeMatthew Ahrens}
552