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 245*eb633035STom Caputi zio_checksum_gang_verifier(zio_cksum_t *zcp, const blkptr_t *bp) 246e14bb325SJeff Bonwick { 247*eb633035STom 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 290*eb633035STom Caputi /* convenience function to update a checksum to accomodate an encryption MAC */ 291*eb633035STom Caputi static void 292*eb633035STom Caputi zio_checksum_handle_crypt(zio_cksum_t *cksum, zio_cksum_t *saved, boolean_t xor) 293*eb633035STom Caputi { 294*eb633035STom Caputi /* 295*eb633035STom Caputi * Weak checksums do not have their entropy spread evenly 296*eb633035STom Caputi * across the bits of the checksum. Therefore, when truncating 297*eb633035STom Caputi * a weak checksum we XOR the first 2 words with the last 2 so 298*eb633035STom Caputi * that we don't "lose" any entropy unnecessarily. 299*eb633035STom Caputi */ 300*eb633035STom Caputi if (xor) { 301*eb633035STom Caputi cksum->zc_word[0] ^= cksum->zc_word[2]; 302*eb633035STom Caputi cksum->zc_word[1] ^= cksum->zc_word[3]; 303*eb633035STom Caputi } 304*eb633035STom Caputi 305*eb633035STom Caputi cksum->zc_word[2] = saved->zc_word[2]; 306*eb633035STom Caputi cksum->zc_word[3] = saved->zc_word[3]; 307*eb633035STom Caputi } 308*eb633035STom 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 { 316*eb633035STom 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; 319fa9e4066Sahrens zio_checksum_info_t *ci = &zio_checksum_table[checksum]; 320*eb633035STom Caputi zio_cksum_t cksum, saved; 32145818ee1SMatthew Ahrens spa_t *spa = zio->io_spa; 322*eb633035STom Caputi boolean_t insecure = (ci->ci_flags & ZCHECKSUM_FLAG_DEDUP) == 0; 323fa9e4066Sahrens 324e14bb325SJeff Bonwick ASSERT((uint_t)checksum < ZIO_CHECKSUM_FUNCTIONS); 325fa9e4066Sahrens ASSERT(ci->ci_func[0] != NULL); 326fa9e4066Sahrens 32745818ee1SMatthew Ahrens zio_checksum_template_init(checksum, spa); 32845818ee1SMatthew Ahrens 32945818ee1SMatthew Ahrens if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) { 330*eb633035STom Caputi zio_eck_t eck; 331*eb633035STom Caputi size_t eck_offset; 332*eb633035STom Caputi 333*eb633035STom Caputi bzero(&saved, sizeof (zio_cksum_t)); 3346e1f5caaSNeil Perrin 3356e1f5caaSNeil Perrin if (checksum == ZIO_CHECKSUM_ZILOG2) { 336*eb633035STom Caputi zil_chain_t zilc; 337*eb633035STom Caputi abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t)); 3386e1f5caaSNeil Perrin 339*eb633035STom Caputi size = P2ROUNDUP_TYPED(zilc.zc_nused, ZIL_MIN_BLKSZ, 3406e1f5caaSNeil Perrin uint64_t); 341*eb633035STom Caputi eck = zilc.zc_eck; 342*eb633035STom Caputi eck_offset = offsetof(zil_chain_t, zc_eck); 3436e1f5caaSNeil Perrin } else { 344*eb633035STom Caputi eck_offset = size - sizeof (zio_eck_t); 345*eb633035STom Caputi abd_copy_to_buf_off(&eck, abd, eck_offset, 346*eb633035STom Caputi sizeof (zio_eck_t)); 3476e1f5caaSNeil Perrin } 348*eb633035STom Caputi 349*eb633035STom Caputi if (checksum == ZIO_CHECKSUM_GANG_HEADER) { 350*eb633035STom Caputi zio_checksum_gang_verifier(&eck.zec_cksum, bp); 351*eb633035STom Caputi } else if (checksum == ZIO_CHECKSUM_LABEL) { 352*eb633035STom Caputi zio_checksum_label_verifier(&eck.zec_cksum, offset); 353*eb633035STom Caputi } else { 354*eb633035STom Caputi saved = eck.zec_cksum; 355*eb633035STom Caputi eck.zec_cksum = bp->blk_cksum; 356*eb633035STom Caputi } 357*eb633035STom Caputi 358*eb633035STom Caputi abd_copy_from_buf_off(abd, &zec_magic, 359*eb633035STom Caputi eck_offset + offsetof(zio_eck_t, zec_magic), 360*eb633035STom Caputi sizeof (zec_magic)); 361*eb633035STom Caputi abd_copy_from_buf_off(abd, &eck.zec_cksum, 362*eb633035STom Caputi eck_offset + offsetof(zio_eck_t, zec_cksum), 363*eb633035STom Caputi sizeof (zio_cksum_t)); 364*eb633035STom Caputi 365770499e1SDan Kimmel ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum], 36645818ee1SMatthew Ahrens &cksum); 367*eb633035STom Caputi if (bp != NULL && BP_USES_CRYPT(bp) && 368*eb633035STom Caputi BP_GET_TYPE(bp) != DMU_OT_OBJSET) 369*eb633035STom Caputi zio_checksum_handle_crypt(&cksum, &saved, insecure); 370*eb633035STom Caputi 371*eb633035STom Caputi abd_copy_from_buf_off(abd, &cksum, 372*eb633035STom Caputi eck_offset + offsetof(zio_eck_t, zec_cksum), 373*eb633035STom Caputi sizeof (zio_cksum_t)); 374fa9e4066Sahrens } else { 375*eb633035STom Caputi saved = bp->blk_cksum; 376770499e1SDan Kimmel ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum], 377*eb633035STom Caputi &cksum); 378*eb633035STom Caputi if (BP_USES_CRYPT(bp) && BP_GET_TYPE(bp) != DMU_OT_OBJSET) 379*eb633035STom Caputi zio_checksum_handle_crypt(&cksum, &saved, insecure); 380*eb633035STom Caputi bp->blk_cksum = cksum; 381fa9e4066Sahrens } 382fa9e4066Sahrens } 383fa9e4066Sahrens 384fa9e4066Sahrens int 385*eb633035STom Caputi zio_checksum_error_impl(spa_t *spa, const blkptr_t *bp, 386*eb633035STom Caputi enum zio_checksum checksum, abd_t *abd, uint64_t size, 387*eb633035STom Caputi uint64_t offset, zio_bad_cksum_t *info) 388fa9e4066Sahrens { 389fa9e4066Sahrens zio_checksum_info_t *ci = &zio_checksum_table[checksum]; 390dcbf3bd6SGeorge Wilson zio_cksum_t actual_cksum, expected_cksum; 391*eb633035STom Caputi zio_eck_t eck; 392dcbf3bd6SGeorge Wilson int byteswap; 393fa9e4066Sahrens 394fa9e4066Sahrens if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL) 395be6fd75aSMatthew Ahrens return (SET_ERROR(EINVAL)); 396fa9e4066Sahrens 39745818ee1SMatthew Ahrens zio_checksum_template_init(checksum, spa); 39845818ee1SMatthew Ahrens 39945818ee1SMatthew Ahrens if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) { 400dcbf3bd6SGeorge Wilson zio_cksum_t verifier; 401*eb633035STom Caputi size_t eck_offset; 4026e1f5caaSNeil Perrin 4036e1f5caaSNeil Perrin if (checksum == ZIO_CHECKSUM_ZILOG2) { 404*eb633035STom Caputi zil_chain_t zilc; 4056e1f5caaSNeil Perrin uint64_t nused; 4066e1f5caaSNeil Perrin 407*eb633035STom Caputi abd_copy_to_buf(&zilc, abd, sizeof (zil_chain_t)); 408*eb633035STom Caputi 409*eb633035STom Caputi eck = zilc.zc_eck; 410*eb633035STom Caputi eck_offset = offsetof(zil_chain_t, zc_eck) + 411*eb633035STom Caputi offsetof(zio_eck_t, zec_cksum); 412*eb633035STom Caputi 413*eb633035STom Caputi if (eck.zec_magic == ZEC_MAGIC) { 414*eb633035STom Caputi nused = zilc.zc_nused; 415*eb633035STom Caputi } else if (eck.zec_magic == BSWAP_64(ZEC_MAGIC)) { 416*eb633035STom Caputi nused = BSWAP_64(zilc.zc_nused); 417770499e1SDan Kimmel } else { 418be6fd75aSMatthew Ahrens return (SET_ERROR(ECKSUM)); 419770499e1SDan Kimmel } 4206e1f5caaSNeil Perrin 421*eb633035STom Caputi if (nused > size) { 422be6fd75aSMatthew Ahrens return (SET_ERROR(ECKSUM)); 423770499e1SDan Kimmel } 4246e1f5caaSNeil Perrin 4256e1f5caaSNeil Perrin size = P2ROUNDUP_TYPED(nused, ZIL_MIN_BLKSZ, uint64_t); 4266e1f5caaSNeil Perrin } else { 427*eb633035STom Caputi eck_offset = size - sizeof (zio_eck_t); 428*eb633035STom Caputi abd_copy_to_buf_off(&eck, abd, eck_offset, 429*eb633035STom Caputi sizeof (zio_eck_t)); 430*eb633035STom Caputi eck_offset += offsetof(zio_eck_t, zec_cksum); 4316e1f5caaSNeil Perrin } 4326e1f5caaSNeil Perrin 433fa9e4066Sahrens if (checksum == ZIO_CHECKSUM_GANG_HEADER) 434e14bb325SJeff Bonwick zio_checksum_gang_verifier(&verifier, bp); 435e14bb325SJeff Bonwick else if (checksum == ZIO_CHECKSUM_LABEL) 436e14bb325SJeff Bonwick zio_checksum_label_verifier(&verifier, offset); 437e14bb325SJeff Bonwick else 438e14bb325SJeff Bonwick verifier = bp->blk_cksum; 439e14bb325SJeff Bonwick 440*eb633035STom Caputi byteswap = (eck.zec_magic == BSWAP_64(ZEC_MAGIC)); 441fa9e4066Sahrens 442e14bb325SJeff Bonwick if (byteswap) 443e14bb325SJeff Bonwick byteswap_uint64_array(&verifier, sizeof (zio_cksum_t)); 444e14bb325SJeff Bonwick 445*eb633035STom Caputi expected_cksum = eck.zec_cksum; 446*eb633035STom Caputi 447*eb633035STom Caputi abd_copy_from_buf_off(abd, &verifier, eck_offset, 448*eb633035STom Caputi sizeof (zio_cksum_t)); 449770499e1SDan Kimmel 450770499e1SDan Kimmel ci->ci_func[byteswap](abd, size, 45145818ee1SMatthew Ahrens spa->spa_cksum_tmpls[checksum], &actual_cksum); 452*eb633035STom Caputi 453*eb633035STom Caputi abd_copy_from_buf_off(abd, &expected_cksum, eck_offset, 454*eb633035STom Caputi sizeof (zio_cksum_t)); 455e14bb325SJeff Bonwick 456dcbf3bd6SGeorge Wilson if (byteswap) { 457fa9e4066Sahrens byteswap_uint64_array(&expected_cksum, 458fa9e4066Sahrens sizeof (zio_cksum_t)); 459dcbf3bd6SGeorge Wilson } 460fa9e4066Sahrens } else { 461e14bb325SJeff Bonwick byteswap = BP_SHOULD_BYTESWAP(bp); 462e14bb325SJeff Bonwick expected_cksum = bp->blk_cksum; 463770499e1SDan Kimmel ci->ci_func[byteswap](abd, size, 46445818ee1SMatthew Ahrens spa->spa_cksum_tmpls[checksum], &actual_cksum); 465fa9e4066Sahrens } 466fa9e4066Sahrens 467*eb633035STom Caputi /* 468*eb633035STom Caputi * MAC checksums are a special case since half of this checksum will 469*eb633035STom Caputi * actually be the encryption MAC. This will be verified by the 470*eb633035STom Caputi * decryption process, so we just check the truncated checksum now. 471*eb633035STom Caputi * Objset blocks use embedded MACs so we don't truncate the checksum 472*eb633035STom Caputi * for them. 473*eb633035STom Caputi */ 474*eb633035STom Caputi if (bp != NULL && BP_USES_CRYPT(bp) && 475*eb633035STom Caputi BP_GET_TYPE(bp) != DMU_OT_OBJSET) { 476*eb633035STom Caputi if (!(ci->ci_flags & ZCHECKSUM_FLAG_DEDUP)) { 477*eb633035STom Caputi actual_cksum.zc_word[0] ^= actual_cksum.zc_word[2]; 478*eb633035STom Caputi actual_cksum.zc_word[1] ^= actual_cksum.zc_word[3]; 479*eb633035STom Caputi } 480*eb633035STom Caputi 481*eb633035STom Caputi actual_cksum.zc_word[2] = 0; 482*eb633035STom Caputi actual_cksum.zc_word[3] = 0; 483*eb633035STom Caputi expected_cksum.zc_word[2] = 0; 484*eb633035STom Caputi expected_cksum.zc_word[3] = 0; 485*eb633035STom Caputi } 486*eb633035STom Caputi 487dcbf3bd6SGeorge Wilson if (info != NULL) { 488dcbf3bd6SGeorge Wilson info->zbc_expected = expected_cksum; 489dcbf3bd6SGeorge Wilson info->zbc_actual = actual_cksum; 490dcbf3bd6SGeorge Wilson info->zbc_checksum_name = ci->ci_name; 491dcbf3bd6SGeorge Wilson info->zbc_byteswapped = byteswap; 492dcbf3bd6SGeorge Wilson info->zbc_injected = 0; 493dcbf3bd6SGeorge Wilson info->zbc_has_cksum = 1; 494dcbf3bd6SGeorge Wilson } 495e14bb325SJeff Bonwick if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum)) 496be6fd75aSMatthew Ahrens return (SET_ERROR(ECKSUM)); 497fa9e4066Sahrens 498dcbf3bd6SGeorge Wilson return (0); 499dcbf3bd6SGeorge Wilson } 500dcbf3bd6SGeorge Wilson 501dcbf3bd6SGeorge Wilson int 502dcbf3bd6SGeorge Wilson zio_checksum_error(zio_t *zio, zio_bad_cksum_t *info) 503dcbf3bd6SGeorge Wilson { 504dcbf3bd6SGeorge Wilson blkptr_t *bp = zio->io_bp; 505dcbf3bd6SGeorge Wilson uint_t checksum = (bp == NULL ? zio->io_prop.zp_checksum : 506dcbf3bd6SGeorge Wilson (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp))); 507dcbf3bd6SGeorge Wilson int error; 508dcbf3bd6SGeorge Wilson uint64_t size = (bp == NULL ? zio->io_size : 509dcbf3bd6SGeorge Wilson (BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp))); 510dcbf3bd6SGeorge Wilson uint64_t offset = zio->io_offset; 511770499e1SDan Kimmel abd_t *data = zio->io_abd; 512dcbf3bd6SGeorge Wilson spa_t *spa = zio->io_spa; 513dcbf3bd6SGeorge Wilson 514dcbf3bd6SGeorge Wilson error = zio_checksum_error_impl(spa, bp, checksum, data, size, 515dcbf3bd6SGeorge Wilson offset, info); 51622fe2c88SJonathan Adams 5176cedfc39SPavel Zakharov if (zio_injection_enabled && error == 0 && zio->io_error == 0) { 5186cedfc39SPavel Zakharov error = zio_handle_fault_injection(zio, ECKSUM); 5196cedfc39SPavel Zakharov if (error != 0) 5206cedfc39SPavel Zakharov info->zbc_injected = 1; 52122fe2c88SJonathan Adams } 5226cedfc39SPavel Zakharov 523dcbf3bd6SGeorge Wilson return (error); 524fa9e4066Sahrens } 52545818ee1SMatthew Ahrens 52645818ee1SMatthew Ahrens /* 52745818ee1SMatthew Ahrens * Called by a spa_t that's about to be deallocated. This steps through 52845818ee1SMatthew Ahrens * all of the checksum context templates and deallocates any that were 52945818ee1SMatthew Ahrens * initialized using the algorithm-specific template init function. 53045818ee1SMatthew Ahrens */ 53145818ee1SMatthew Ahrens void 53245818ee1SMatthew Ahrens zio_checksum_templates_free(spa_t *spa) 53345818ee1SMatthew Ahrens { 53445818ee1SMatthew Ahrens for (enum zio_checksum checksum = 0; 53545818ee1SMatthew Ahrens checksum < ZIO_CHECKSUM_FUNCTIONS; checksum++) { 53645818ee1SMatthew Ahrens if (spa->spa_cksum_tmpls[checksum] != NULL) { 53745818ee1SMatthew Ahrens zio_checksum_info_t *ci = &zio_checksum_table[checksum]; 53845818ee1SMatthew Ahrens 53945818ee1SMatthew Ahrens VERIFY(ci->ci_tmpl_free != NULL); 54045818ee1SMatthew Ahrens ci->ci_tmpl_free(spa->spa_cksum_tmpls[checksum]); 54145818ee1SMatthew Ahrens spa->spa_cksum_tmpls[checksum] = NULL; 54245818ee1SMatthew Ahrens } 54345818ee1SMatthew Ahrens } 54445818ee1SMatthew Ahrens } 545