1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22cde58dbcSMatthew Ahrens  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
236cedfc39SPavel Zakharov  * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
24810e43b2SBill Pijewski  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
2545818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov. All rights reserved.
26fa9e4066Sahrens  */
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/zfs_context.h>
29fa9e4066Sahrens #include <sys/spa.h>
3045818ee1SMatthew Ahrens #include <sys/spa_impl.h>
31fa9e4066Sahrens #include <sys/zio.h>
32fa9e4066Sahrens #include <sys/zio_checksum.h>
336e1f5caaSNeil Perrin #include <sys/zil.h>
34770499e1SDan Kimmel #include <sys/abd.h>
35cde58dbcSMatthew Ahrens #include <zfs_fletcher.h>
36fa9e4066Sahrens 
37fa9e4066Sahrens /*
38fa9e4066Sahrens  * Checksum vectors.
39fa9e4066Sahrens  *
40fa9e4066Sahrens  * In the SPA, everything is checksummed.  We support checksum vectors
41fa9e4066Sahrens  * for three distinct reasons:
42fa9e4066Sahrens  *
43fa9e4066Sahrens  *   1. Different kinds of data need different levels of protection.
44fa9e4066Sahrens  *	For SPA metadata, we always want a very strong checksum.
45fa9e4066Sahrens  *	For user data, we let users make the trade-off between speed
46fa9e4066Sahrens  *	and checksum strength.
47fa9e4066Sahrens  *
48fa9e4066Sahrens  *   2. Cryptographic hash and MAC algorithms are an area of active research.
49fa9e4066Sahrens  *	It is likely that in future hash functions will be at least as strong
50fa9e4066Sahrens  *	as current best-of-breed, and may be substantially faster as well.
51fa9e4066Sahrens  *	We want the ability to take advantage of these new hashes as soon as
52fa9e4066Sahrens  *	they become available.
53fa9e4066Sahrens  *
54fa9e4066Sahrens  *   3. If someone develops hardware that can compute a strong hash quickly,
55fa9e4066Sahrens  *	we want the ability to take advantage of that hardware.
56fa9e4066Sahrens  *
57fa9e4066Sahrens  * Of course, we don't want a checksum upgrade to invalidate existing
58b24ab676SJeff Bonwick  * data, so we store the checksum *function* in eight bits of the bp.
59b24ab676SJeff Bonwick  * This gives us room for up to 256 different checksum functions.
60fa9e4066Sahrens  *
61fa9e4066Sahrens  * When writing a block, we always checksum it with the latest-and-greatest
62fa9e4066Sahrens  * checksum function of the appropriate strength.  When reading a block,
63fa9e4066Sahrens  * we compare the expected checksum against the actual checksum, which we
64b24ab676SJeff Bonwick  * compute via the checksum function specified by BP_GET_CHECKSUM(bp).
6545818ee1SMatthew Ahrens  *
6645818ee1SMatthew Ahrens  * SALTED CHECKSUMS
6745818ee1SMatthew Ahrens  *
6845818ee1SMatthew Ahrens  * To enable the use of less secure hash algorithms with dedup, we
6945818ee1SMatthew Ahrens  * introduce the notion of salted checksums (MACs, really).  A salted
7045818ee1SMatthew Ahrens  * checksum is fed both a random 256-bit value (the salt) and the data
7145818ee1SMatthew Ahrens  * to be checksummed.  This salt is kept secret (stored on the pool, but
7245818ee1SMatthew Ahrens  * never shown to the user).  Thus even if an attacker knew of collision
7345818ee1SMatthew Ahrens  * weaknesses in the hash algorithm, they won't be able to mount a known
7445818ee1SMatthew Ahrens  * plaintext attack on the DDT, since the actual hash value cannot be
7545818ee1SMatthew Ahrens  * known ahead of time.  How the salt is used is algorithm-specific
7645818ee1SMatthew Ahrens  * (some might simply prefix it to the data block, others might need to
7745818ee1SMatthew Ahrens  * utilize a full-blown HMAC).  On disk the salt is stored in a ZAP
7845818ee1SMatthew Ahrens  * object in the MOS (DMU_POOL_CHECKSUM_SALT).
7945818ee1SMatthew Ahrens  *
8045818ee1SMatthew Ahrens  * CONTEXT TEMPLATES
8145818ee1SMatthew Ahrens  *
8245818ee1SMatthew Ahrens  * Some hashing algorithms need to perform a substantial amount of
8345818ee1SMatthew Ahrens  * initialization work (e.g. salted checksums above may need to pre-hash
8445818ee1SMatthew Ahrens  * the salt) before being able to process data.  Performing this
8545818ee1SMatthew Ahrens  * redundant work for each block would be wasteful, so we instead allow
8645818ee1SMatthew Ahrens  * a checksum algorithm to do the work once (the first time it's used)
8745818ee1SMatthew Ahrens  * and then keep this pre-initialized context as a template inside the
8845818ee1SMatthew Ahrens  * spa_t (spa_cksum_tmpls).  If the zio_checksum_info_t contains
8945818ee1SMatthew Ahrens  * non-NULL ci_tmpl_init and ci_tmpl_free callbacks, they are used to
9045818ee1SMatthew Ahrens  * construct and destruct the pre-initialized checksum context.  The
9145818ee1SMatthew Ahrens  * pre-initialized context is then reused during each checksum
9245818ee1SMatthew Ahrens  * invocation and passed to the checksum function.
93fa9e4066Sahrens  */
94fa9e4066Sahrens 
95fa9e4066Sahrens /*ARGSUSED*/
96fa9e4066Sahrens static void
abd_checksum_off(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)97770499e1SDan Kimmel abd_checksum_off(abd_t *abd, uint64_t size,
9845818ee1SMatthew Ahrens     const void *ctx_template, zio_cksum_t *zcp)
99fa9e4066Sahrens {
100fa9e4066Sahrens 	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
101fa9e4066Sahrens }
102fa9e4066Sahrens 
103770499e1SDan Kimmel /*ARGSUSED*/
104770499e1SDan Kimmel void
abd_fletcher_2_native(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)105770499e1SDan Kimmel abd_fletcher_2_native(abd_t *abd, uint64_t size,
106770499e1SDan Kimmel     const void *ctx_template, zio_cksum_t *zcp)
107770499e1SDan Kimmel {
108770499e1SDan Kimmel 	fletcher_init(zcp);
109770499e1SDan Kimmel 	(void) abd_iterate_func(abd, 0, size,
110770499e1SDan Kimmel 	    fletcher_2_incremental_native, zcp);
111770499e1SDan Kimmel }
112770499e1SDan Kimmel 
113770499e1SDan Kimmel /*ARGSUSED*/
114770499e1SDan Kimmel void
abd_fletcher_2_byteswap(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)115770499e1SDan Kimmel abd_fletcher_2_byteswap(abd_t *abd, uint64_t size,
116770499e1SDan Kimmel     const void *ctx_template, zio_cksum_t *zcp)
117770499e1SDan Kimmel {
118770499e1SDan Kimmel 	fletcher_init(zcp);
119770499e1SDan Kimmel 	(void) abd_iterate_func(abd, 0, size,
120770499e1SDan Kimmel 	    fletcher_2_incremental_byteswap, zcp);
121770499e1SDan Kimmel }
122770499e1SDan Kimmel 
123*0886dcadSAndy Fiddaman static inline void
abd_fletcher_4_impl(abd_t * abd,uint64_t size,zio_abd_checksum_data_t * acdp)124*0886dcadSAndy Fiddaman abd_fletcher_4_impl(abd_t *abd, uint64_t size, zio_abd_checksum_data_t *acdp)
125*0886dcadSAndy Fiddaman {
126*0886dcadSAndy Fiddaman 	fletcher_4_abd_ops.acf_init(acdp);
127*0886dcadSAndy Fiddaman 	abd_iterate_func(abd, 0, size, fletcher_4_abd_ops.acf_iter, acdp);
128*0886dcadSAndy Fiddaman 	fletcher_4_abd_ops.acf_fini(acdp);
129*0886dcadSAndy Fiddaman }
130*0886dcadSAndy Fiddaman 
131770499e1SDan Kimmel /*ARGSUSED*/
132770499e1SDan Kimmel void
abd_fletcher_4_native(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)133770499e1SDan Kimmel abd_fletcher_4_native(abd_t *abd, uint64_t size,
134770499e1SDan Kimmel     const void *ctx_template, zio_cksum_t *zcp)
135770499e1SDan Kimmel {
136*0886dcadSAndy Fiddaman 	fletcher_4_ctx_t ctx;
137*0886dcadSAndy Fiddaman 
138*0886dcadSAndy Fiddaman 	zio_abd_checksum_data_t acd = {
139*0886dcadSAndy Fiddaman 		.acd_byteorder	= ZIO_CHECKSUM_NATIVE,
140*0886dcadSAndy Fiddaman 		.acd_zcp	= zcp,
141*0886dcadSAndy Fiddaman 		.acd_ctx	= &ctx
142*0886dcadSAndy Fiddaman 	};
143*0886dcadSAndy Fiddaman 
144*0886dcadSAndy Fiddaman 	abd_fletcher_4_impl(abd, size, &acd);
145*0886dcadSAndy Fiddaman 
146770499e1SDan Kimmel }
147770499e1SDan Kimmel 
148770499e1SDan Kimmel /*ARGSUSED*/
149770499e1SDan Kimmel void
abd_fletcher_4_byteswap(abd_t * abd,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)150770499e1SDan Kimmel abd_fletcher_4_byteswap(abd_t *abd, uint64_t size,
151770499e1SDan Kimmel     const void *ctx_template, zio_cksum_t *zcp)
152770499e1SDan Kimmel {
153*0886dcadSAndy Fiddaman 	fletcher_4_ctx_t ctx;
154*0886dcadSAndy Fiddaman 
155*0886dcadSAndy Fiddaman 	zio_abd_checksum_data_t acd = {
156*0886dcadSAndy Fiddaman 		.acd_byteorder	= ZIO_CHECKSUM_BYTESWAP,
157*0886dcadSAndy Fiddaman 		.acd_zcp	= zcp,
158*0886dcadSAndy Fiddaman 		.acd_ctx	= &ctx
159*0886dcadSAndy Fiddaman 	};
160*0886dcadSAndy Fiddaman 
161*0886dcadSAndy Fiddaman 	abd_fletcher_4_impl(abd, size, &acd);
162770499e1SDan Kimmel }
163770499e1SDan Kimmel 
164fa9e4066Sahrens zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
16545818ee1SMatthew Ahrens 	{{NULL, NULL}, NULL, NULL, 0, "inherit"},
16645818ee1SMatthew Ahrens 	{{NULL, NULL}, NULL, NULL, 0, "on"},
167770499e1SDan Kimmel 	{{abd_checksum_off,		abd_checksum_off},
16845818ee1SMatthew Ahrens 	    NULL, NULL, 0, "off"},
169770499e1SDan Kimmel 	{{abd_checksum_SHA256,		abd_checksum_SHA256},
17045818ee1SMatthew Ahrens 	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
17145818ee1SMatthew Ahrens 	    "label"},
172770499e1SDan Kimmel 	{{abd_checksum_SHA256,		abd_checksum_SHA256},
17345818ee1SMatthew Ahrens 	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
17445818ee1SMatthew Ahrens 	    "gang_header"},
175770499e1SDan Kimmel 	{{abd_fletcher_2_native,	abd_fletcher_2_byteswap},
17645818ee1SMatthew Ahrens 	    NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog"},
177770499e1SDan Kimmel 	{{abd_fletcher_2_native,	abd_fletcher_2_byteswap},
17845818ee1SMatthew Ahrens 	    NULL, NULL, 0, "fletcher2"},
179770499e1SDan Kimmel 	{{abd_fletcher_4_native,	abd_fletcher_4_byteswap},
18045818ee1SMatthew Ahrens 	    NULL, NULL, ZCHECKSUM_FLAG_METADATA, "fletcher4"},
181770499e1SDan Kimmel 	{{abd_checksum_SHA256,		abd_checksum_SHA256},
18245818ee1SMatthew Ahrens 	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
18345818ee1SMatthew Ahrens 	    ZCHECKSUM_FLAG_NOPWRITE, "sha256"},
184770499e1SDan Kimmel 	{{abd_fletcher_4_native,	abd_fletcher_4_byteswap},
18545818ee1SMatthew Ahrens 	    NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog2"},
186770499e1SDan Kimmel 	{{abd_checksum_off,		abd_checksum_off},
18745818ee1SMatthew Ahrens 	    NULL, NULL, 0, "noparity"},
188770499e1SDan Kimmel 	{{abd_checksum_SHA512_native,	abd_checksum_SHA512_byteswap},
18945818ee1SMatthew Ahrens 	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
19045818ee1SMatthew Ahrens 	    ZCHECKSUM_FLAG_NOPWRITE, "sha512"},
191770499e1SDan Kimmel 	{{abd_checksum_skein_native,	abd_checksum_skein_byteswap},
192770499e1SDan Kimmel 	    abd_checksum_skein_tmpl_init, abd_checksum_skein_tmpl_free,
19345818ee1SMatthew Ahrens 	    ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
19445818ee1SMatthew Ahrens 	    ZCHECKSUM_FLAG_SALTED | ZCHECKSUM_FLAG_NOPWRITE, "skein"},
195770499e1SDan Kimmel 	{{abd_checksum_edonr_native,	abd_checksum_edonr_byteswap},
196770499e1SDan Kimmel 	    abd_checksum_edonr_tmpl_init, abd_checksum_edonr_tmpl_free,
19745818ee1SMatthew Ahrens 	    ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_SALTED |
19845818ee1SMatthew Ahrens 	    ZCHECKSUM_FLAG_NOPWRITE, "edonr"},
199fa9e4066Sahrens };
200fa9e4066Sahrens 
201971640e6Silovezfs /*
202971640e6Silovezfs  * The flag corresponding to the "verify" in dedup=[checksum,]verify
203971640e6Silovezfs  * must be cleared first, so callers should use ZIO_CHECKSUM_MASK.
204971640e6Silovezfs  */
20545818ee1SMatthew Ahrens spa_feature_t
zio_checksum_to_feature(enum zio_checksum cksum)20645818ee1SMatthew Ahrens zio_checksum_to_feature(enum zio_checksum cksum)
20745818ee1SMatthew Ahrens {
208971640e6Silovezfs 	VERIFY((cksum & ~ZIO_CHECKSUM_MASK) == 0);
209971640e6Silovezfs 
21045818ee1SMatthew Ahrens 	switch (cksum) {
21145818ee1SMatthew Ahrens 	case ZIO_CHECKSUM_SHA512:
21245818ee1SMatthew Ahrens 		return (SPA_FEATURE_SHA512);
21345818ee1SMatthew Ahrens 	case ZIO_CHECKSUM_SKEIN:
21445818ee1SMatthew Ahrens 		return (SPA_FEATURE_SKEIN);
21545818ee1SMatthew Ahrens 	case ZIO_CHECKSUM_EDONR:
21645818ee1SMatthew Ahrens 		return (SPA_FEATURE_EDONR);
21745818ee1SMatthew Ahrens 	}
21845818ee1SMatthew Ahrens 	return (SPA_FEATURE_NONE);
21945818ee1SMatthew Ahrens }
22045818ee1SMatthew Ahrens 
221b24ab676SJeff Bonwick enum zio_checksum
zio_checksum_select(enum zio_checksum child,enum zio_checksum parent)222b24ab676SJeff Bonwick zio_checksum_select(enum zio_checksum child, enum zio_checksum parent)
223fa9e4066Sahrens {
224fa9e4066Sahrens 	ASSERT(child < ZIO_CHECKSUM_FUNCTIONS);
225fa9e4066Sahrens 	ASSERT(parent < ZIO_CHECKSUM_FUNCTIONS);
226fa9e4066Sahrens 	ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
227fa9e4066Sahrens 
228fa9e4066Sahrens 	if (child == ZIO_CHECKSUM_INHERIT)
229fa9e4066Sahrens 		return (parent);
230fa9e4066Sahrens 
231fa9e4066Sahrens 	if (child == ZIO_CHECKSUM_ON)
232fa9e4066Sahrens 		return (ZIO_CHECKSUM_ON_VALUE);
233fa9e4066Sahrens 
234fa9e4066Sahrens 	return (child);
235fa9e4066Sahrens }
236fa9e4066Sahrens 
237b24ab676SJeff Bonwick enum zio_checksum
zio_checksum_dedup_select(spa_t * spa,enum zio_checksum child,enum zio_checksum parent)238b24ab676SJeff Bonwick zio_checksum_dedup_select(spa_t *spa, enum zio_checksum child,
239b24ab676SJeff Bonwick     enum zio_checksum parent)
240b24ab676SJeff Bonwick {
241b24ab676SJeff Bonwick 	ASSERT((child & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
242b24ab676SJeff Bonwick 	ASSERT((parent & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
243b24ab676SJeff Bonwick 	ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
244b24ab676SJeff Bonwick 
245b24ab676SJeff Bonwick 	if (child == ZIO_CHECKSUM_INHERIT)
246b24ab676SJeff Bonwick 		return (parent);
247b24ab676SJeff Bonwick 
248b24ab676SJeff Bonwick 	if (child == ZIO_CHECKSUM_ON)
249b24ab676SJeff Bonwick 		return (spa_dedup_checksum(spa));
250b24ab676SJeff Bonwick 
251b24ab676SJeff Bonwick 	if (child == (ZIO_CHECKSUM_ON | ZIO_CHECKSUM_VERIFY))
252b24ab676SJeff Bonwick 		return (spa_dedup_checksum(spa) | ZIO_CHECKSUM_VERIFY);
253b24ab676SJeff Bonwick 
25445818ee1SMatthew Ahrens 	ASSERT((zio_checksum_table[child & ZIO_CHECKSUM_MASK].ci_flags &
25545818ee1SMatthew Ahrens 	    ZCHECKSUM_FLAG_DEDUP) ||
256b24ab676SJeff Bonwick 	    (child & ZIO_CHECKSUM_VERIFY) || child == ZIO_CHECKSUM_OFF);
257b24ab676SJeff Bonwick 
258b24ab676SJeff Bonwick 	return (child);
259b24ab676SJeff Bonwick }
260b24ab676SJeff Bonwick 
261e14bb325SJeff Bonwick /*
262e14bb325SJeff Bonwick  * Set the external verifier for a gang block based on <vdev, offset, txg>,
263e14bb325SJeff Bonwick  * a tuple which is guaranteed to be unique for the life of the pool.
264e14bb325SJeff Bonwick  */
265e14bb325SJeff Bonwick static void
zio_checksum_gang_verifier(zio_cksum_t * zcp,const blkptr_t * bp)266eb633035STom Caputi zio_checksum_gang_verifier(zio_cksum_t *zcp, const blkptr_t *bp)
267e14bb325SJeff Bonwick {
268eb633035STom Caputi 	const dva_t *dva = BP_IDENTITY(bp);
269b24ab676SJeff Bonwick 	uint64_t txg = BP_PHYSICAL_BIRTH(bp);
270e14bb325SJeff Bonwick 
271e14bb325SJeff Bonwick 	ASSERT(BP_IS_GANG(bp));
272e14bb325SJeff Bonwick 
273e14bb325SJeff Bonwick 	ZIO_SET_CHECKSUM(zcp, DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva), txg, 0);
274e14bb325SJeff Bonwick }
275e14bb325SJeff Bonwick 
276e14bb325SJeff Bonwick /*
277e14bb325SJeff Bonwick  * Set the external verifier for a label block based on its offset.
278e14bb325SJeff Bonwick  * The vdev is implicit, and the txg is unknowable at pool open time --
279e14bb325SJeff Bonwick  * hence the logic in vdev_uberblock_load() to find the most recent copy.
280e14bb325SJeff Bonwick  */
281e14bb325SJeff Bonwick static void
zio_checksum_label_verifier(zio_cksum_t * zcp,uint64_t offset)282e14bb325SJeff Bonwick zio_checksum_label_verifier(zio_cksum_t *zcp, uint64_t offset)
283e14bb325SJeff Bonwick {
284e14bb325SJeff Bonwick 	ZIO_SET_CHECKSUM(zcp, offset, 0, 0, 0);
285e14bb325SJeff Bonwick }
286e14bb325SJeff Bonwick 
28745818ee1SMatthew Ahrens /*
28845818ee1SMatthew Ahrens  * Calls the template init function of a checksum which supports context
28945818ee1SMatthew Ahrens  * templates and installs the template into the spa_t.
29045818ee1SMatthew Ahrens  */
29145818ee1SMatthew Ahrens static void
zio_checksum_template_init(enum zio_checksum checksum,spa_t * spa)29245818ee1SMatthew Ahrens zio_checksum_template_init(enum zio_checksum checksum, spa_t *spa)
29345818ee1SMatthew Ahrens {
29445818ee1SMatthew Ahrens 	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
29545818ee1SMatthew Ahrens 
29645818ee1SMatthew Ahrens 	if (ci->ci_tmpl_init == NULL)
29745818ee1SMatthew Ahrens 		return;
29845818ee1SMatthew Ahrens 	if (spa->spa_cksum_tmpls[checksum] != NULL)
29945818ee1SMatthew Ahrens 		return;
30045818ee1SMatthew Ahrens 
30145818ee1SMatthew Ahrens 	VERIFY(ci->ci_tmpl_free != NULL);
30245818ee1SMatthew Ahrens 	mutex_enter(&spa->spa_cksum_tmpls_lock);
30345818ee1SMatthew Ahrens 	if (spa->spa_cksum_tmpls[checksum] == NULL) {
30445818ee1SMatthew Ahrens 		spa->spa_cksum_tmpls[checksum] =
30545818ee1SMatthew Ahrens 		    ci->ci_tmpl_init(&spa->spa_cksum_salt);
30645818ee1SMatthew Ahrens 		VERIFY(spa->spa_cksum_tmpls[checksum] != NULL);
30745818ee1SMatthew Ahrens 	}
30845818ee1SMatthew Ahrens 	mutex_exit(&spa->spa_cksum_tmpls_lock);
30945818ee1SMatthew Ahrens }
31045818ee1SMatthew Ahrens 
311eb633035STom Caputi /* convenience function to update a checksum to accomodate an encryption MAC */
312eb633035STom Caputi static void
zio_checksum_handle_crypt(zio_cksum_t * cksum,zio_cksum_t * saved,boolean_t xor)313eb633035STom Caputi zio_checksum_handle_crypt(zio_cksum_t *cksum, zio_cksum_t *saved, boolean_t xor)
314eb633035STom Caputi {
315eb633035STom Caputi 	/*
316eb633035STom Caputi 	 * Weak checksums do not have their entropy spread evenly
317eb633035STom Caputi 	 * across the bits of the checksum. Therefore, when truncating
318eb633035STom Caputi 	 * a weak checksum we XOR the first 2 words with the last 2 so
319eb633035STom Caputi 	 * that we don't "lose" any entropy unnecessarily.
320eb633035STom Caputi 	 */
321eb633035STom Caputi 	if (xor) {
322eb633035STom Caputi 		cksum->zc_word[0] ^= cksum->zc_word[2];
323eb633035STom Caputi 		cksum->zc_word[1] ^= cksum->zc_word[3];
324eb633035STom Caputi 	}
325eb633035STom Caputi 
326eb633035STom Caputi 	cksum->zc_word[2] = saved->zc_word[2];
327eb633035STom Caputi 	cksum->zc_word[3] = saved->zc_word[3];
328eb633035STom Caputi }
329eb633035STom Caputi 
330fa9e4066Sahrens /*
331fa9e4066Sahrens  * Generate the checksum.
332fa9e4066Sahrens  */
333fa9e4066Sahrens void
zio_checksum_compute(zio_t * zio,enum zio_checksum checksum,abd_t * abd,uint64_t size)334e14bb325SJeff Bonwick zio_checksum_compute(zio_t *zio, enum zio_checksum checksum,
335770499e1SDan Kimmel     abd_t *abd, uint64_t size)
336fa9e4066Sahrens {
337eb633035STom Caputi 	static const uint64_t zec_magic = ZEC_MAGIC;
338e14bb325SJeff Bonwick 	blkptr_t *bp = zio->io_bp;
339e14bb325SJeff Bonwick 	uint64_t offset = zio->io_offset;
34029de914fSToomas Soome 	zio_checksum_info_t *ci;
341eb633035STom Caputi 	zio_cksum_t cksum, saved;
34245818ee1SMatthew Ahrens 	spa_t *spa = zio->io_spa;
34329de914fSToomas Soome 	boolean_t insecure;
344fa9e4066Sahrens 
345e14bb325SJeff Bonwick 	ASSERT((uint_t)checksum < ZIO_CHECKSUM_FUNCTIONS);
34629de914fSToomas Soome 	ci = &zio_checksum_table[checksum];
347fa9e4066Sahrens 	ASSERT(ci->ci_func[0] != NULL);
34829de914fSToomas Soome 	insecure = (ci->ci_flags & ZCHECKSUM_FLAG_DEDUP) == 0;
349fa9e4066Sahrens 
35045818ee1SMatthew Ahrens 	zio_checksum_template_init(checksum, spa);
35145818ee1SMatthew Ahrens 
35245818ee1SMatthew Ahrens 	if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
353eb633035STom Caputi 		zio_eck_t eck;
354eb633035STom Caputi 		size_t eck_offset;
355eb633035STom Caputi 
356eb633035STom Caputi 		bzero(&saved, sizeof (zio_cksum_t));
3576e1f5caaSNeil Perrin 
3586e1f5caaSNeil Perrin 		if (checksum == ZIO_CHECKSUM_ZILOG2) {
359eb633035STom Caputi 			zil_chain_t zilc;
360eb633035STom Caputi 			abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t));
3616e1f5caaSNeil Perrin 
362eb633035STom Caputi 			size = P2ROUNDUP_TYPED(zilc.zc_nused, ZIL_MIN_BLKSZ,
3636e1f5caaSNeil Perrin 			    uint64_t);
364eb633035STom Caputi 			eck = zilc.zc_eck;
365eb633035STom Caputi 			eck_offset = offsetof(zil_chain_t, zc_eck);
3666e1f5caaSNeil Perrin 		} else {
367eb633035STom Caputi 			eck_offset = size - sizeof (zio_eck_t);
368eb633035STom Caputi 			abd_copy_to_buf_off(&eck, abd, eck_offset,
369eb633035STom Caputi 			    sizeof (zio_eck_t));
3706e1f5caaSNeil Perrin 		}
371eb633035STom Caputi 
372eb633035STom Caputi 		if (checksum == ZIO_CHECKSUM_GANG_HEADER) {
373eb633035STom Caputi 			zio_checksum_gang_verifier(&eck.zec_cksum, bp);
374eb633035STom Caputi 		} else if (checksum == ZIO_CHECKSUM_LABEL) {
375eb633035STom Caputi 			zio_checksum_label_verifier(&eck.zec_cksum, offset);
376eb633035STom Caputi 		} else {
377eb633035STom Caputi 			saved = eck.zec_cksum;
378eb633035STom Caputi 			eck.zec_cksum = bp->blk_cksum;
379eb633035STom Caputi 		}
380eb633035STom Caputi 
381eb633035STom Caputi 		abd_copy_from_buf_off(abd, &zec_magic,
382eb633035STom Caputi 		    eck_offset + offsetof(zio_eck_t, zec_magic),
383eb633035STom Caputi 		    sizeof (zec_magic));
384eb633035STom Caputi 		abd_copy_from_buf_off(abd, &eck.zec_cksum,
385eb633035STom Caputi 		    eck_offset + offsetof(zio_eck_t, zec_cksum),
386eb633035STom Caputi 		    sizeof (zio_cksum_t));
387eb633035STom Caputi 
388770499e1SDan Kimmel 		ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum],
38945818ee1SMatthew Ahrens 		    &cksum);
390eb633035STom Caputi 		if (bp != NULL && BP_USES_CRYPT(bp) &&
391eb633035STom Caputi 		    BP_GET_TYPE(bp) != DMU_OT_OBJSET)
392eb633035STom Caputi 			zio_checksum_handle_crypt(&cksum, &saved, insecure);
393eb633035STom Caputi 
394eb633035STom Caputi 		abd_copy_from_buf_off(abd, &cksum,
395eb633035STom Caputi 		    eck_offset + offsetof(zio_eck_t, zec_cksum),
396eb633035STom Caputi 		    sizeof (zio_cksum_t));
397fa9e4066Sahrens 	} else {
398eb633035STom Caputi 		saved = bp->blk_cksum;
399770499e1SDan Kimmel 		ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum],
400eb633035STom Caputi 		    &cksum);
401eb633035STom Caputi 		if (BP_USES_CRYPT(bp) && BP_GET_TYPE(bp) != DMU_OT_OBJSET)
402eb633035STom Caputi 			zio_checksum_handle_crypt(&cksum, &saved, insecure);
403eb633035STom Caputi 		bp->blk_cksum = cksum;
404fa9e4066Sahrens 	}
405fa9e4066Sahrens }
406fa9e4066Sahrens 
407fa9e4066Sahrens 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)408eb633035STom Caputi zio_checksum_error_impl(spa_t *spa, const blkptr_t *bp,
409eb633035STom Caputi     enum zio_checksum checksum, abd_t *abd, uint64_t size,
410eb633035STom Caputi     uint64_t offset, zio_bad_cksum_t *info)
411fa9e4066Sahrens {
41229de914fSToomas Soome 	zio_checksum_info_t *ci;
413dcbf3bd6SGeorge Wilson 	zio_cksum_t actual_cksum, expected_cksum;
414eb633035STom Caputi 	zio_eck_t eck;
415dcbf3bd6SGeorge Wilson 	int byteswap;
416fa9e4066Sahrens 
41729de914fSToomas Soome 	if (checksum >= ZIO_CHECKSUM_FUNCTIONS)
41829de914fSToomas Soome 		return (SET_ERROR(EINVAL));
41929de914fSToomas Soome 
42029de914fSToomas Soome 	ci = &zio_checksum_table[checksum];
42129de914fSToomas Soome 
42229de914fSToomas Soome 	if (ci->ci_func[0] == NULL)
423be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
424fa9e4066Sahrens 
42545818ee1SMatthew Ahrens 	zio_checksum_template_init(checksum, spa);
42645818ee1SMatthew Ahrens 
42745818ee1SMatthew Ahrens 	if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
428dcbf3bd6SGeorge Wilson 		zio_cksum_t verifier;
429eb633035STom Caputi 		size_t eck_offset;
4306e1f5caaSNeil Perrin 
4316e1f5caaSNeil Perrin 		if (checksum == ZIO_CHECKSUM_ZILOG2) {
432eb633035STom Caputi 			zil_chain_t zilc;
4336e1f5caaSNeil Perrin 			uint64_t nused;
4346e1f5caaSNeil Perrin 
435eb633035STom Caputi 			abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t));
436eb633035STom Caputi 
437eb633035STom Caputi 			eck = zilc.zc_eck;
438eb633035STom Caputi 			eck_offset = offsetof(zil_chain_t, zc_eck) +
439eb633035STom Caputi 			    offsetof(zio_eck_t, zec_cksum);
440eb633035STom Caputi 
441eb633035STom Caputi 			if (eck.zec_magic == ZEC_MAGIC) {
442eb633035STom Caputi 				nused = zilc.zc_nused;
443eb633035STom Caputi 			} else if (eck.zec_magic == BSWAP_64(ZEC_MAGIC)) {
444eb633035STom Caputi 				nused = BSWAP_64(zilc.zc_nused);
445770499e1SDan Kimmel 			} else {
446be6fd75aSMatthew Ahrens 				return (SET_ERROR(ECKSUM));
447770499e1SDan Kimmel 			}
4486e1f5caaSNeil Perrin 
449eb633035STom Caputi 			if (nused > size) {
450be6fd75aSMatthew Ahrens 				return (SET_ERROR(ECKSUM));
451770499e1SDan Kimmel 			}
4526e1f5caaSNeil Perrin 
4536e1f5caaSNeil Perrin 			size = P2ROUNDUP_TYPED(nused, ZIL_MIN_BLKSZ, uint64_t);
4546e1f5caaSNeil Perrin 		} else {
455eb633035STom Caputi 			eck_offset = size - sizeof (zio_eck_t);
456eb633035STom Caputi 			abd_copy_to_buf_off(&eck, abd, eck_offset,
457eb633035STom Caputi 			    sizeof (zio_eck_t));
458eb633035STom Caputi 			eck_offset += offsetof(zio_eck_t, zec_cksum);
4596e1f5caaSNeil Perrin 		}
4606e1f5caaSNeil Perrin 
461fa9e4066Sahrens 		if (checksum == ZIO_CHECKSUM_GANG_HEADER)
462e14bb325SJeff Bonwick 			zio_checksum_gang_verifier(&verifier, bp);
463e14bb325SJeff Bonwick 		else if (checksum == ZIO_CHECKSUM_LABEL)
464e14bb325SJeff Bonwick 			zio_checksum_label_verifier(&verifier, offset);
465e14bb325SJeff Bonwick 		else
466e14bb325SJeff Bonwick 			verifier = bp->blk_cksum;
467e14bb325SJeff Bonwick 
468eb633035STom Caputi 		byteswap = (eck.zec_magic == BSWAP_64(ZEC_MAGIC));
469fa9e4066Sahrens 
470e14bb325SJeff Bonwick 		if (byteswap)
471e14bb325SJeff Bonwick 			byteswap_uint64_array(&verifier, sizeof (zio_cksum_t));
472e14bb325SJeff Bonwick 
473eb633035STom Caputi 		expected_cksum = eck.zec_cksum;
474eb633035STom Caputi 
475eb633035STom Caputi 		abd_copy_from_buf_off(abd, &verifier, eck_offset,
476eb633035STom Caputi 		    sizeof (zio_cksum_t));
477770499e1SDan Kimmel 
478770499e1SDan Kimmel 		ci->ci_func[byteswap](abd, size,
47945818ee1SMatthew Ahrens 		    spa->spa_cksum_tmpls[checksum], &actual_cksum);
480eb633035STom Caputi 
481eb633035STom Caputi 		abd_copy_from_buf_off(abd, &expected_cksum, eck_offset,
482eb633035STom Caputi 		    sizeof (zio_cksum_t));
483e14bb325SJeff Bonwick 
484dcbf3bd6SGeorge Wilson 		if (byteswap) {
485fa9e4066Sahrens 			byteswap_uint64_array(&expected_cksum,
486fa9e4066Sahrens 			    sizeof (zio_cksum_t));
487dcbf3bd6SGeorge Wilson 		}
488fa9e4066Sahrens 	} else {
489e14bb325SJeff Bonwick 		byteswap = BP_SHOULD_BYTESWAP(bp);
490e14bb325SJeff Bonwick 		expected_cksum = bp->blk_cksum;
491770499e1SDan Kimmel 		ci->ci_func[byteswap](abd, size,
49245818ee1SMatthew Ahrens 		    spa->spa_cksum_tmpls[checksum], &actual_cksum);
493fa9e4066Sahrens 	}
494fa9e4066Sahrens 
495eb633035STom Caputi 	/*
496eb633035STom Caputi 	 * MAC checksums are a special case since half of this checksum will
497eb633035STom Caputi 	 * actually be the encryption MAC. This will be verified by the
498eb633035STom Caputi 	 * decryption process, so we just check the truncated checksum now.
499eb633035STom Caputi 	 * Objset blocks use embedded MACs so we don't truncate the checksum
500eb633035STom Caputi 	 * for them.
501eb633035STom Caputi 	 */
502eb633035STom Caputi 	if (bp != NULL && BP_USES_CRYPT(bp) &&
503eb633035STom Caputi 	    BP_GET_TYPE(bp) != DMU_OT_OBJSET) {
504eb633035STom Caputi 		if (!(ci->ci_flags & ZCHECKSUM_FLAG_DEDUP)) {
505eb633035STom Caputi 			actual_cksum.zc_word[0] ^= actual_cksum.zc_word[2];
506eb633035STom Caputi 			actual_cksum.zc_word[1] ^= actual_cksum.zc_word[3];
507eb633035STom Caputi 		}
508eb633035STom Caputi 
509eb633035STom Caputi 		actual_cksum.zc_word[2] = 0;
510eb633035STom Caputi 		actual_cksum.zc_word[3] = 0;
511eb633035STom Caputi 		expected_cksum.zc_word[2] = 0;
512eb633035STom Caputi 		expected_cksum.zc_word[3] = 0;
513eb633035STom Caputi 	}
514eb633035STom Caputi 
515dcbf3bd6SGeorge Wilson 	if (info != NULL) {
516dcbf3bd6SGeorge Wilson 		info->zbc_expected = expected_cksum;
517dcbf3bd6SGeorge Wilson 		info->zbc_actual = actual_cksum;
518dcbf3bd6SGeorge Wilson 		info->zbc_checksum_name = ci->ci_name;
519dcbf3bd6SGeorge Wilson 		info->zbc_byteswapped = byteswap;
520dcbf3bd6SGeorge Wilson 		info->zbc_injected = 0;
521dcbf3bd6SGeorge Wilson 		info->zbc_has_cksum = 1;
522dcbf3bd6SGeorge Wilson 	}
523e14bb325SJeff Bonwick 	if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum))
524be6fd75aSMatthew Ahrens 		return (SET_ERROR(ECKSUM));
525fa9e4066Sahrens 
526dcbf3bd6SGeorge Wilson 	return (0);
527dcbf3bd6SGeorge Wilson }
528dcbf3bd6SGeorge Wilson 
529dcbf3bd6SGeorge Wilson int
zio_checksum_error(zio_t * zio,zio_bad_cksum_t * info)530dcbf3bd6SGeorge Wilson zio_checksum_error(zio_t *zio, zio_bad_cksum_t *info)
531dcbf3bd6SGeorge Wilson {
532dcbf3bd6SGeorge Wilson 	blkptr_t *bp = zio->io_bp;
533dcbf3bd6SGeorge Wilson 	uint_t checksum = (bp == NULL ? zio->io_prop.zp_checksum :
534dcbf3bd6SGeorge Wilson 	    (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp)));
535dcbf3bd6SGeorge Wilson 	int error;
536dcbf3bd6SGeorge Wilson 	uint64_t size = (bp == NULL ? zio->io_size :
537dcbf3bd6SGeorge Wilson 	    (BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp)));
538dcbf3bd6SGeorge Wilson 	uint64_t offset = zio->io_offset;
539770499e1SDan Kimmel 	abd_t *data = zio->io_abd;
540dcbf3bd6SGeorge Wilson 	spa_t *spa = zio->io_spa;
541dcbf3bd6SGeorge Wilson 
542dcbf3bd6SGeorge Wilson 	error = zio_checksum_error_impl(spa, bp, checksum, data, size,
543dcbf3bd6SGeorge Wilson 	    offset, info);
54422fe2c88SJonathan Adams 
5456cedfc39SPavel Zakharov 	if (zio_injection_enabled && error == 0 && zio->io_error == 0) {
5466cedfc39SPavel Zakharov 		error = zio_handle_fault_injection(zio, ECKSUM);
5476cedfc39SPavel Zakharov 		if (error != 0)
5486cedfc39SPavel Zakharov 			info->zbc_injected = 1;
54922fe2c88SJonathan Adams 	}
5506cedfc39SPavel Zakharov 
551dcbf3bd6SGeorge Wilson 	return (error);
552fa9e4066Sahrens }
55345818ee1SMatthew Ahrens 
55445818ee1SMatthew Ahrens /*
55545818ee1SMatthew Ahrens  * Called by a spa_t that's about to be deallocated. This steps through
55645818ee1SMatthew Ahrens  * all of the checksum context templates and deallocates any that were
55745818ee1SMatthew Ahrens  * initialized using the algorithm-specific template init function.
55845818ee1SMatthew Ahrens  */
55945818ee1SMatthew Ahrens void
zio_checksum_templates_free(spa_t * spa)56045818ee1SMatthew Ahrens zio_checksum_templates_free(spa_t *spa)
56145818ee1SMatthew Ahrens {
56245818ee1SMatthew Ahrens 	for (enum zio_checksum checksum = 0;
56345818ee1SMatthew Ahrens 	    checksum < ZIO_CHECKSUM_FUNCTIONS; checksum++) {
56445818ee1SMatthew Ahrens 		if (spa->spa_cksum_tmpls[checksum] != NULL) {
56545818ee1SMatthew Ahrens 			zio_checksum_info_t *ci = &zio_checksum_table[checksum];
56645818ee1SMatthew Ahrens 
56745818ee1SMatthew Ahrens 			VERIFY(ci->ci_tmpl_free != NULL);
56845818ee1SMatthew Ahrens 			ci->ci_tmpl_free(spa->spa_cksum_tmpls[checksum]);
56945818ee1SMatthew Ahrens 			spa->spa_cksum_tmpls[checksum] = NULL;
57045818ee1SMatthew Ahrens 		}
57145818ee1SMatthew Ahrens 	}
57245818ee1SMatthew Ahrens }
573