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