xref: /titanic_52/usr/src/uts/common/fs/zfs/zio_checksum.c (revision e1d3217b9afde782c4d3e946fda0e6ef36a61306)
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 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2013 by Delphix. All rights reserved.
24  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25  */
26 
27 #include <sys/zfs_context.h>
28 #include <sys/spa.h>
29 #include <sys/zio.h>
30 #include <sys/zio_checksum.h>
31 #include <sys/zil.h>
32 #include <zfs_fletcher.h>
33 
34 /*
35  * Checksum vectors.
36  *
37  * In the SPA, everything is checksummed.  We support checksum vectors
38  * for three distinct reasons:
39  *
40  *   1. Different kinds of data need different levels of protection.
41  *	For SPA metadata, we always want a very strong checksum.
42  *	For user data, we let users make the trade-off between speed
43  *	and checksum strength.
44  *
45  *   2. Cryptographic hash and MAC algorithms are an area of active research.
46  *	It is likely that in future hash functions will be at least as strong
47  *	as current best-of-breed, and may be substantially faster as well.
48  *	We want the ability to take advantage of these new hashes as soon as
49  *	they become available.
50  *
51  *   3. If someone develops hardware that can compute a strong hash quickly,
52  *	we want the ability to take advantage of that hardware.
53  *
54  * Of course, we don't want a checksum upgrade to invalidate existing
55  * data, so we store the checksum *function* in eight bits of the bp.
56  * This gives us room for up to 256 different checksum functions.
57  *
58  * When writing a block, we always checksum it with the latest-and-greatest
59  * checksum function of the appropriate strength.  When reading a block,
60  * we compare the expected checksum against the actual checksum, which we
61  * compute via the checksum function specified by BP_GET_CHECKSUM(bp).
62  */
63 
64 /*ARGSUSED*/
65 static void
66 zio_checksum_off(const void *buf, uint64_t size, zio_cksum_t *zcp)
67 {
68 	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
69 }
70 
71 zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
72 	{{NULL,			NULL},			0, 0, 0, "inherit"},
73 	{{NULL,			NULL},			0, 0, 0, "on"},
74 	{{zio_checksum_off,	zio_checksum_off},	0, 0, 0, "off"},
75 	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 1, 0, "label"},
76 	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 1, 0, "gang_header"},
77 	{{fletcher_2_native,	fletcher_2_byteswap},	0, 1, 0, "zilog"},
78 	{{fletcher_2_native,	fletcher_2_byteswap},	0, 0, 0, "fletcher2"},
79 	{{fletcher_4_native,	fletcher_4_byteswap},	1, 0, 0, "fletcher4"},
80 	{{zio_checksum_SHA256,	zio_checksum_SHA256},	1, 0, 1, "sha256"},
81 	{{fletcher_4_native,	fletcher_4_byteswap},	0, 1, 0, "zilog2"},
82 	{{zio_checksum_off,	zio_checksum_off},	0, 0, 0, "noparity"},
83 };
84 
85 enum zio_checksum
86 zio_checksum_select(enum zio_checksum child, enum zio_checksum parent)
87 {
88 	ASSERT(child < ZIO_CHECKSUM_FUNCTIONS);
89 	ASSERT(parent < ZIO_CHECKSUM_FUNCTIONS);
90 	ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
91 
92 	if (child == ZIO_CHECKSUM_INHERIT)
93 		return (parent);
94 
95 	if (child == ZIO_CHECKSUM_ON)
96 		return (ZIO_CHECKSUM_ON_VALUE);
97 
98 	return (child);
99 }
100 
101 enum zio_checksum
102 zio_checksum_dedup_select(spa_t *spa, enum zio_checksum child,
103     enum zio_checksum parent)
104 {
105 	ASSERT((child & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
106 	ASSERT((parent & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
107 	ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
108 
109 	if (child == ZIO_CHECKSUM_INHERIT)
110 		return (parent);
111 
112 	if (child == ZIO_CHECKSUM_ON)
113 		return (spa_dedup_checksum(spa));
114 
115 	if (child == (ZIO_CHECKSUM_ON | ZIO_CHECKSUM_VERIFY))
116 		return (spa_dedup_checksum(spa) | ZIO_CHECKSUM_VERIFY);
117 
118 	ASSERT(zio_checksum_table[child & ZIO_CHECKSUM_MASK].ci_dedup ||
119 	    (child & ZIO_CHECKSUM_VERIFY) || child == ZIO_CHECKSUM_OFF);
120 
121 	return (child);
122 }
123 
124 /*
125  * Set the external verifier for a gang block based on <vdev, offset, txg>,
126  * a tuple which is guaranteed to be unique for the life of the pool.
127  */
128 static void
129 zio_checksum_gang_verifier(zio_cksum_t *zcp, blkptr_t *bp)
130 {
131 	dva_t *dva = BP_IDENTITY(bp);
132 	uint64_t txg = BP_PHYSICAL_BIRTH(bp);
133 
134 	ASSERT(BP_IS_GANG(bp));
135 
136 	ZIO_SET_CHECKSUM(zcp, DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva), txg, 0);
137 }
138 
139 /*
140  * Set the external verifier for a label block based on its offset.
141  * The vdev is implicit, and the txg is unknowable at pool open time --
142  * hence the logic in vdev_uberblock_load() to find the most recent copy.
143  */
144 static void
145 zio_checksum_label_verifier(zio_cksum_t *zcp, uint64_t offset)
146 {
147 	ZIO_SET_CHECKSUM(zcp, offset, 0, 0, 0);
148 }
149 
150 /*
151  * Generate the checksum.
152  */
153 void
154 zio_checksum_compute(zio_t *zio, enum zio_checksum checksum,
155 	void *data, uint64_t size)
156 {
157 	blkptr_t *bp = zio->io_bp;
158 	uint64_t offset = zio->io_offset;
159 	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
160 	zio_cksum_t cksum;
161 
162 	ASSERT((uint_t)checksum < ZIO_CHECKSUM_FUNCTIONS);
163 	ASSERT(ci->ci_func[0] != NULL);
164 
165 	if (ci->ci_eck) {
166 		zio_eck_t *eck;
167 
168 		if (checksum == ZIO_CHECKSUM_ZILOG2) {
169 			zil_chain_t *zilc = data;
170 
171 			size = P2ROUNDUP_TYPED(zilc->zc_nused, ZIL_MIN_BLKSZ,
172 			    uint64_t);
173 			eck = &zilc->zc_eck;
174 		} else {
175 			eck = (zio_eck_t *)((char *)data + size) - 1;
176 		}
177 		if (checksum == ZIO_CHECKSUM_GANG_HEADER)
178 			zio_checksum_gang_verifier(&eck->zec_cksum, bp);
179 		else if (checksum == ZIO_CHECKSUM_LABEL)
180 			zio_checksum_label_verifier(&eck->zec_cksum, offset);
181 		else
182 			bp->blk_cksum = eck->zec_cksum;
183 		eck->zec_magic = ZEC_MAGIC;
184 		ci->ci_func[0](data, size, &cksum);
185 		eck->zec_cksum = cksum;
186 	} else {
187 		ci->ci_func[0](data, size, &bp->blk_cksum);
188 	}
189 }
190 
191 int
192 zio_checksum_error(zio_t *zio, zio_bad_cksum_t *info)
193 {
194 	blkptr_t *bp = zio->io_bp;
195 	uint_t checksum = (bp == NULL ? zio->io_prop.zp_checksum :
196 	    (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp)));
197 	int byteswap;
198 	int error;
199 	uint64_t size = (bp == NULL ? zio->io_size :
200 	    (BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp)));
201 	uint64_t offset = zio->io_offset;
202 	void *data = zio->io_data;
203 	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
204 	zio_cksum_t actual_cksum, expected_cksum, verifier;
205 
206 	if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
207 		return (SET_ERROR(EINVAL));
208 
209 	if (ci->ci_eck) {
210 		zio_eck_t *eck;
211 
212 		if (checksum == ZIO_CHECKSUM_ZILOG2) {
213 			zil_chain_t *zilc = data;
214 			uint64_t nused;
215 
216 			eck = &zilc->zc_eck;
217 			if (eck->zec_magic == ZEC_MAGIC)
218 				nused = zilc->zc_nused;
219 			else if (eck->zec_magic == BSWAP_64(ZEC_MAGIC))
220 				nused = BSWAP_64(zilc->zc_nused);
221 			else
222 				return (SET_ERROR(ECKSUM));
223 
224 			if (nused > size)
225 				return (SET_ERROR(ECKSUM));
226 
227 			size = P2ROUNDUP_TYPED(nused, ZIL_MIN_BLKSZ, uint64_t);
228 		} else {
229 			eck = (zio_eck_t *)((char *)data + size) - 1;
230 		}
231 
232 		if (checksum == ZIO_CHECKSUM_GANG_HEADER)
233 			zio_checksum_gang_verifier(&verifier, bp);
234 		else if (checksum == ZIO_CHECKSUM_LABEL)
235 			zio_checksum_label_verifier(&verifier, offset);
236 		else
237 			verifier = bp->blk_cksum;
238 
239 		byteswap = (eck->zec_magic == BSWAP_64(ZEC_MAGIC));
240 
241 		if (byteswap)
242 			byteswap_uint64_array(&verifier, sizeof (zio_cksum_t));
243 
244 		expected_cksum = eck->zec_cksum;
245 		eck->zec_cksum = verifier;
246 		ci->ci_func[byteswap](data, size, &actual_cksum);
247 		eck->zec_cksum = expected_cksum;
248 
249 		if (byteswap)
250 			byteswap_uint64_array(&expected_cksum,
251 			    sizeof (zio_cksum_t));
252 	} else {
253 		ASSERT(!BP_IS_GANG(bp));
254 		byteswap = BP_SHOULD_BYTESWAP(bp);
255 		expected_cksum = bp->blk_cksum;
256 		ci->ci_func[byteswap](data, size, &actual_cksum);
257 	}
258 
259 	info->zbc_expected = expected_cksum;
260 	info->zbc_actual = actual_cksum;
261 	info->zbc_checksum_name = ci->ci_name;
262 	info->zbc_byteswapped = byteswap;
263 	info->zbc_injected = 0;
264 	info->zbc_has_cksum = 1;
265 
266 	if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum))
267 		return (SET_ERROR(ECKSUM));
268 
269 	if (zio_injection_enabled && !zio->io_error &&
270 	    (error = zio_handle_fault_injection(zio, ECKSUM)) != 0) {
271 
272 		info->zbc_injected = 1;
273 		return (error);
274 	}
275 
276 	return (0);
277 }
278