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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 #include <sys/zfs_context.h>
28 #include <sys/zio_checksum.h>
30 #include <zfs_fletcher.h>
35 * In the SPA, everything is checksummed. We support checksum vectors
36 * for three distinct reasons:
38 * 1. Different kinds of data need different levels of protection.
39 * For SPA metadata, we always want a very strong checksum.
40 * For user data, we let users make the trade-off between speed
41 * and checksum strength.
43 * 2. Cryptographic hash and MAC algorithms are an area of active research.
44 * It is likely that in future hash functions will be at least as strong
45 * as current best-of-breed, and may be substantially faster as well.
46 * We want the ability to take advantage of these new hashes as soon as
47 * they become available.
49 * 3. If someone develops hardware that can compute a strong hash quickly,
50 * we want the ability to take advantage of that hardware.
52 * Of course, we don't want a checksum upgrade to invalidate existing
53 * data, so we store the checksum *function* in eight bits of the bp.
54 * This gives us room for up to 256 different checksum functions.
56 * When writing a block, we always checksum it with the latest-and-greatest
57 * checksum function of the appropriate strength. When reading a block,
58 * we compare the expected checksum against the actual checksum, which we
59 * compute via the checksum function specified by BP_GET_CHECKSUM(bp).
64 zio_checksum_off(const void *buf
, uint64_t size
, zio_cksum_t
*zcp
)
66 ZIO_SET_CHECKSUM(zcp
, 0, 0, 0, 0);
69 zio_checksum_info_t zio_checksum_table
[ZIO_CHECKSUM_FUNCTIONS
] = {
70 {{NULL
, NULL
}, 0, 0, 0, "inherit"},
71 {{NULL
, NULL
}, 0, 0, 0, "on"},
72 {{zio_checksum_off
, zio_checksum_off
}, 0, 0, 0, "off"},
73 {{zio_checksum_SHA256
, zio_checksum_SHA256
}, 1, 1, 0, "label"},
74 {{zio_checksum_SHA256
, zio_checksum_SHA256
}, 1, 1, 0, "gang_header"},
75 {{fletcher_2_native
, fletcher_2_byteswap
}, 0, 1, 0, "zilog"},
76 {{fletcher_2_native
, fletcher_2_byteswap
}, 0, 0, 0, "fletcher2"},
77 {{fletcher_4_native
, fletcher_4_byteswap
}, 1, 0, 0, "fletcher4"},
78 {{zio_checksum_SHA256
, zio_checksum_SHA256
}, 1, 0, 1, "sha256"},
79 {{fletcher_4_native
, fletcher_4_byteswap
}, 0, 1, 0, "zilog2"},
83 zio_checksum_select(enum zio_checksum child
, enum zio_checksum parent
)
85 ASSERT(child
< ZIO_CHECKSUM_FUNCTIONS
);
86 ASSERT(parent
< ZIO_CHECKSUM_FUNCTIONS
);
87 ASSERT(parent
!= ZIO_CHECKSUM_INHERIT
&& parent
!= ZIO_CHECKSUM_ON
);
89 if (child
== ZIO_CHECKSUM_INHERIT
)
92 if (child
== ZIO_CHECKSUM_ON
)
93 return (ZIO_CHECKSUM_ON_VALUE
);
99 zio_checksum_dedup_select(spa_t
*spa
, enum zio_checksum child
,
100 enum zio_checksum parent
)
102 ASSERT((child
& ZIO_CHECKSUM_MASK
) < ZIO_CHECKSUM_FUNCTIONS
);
103 ASSERT((parent
& ZIO_CHECKSUM_MASK
) < ZIO_CHECKSUM_FUNCTIONS
);
104 ASSERT(parent
!= ZIO_CHECKSUM_INHERIT
&& parent
!= ZIO_CHECKSUM_ON
);
106 if (child
== ZIO_CHECKSUM_INHERIT
)
109 if (child
== ZIO_CHECKSUM_ON
)
110 return (spa_dedup_checksum(spa
));
112 if (child
== (ZIO_CHECKSUM_ON
| ZIO_CHECKSUM_VERIFY
))
113 return (spa_dedup_checksum(spa
) | ZIO_CHECKSUM_VERIFY
);
115 ASSERT(zio_checksum_table
[child
& ZIO_CHECKSUM_MASK
].ci_dedup
||
116 (child
& ZIO_CHECKSUM_VERIFY
) || child
== ZIO_CHECKSUM_OFF
);
122 * Set the external verifier for a gang block based on <vdev, offset, txg>,
123 * a tuple which is guaranteed to be unique for the life of the pool.
126 zio_checksum_gang_verifier(zio_cksum_t
*zcp
, blkptr_t
*bp
)
128 dva_t
*dva
= BP_IDENTITY(bp
);
129 uint64_t txg
= BP_PHYSICAL_BIRTH(bp
);
131 ASSERT(BP_IS_GANG(bp
));
133 ZIO_SET_CHECKSUM(zcp
, DVA_GET_VDEV(dva
), DVA_GET_OFFSET(dva
), txg
, 0);
137 * Set the external verifier for a label block based on its offset.
138 * The vdev is implicit, and the txg is unknowable at pool open time --
139 * hence the logic in vdev_uberblock_load() to find the most recent copy.
142 zio_checksum_label_verifier(zio_cksum_t
*zcp
, uint64_t offset
)
144 ZIO_SET_CHECKSUM(zcp
, offset
, 0, 0, 0);
148 * Generate the checksum.
151 zio_checksum_compute(zio_t
*zio
, enum zio_checksum checksum
,
152 void *data
, uint64_t size
)
154 blkptr_t
*bp
= zio
->io_bp
;
155 uint64_t offset
= zio
->io_offset
;
156 zio_checksum_info_t
*ci
= &zio_checksum_table
[checksum
];
159 ASSERT((uint_t
)checksum
< ZIO_CHECKSUM_FUNCTIONS
);
160 ASSERT(ci
->ci_func
[0] != NULL
);
165 if (checksum
== ZIO_CHECKSUM_ZILOG2
) {
166 zil_chain_t
*zilc
= data
;
168 size
= P2ROUNDUP_TYPED(zilc
->zc_nused
, ZIL_MIN_BLKSZ
,
172 eck
= (zio_eck_t
*)((char *)data
+ size
) - 1;
174 if (checksum
== ZIO_CHECKSUM_GANG_HEADER
)
175 zio_checksum_gang_verifier(&eck
->zec_cksum
, bp
);
176 else if (checksum
== ZIO_CHECKSUM_LABEL
)
177 zio_checksum_label_verifier(&eck
->zec_cksum
, offset
);
179 bp
->blk_cksum
= eck
->zec_cksum
;
180 eck
->zec_magic
= ZEC_MAGIC
;
181 ci
->ci_func
[0](data
, size
, &cksum
);
182 eck
->zec_cksum
= cksum
;
184 ci
->ci_func
[0](data
, size
, &bp
->blk_cksum
);
189 zio_checksum_error(zio_t
*zio
, zio_bad_cksum_t
*info
)
191 blkptr_t
*bp
= zio
->io_bp
;
192 uint_t checksum
= (bp
== NULL
? zio
->io_prop
.zp_checksum
:
193 (BP_IS_GANG(bp
) ? ZIO_CHECKSUM_GANG_HEADER
: BP_GET_CHECKSUM(bp
)));
196 uint64_t size
= (bp
== NULL
? zio
->io_size
:
197 (BP_IS_GANG(bp
) ? SPA_GANGBLOCKSIZE
: BP_GET_PSIZE(bp
)));
198 uint64_t offset
= zio
->io_offset
;
199 void *data
= zio
->io_data
;
200 zio_checksum_info_t
*ci
= &zio_checksum_table
[checksum
];
201 zio_cksum_t actual_cksum
, expected_cksum
, verifier
;
203 if (checksum
>= ZIO_CHECKSUM_FUNCTIONS
|| ci
->ci_func
[0] == NULL
)
209 if (checksum
== ZIO_CHECKSUM_ZILOG2
) {
210 zil_chain_t
*zilc
= data
;
214 if (eck
->zec_magic
== ZEC_MAGIC
)
215 nused
= zilc
->zc_nused
;
216 else if (eck
->zec_magic
== BSWAP_64(ZEC_MAGIC
))
217 nused
= BSWAP_64(zilc
->zc_nused
);
224 size
= P2ROUNDUP_TYPED(nused
, ZIL_MIN_BLKSZ
, uint64_t);
226 eck
= (zio_eck_t
*)((char *)data
+ size
) - 1;
229 if (checksum
== ZIO_CHECKSUM_GANG_HEADER
)
230 zio_checksum_gang_verifier(&verifier
, bp
);
231 else if (checksum
== ZIO_CHECKSUM_LABEL
)
232 zio_checksum_label_verifier(&verifier
, offset
);
234 verifier
= bp
->blk_cksum
;
236 byteswap
= (eck
->zec_magic
== BSWAP_64(ZEC_MAGIC
));
239 byteswap_uint64_array(&verifier
, sizeof (zio_cksum_t
));
241 expected_cksum
= eck
->zec_cksum
;
242 eck
->zec_cksum
= verifier
;
243 ci
->ci_func
[byteswap
](data
, size
, &actual_cksum
);
244 eck
->zec_cksum
= expected_cksum
;
247 byteswap_uint64_array(&expected_cksum
,
248 sizeof (zio_cksum_t
));
250 ASSERT(!BP_IS_GANG(bp
));
251 byteswap
= BP_SHOULD_BYTESWAP(bp
);
252 expected_cksum
= bp
->blk_cksum
;
253 ci
->ci_func
[byteswap
](data
, size
, &actual_cksum
);
256 info
->zbc_expected
= expected_cksum
;
257 info
->zbc_actual
= actual_cksum
;
258 info
->zbc_checksum_name
= ci
->ci_name
;
259 info
->zbc_byteswapped
= byteswap
;
260 info
->zbc_injected
= 0;
261 info
->zbc_has_cksum
= 1;
263 if (!ZIO_CHECKSUM_EQUAL(actual_cksum
, expected_cksum
))
266 if (zio_injection_enabled
&& !zio
->io_error
&&
267 (error
= zio_handle_fault_injection(zio
, ECKSUM
)) != 0) {
269 info
->zbc_injected
= 1;