]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zio_checksum.c
Introduce ARC Buffer Data (ABD)
[mirror_zfs.git] / module / zfs / zio_checksum.c
CommitLineData
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.
3c67d83a 23 * Copyright 2013 Saso Kiselkov. All rights reserved.
a6255b7f 24 * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
34dc7c2f
BB
25 */
26
34dc7c2f
BB
27#include <sys/zfs_context.h>
28#include <sys/spa.h>
3c67d83a 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>
a6255b7f 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).
3c67d83a
TH
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*/
95static void
a6255b7f
DQ
96abd_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
a6255b7f
DQ
102/*ARGSUSED*/
103void
104abd_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*/
113void
114abd_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/*ARGSUSED*/
123void
124abd_fletcher_4_native(abd_t *abd, uint64_t size,
125 const void *ctx_template, zio_cksum_t *zcp)
126{
127 fletcher_init(zcp);
128 (void) abd_iterate_func(abd, 0, size,
129 fletcher_4_incremental_native, zcp);
130}
131
132/*ARGSUSED*/
133void
134abd_fletcher_4_byteswap(abd_t *abd, uint64_t size,
135 const void *ctx_template, zio_cksum_t *zcp)
136{
137 fletcher_init(zcp);
138 (void) abd_iterate_func(abd, 0, size,
139 fletcher_4_incremental_byteswap, zcp);
140}
141
34dc7c2f 142zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
3c67d83a
TH
143 {{NULL, NULL}, NULL, NULL, 0, "inherit"},
144 {{NULL, NULL}, NULL, NULL, 0, "on"},
a6255b7f 145 {{abd_checksum_off, abd_checksum_off},
3c67d83a 146 NULL, NULL, 0, "off"},
a6255b7f 147 {{abd_checksum_SHA256, abd_checksum_SHA256},
3c67d83a
TH
148 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
149 "label"},
a6255b7f 150 {{abd_checksum_SHA256, abd_checksum_SHA256},
3c67d83a
TH
151 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
152 "gang_header"},
a6255b7f 153 {{abd_fletcher_2_native, abd_fletcher_2_byteswap},
3c67d83a 154 NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog"},
a6255b7f 155 {{abd_fletcher_2_native, abd_fletcher_2_byteswap},
3c67d83a 156 NULL, NULL, 0, "fletcher2"},
a6255b7f 157 {{abd_fletcher_4_native, abd_fletcher_4_byteswap},
3c67d83a 158 NULL, NULL, ZCHECKSUM_FLAG_METADATA, "fletcher4"},
a6255b7f 159 {{abd_checksum_SHA256, abd_checksum_SHA256},
3c67d83a
TH
160 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
161 ZCHECKSUM_FLAG_NOPWRITE, "sha256"},
a6255b7f 162 {{abd_fletcher_4_native, abd_fletcher_4_byteswap},
3c67d83a 163 NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog2"},
a6255b7f 164 {{abd_checksum_off, abd_checksum_off},
3c67d83a 165 NULL, NULL, 0, "noparity"},
a6255b7f 166 {{abd_checksum_SHA512_native, abd_checksum_SHA512_byteswap},
3c67d83a
TH
167 NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
168 ZCHECKSUM_FLAG_NOPWRITE, "sha512"},
a6255b7f
DQ
169 {{abd_checksum_skein_native, abd_checksum_skein_byteswap},
170 abd_checksum_skein_tmpl_init, abd_checksum_skein_tmpl_free,
3c67d83a
TH
171 ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
172 ZCHECKSUM_FLAG_SALTED | ZCHECKSUM_FLAG_NOPWRITE, "skein"},
a6255b7f
DQ
173 {{abd_checksum_edonr_native, abd_checksum_edonr_byteswap},
174 abd_checksum_edonr_tmpl_init, abd_checksum_edonr_tmpl_free,
3c67d83a
TH
175 ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_SALTED |
176 ZCHECKSUM_FLAG_NOPWRITE, "edonr"},
34dc7c2f
BB
177};
178
4a2e9a17 179/*
180 * The flag corresponding to the "verify" in dedup=[checksum,]verify
181 * must be cleared first, so callers should use ZIO_CHECKSUM_MASK.
182 */
3c67d83a
TH
183spa_feature_t
184zio_checksum_to_feature(enum zio_checksum cksum)
185{
4a2e9a17 186 VERIFY((cksum & ~ZIO_CHECKSUM_MASK) == 0);
187
3c67d83a
TH
188 switch (cksum) {
189 case ZIO_CHECKSUM_SHA512:
190 return (SPA_FEATURE_SHA512);
191 case ZIO_CHECKSUM_SKEIN:
192 return (SPA_FEATURE_SKEIN);
193 case ZIO_CHECKSUM_EDONR:
194 return (SPA_FEATURE_EDONR);
195 default:
196 return (SPA_FEATURE_NONE);
197 }
198}
199
428870ff
BB
200enum zio_checksum
201zio_checksum_select(enum zio_checksum child, enum zio_checksum parent)
34dc7c2f
BB
202{
203 ASSERT(child < ZIO_CHECKSUM_FUNCTIONS);
204 ASSERT(parent < ZIO_CHECKSUM_FUNCTIONS);
205 ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
206
207 if (child == ZIO_CHECKSUM_INHERIT)
208 return (parent);
209
210 if (child == ZIO_CHECKSUM_ON)
211 return (ZIO_CHECKSUM_ON_VALUE);
212
213 return (child);
214}
215
428870ff
BB
216enum zio_checksum
217zio_checksum_dedup_select(spa_t *spa, enum zio_checksum child,
218 enum zio_checksum parent)
219{
220 ASSERT((child & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
221 ASSERT((parent & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
222 ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
223
224 if (child == ZIO_CHECKSUM_INHERIT)
225 return (parent);
226
227 if (child == ZIO_CHECKSUM_ON)
228 return (spa_dedup_checksum(spa));
229
230 if (child == (ZIO_CHECKSUM_ON | ZIO_CHECKSUM_VERIFY))
231 return (spa_dedup_checksum(spa) | ZIO_CHECKSUM_VERIFY);
232
3c67d83a
TH
233 ASSERT((zio_checksum_table[child & ZIO_CHECKSUM_MASK].ci_flags &
234 ZCHECKSUM_FLAG_DEDUP) ||
428870ff
BB
235 (child & ZIO_CHECKSUM_VERIFY) || child == ZIO_CHECKSUM_OFF);
236
237 return (child);
238}
239
b128c09f
BB
240/*
241 * Set the external verifier for a gang block based on <vdev, offset, txg>,
242 * a tuple which is guaranteed to be unique for the life of the pool.
243 */
244static void
245zio_checksum_gang_verifier(zio_cksum_t *zcp, blkptr_t *bp)
246{
9b67f605 247 const dva_t *dva = BP_IDENTITY(bp);
428870ff 248 uint64_t txg = BP_PHYSICAL_BIRTH(bp);
b128c09f
BB
249
250 ASSERT(BP_IS_GANG(bp));
251
252 ZIO_SET_CHECKSUM(zcp, DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva), txg, 0);
253}
254
255/*
256 * Set the external verifier for a label block based on its offset.
257 * The vdev is implicit, and the txg is unknowable at pool open time --
258 * hence the logic in vdev_uberblock_load() to find the most recent copy.
259 */
260static void
261zio_checksum_label_verifier(zio_cksum_t *zcp, uint64_t offset)
262{
263 ZIO_SET_CHECKSUM(zcp, offset, 0, 0, 0);
264}
265
3c67d83a
TH
266/*
267 * Calls the template init function of a checksum which supports context
268 * templates and installs the template into the spa_t.
269 */
270static void
271zio_checksum_template_init(enum zio_checksum checksum, spa_t *spa)
272{
273 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
274
275 if (ci->ci_tmpl_init == NULL)
276 return;
277 if (spa->spa_cksum_tmpls[checksum] != NULL)
278 return;
279
280 VERIFY(ci->ci_tmpl_free != NULL);
281 mutex_enter(&spa->spa_cksum_tmpls_lock);
282 if (spa->spa_cksum_tmpls[checksum] == NULL) {
283 spa->spa_cksum_tmpls[checksum] =
284 ci->ci_tmpl_init(&spa->spa_cksum_salt);
285 VERIFY(spa->spa_cksum_tmpls[checksum] != NULL);
286 }
287 mutex_exit(&spa->spa_cksum_tmpls_lock);
288}
289
34dc7c2f
BB
290/*
291 * Generate the checksum.
292 */
293void
b128c09f 294zio_checksum_compute(zio_t *zio, enum zio_checksum checksum,
a6255b7f 295 abd_t *abd, uint64_t size)
34dc7c2f 296{
b128c09f
BB
297 blkptr_t *bp = zio->io_bp;
298 uint64_t offset = zio->io_offset;
34dc7c2f 299 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
428870ff 300 zio_cksum_t cksum;
3c67d83a 301 spa_t *spa = zio->io_spa;
34dc7c2f 302
b128c09f 303 ASSERT((uint_t)checksum < ZIO_CHECKSUM_FUNCTIONS);
34dc7c2f
BB
304 ASSERT(ci->ci_func[0] != NULL);
305
3c67d83a
TH
306 zio_checksum_template_init(checksum, spa);
307
308 if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
428870ff 309 zio_eck_t *eck;
a6255b7f 310 void *data = abd_to_buf(abd);
428870ff
BB
311
312 if (checksum == ZIO_CHECKSUM_ZILOG2) {
313 zil_chain_t *zilc = data;
314
315 size = P2ROUNDUP_TYPED(zilc->zc_nused, ZIL_MIN_BLKSZ,
316 uint64_t);
317 eck = &zilc->zc_eck;
318 } else {
319 eck = (zio_eck_t *)((char *)data + size) - 1;
320 }
b128c09f 321 if (checksum == ZIO_CHECKSUM_GANG_HEADER)
428870ff 322 zio_checksum_gang_verifier(&eck->zec_cksum, bp);
b128c09f 323 else if (checksum == ZIO_CHECKSUM_LABEL)
428870ff 324 zio_checksum_label_verifier(&eck->zec_cksum, offset);
b128c09f 325 else
428870ff
BB
326 bp->blk_cksum = eck->zec_cksum;
327 eck->zec_magic = ZEC_MAGIC;
a6255b7f 328 ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum],
3c67d83a 329 &cksum);
428870ff 330 eck->zec_cksum = cksum;
34dc7c2f 331 } else {
a6255b7f 332 ci->ci_func[0](abd, size, spa->spa_cksum_tmpls[checksum],
3c67d83a 333 &bp->blk_cksum);
34dc7c2f
BB
334 }
335}
336
337int
d3c2ae1c 338zio_checksum_error_impl(spa_t *spa, blkptr_t *bp, enum zio_checksum checksum,
a6255b7f 339 abd_t *abd, uint64_t size, uint64_t offset, zio_bad_cksum_t *info)
34dc7c2f 340{
34dc7c2f 341 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
d3c2ae1c 342 int byteswap;
3c67d83a 343 zio_cksum_t actual_cksum, expected_cksum;
34dc7c2f
BB
344
345 if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
2e528b49 346 return (SET_ERROR(EINVAL));
34dc7c2f 347
3c67d83a
TH
348 zio_checksum_template_init(checksum, spa);
349
350 if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
428870ff 351 zio_eck_t *eck;
d3c2ae1c 352 zio_cksum_t verifier;
a6255b7f
DQ
353 size_t eck_offset;
354 uint64_t data_size = size;
355 void *data = abd_borrow_buf_copy(abd, data_size);
428870ff
BB
356
357 if (checksum == ZIO_CHECKSUM_ZILOG2) {
358 zil_chain_t *zilc = data;
359 uint64_t nused;
360
361 eck = &zilc->zc_eck;
a6255b7f 362 if (eck->zec_magic == ZEC_MAGIC) {
428870ff 363 nused = zilc->zc_nused;
a6255b7f 364 } else if (eck->zec_magic == BSWAP_64(ZEC_MAGIC)) {
428870ff 365 nused = BSWAP_64(zilc->zc_nused);
a6255b7f
DQ
366 } else {
367 abd_return_buf(abd, data, data_size);
2e528b49 368 return (SET_ERROR(ECKSUM));
a6255b7f 369 }
428870ff 370
a6255b7f
DQ
371 if (nused > data_size) {
372 abd_return_buf(abd, data, data_size);
2e528b49 373 return (SET_ERROR(ECKSUM));
a6255b7f 374 }
428870ff
BB
375
376 size = P2ROUNDUP_TYPED(nused, ZIL_MIN_BLKSZ, uint64_t);
377 } else {
a6255b7f 378 eck = (zio_eck_t *)((char *)data + data_size) - 1;
428870ff
BB
379 }
380
34dc7c2f 381 if (checksum == ZIO_CHECKSUM_GANG_HEADER)
b128c09f
BB
382 zio_checksum_gang_verifier(&verifier, bp);
383 else if (checksum == ZIO_CHECKSUM_LABEL)
384 zio_checksum_label_verifier(&verifier, offset);
385 else
386 verifier = bp->blk_cksum;
387
428870ff 388 byteswap = (eck->zec_magic == BSWAP_64(ZEC_MAGIC));
34dc7c2f 389
b128c09f
BB
390 if (byteswap)
391 byteswap_uint64_array(&verifier, sizeof (zio_cksum_t));
392
a6255b7f 393 eck_offset = (size_t)(&eck->zec_cksum) - (size_t)data;
428870ff
BB
394 expected_cksum = eck->zec_cksum;
395 eck->zec_cksum = verifier;
a6255b7f
DQ
396 abd_return_buf_copy(abd, data, data_size);
397
398 ci->ci_func[byteswap](abd, size,
3c67d83a 399 spa->spa_cksum_tmpls[checksum], &actual_cksum);
a6255b7f
DQ
400 abd_copy_from_buf_off(abd, &expected_cksum,
401 eck_offset, sizeof (zio_cksum_t));
b128c09f 402
d3c2ae1c 403 if (byteswap) {
34dc7c2f
BB
404 byteswap_uint64_array(&expected_cksum,
405 sizeof (zio_cksum_t));
d3c2ae1c 406 }
34dc7c2f 407 } else {
b128c09f
BB
408 byteswap = BP_SHOULD_BYTESWAP(bp);
409 expected_cksum = bp->blk_cksum;
a6255b7f 410 ci->ci_func[byteswap](abd, size,
3c67d83a 411 spa->spa_cksum_tmpls[checksum], &actual_cksum);
34dc7c2f
BB
412 }
413
d3c2ae1c
GW
414 if (info != NULL) {
415 info->zbc_expected = expected_cksum;
416 info->zbc_actual = actual_cksum;
417 info->zbc_checksum_name = ci->ci_name;
418 info->zbc_byteswapped = byteswap;
419 info->zbc_injected = 0;
420 info->zbc_has_cksum = 1;
421 }
428870ff 422
b128c09f 423 if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum))
2e528b49 424 return (SET_ERROR(ECKSUM));
34dc7c2f 425
d3c2ae1c
GW
426 return (0);
427}
428
429int
430zio_checksum_error(zio_t *zio, zio_bad_cksum_t *info)
431{
432 blkptr_t *bp = zio->io_bp;
433 uint_t checksum = (bp == NULL ? zio->io_prop.zp_checksum :
434 (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp)));
435 int error;
436 uint64_t size = (bp == NULL ? zio->io_size :
437 (BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp)));
438 uint64_t offset = zio->io_offset;
a6255b7f 439 abd_t *data = zio->io_abd;
d3c2ae1c
GW
440 spa_t *spa = zio->io_spa;
441
442 error = zio_checksum_error_impl(spa, bp, checksum, data, size,
443 offset, info);
444 if (error != 0 && zio_injection_enabled && !zio->io_error &&
428870ff
BB
445 (error = zio_handle_fault_injection(zio, ECKSUM)) != 0) {
446
447 info->zbc_injected = 1;
448 return (error);
449 }
d3c2ae1c 450 return (error);
34dc7c2f 451}
3c67d83a
TH
452
453/*
454 * Called by a spa_t that's about to be deallocated. This steps through
455 * all of the checksum context templates and deallocates any that were
456 * initialized using the algorithm-specific template init function.
457 */
458void
459zio_checksum_templates_free(spa_t *spa)
460{
461 enum zio_checksum checksum;
462 for (checksum = 0; checksum < ZIO_CHECKSUM_FUNCTIONS;
463 checksum++) {
464 if (spa->spa_cksum_tmpls[checksum] != NULL) {
465 zio_checksum_info_t *ci = &zio_checksum_table[checksum];
466
467 VERIFY(ci->ci_tmpl_free != NULL);
468 ci->ci_tmpl_free(spa->spa_cksum_tmpls[checksum]);
469 spa->spa_cksum_tmpls[checksum] = NULL;
470 }
471 }
472}