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 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 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 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 123770499e1SDan Kimmel /*ARGSUSED*/ 124770499e1SDan Kimmel void 125770499e1SDan Kimmel abd_fletcher_4_native(abd_t *abd, uint64_t size, 126770499e1SDan Kimmel const void *ctx_template, zio_cksum_t *zcp) 127770499e1SDan Kimmel { 128770499e1SDan Kimmel fletcher_init(zcp); 129770499e1SDan Kimmel (void) abd_iterate_func(abd, 0, size, 130770499e1SDan Kimmel fletcher_4_incremental_native, zcp); 131770499e1SDan Kimmel } 132770499e1SDan Kimmel 133770499e1SDan Kimmel /*ARGSUSED*/ 134770499e1SDan Kimmel void 135770499e1SDan Kimmel abd_fletcher_4_byteswap(abd_t *abd, uint64_t size, 136770499e1SDan Kimmel const void *ctx_template, zio_cksum_t *zcp) 137770499e1SDan Kimmel { 138770499e1SDan Kimmel fletcher_init(zcp); 139770499e1SDan Kimmel (void) abd_iterate_func(abd, 0, size, 140770499e1SDan Kimmel fletcher_4_incremental_byteswap, zcp); 141770499e1SDan Kimmel } 142770499e1SDan Kimmel 143fa9e4066Sahrens zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = { 14445818ee1SMatthew Ahrens {{NULL, NULL}, NULL, NULL, 0, "inherit"}, 14545818ee1SMatthew Ahrens {{NULL, NULL}, NULL, NULL, 0, "on"}, 146770499e1SDan Kimmel {{abd_checksum_off, abd_checksum_off}, 14745818ee1SMatthew Ahrens NULL, NULL, 0, "off"}, 148770499e1SDan Kimmel {{abd_checksum_SHA256, abd_checksum_SHA256}, 14945818ee1SMatthew Ahrens NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED, 15045818ee1SMatthew Ahrens "label"}, 151770499e1SDan Kimmel {{abd_checksum_SHA256, abd_checksum_SHA256}, 15245818ee1SMatthew Ahrens NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED, 15345818ee1SMatthew Ahrens "gang_header"}, 154770499e1SDan Kimmel {{abd_fletcher_2_native, abd_fletcher_2_byteswap}, 15545818ee1SMatthew Ahrens NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog"}, 156770499e1SDan Kimmel {{abd_fletcher_2_native, abd_fletcher_2_byteswap}, 15745818ee1SMatthew Ahrens NULL, NULL, 0, "fletcher2"}, 158770499e1SDan Kimmel {{abd_fletcher_4_native, abd_fletcher_4_byteswap}, 15945818ee1SMatthew Ahrens NULL, NULL, ZCHECKSUM_FLAG_METADATA, "fletcher4"}, 160770499e1SDan Kimmel {{abd_checksum_SHA256, abd_checksum_SHA256}, 16145818ee1SMatthew Ahrens NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP | 16245818ee1SMatthew Ahrens ZCHECKSUM_FLAG_NOPWRITE, "sha256"}, 163770499e1SDan Kimmel {{abd_fletcher_4_native, abd_fletcher_4_byteswap}, 16445818ee1SMatthew Ahrens NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog2"}, 165770499e1SDan Kimmel {{abd_checksum_off, abd_checksum_off}, 16645818ee1SMatthew Ahrens NULL, NULL, 0, "noparity"}, 167770499e1SDan Kimmel {{abd_checksum_SHA512_native, abd_checksum_SHA512_byteswap}, 16845818ee1SMatthew Ahrens NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP | 16945818ee1SMatthew Ahrens ZCHECKSUM_FLAG_NOPWRITE, "sha512"}, 170770499e1SDan Kimmel {{abd_checksum_skein_native, abd_checksum_skein_byteswap}, 171770499e1SDan Kimmel abd_checksum_skein_tmpl_init, abd_checksum_skein_tmpl_free, 17245818ee1SMatthew Ahrens ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP | 17345818ee1SMatthew Ahrens ZCHECKSUM_FLAG_SALTED | ZCHECKSUM_FLAG_NOPWRITE, "skein"}, 174770499e1SDan Kimmel {{abd_checksum_edonr_native, abd_checksum_edonr_byteswap}, 175770499e1SDan Kimmel abd_checksum_edonr_tmpl_init, abd_checksum_edonr_tmpl_free, 17645818ee1SMatthew Ahrens ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_SALTED | 17745818ee1SMatthew Ahrens ZCHECKSUM_FLAG_NOPWRITE, "edonr"}, 178fa9e4066Sahrens }; 179fa9e4066Sahrens 180971640e6Silovezfs /* 181971640e6Silovezfs * The flag corresponding to the "verify" in dedup=[checksum,]verify 182971640e6Silovezfs * must be cleared first, so callers should use ZIO_CHECKSUM_MASK. 183971640e6Silovezfs */ 18445818ee1SMatthew Ahrens spa_feature_t 18545818ee1SMatthew Ahrens zio_checksum_to_feature(enum zio_checksum cksum) 18645818ee1SMatthew Ahrens { 187971640e6Silovezfs VERIFY((cksum & ~ZIO_CHECKSUM_MASK) == 0); 188971640e6Silovezfs 18945818ee1SMatthew Ahrens switch (cksum) { 19045818ee1SMatthew Ahrens case ZIO_CHECKSUM_SHA512: 19145818ee1SMatthew Ahrens return (SPA_FEATURE_SHA512); 19245818ee1SMatthew Ahrens case ZIO_CHECKSUM_SKEIN: 19345818ee1SMatthew Ahrens return (SPA_FEATURE_SKEIN); 19445818ee1SMatthew Ahrens case ZIO_CHECKSUM_EDONR: 19545818ee1SMatthew Ahrens return (SPA_FEATURE_EDONR); 19645818ee1SMatthew Ahrens } 19745818ee1SMatthew Ahrens return (SPA_FEATURE_NONE); 19845818ee1SMatthew Ahrens } 19945818ee1SMatthew Ahrens 200b24ab676SJeff Bonwick enum zio_checksum 201b24ab676SJeff Bonwick zio_checksum_select(enum zio_checksum child, enum zio_checksum parent) 202fa9e4066Sahrens { 203fa9e4066Sahrens ASSERT(child < ZIO_CHECKSUM_FUNCTIONS); 204fa9e4066Sahrens ASSERT(parent < ZIO_CHECKSUM_FUNCTIONS); 205fa9e4066Sahrens ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON); 206fa9e4066Sahrens 207fa9e4066Sahrens if (child == ZIO_CHECKSUM_INHERIT) 208fa9e4066Sahrens return (parent); 209fa9e4066Sahrens 210fa9e4066Sahrens if (child == ZIO_CHECKSUM_ON) 211fa9e4066Sahrens return (ZIO_CHECKSUM_ON_VALUE); 212fa9e4066Sahrens 213fa9e4066Sahrens return (child); 214fa9e4066Sahrens } 215fa9e4066Sahrens 216b24ab676SJeff Bonwick enum zio_checksum 217b24ab676SJeff Bonwick zio_checksum_dedup_select(spa_t *spa, enum zio_checksum child, 218b24ab676SJeff Bonwick enum zio_checksum parent) 219b24ab676SJeff Bonwick { 220b24ab676SJeff Bonwick ASSERT((child & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS); 221b24ab676SJeff Bonwick ASSERT((parent & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS); 222b24ab676SJeff Bonwick ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON); 223b24ab676SJeff Bonwick 224b24ab676SJeff Bonwick if (child == ZIO_CHECKSUM_INHERIT) 225b24ab676SJeff Bonwick return (parent); 226b24ab676SJeff Bonwick 227b24ab676SJeff Bonwick if (child == ZIO_CHECKSUM_ON) 228b24ab676SJeff Bonwick return (spa_dedup_checksum(spa)); 229b24ab676SJeff Bonwick 230b24ab676SJeff Bonwick if (child == (ZIO_CHECKSUM_ON | ZIO_CHECKSUM_VERIFY)) 231b24ab676SJeff Bonwick return (spa_dedup_checksum(spa) | ZIO_CHECKSUM_VERIFY); 232b24ab676SJeff Bonwick 23345818ee1SMatthew Ahrens ASSERT((zio_checksum_table[child & ZIO_CHECKSUM_MASK].ci_flags & 23445818ee1SMatthew Ahrens ZCHECKSUM_FLAG_DEDUP) || 235b24ab676SJeff Bonwick (child & ZIO_CHECKSUM_VERIFY) || child == ZIO_CHECKSUM_OFF); 236b24ab676SJeff Bonwick 237b24ab676SJeff Bonwick return (child); 238b24ab676SJeff Bonwick } 239b24ab676SJeff Bonwick 240e14bb325SJeff Bonwick /* 241e14bb325SJeff Bonwick * Set the external verifier for a gang block based on <vdev, offset, txg>, 242e14bb325SJeff Bonwick * a tuple which is guaranteed to be unique for the life of the pool. 243e14bb325SJeff Bonwick */ 244e14bb325SJeff Bonwick static void 245eb633035STom Caputi zio_checksum_gang_verifier(zio_cksum_t *zcp, const blkptr_t *bp) 246e14bb325SJeff Bonwick { 247eb633035STom Caputi const dva_t *dva = BP_IDENTITY(bp); 248b24ab676SJeff Bonwick uint64_t txg = BP_PHYSICAL_BIRTH(bp); 249e14bb325SJeff Bonwick 250e14bb325SJeff Bonwick ASSERT(BP_IS_GANG(bp)); 251e14bb325SJeff Bonwick 252e14bb325SJeff Bonwick ZIO_SET_CHECKSUM(zcp, DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva), txg, 0); 253e14bb325SJeff Bonwick } 254e14bb325SJeff Bonwick 255e14bb325SJeff Bonwick /* 256e14bb325SJeff Bonwick * Set the external verifier for a label block based on its offset. 257e14bb325SJeff Bonwick * The vdev is implicit, and the txg is unknowable at pool open time -- 258e14bb325SJeff Bonwick * hence the logic in vdev_uberblock_load() to find the most recent copy. 259e14bb325SJeff Bonwick */ 260e14bb325SJeff Bonwick static void 261e14bb325SJeff Bonwick zio_checksum_label_verifier(zio_cksum_t *zcp, uint64_t offset) 262e14bb325SJeff Bonwick { 263e14bb325SJeff Bonwick ZIO_SET_CHECKSUM(zcp, offset, 0, 0, 0); 264e14bb325SJeff Bonwick } 265e14bb325SJeff Bonwick 26645818ee1SMatthew Ahrens /* 26745818ee1SMatthew Ahrens * Calls the template init function of a checksum which supports context 26845818ee1SMatthew Ahrens * templates and installs the template into the spa_t. 26945818ee1SMatthew Ahrens */ 27045818ee1SMatthew Ahrens static void 27145818ee1SMatthew Ahrens zio_checksum_template_init(enum zio_checksum checksum, spa_t *spa) 27245818ee1SMatthew Ahrens { 27345818ee1SMatthew Ahrens zio_checksum_info_t *ci = &zio_checksum_table[checksum]; 27445818ee1SMatthew Ahrens 27545818ee1SMatthew Ahrens if (ci->ci_tmpl_init == NULL) 27645818ee1SMatthew Ahrens return; 27745818ee1SMatthew Ahrens if (spa->spa_cksum_tmpls[checksum] != NULL) 27845818ee1SMatthew Ahrens return; 27945818ee1SMatthew Ahrens 28045818ee1SMatthew Ahrens VERIFY(ci->ci_tmpl_free != NULL); 28145818ee1SMatthew Ahrens mutex_enter(&spa->spa_cksum_tmpls_lock); 28245818ee1SMatthew Ahrens if (spa->spa_cksum_tmpls[checksum] == NULL) { 28345818ee1SMatthew Ahrens spa->spa_cksum_tmpls[checksum] = 28445818ee1SMatthew Ahrens ci->ci_tmpl_init(&spa->spa_cksum_salt); 28545818ee1SMatthew Ahrens VERIFY(spa->spa_cksum_tmpls[checksum] != NULL); 28645818ee1SMatthew Ahrens } 28745818ee1SMatthew Ahrens mutex_exit(&spa->spa_cksum_tmpls_lock); 28845818ee1SMatthew Ahrens } 28945818ee1SMatthew Ahrens 290eb633035STom Caputi /* convenience function to update a checksum to accomodate an encryption MAC */ 291eb633035STom Caputi static void 292eb633035STom Caputi zio_checksum_handle_crypt(zio_cksum_t *cksum, zio_cksum_t *saved, boolean_t xor) 293eb633035STom Caputi { 294eb633035STom Caputi /* 295eb633035STom Caputi * Weak checksums do not have their entropy spread evenly 296eb633035STom Caputi * across the bits of the checksum. Therefore, when truncating 297eb633035STom Caputi * a weak checksum we XOR the first 2 words with the last 2 so 298eb633035STom Caputi * that we don't "lose" any entropy unnecessarily. 299eb633035STom Caputi */ 300eb633035STom Caputi if (xor) { 301eb633035STom Caputi cksum->zc_word[0] ^= cksum->zc_word[2]; 302eb633035STom Caputi cksum->zc_word[1] ^= cksum->zc_word[3]; 303eb633035STom Caputi } 304eb633035STom Caputi 305eb633035STom Caputi cksum->zc_word[2] = saved->zc_word[2]; 306eb633035STom Caputi cksum->zc_word[3] = saved->zc_word[3]; 307eb633035STom Caputi } 308eb633035STom Caputi 309fa9e4066Sahrens /* 310fa9e4066Sahrens * Generate the checksum. 311fa9e4066Sahrens */ 312fa9e4066Sahrens void 313e14bb325SJeff Bonwick zio_checksum_compute(zio_t *zio, enum zio_checksum checksum, 314770499e1SDan Kimmel abd_t *abd, uint64_t size) 315fa9e4066Sahrens { 316eb633035STom Caputi static const uint64_t zec_magic = ZEC_MAGIC; 317e14bb325SJeff Bonwick blkptr_t *bp = zio->io_bp; 318e14bb325SJeff Bonwick uint64_t offset = zio->io_offset; 319*29de914fSToomas Soome zio_checksum_info_t *ci; 320eb633035STom Caputi zio_cksum_t cksum, saved; 32145818ee1SMatthew Ahrens spa_t *spa = zio->io_spa; 322*29de914fSToomas Soome boolean_t insecure; 323fa9e4066Sahrens 324e14bb325SJeff Bonwick ASSERT((uint_t)checksum < ZIO_CHECKSUM_FUNCTIONS); 325*29de914fSToomas Soome ci = &zio_checksum_table[checksum]; 326fa9e4066Sahrens ASSERT(ci->ci_func[0] != NULL); 327*29de914fSToomas Soome insecure = (ci->ci_flags & ZCHECKSUM_FLAG_DEDUP) == 0; 328fa9e4066Sahrens 32945818ee1SMatthew Ahrens zio_checksum_template_init(checksum, spa); 33045818ee1SMatthew Ahrens 33145818ee1SMatthew Ahrens if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) { 332eb633035STom Caputi zio_eck_t eck; 333eb633035STom Caputi size_t eck_offset; 334eb633035STom Caputi 335eb633035STom Caputi bzero(&saved, sizeof (zio_cksum_t)); 3366e1f5caaSNeil Perrin 3376e1f5caaSNeil Perrin if (checksum == ZIO_CHECKSUM_ZILOG2) { 338eb633035STom Caputi zil_chain_t zilc; 339eb633035STom Caputi abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t)); 3406e1f5caaSNeil Perrin 341eb633035STom Caputi size = P2ROUNDUP_TYPED(zilc.zc_nused, ZIL_MIN_BLKSZ, 3426e1f5caaSNeil Perrin uint64_t); 343eb633035STom Caputi eck = zilc.zc_eck; 344eb633035STom Caputi eck_offset = offsetof(zil_chain_t, zc_eck); 3456e1f5caaSNeil Perrin } else { 346eb633035STom Caputi eck_offset = size - sizeof (zio_eck_t); 347eb633035STom Caputi abd_copy_to_buf_off(&eck, abd, eck_offset, 348eb633035STom Caputi sizeof (zio_eck_t)); 3496e1f5caaSNeil Perrin } 350eb633035STom Caputi 351eb633035STom Caputi if (checksum == ZIO_CHECKSUM_GANG_HEADER) { 352eb633035STom Caputi zio_checksum_gang_verifier(&eck.zec_cksum, bp); 353eb633035STom Caputi } else if (checksum == ZIO_CHECKSUM_LABEL) { 354eb633035STom Caputi zio_checksum_label_verifier(&eck.zec_cksum, offset); 355eb633035STom Caputi } else { 356eb633035STom Caputi saved = eck.zec_cksum; 357eb633035STom Caputi eck.zec_cksum = bp->blk_cksum; 358eb633035STom Caputi } 359eb633035STom Caputi 360eb633035STom Caputi abd_copy_from_buf_off(abd, &zec_magic, 361eb633035STom Caputi eck_offset + offsetof(zio_eck_t, zec_magic), 362eb633035STom Caputi sizeof (zec_magic)); 363eb633035STom Caputi abd_copy_from_buf_off(abd, &eck.zec_cksum, 364eb633035STom Caputi eck_offset + offsetof(zio_eck_t, zec_cksum), 365eb633035STom Caputi sizeof (zio_cksum_t)); 366eb633035STom Caputi 367770499e1SDan Kimmel ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum], 36845818ee1SMatthew Ahrens &cksum); 369eb633035STom Caputi if (bp != NULL && BP_USES_CRYPT(bp) && 370eb633035STom Caputi BP_GET_TYPE(bp) != DMU_OT_OBJSET) 371eb633035STom Caputi zio_checksum_handle_crypt(&cksum, &saved, insecure); 372eb633035STom Caputi 373eb633035STom Caputi abd_copy_from_buf_off(abd, &cksum, 374eb633035STom Caputi eck_offset + offsetof(zio_eck_t, zec_cksum), 375eb633035STom Caputi sizeof (zio_cksum_t)); 376fa9e4066Sahrens } else { 377eb633035STom Caputi saved = bp->blk_cksum; 378770499e1SDan Kimmel ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum], 379eb633035STom Caputi &cksum); 380eb633035STom Caputi if (BP_USES_CRYPT(bp) && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 381eb633035STom Caputi zio_checksum_handle_crypt(&cksum, &saved, insecure); 382eb633035STom Caputi bp->blk_cksum = cksum; 383fa9e4066Sahrens } 384fa9e4066Sahrens } 385fa9e4066Sahrens 386fa9e4066Sahrens int 387eb633035STom Caputi zio_checksum_error_impl(spa_t *spa, const blkptr_t *bp, 388eb633035STom Caputi enum zio_checksum checksum, abd_t *abd, uint64_t size, 389eb633035STom Caputi uint64_t offset, zio_bad_cksum_t *info) 390fa9e4066Sahrens { 391*29de914fSToomas Soome zio_checksum_info_t *ci; 392dcbf3bd6SGeorge Wilson zio_cksum_t actual_cksum, expected_cksum; 393eb633035STom Caputi zio_eck_t eck; 394dcbf3bd6SGeorge Wilson int byteswap; 395fa9e4066Sahrens 396*29de914fSToomas Soome if (checksum >= ZIO_CHECKSUM_FUNCTIONS) 397*29de914fSToomas Soome return (SET_ERROR(EINVAL)); 398*29de914fSToomas Soome 399*29de914fSToomas Soome ci = &zio_checksum_table[checksum]; 400*29de914fSToomas Soome 401*29de914fSToomas Soome if (ci->ci_func[0] == NULL) 402be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 403fa9e4066Sahrens 40445818ee1SMatthew Ahrens zio_checksum_template_init(checksum, spa); 40545818ee1SMatthew Ahrens 40645818ee1SMatthew Ahrens if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) { 407dcbf3bd6SGeorge Wilson zio_cksum_t verifier; 408eb633035STom Caputi size_t eck_offset; 4096e1f5caaSNeil Perrin 4106e1f5caaSNeil Perrin if (checksum == ZIO_CHECKSUM_ZILOG2) { 411eb633035STom Caputi zil_chain_t zilc; 4126e1f5caaSNeil Perrin uint64_t nused; 4136e1f5caaSNeil Perrin 414eb633035STom Caputi abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t)); 415eb633035STom Caputi 416eb633035STom Caputi eck = zilc.zc_eck; 417eb633035STom Caputi eck_offset = offsetof(zil_chain_t, zc_eck) + 418eb633035STom Caputi offsetof(zio_eck_t, zec_cksum); 419eb633035STom Caputi 420eb633035STom Caputi if (eck.zec_magic == ZEC_MAGIC) { 421eb633035STom Caputi nused = zilc.zc_nused; 422eb633035STom Caputi } else if (eck.zec_magic == BSWAP_64(ZEC_MAGIC)) { 423eb633035STom Caputi nused = BSWAP_64(zilc.zc_nused); 424770499e1SDan Kimmel } else { 425be6fd75aSMatthew Ahrens return (SET_ERROR(ECKSUM)); 426770499e1SDan Kimmel } 4276e1f5caaSNeil Perrin 428eb633035STom Caputi if (nused > size) { 429be6fd75aSMatthew Ahrens return (SET_ERROR(ECKSUM)); 430770499e1SDan Kimmel } 4316e1f5caaSNeil Perrin 4326e1f5caaSNeil Perrin size = P2ROUNDUP_TYPED(nused, ZIL_MIN_BLKSZ, uint64_t); 4336e1f5caaSNeil Perrin } else { 434eb633035STom Caputi eck_offset = size - sizeof (zio_eck_t); 435eb633035STom Caputi abd_copy_to_buf_off(&eck, abd, eck_offset, 436eb633035STom Caputi sizeof (zio_eck_t)); 437eb633035STom Caputi eck_offset += offsetof(zio_eck_t, zec_cksum); 4386e1f5caaSNeil Perrin } 4396e1f5caaSNeil Perrin 440fa9e4066Sahrens if (checksum == ZIO_CHECKSUM_GANG_HEADER) 441e14bb325SJeff Bonwick zio_checksum_gang_verifier(&verifier, bp); 442e14bb325SJeff Bonwick else if (checksum == ZIO_CHECKSUM_LABEL) 443e14bb325SJeff Bonwick zio_checksum_label_verifier(&verifier, offset); 444e14bb325SJeff Bonwick else 445e14bb325SJeff Bonwick verifier = bp->blk_cksum; 446e14bb325SJeff Bonwick 447eb633035STom Caputi byteswap = (eck.zec_magic == BSWAP_64(ZEC_MAGIC)); 448fa9e4066Sahrens 449e14bb325SJeff Bonwick if (byteswap) 450e14bb325SJeff Bonwick byteswap_uint64_array(&verifier, sizeof (zio_cksum_t)); 451e14bb325SJeff Bonwick 452eb633035STom Caputi expected_cksum = eck.zec_cksum; 453eb633035STom Caputi 454eb633035STom Caputi abd_copy_from_buf_off(abd, &verifier, eck_offset, 455eb633035STom Caputi sizeof (zio_cksum_t)); 456770499e1SDan Kimmel 457770499e1SDan Kimmel ci->ci_func[byteswap](abd, size, 45845818ee1SMatthew Ahrens spa->spa_cksum_tmpls[checksum], &actual_cksum); 459eb633035STom Caputi 460eb633035STom Caputi abd_copy_from_buf_off(abd, &expected_cksum, eck_offset, 461eb633035STom Caputi sizeof (zio_cksum_t)); 462e14bb325SJeff Bonwick 463dcbf3bd6SGeorge Wilson if (byteswap) { 464fa9e4066Sahrens byteswap_uint64_array(&expected_cksum, 465fa9e4066Sahrens sizeof (zio_cksum_t)); 466dcbf3bd6SGeorge Wilson } 467fa9e4066Sahrens } else { 468e14bb325SJeff Bonwick byteswap = BP_SHOULD_BYTESWAP(bp); 469e14bb325SJeff Bonwick expected_cksum = bp->blk_cksum; 470770499e1SDan Kimmel ci->ci_func[byteswap](abd, size, 47145818ee1SMatthew Ahrens spa->spa_cksum_tmpls[checksum], &actual_cksum); 472fa9e4066Sahrens } 473fa9e4066Sahrens 474eb633035STom Caputi /* 475eb633035STom Caputi * MAC checksums are a special case since half of this checksum will 476eb633035STom Caputi * actually be the encryption MAC. This will be verified by the 477eb633035STom Caputi * decryption process, so we just check the truncated checksum now. 478eb633035STom Caputi * Objset blocks use embedded MACs so we don't truncate the checksum 479eb633035STom Caputi * for them. 480eb633035STom Caputi */ 481eb633035STom Caputi if (bp != NULL && BP_USES_CRYPT(bp) && 482eb633035STom Caputi BP_GET_TYPE(bp) != DMU_OT_OBJSET) { 483eb633035STom Caputi if (!(ci->ci_flags & ZCHECKSUM_FLAG_DEDUP)) { 484eb633035STom Caputi actual_cksum.zc_word[0] ^= actual_cksum.zc_word[2]; 485eb633035STom Caputi actual_cksum.zc_word[1] ^= actual_cksum.zc_word[3]; 486eb633035STom Caputi } 487eb633035STom Caputi 488eb633035STom Caputi actual_cksum.zc_word[2] = 0; 489eb633035STom Caputi actual_cksum.zc_word[3] = 0; 490eb633035STom Caputi expected_cksum.zc_word[2] = 0; 491eb633035STom Caputi expected_cksum.zc_word[3] = 0; 492eb633035STom Caputi } 493eb633035STom Caputi 494dcbf3bd6SGeorge Wilson if (info != NULL) { 495dcbf3bd6SGeorge Wilson info->zbc_expected = expected_cksum; 496dcbf3bd6SGeorge Wilson info->zbc_actual = actual_cksum; 497dcbf3bd6SGeorge Wilson info->zbc_checksum_name = ci->ci_name; 498dcbf3bd6SGeorge Wilson info->zbc_byteswapped = byteswap; 499dcbf3bd6SGeorge Wilson info->zbc_injected = 0; 500dcbf3bd6SGeorge Wilson info->zbc_has_cksum = 1; 501dcbf3bd6SGeorge Wilson } 502e14bb325SJeff Bonwick if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum)) 503be6fd75aSMatthew Ahrens return (SET_ERROR(ECKSUM)); 504fa9e4066Sahrens 505dcbf3bd6SGeorge Wilson return (0); 506dcbf3bd6SGeorge Wilson } 507dcbf3bd6SGeorge Wilson 508dcbf3bd6SGeorge Wilson int 509dcbf3bd6SGeorge Wilson zio_checksum_error(zio_t *zio, zio_bad_cksum_t *info) 510dcbf3bd6SGeorge Wilson { 511dcbf3bd6SGeorge Wilson blkptr_t *bp = zio->io_bp; 512dcbf3bd6SGeorge Wilson uint_t checksum = (bp == NULL ? zio->io_prop.zp_checksum : 513dcbf3bd6SGeorge Wilson (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp))); 514dcbf3bd6SGeorge Wilson int error; 515dcbf3bd6SGeorge Wilson uint64_t size = (bp == NULL ? zio->io_size : 516dcbf3bd6SGeorge Wilson (BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp))); 517dcbf3bd6SGeorge Wilson uint64_t offset = zio->io_offset; 518770499e1SDan Kimmel abd_t *data = zio->io_abd; 519dcbf3bd6SGeorge Wilson spa_t *spa = zio->io_spa; 520dcbf3bd6SGeorge Wilson 521dcbf3bd6SGeorge Wilson error = zio_checksum_error_impl(spa, bp, checksum, data, size, 522dcbf3bd6SGeorge Wilson offset, info); 52322fe2c88SJonathan Adams 5246cedfc39SPavel Zakharov if (zio_injection_enabled && error == 0 && zio->io_error == 0) { 5256cedfc39SPavel Zakharov error = zio_handle_fault_injection(zio, ECKSUM); 5266cedfc39SPavel Zakharov if (error != 0) 5276cedfc39SPavel Zakharov info->zbc_injected = 1; 52822fe2c88SJonathan Adams } 5296cedfc39SPavel Zakharov 530dcbf3bd6SGeorge Wilson return (error); 531fa9e4066Sahrens } 53245818ee1SMatthew Ahrens 53345818ee1SMatthew Ahrens /* 53445818ee1SMatthew Ahrens * Called by a spa_t that's about to be deallocated. This steps through 53545818ee1SMatthew Ahrens * all of the checksum context templates and deallocates any that were 53645818ee1SMatthew Ahrens * initialized using the algorithm-specific template init function. 53745818ee1SMatthew Ahrens */ 53845818ee1SMatthew Ahrens void 53945818ee1SMatthew Ahrens zio_checksum_templates_free(spa_t *spa) 54045818ee1SMatthew Ahrens { 54145818ee1SMatthew Ahrens for (enum zio_checksum checksum = 0; 54245818ee1SMatthew Ahrens checksum < ZIO_CHECKSUM_FUNCTIONS; checksum++) { 54345818ee1SMatthew Ahrens if (spa->spa_cksum_tmpls[checksum] != NULL) { 54445818ee1SMatthew Ahrens zio_checksum_info_t *ci = &zio_checksum_table[checksum]; 54545818ee1SMatthew Ahrens 54645818ee1SMatthew Ahrens VERIFY(ci->ci_tmpl_free != NULL); 54745818ee1SMatthew Ahrens ci->ci_tmpl_free(spa->spa_cksum_tmpls[checksum]); 54845818ee1SMatthew Ahrens spa->spa_cksum_tmpls[checksum] = NULL; 54945818ee1SMatthew Ahrens } 55045818ee1SMatthew Ahrens } 55145818ee1SMatthew Ahrens } 552