xref: /NextBSD/sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zio_checksum.c (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
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, 2015 by Delphix. All rights reserved.
24  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25  * Copyright 2013 Saso Kiselkov. All rights reserved.
26  */
27 
28 #include <sys/zfs_context.h>
29 #include <sys/spa.h>
30 #include <sys/spa_impl.h>
31 #include <sys/zio.h>
32 #include <sys/zio_checksum.h>
33 #include <sys/zil.h>
34 #include <zfs_fletcher.h>
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
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.
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
63  * compute via the checksum function specified by BP_GET_CHECKSUM(bp).
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.
92  */
93 
94 /*ARGSUSED*/
95 static void
zio_checksum_off(const void * buf,uint64_t size,const void * ctx_template,zio_cksum_t * zcp)96 zio_checksum_off(const void *buf, uint64_t size,
97     const void *ctx_template, zio_cksum_t *zcp)
98 {
99 	ZIO_SET_CHECKSUM(zcp, 0, 0, 0, 0);
100 }
101 
102 zio_checksum_info_t zio_checksum_table[ZIO_CHECKSUM_FUNCTIONS] = {
103 	{{NULL, NULL}, NULL, NULL, 0, "inherit"},
104 	{{NULL, NULL}, NULL, NULL, 0, "on"},
105 	{{zio_checksum_off,		zio_checksum_off},
106 	    NULL, NULL, 0, "off"},
107 	{{zio_checksum_SHA256,		zio_checksum_SHA256},
108 	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
109 	    "label"},
110 	{{zio_checksum_SHA256,		zio_checksum_SHA256},
111 	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_EMBEDDED,
112 	    "gang_header"},
113 	{{fletcher_2_native,		fletcher_2_byteswap},
114 	    NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog"},
115 	{{fletcher_2_native,		fletcher_2_byteswap},
116 	    NULL, NULL, 0, "fletcher2"},
117 	{{fletcher_4_native,		fletcher_4_byteswap},
118 	    NULL, NULL, ZCHECKSUM_FLAG_METADATA, "fletcher4"},
119 	{{zio_checksum_SHA256,		zio_checksum_SHA256},
120 	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
121 	    ZCHECKSUM_FLAG_NOPWRITE, "sha256"},
122 	{{fletcher_4_native,		fletcher_4_byteswap},
123 	    NULL, NULL, ZCHECKSUM_FLAG_EMBEDDED, "zilog2"},
124 	{{zio_checksum_off,		zio_checksum_off},
125 	    NULL, NULL, 0, "noparity"},
126 #ifdef illumos
127 	{{zio_checksum_SHA512_native,	zio_checksum_SHA512_byteswap},
128 	    NULL, NULL, ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
129 	    ZCHECKSUM_FLAG_NOPWRITE, "sha512"},
130 	{{zio_checksum_skein_native,	zio_checksum_skein_byteswap},
131 	    zio_checksum_skein_tmpl_init, zio_checksum_skein_tmpl_free,
132 	    ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_DEDUP |
133 	    ZCHECKSUM_FLAG_SALTED | ZCHECKSUM_FLAG_NOPWRITE, "skein"},
134 	{{zio_checksum_edonr_native,	zio_checksum_edonr_byteswap},
135 	    zio_checksum_edonr_tmpl_init, zio_checksum_edonr_tmpl_free,
136 	    ZCHECKSUM_FLAG_METADATA | ZCHECKSUM_FLAG_SALTED |
137 	    ZCHECKSUM_FLAG_NOPWRITE, "edonr"},
138 #endif
139 };
140 
141 spa_feature_t
zio_checksum_to_feature(enum zio_checksum cksum)142 zio_checksum_to_feature(enum zio_checksum cksum)
143 {
144 #ifdef illumos
145 	switch (cksum) {
146 	case ZIO_CHECKSUM_SHA512:
147 		return (SPA_FEATURE_SHA512);
148 	case ZIO_CHECKSUM_SKEIN:
149 		return (SPA_FEATURE_SKEIN);
150 	case ZIO_CHECKSUM_EDONR:
151 		return (SPA_FEATURE_EDONR);
152 	}
153 #endif
154 	return (SPA_FEATURE_NONE);
155 }
156 
157 enum zio_checksum
zio_checksum_select(enum zio_checksum child,enum zio_checksum parent)158 zio_checksum_select(enum zio_checksum child, enum zio_checksum parent)
159 {
160 	ASSERT(child < ZIO_CHECKSUM_FUNCTIONS);
161 	ASSERT(parent < ZIO_CHECKSUM_FUNCTIONS);
162 	ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
163 
164 	if (child == ZIO_CHECKSUM_INHERIT)
165 		return (parent);
166 
167 	if (child == ZIO_CHECKSUM_ON)
168 		return (ZIO_CHECKSUM_ON_VALUE);
169 
170 	return (child);
171 }
172 
173 enum zio_checksum
zio_checksum_dedup_select(spa_t * spa,enum zio_checksum child,enum zio_checksum parent)174 zio_checksum_dedup_select(spa_t *spa, enum zio_checksum child,
175     enum zio_checksum parent)
176 {
177 	ASSERT((child & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
178 	ASSERT((parent & ZIO_CHECKSUM_MASK) < ZIO_CHECKSUM_FUNCTIONS);
179 	ASSERT(parent != ZIO_CHECKSUM_INHERIT && parent != ZIO_CHECKSUM_ON);
180 
181 	if (child == ZIO_CHECKSUM_INHERIT)
182 		return (parent);
183 
184 	if (child == ZIO_CHECKSUM_ON)
185 		return (spa_dedup_checksum(spa));
186 
187 	if (child == (ZIO_CHECKSUM_ON | ZIO_CHECKSUM_VERIFY))
188 		return (spa_dedup_checksum(spa) | ZIO_CHECKSUM_VERIFY);
189 
190 	ASSERT((zio_checksum_table[child & ZIO_CHECKSUM_MASK].ci_flags &
191 	    ZCHECKSUM_FLAG_DEDUP) ||
192 	    (child & ZIO_CHECKSUM_VERIFY) || child == ZIO_CHECKSUM_OFF);
193 
194 	return (child);
195 }
196 
197 /*
198  * Set the external verifier for a gang block based on <vdev, offset, txg>,
199  * a tuple which is guaranteed to be unique for the life of the pool.
200  */
201 static void
zio_checksum_gang_verifier(zio_cksum_t * zcp,blkptr_t * bp)202 zio_checksum_gang_verifier(zio_cksum_t *zcp, blkptr_t *bp)
203 {
204 	dva_t *dva = BP_IDENTITY(bp);
205 	uint64_t txg = BP_PHYSICAL_BIRTH(bp);
206 
207 	ASSERT(BP_IS_GANG(bp));
208 
209 	ZIO_SET_CHECKSUM(zcp, DVA_GET_VDEV(dva), DVA_GET_OFFSET(dva), txg, 0);
210 }
211 
212 /*
213  * Set the external verifier for a label block based on its offset.
214  * The vdev is implicit, and the txg is unknowable at pool open time --
215  * hence the logic in vdev_uberblock_load() to find the most recent copy.
216  */
217 static void
zio_checksum_label_verifier(zio_cksum_t * zcp,uint64_t offset)218 zio_checksum_label_verifier(zio_cksum_t *zcp, uint64_t offset)
219 {
220 	ZIO_SET_CHECKSUM(zcp, offset, 0, 0, 0);
221 }
222 
223 /*
224  * Calls the template init function of a checksum which supports context
225  * templates and installs the template into the spa_t.
226  */
227 static void
zio_checksum_template_init(enum zio_checksum checksum,spa_t * spa)228 zio_checksum_template_init(enum zio_checksum checksum, spa_t *spa)
229 {
230 	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
231 
232 	if (ci->ci_tmpl_init == NULL)
233 		return;
234 	if (spa->spa_cksum_tmpls[checksum] != NULL)
235 		return;
236 
237 	VERIFY(ci->ci_tmpl_free != NULL);
238 	mutex_enter(&spa->spa_cksum_tmpls_lock);
239 	if (spa->spa_cksum_tmpls[checksum] == NULL) {
240 		spa->spa_cksum_tmpls[checksum] =
241 		    ci->ci_tmpl_init(&spa->spa_cksum_salt);
242 		VERIFY(spa->spa_cksum_tmpls[checksum] != NULL);
243 	}
244 	mutex_exit(&spa->spa_cksum_tmpls_lock);
245 }
246 
247 /*
248  * Generate the checksum.
249  */
250 void
zio_checksum_compute(zio_t * zio,enum zio_checksum checksum,void * data,uint64_t size)251 zio_checksum_compute(zio_t *zio, enum zio_checksum checksum,
252     void *data, uint64_t size)
253 {
254 	blkptr_t *bp = zio->io_bp;
255 	uint64_t offset = zio->io_offset;
256 	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
257 	zio_cksum_t cksum;
258 	spa_t *spa = zio->io_spa;
259 
260 	ASSERT((uint_t)checksum < ZIO_CHECKSUM_FUNCTIONS);
261 	ASSERT(ci->ci_func[0] != NULL);
262 
263 	zio_checksum_template_init(checksum, spa);
264 
265 	if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
266 		zio_eck_t *eck;
267 
268 		if (checksum == ZIO_CHECKSUM_ZILOG2) {
269 			zil_chain_t *zilc = data;
270 
271 			size = P2ROUNDUP_TYPED(zilc->zc_nused, ZIL_MIN_BLKSZ,
272 			    uint64_t);
273 			eck = &zilc->zc_eck;
274 		} else {
275 			eck = (zio_eck_t *)((char *)data + size) - 1;
276 		}
277 		if (checksum == ZIO_CHECKSUM_GANG_HEADER)
278 			zio_checksum_gang_verifier(&eck->zec_cksum, bp);
279 		else if (checksum == ZIO_CHECKSUM_LABEL)
280 			zio_checksum_label_verifier(&eck->zec_cksum, offset);
281 		else
282 			bp->blk_cksum = eck->zec_cksum;
283 		eck->zec_magic = ZEC_MAGIC;
284 		ci->ci_func[0](data, size, spa->spa_cksum_tmpls[checksum],
285 		    &cksum);
286 		eck->zec_cksum = cksum;
287 	} else {
288 		ci->ci_func[0](data, size, spa->spa_cksum_tmpls[checksum],
289 		    &bp->blk_cksum);
290 	}
291 }
292 
293 int
zio_checksum_error(zio_t * zio,zio_bad_cksum_t * info)294 zio_checksum_error(zio_t *zio, zio_bad_cksum_t *info)
295 {
296 	blkptr_t *bp = zio->io_bp;
297 	uint_t checksum = (bp == NULL ? zio->io_prop.zp_checksum :
298 	    (BP_IS_GANG(bp) ? ZIO_CHECKSUM_GANG_HEADER : BP_GET_CHECKSUM(bp)));
299 	int byteswap;
300 	int error;
301 	uint64_t size = (bp == NULL ? zio->io_size :
302 	    (BP_IS_GANG(bp) ? SPA_GANGBLOCKSIZE : BP_GET_PSIZE(bp)));
303 	uint64_t offset = zio->io_offset;
304 	void *data = zio->io_data;
305 	zio_checksum_info_t *ci = &zio_checksum_table[checksum];
306 	zio_cksum_t actual_cksum, expected_cksum, verifier;
307 	spa_t *spa = zio->io_spa;
308 
309 	if (checksum >= ZIO_CHECKSUM_FUNCTIONS || ci->ci_func[0] == NULL)
310 		return (SET_ERROR(EINVAL));
311 
312 	zio_checksum_template_init(checksum, spa);
313 
314 	if (ci->ci_flags & ZCHECKSUM_FLAG_EMBEDDED) {
315 		zio_eck_t *eck;
316 
317 		if (checksum == ZIO_CHECKSUM_ZILOG2) {
318 			zil_chain_t *zilc = data;
319 			uint64_t nused;
320 
321 			eck = &zilc->zc_eck;
322 			if (eck->zec_magic == ZEC_MAGIC)
323 				nused = zilc->zc_nused;
324 			else if (eck->zec_magic == BSWAP_64(ZEC_MAGIC))
325 				nused = BSWAP_64(zilc->zc_nused);
326 			else
327 				return (SET_ERROR(ECKSUM));
328 
329 			if (nused > size)
330 				return (SET_ERROR(ECKSUM));
331 
332 			size = P2ROUNDUP_TYPED(nused, ZIL_MIN_BLKSZ, uint64_t);
333 		} else {
334 			eck = (zio_eck_t *)((char *)data + size) - 1;
335 		}
336 
337 		if (checksum == ZIO_CHECKSUM_GANG_HEADER)
338 			zio_checksum_gang_verifier(&verifier, bp);
339 		else if (checksum == ZIO_CHECKSUM_LABEL)
340 			zio_checksum_label_verifier(&verifier, offset);
341 		else
342 			verifier = bp->blk_cksum;
343 
344 		byteswap = (eck->zec_magic == BSWAP_64(ZEC_MAGIC));
345 
346 		if (byteswap)
347 			byteswap_uint64_array(&verifier, sizeof (zio_cksum_t));
348 
349 		expected_cksum = eck->zec_cksum;
350 		eck->zec_cksum = verifier;
351 		ci->ci_func[byteswap](data, size,
352 		    spa->spa_cksum_tmpls[checksum], &actual_cksum);
353 		eck->zec_cksum = expected_cksum;
354 
355 		if (byteswap)
356 			byteswap_uint64_array(&expected_cksum,
357 			    sizeof (zio_cksum_t));
358 	} else {
359 		ASSERT(!BP_IS_GANG(bp));
360 		byteswap = BP_SHOULD_BYTESWAP(bp);
361 		expected_cksum = bp->blk_cksum;
362 		ci->ci_func[byteswap](data, size,
363 		    spa->spa_cksum_tmpls[checksum], &actual_cksum);
364 	}
365 
366 	info->zbc_expected = expected_cksum;
367 	info->zbc_actual = actual_cksum;
368 	info->zbc_checksum_name = ci->ci_name;
369 	info->zbc_byteswapped = byteswap;
370 	info->zbc_injected = 0;
371 	info->zbc_has_cksum = 1;
372 
373 	if (!ZIO_CHECKSUM_EQUAL(actual_cksum, expected_cksum))
374 		return (SET_ERROR(ECKSUM));
375 
376 	if (zio_injection_enabled && !zio->io_error &&
377 	    (error = zio_handle_fault_injection(zio, ECKSUM)) != 0) {
378 
379 		info->zbc_injected = 1;
380 		return (error);
381 	}
382 
383 	return (0);
384 }
385 
386 /*
387  * Called by a spa_t that's about to be deallocated. This steps through
388  * all of the checksum context templates and deallocates any that were
389  * initialized using the algorithm-specific template init function.
390  */
391 void
zio_checksum_templates_free(spa_t * spa)392 zio_checksum_templates_free(spa_t *spa)
393 {
394 	for (enum zio_checksum checksum = 0;
395 	    checksum < ZIO_CHECKSUM_FUNCTIONS; checksum++) {
396 		if (spa->spa_cksum_tmpls[checksum] != NULL) {
397 			zio_checksum_info_t *ci = &zio_checksum_table[checksum];
398 
399 			VERIFY(ci->ci_tmpl_free != NULL);
400 			ci->ci_tmpl_free(spa->spa_cksum_tmpls[checksum]);
401 			spa->spa_cksum_tmpls[checksum] = NULL;
402 		}
403 	}
404 }
405