diff options
author | Joel Becker <joel.becker@oracle.com> | 2008-10-16 20:54:25 -0400 |
---|---|---|
committer | Mark Fasheh <mfasheh@suse.com> | 2009-01-05 11:40:31 -0500 |
commit | 70ad1ba7b48364d758a112df0823edc5ca6632aa (patch) | |
tree | 0ec30f4a812c1e66e3e3e7457eca1db8a451549a /fs/ocfs2/blockcheck.c | |
parent | ab552d54673f262d7f70014003d3928d29270f22 (diff) |
ocfs2: Add the underlying blockcheck code.
This is the code that computes crc32 and ecc for ocfs2 metadata blocks.
There are high-level functions that check whether the filesystem has the
ecc feature, mid-level functions that work on a single block or array of
buffer_heads, and the low-level ecc hamming code that can handle
multiple buffers like crc32_le().
It's not hooked up to the filesystem yet.
Signed-off-by: Joel Becker <joel.becker@oracle.com>
Cc: Christoph Hellwig <hch@lst.de>
Signed-off-by: Mark Fasheh <mfasheh@suse.com>
Diffstat (limited to 'fs/ocfs2/blockcheck.c')
-rw-r--r-- | fs/ocfs2/blockcheck.c | 480 |
1 files changed, 480 insertions, 0 deletions
diff --git a/fs/ocfs2/blockcheck.c b/fs/ocfs2/blockcheck.c new file mode 100644 index 000000000000..2bf3d7f61aec --- /dev/null +++ b/fs/ocfs2/blockcheck.c | |||
@@ -0,0 +1,480 @@ | |||
1 | /* -*- mode: c; c-basic-offset: 8; -*- | ||
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | ||
3 | * | ||
4 | * blockcheck.c | ||
5 | * | ||
6 | * Checksum and ECC codes for the OCFS2 userspace library. | ||
7 | * | ||
8 | * Copyright (C) 2006, 2008 Oracle. All rights reserved. | ||
9 | * | ||
10 | * This program is free software; you can redistribute it and/or | ||
11 | * modify it under the terms of the GNU General Public | ||
12 | * License, version 2, as published by the Free Software Foundation. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | */ | ||
19 | |||
20 | #include <linux/kernel.h> | ||
21 | #include <linux/types.h> | ||
22 | #include <linux/crc32.h> | ||
23 | #include <linux/buffer_head.h> | ||
24 | #include <linux/bitops.h> | ||
25 | #include <asm/byteorder.h> | ||
26 | |||
27 | #include "ocfs2.h" | ||
28 | |||
29 | #include "blockcheck.h" | ||
30 | |||
31 | |||
32 | |||
33 | /* | ||
34 | * We use the following conventions: | ||
35 | * | ||
36 | * d = # data bits | ||
37 | * p = # parity bits | ||
38 | * c = # total code bits (d + p) | ||
39 | */ | ||
40 | static int calc_parity_bits(unsigned int d) | ||
41 | { | ||
42 | unsigned int p; | ||
43 | |||
44 | /* | ||
45 | * Bits required for Single Error Correction is as follows: | ||
46 | * | ||
47 | * d + p + 1 <= 2^p | ||
48 | * | ||
49 | * We're restricting ourselves to 31 bits of parity, that should be | ||
50 | * sufficient. | ||
51 | */ | ||
52 | for (p = 1; p < 32; p++) | ||
53 | { | ||
54 | if ((d + p + 1) <= (1 << p)) | ||
55 | return p; | ||
56 | } | ||
57 | |||
58 | return 0; | ||
59 | } | ||
60 | |||
61 | /* | ||
62 | * Calculate the bit offset in the hamming code buffer based on the bit's | ||
63 | * offset in the data buffer. Since the hamming code reserves all | ||
64 | * power-of-two bits for parity, the data bit number and the code bit | ||
65 | * number are offest by all the parity bits beforehand. | ||
66 | * | ||
67 | * Recall that bit numbers in hamming code are 1-based. This function | ||
68 | * takes the 0-based data bit from the caller. | ||
69 | * | ||
70 | * An example. Take bit 1 of the data buffer. 1 is a power of two (2^0), | ||
71 | * so it's a parity bit. 2 is a power of two (2^1), so it's a parity bit. | ||
72 | * 3 is not a power of two. So bit 1 of the data buffer ends up as bit 3 | ||
73 | * in the code buffer. | ||
74 | */ | ||
75 | static unsigned int calc_code_bit(unsigned int i) | ||
76 | { | ||
77 | unsigned int b, p; | ||
78 | |||
79 | /* | ||
80 | * Data bits are 0-based, but we're talking code bits, which | ||
81 | * are 1-based. | ||
82 | */ | ||
83 | b = i + 1; | ||
84 | |||
85 | /* | ||
86 | * For every power of two below our bit number, bump our bit. | ||
87 | * | ||
88 | * We compare with (b + 1) becuase we have to compare with what b | ||
89 | * would be _if_ it were bumped up by the parity bit. Capice? | ||
90 | */ | ||
91 | for (p = 0; (1 << p) < (b + 1); p++) | ||
92 | b++; | ||
93 | |||
94 | return b; | ||
95 | } | ||
96 | |||
97 | /* | ||
98 | * This is the low level encoder function. It can be called across | ||
99 | * multiple hunks just like the crc32 code. 'd' is the number of bits | ||
100 | * _in_this_hunk_. nr is the bit offset of this hunk. So, if you had | ||
101 | * two 512B buffers, you would do it like so: | ||
102 | * | ||
103 | * parity = ocfs2_hamming_encode(0, buf1, 512 * 8, 0); | ||
104 | * parity = ocfs2_hamming_encode(parity, buf2, 512 * 8, 512 * 8); | ||
105 | * | ||
106 | * If you just have one buffer, use ocfs2_hamming_encode_block(). | ||
107 | */ | ||
108 | u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d, unsigned int nr) | ||
109 | { | ||
110 | unsigned int p = calc_parity_bits(nr + d); | ||
111 | unsigned int i, j, b; | ||
112 | |||
113 | BUG_ON(!p); | ||
114 | |||
115 | /* | ||
116 | * b is the hamming code bit number. Hamming code specifies a | ||
117 | * 1-based array, but C uses 0-based. So 'i' is for C, and 'b' is | ||
118 | * for the algorithm. | ||
119 | * | ||
120 | * The i++ in the for loop is so that the start offset passed | ||
121 | * to ocfs2_find_next_bit_set() is one greater than the previously | ||
122 | * found bit. | ||
123 | */ | ||
124 | for (i = 0; (i = ocfs2_find_next_bit(data, d, i)) < d; i++) | ||
125 | { | ||
126 | /* | ||
127 | * i is the offset in this hunk, nr + i is the total bit | ||
128 | * offset. | ||
129 | */ | ||
130 | b = calc_code_bit(nr + i); | ||
131 | |||
132 | for (j = 0; j < p; j++) | ||
133 | { | ||
134 | /* | ||
135 | * Data bits in the resultant code are checked by | ||
136 | * parity bits that are part of the bit number | ||
137 | * representation. Huh? | ||
138 | * | ||
139 | * <wikipedia href="http://en.wikipedia.org/wiki/Hamming_code"> | ||
140 | * In other words, the parity bit at position 2^k | ||
141 | * checks bits in positions having bit k set in | ||
142 | * their binary representation. Conversely, for | ||
143 | * instance, bit 13, i.e. 1101(2), is checked by | ||
144 | * bits 1000(2) = 8, 0100(2)=4 and 0001(2) = 1. | ||
145 | * </wikipedia> | ||
146 | * | ||
147 | * Note that 'k' is the _code_ bit number. 'b' in | ||
148 | * our loop. | ||
149 | */ | ||
150 | if (b & (1 << j)) | ||
151 | parity ^= (1 << j); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | /* While the data buffer was treated as little endian, the | ||
156 | * return value is in host endian. */ | ||
157 | return parity; | ||
158 | } | ||
159 | |||
160 | u32 ocfs2_hamming_encode_block(void *data, unsigned int blocksize) | ||
161 | { | ||
162 | return ocfs2_hamming_encode(0, data, blocksize * 8, 0); | ||
163 | } | ||
164 | |||
165 | /* | ||
166 | * Like ocfs2_hamming_encode(), this can handle hunks. nr is the bit | ||
167 | * offset of the current hunk. If bit to be fixed is not part of the | ||
168 | * current hunk, this does nothing. | ||
169 | * | ||
170 | * If you only have one hunk, use ocfs2_hamming_fix_block(). | ||
171 | */ | ||
172 | void ocfs2_hamming_fix(void *data, unsigned int d, unsigned int nr, | ||
173 | unsigned int fix) | ||
174 | { | ||
175 | unsigned int p = calc_parity_bits(nr + d); | ||
176 | unsigned int i, b; | ||
177 | |||
178 | BUG_ON(!p); | ||
179 | |||
180 | /* | ||
181 | * If the bit to fix has an hweight of 1, it's a parity bit. One | ||
182 | * busted parity bit is its own error. Nothing to do here. | ||
183 | */ | ||
184 | if (hweight32(fix) == 1) | ||
185 | return; | ||
186 | |||
187 | /* | ||
188 | * nr + d is the bit right past the data hunk we're looking at. | ||
189 | * If fix after that, nothing to do | ||
190 | */ | ||
191 | if (fix >= calc_code_bit(nr + d)) | ||
192 | return; | ||
193 | |||
194 | /* | ||
195 | * nr is the offset in the data hunk we're starting at. Let's | ||
196 | * start b at the offset in the code buffer. See hamming_encode() | ||
197 | * for a more detailed description of 'b'. | ||
198 | */ | ||
199 | b = calc_code_bit(nr); | ||
200 | /* If the fix is before this hunk, nothing to do */ | ||
201 | if (fix < b) | ||
202 | return; | ||
203 | |||
204 | for (i = 0; i < d; i++, b++) | ||
205 | { | ||
206 | /* Skip past parity bits */ | ||
207 | while (hweight32(b) == 1) | ||
208 | b++; | ||
209 | |||
210 | /* | ||
211 | * i is the offset in this data hunk. | ||
212 | * nr + i is the offset in the total data buffer. | ||
213 | * b is the offset in the total code buffer. | ||
214 | * | ||
215 | * Thus, when b == fix, bit i in the current hunk needs | ||
216 | * fixing. | ||
217 | */ | ||
218 | if (b == fix) | ||
219 | { | ||
220 | if (ocfs2_test_bit(i, data)) | ||
221 | ocfs2_clear_bit(i, data); | ||
222 | else | ||
223 | ocfs2_set_bit(i, data); | ||
224 | break; | ||
225 | } | ||
226 | } | ||
227 | } | ||
228 | |||
229 | void ocfs2_hamming_fix_block(void *data, unsigned int blocksize, | ||
230 | unsigned int fix) | ||
231 | { | ||
232 | ocfs2_hamming_fix(data, blocksize * 8, 0, fix); | ||
233 | } | ||
234 | |||
235 | /* | ||
236 | * This function generates check information for a block. | ||
237 | * data is the block to be checked. bc is a pointer to the | ||
238 | * ocfs2_block_check structure describing the crc32 and the ecc. | ||
239 | * | ||
240 | * bc should be a pointer inside data, as the function will | ||
241 | * take care of zeroing it before calculating the check information. If | ||
242 | * bc does not point inside data, the caller must make sure any inline | ||
243 | * ocfs2_block_check structures are zeroed. | ||
244 | * | ||
245 | * The data buffer must be in on-disk endian (little endian for ocfs2). | ||
246 | * bc will be filled with little-endian values and will be ready to go to | ||
247 | * disk. | ||
248 | */ | ||
249 | void ocfs2_block_check_compute(void *data, size_t blocksize, | ||
250 | struct ocfs2_block_check *bc) | ||
251 | { | ||
252 | u32 crc; | ||
253 | u32 ecc; | ||
254 | |||
255 | memset(bc, 0, sizeof(struct ocfs2_block_check)); | ||
256 | |||
257 | crc = crc32_le(~0, data, blocksize); | ||
258 | ecc = ocfs2_hamming_encode_block(data, blocksize); | ||
259 | |||
260 | /* | ||
261 | * No ecc'd ocfs2 structure is larger than 4K, so ecc will be no | ||
262 | * larger than 16 bits. | ||
263 | */ | ||
264 | BUG_ON(ecc > USHORT_MAX); | ||
265 | |||
266 | bc->bc_crc32e = cpu_to_le32(crc); | ||
267 | bc->bc_ecc = cpu_to_le16((u16)ecc); | ||
268 | } | ||
269 | |||
270 | /* | ||
271 | * This function validates existing check information. Like _compute, | ||
272 | * the function will take care of zeroing bc before calculating check codes. | ||
273 | * If bc is not a pointer inside data, the caller must have zeroed any | ||
274 | * inline ocfs2_block_check structures. | ||
275 | * | ||
276 | * Again, the data passed in should be the on-disk endian. | ||
277 | */ | ||
278 | int ocfs2_block_check_validate(void *data, size_t blocksize, | ||
279 | struct ocfs2_block_check *bc) | ||
280 | { | ||
281 | int rc = 0; | ||
282 | struct ocfs2_block_check check; | ||
283 | u32 crc, ecc; | ||
284 | |||
285 | check.bc_crc32e = le32_to_cpu(bc->bc_crc32e); | ||
286 | check.bc_ecc = le16_to_cpu(bc->bc_ecc); | ||
287 | |||
288 | memset(bc, 0, sizeof(struct ocfs2_block_check)); | ||
289 | |||
290 | /* Fast path - if the crc32 validates, we're good to go */ | ||
291 | crc = crc32_le(~0, data, blocksize); | ||
292 | if (crc == check.bc_crc32e) | ||
293 | goto out; | ||
294 | |||
295 | /* Ok, try ECC fixups */ | ||
296 | ecc = ocfs2_hamming_encode_block(data, blocksize); | ||
297 | ocfs2_hamming_fix_block(data, blocksize, ecc ^ check.bc_ecc); | ||
298 | |||
299 | /* And check the crc32 again */ | ||
300 | crc = crc32_le(~0, data, blocksize); | ||
301 | if (crc == check.bc_crc32e) | ||
302 | goto out; | ||
303 | |||
304 | rc = -EIO; | ||
305 | |||
306 | out: | ||
307 | bc->bc_crc32e = cpu_to_le32(check.bc_crc32e); | ||
308 | bc->bc_ecc = cpu_to_le16(check.bc_ecc); | ||
309 | |||
310 | return rc; | ||
311 | } | ||
312 | |||
313 | /* | ||
314 | * This function generates check information for a list of buffer_heads. | ||
315 | * bhs is the blocks to be checked. bc is a pointer to the | ||
316 | * ocfs2_block_check structure describing the crc32 and the ecc. | ||
317 | * | ||
318 | * bc should be a pointer inside data, as the function will | ||
319 | * take care of zeroing it before calculating the check information. If | ||
320 | * bc does not point inside data, the caller must make sure any inline | ||
321 | * ocfs2_block_check structures are zeroed. | ||
322 | * | ||
323 | * The data buffer must be in on-disk endian (little endian for ocfs2). | ||
324 | * bc will be filled with little-endian values and will be ready to go to | ||
325 | * disk. | ||
326 | */ | ||
327 | void ocfs2_block_check_compute_bhs(struct buffer_head **bhs, int nr, | ||
328 | struct ocfs2_block_check *bc) | ||
329 | { | ||
330 | int i; | ||
331 | u32 crc, ecc; | ||
332 | |||
333 | BUG_ON(nr < 0); | ||
334 | |||
335 | if (!nr) | ||
336 | return; | ||
337 | |||
338 | memset(bc, 0, sizeof(struct ocfs2_block_check)); | ||
339 | |||
340 | for (i = 0, crc = ~0, ecc = 0; i < nr; i++) { | ||
341 | crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size); | ||
342 | /* | ||
343 | * The number of bits in a buffer is obviously b_size*8. | ||
344 | * The offset of this buffer is b_size*i, so the bit offset | ||
345 | * of this buffer is b_size*8*i. | ||
346 | */ | ||
347 | ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data, | ||
348 | bhs[i]->b_size * 8, | ||
349 | bhs[i]->b_size * 8 * i); | ||
350 | } | ||
351 | |||
352 | /* | ||
353 | * No ecc'd ocfs2 structure is larger than 4K, so ecc will be no | ||
354 | * larger than 16 bits. | ||
355 | */ | ||
356 | BUG_ON(ecc > USHORT_MAX); | ||
357 | |||
358 | bc->bc_crc32e = cpu_to_le32(crc); | ||
359 | bc->bc_ecc = cpu_to_le16((u16)ecc); | ||
360 | } | ||
361 | |||
362 | /* | ||
363 | * This function validates existing check information on a list of | ||
364 | * buffer_heads. Like _compute_bhs, the function will take care of | ||
365 | * zeroing bc before calculating check codes. If bc is not a pointer | ||
366 | * inside data, the caller must have zeroed any inline | ||
367 | * ocfs2_block_check structures. | ||
368 | * | ||
369 | * Again, the data passed in should be the on-disk endian. | ||
370 | */ | ||
371 | int ocfs2_block_check_validate_bhs(struct buffer_head **bhs, int nr, | ||
372 | struct ocfs2_block_check *bc) | ||
373 | { | ||
374 | int i, rc = 0; | ||
375 | struct ocfs2_block_check check; | ||
376 | u32 crc, ecc, fix; | ||
377 | |||
378 | BUG_ON(nr < 0); | ||
379 | |||
380 | if (!nr) | ||
381 | return 0; | ||
382 | |||
383 | check.bc_crc32e = le32_to_cpu(bc->bc_crc32e); | ||
384 | check.bc_ecc = le16_to_cpu(bc->bc_ecc); | ||
385 | |||
386 | memset(bc, 0, sizeof(struct ocfs2_block_check)); | ||
387 | |||
388 | /* Fast path - if the crc32 validates, we're good to go */ | ||
389 | for (i = 0, crc = ~0; i < nr; i++) | ||
390 | crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size); | ||
391 | if (crc == check.bc_crc32e) | ||
392 | goto out; | ||
393 | |||
394 | mlog(ML_ERROR, | ||
395 | "CRC32 failed: stored: %u, computed %u. Applying ECC.\n", | ||
396 | (unsigned int)check.bc_crc32e, (unsigned int)crc); | ||
397 | |||
398 | /* Ok, try ECC fixups */ | ||
399 | for (i = 0, ecc = 0; i < nr; i++) { | ||
400 | /* | ||
401 | * The number of bits in a buffer is obviously b_size*8. | ||
402 | * The offset of this buffer is b_size*i, so the bit offset | ||
403 | * of this buffer is b_size*8*i. | ||
404 | */ | ||
405 | ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data, | ||
406 | bhs[i]->b_size * 8, | ||
407 | bhs[i]->b_size * 8 * i); | ||
408 | } | ||
409 | fix = ecc ^ check.bc_ecc; | ||
410 | for (i = 0; i < nr; i++) { | ||
411 | /* | ||
412 | * Try the fix against each buffer. It will only affect | ||
413 | * one of them. | ||
414 | */ | ||
415 | ocfs2_hamming_fix(bhs[i]->b_data, bhs[i]->b_size * 8, | ||
416 | bhs[i]->b_size * 8 * i, fix); | ||
417 | } | ||
418 | |||
419 | /* And check the crc32 again */ | ||
420 | for (i = 0, crc = ~0; i < nr; i++) | ||
421 | crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size); | ||
422 | if (crc == check.bc_crc32e) | ||
423 | goto out; | ||
424 | |||
425 | mlog(ML_ERROR, "Fixed CRC32 failed: stored: %u, computed %u\n", | ||
426 | (unsigned int)check.bc_crc32e, (unsigned int)crc); | ||
427 | |||
428 | rc = -EIO; | ||
429 | |||
430 | out: | ||
431 | bc->bc_crc32e = cpu_to_le32(check.bc_crc32e); | ||
432 | bc->bc_ecc = cpu_to_le16(check.bc_ecc); | ||
433 | |||
434 | return rc; | ||
435 | } | ||
436 | |||
437 | /* | ||
438 | * These are the main API. They check the superblock flag before | ||
439 | * calling the underlying operations. | ||
440 | * | ||
441 | * They expect the buffer(s) to be in disk format. | ||
442 | */ | ||
443 | void ocfs2_compute_meta_ecc(struct super_block *sb, void *data, | ||
444 | struct ocfs2_block_check *bc) | ||
445 | { | ||
446 | if (ocfs2_meta_ecc(OCFS2_SB(sb))) | ||
447 | ocfs2_block_check_compute(data, sb->s_blocksize, bc); | ||
448 | } | ||
449 | |||
450 | int ocfs2_validate_meta_ecc(struct super_block *sb, void *data, | ||
451 | struct ocfs2_block_check *bc) | ||
452 | { | ||
453 | int rc = 0; | ||
454 | |||
455 | if (ocfs2_meta_ecc(OCFS2_SB(sb))) | ||
456 | rc = ocfs2_block_check_validate(data, sb->s_blocksize, bc); | ||
457 | |||
458 | return rc; | ||
459 | } | ||
460 | |||
461 | void ocfs2_compute_meta_ecc_bhs(struct super_block *sb, | ||
462 | struct buffer_head **bhs, int nr, | ||
463 | struct ocfs2_block_check *bc) | ||
464 | { | ||
465 | if (ocfs2_meta_ecc(OCFS2_SB(sb))) | ||
466 | ocfs2_block_check_compute_bhs(bhs, nr, bc); | ||
467 | } | ||
468 | |||
469 | int ocfs2_validate_meta_ecc_bhs(struct super_block *sb, | ||
470 | struct buffer_head **bhs, int nr, | ||
471 | struct ocfs2_block_check *bc) | ||
472 | { | ||
473 | int rc = 0; | ||
474 | |||
475 | if (ocfs2_meta_ecc(OCFS2_SB(sb))) | ||
476 | rc = ocfs2_block_check_validate_bhs(bhs, nr, bc); | ||
477 | |||
478 | return rc; | ||
479 | } | ||
480 | |||