Commit | Line | Data |
---|---|---|
1802d0be | 1 | // SPDX-License-Identifier: GPL-2.0-only |
70ad1ba7 JB |
2 | /* -*- mode: c; c-basic-offset: 8; -*- |
3 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
4 | * | |
5 | * blockcheck.c | |
6 | * | |
7 | * Checksum and ECC codes for the OCFS2 userspace library. | |
8 | * | |
9 | * Copyright (C) 2006, 2008 Oracle. All rights reserved. | |
70ad1ba7 JB |
10 | */ |
11 | ||
12 | #include <linux/kernel.h> | |
13 | #include <linux/types.h> | |
14 | #include <linux/crc32.h> | |
15 | #include <linux/buffer_head.h> | |
16 | #include <linux/bitops.h> | |
73be192b JB |
17 | #include <linux/debugfs.h> |
18 | #include <linux/module.h> | |
19 | #include <linux/fs.h> | |
70ad1ba7 JB |
20 | #include <asm/byteorder.h> |
21 | ||
d6b32bbb JB |
22 | #include <cluster/masklog.h> |
23 | ||
70ad1ba7 JB |
24 | #include "ocfs2.h" |
25 | ||
26 | #include "blockcheck.h" | |
27 | ||
28 | ||
70ad1ba7 JB |
29 | /* |
30 | * We use the following conventions: | |
31 | * | |
32 | * d = # data bits | |
33 | * p = # parity bits | |
34 | * c = # total code bits (d + p) | |
35 | */ | |
70ad1ba7 | 36 | |
7bb458a5 | 37 | |
70ad1ba7 JB |
38 | /* |
39 | * Calculate the bit offset in the hamming code buffer based on the bit's | |
40 | * offset in the data buffer. Since the hamming code reserves all | |
41 | * power-of-two bits for parity, the data bit number and the code bit | |
bf48aabb | 42 | * number are offset by all the parity bits beforehand. |
70ad1ba7 JB |
43 | * |
44 | * Recall that bit numbers in hamming code are 1-based. This function | |
45 | * takes the 0-based data bit from the caller. | |
46 | * | |
47 | * An example. Take bit 1 of the data buffer. 1 is a power of two (2^0), | |
48 | * so it's a parity bit. 2 is a power of two (2^1), so it's a parity bit. | |
49 | * 3 is not a power of two. So bit 1 of the data buffer ends up as bit 3 | |
50 | * in the code buffer. | |
58896c4d JB |
51 | * |
52 | * The caller can pass in *p if it wants to keep track of the most recent | |
53 | * number of parity bits added. This allows the function to start the | |
54 | * calculation at the last place. | |
70ad1ba7 | 55 | */ |
58896c4d | 56 | static unsigned int calc_code_bit(unsigned int i, unsigned int *p_cache) |
70ad1ba7 | 57 | { |
58896c4d | 58 | unsigned int b, p = 0; |
70ad1ba7 JB |
59 | |
60 | /* | |
61 | * Data bits are 0-based, but we're talking code bits, which | |
62 | * are 1-based. | |
63 | */ | |
64 | b = i + 1; | |
65 | ||
58896c4d JB |
66 | /* Use the cache if it is there */ |
67 | if (p_cache) | |
68 | p = *p_cache; | |
7bb458a5 JB |
69 | b += p; |
70 | ||
70ad1ba7 JB |
71 | /* |
72 | * For every power of two below our bit number, bump our bit. | |
73 | * | |
58896c4d | 74 | * We compare with (b + 1) because we have to compare with what b |
70ad1ba7 | 75 | * would be _if_ it were bumped up by the parity bit. Capice? |
7bb458a5 | 76 | * |
58896c4d | 77 | * p is set above. |
70ad1ba7 | 78 | */ |
58896c4d | 79 | for (; (1 << p) < (b + 1); p++) |
70ad1ba7 JB |
80 | b++; |
81 | ||
58896c4d JB |
82 | if (p_cache) |
83 | *p_cache = p; | |
84 | ||
70ad1ba7 JB |
85 | return b; |
86 | } | |
87 | ||
88 | /* | |
89 | * This is the low level encoder function. It can be called across | |
90 | * multiple hunks just like the crc32 code. 'd' is the number of bits | |
91 | * _in_this_hunk_. nr is the bit offset of this hunk. So, if you had | |
92 | * two 512B buffers, you would do it like so: | |
93 | * | |
94 | * parity = ocfs2_hamming_encode(0, buf1, 512 * 8, 0); | |
95 | * parity = ocfs2_hamming_encode(parity, buf2, 512 * 8, 512 * 8); | |
96 | * | |
97 | * If you just have one buffer, use ocfs2_hamming_encode_block(). | |
98 | */ | |
99 | u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d, unsigned int nr) | |
100 | { | |
58896c4d | 101 | unsigned int i, b, p = 0; |
70ad1ba7 | 102 | |
e798b3f8 | 103 | BUG_ON(!d); |
70ad1ba7 JB |
104 | |
105 | /* | |
106 | * b is the hamming code bit number. Hamming code specifies a | |
107 | * 1-based array, but C uses 0-based. So 'i' is for C, and 'b' is | |
108 | * for the algorithm. | |
109 | * | |
110 | * The i++ in the for loop is so that the start offset passed | |
111 | * to ocfs2_find_next_bit_set() is one greater than the previously | |
112 | * found bit. | |
113 | */ | |
114 | for (i = 0; (i = ocfs2_find_next_bit(data, d, i)) < d; i++) | |
115 | { | |
116 | /* | |
117 | * i is the offset in this hunk, nr + i is the total bit | |
118 | * offset. | |
119 | */ | |
58896c4d | 120 | b = calc_code_bit(nr + i, &p); |
70ad1ba7 | 121 | |
e798b3f8 JB |
122 | /* |
123 | * Data bits in the resultant code are checked by | |
124 | * parity bits that are part of the bit number | |
125 | * representation. Huh? | |
126 | * | |
127 | * <wikipedia href="http://en.wikipedia.org/wiki/Hamming_code"> | |
128 | * In other words, the parity bit at position 2^k | |
129 | * checks bits in positions having bit k set in | |
130 | * their binary representation. Conversely, for | |
131 | * instance, bit 13, i.e. 1101(2), is checked by | |
132 | * bits 1000(2) = 8, 0100(2)=4 and 0001(2) = 1. | |
133 | * </wikipedia> | |
134 | * | |
135 | * Note that 'k' is the _code_ bit number. 'b' in | |
136 | * our loop. | |
137 | */ | |
138 | parity ^= b; | |
70ad1ba7 JB |
139 | } |
140 | ||
141 | /* While the data buffer was treated as little endian, the | |
142 | * return value is in host endian. */ | |
143 | return parity; | |
144 | } | |
145 | ||
146 | u32 ocfs2_hamming_encode_block(void *data, unsigned int blocksize) | |
147 | { | |
148 | return ocfs2_hamming_encode(0, data, blocksize * 8, 0); | |
149 | } | |
150 | ||
151 | /* | |
152 | * Like ocfs2_hamming_encode(), this can handle hunks. nr is the bit | |
153 | * offset of the current hunk. If bit to be fixed is not part of the | |
154 | * current hunk, this does nothing. | |
155 | * | |
156 | * If you only have one hunk, use ocfs2_hamming_fix_block(). | |
157 | */ | |
158 | void ocfs2_hamming_fix(void *data, unsigned int d, unsigned int nr, | |
159 | unsigned int fix) | |
160 | { | |
70ad1ba7 JB |
161 | unsigned int i, b; |
162 | ||
e798b3f8 | 163 | BUG_ON(!d); |
70ad1ba7 JB |
164 | |
165 | /* | |
166 | * If the bit to fix has an hweight of 1, it's a parity bit. One | |
167 | * busted parity bit is its own error. Nothing to do here. | |
168 | */ | |
169 | if (hweight32(fix) == 1) | |
170 | return; | |
171 | ||
172 | /* | |
173 | * nr + d is the bit right past the data hunk we're looking at. | |
174 | * If fix after that, nothing to do | |
175 | */ | |
58896c4d | 176 | if (fix >= calc_code_bit(nr + d, NULL)) |
70ad1ba7 JB |
177 | return; |
178 | ||
179 | /* | |
180 | * nr is the offset in the data hunk we're starting at. Let's | |
181 | * start b at the offset in the code buffer. See hamming_encode() | |
182 | * for a more detailed description of 'b'. | |
183 | */ | |
58896c4d | 184 | b = calc_code_bit(nr, NULL); |
70ad1ba7 JB |
185 | /* If the fix is before this hunk, nothing to do */ |
186 | if (fix < b) | |
187 | return; | |
188 | ||
189 | for (i = 0; i < d; i++, b++) | |
190 | { | |
191 | /* Skip past parity bits */ | |
192 | while (hweight32(b) == 1) | |
193 | b++; | |
194 | ||
195 | /* | |
196 | * i is the offset in this data hunk. | |
197 | * nr + i is the offset in the total data buffer. | |
198 | * b is the offset in the total code buffer. | |
199 | * | |
200 | * Thus, when b == fix, bit i in the current hunk needs | |
201 | * fixing. | |
202 | */ | |
203 | if (b == fix) | |
204 | { | |
205 | if (ocfs2_test_bit(i, data)) | |
206 | ocfs2_clear_bit(i, data); | |
207 | else | |
208 | ocfs2_set_bit(i, data); | |
209 | break; | |
210 | } | |
211 | } | |
212 | } | |
213 | ||
214 | void ocfs2_hamming_fix_block(void *data, unsigned int blocksize, | |
215 | unsigned int fix) | |
216 | { | |
217 | ocfs2_hamming_fix(data, blocksize * 8, 0, fix); | |
218 | } | |
219 | ||
73be192b JB |
220 | |
221 | /* | |
222 | * Debugfs handling. | |
223 | */ | |
224 | ||
225 | #ifdef CONFIG_DEBUG_FS | |
226 | ||
227 | static int blockcheck_u64_get(void *data, u64 *val) | |
228 | { | |
229 | *val = *(u64 *)data; | |
230 | return 0; | |
231 | } | |
232 | DEFINE_SIMPLE_ATTRIBUTE(blockcheck_fops, blockcheck_u64_get, NULL, "%llu\n"); | |
233 | ||
234 | static struct dentry *blockcheck_debugfs_create(const char *name, | |
235 | struct dentry *parent, | |
236 | u64 *value) | |
237 | { | |
238 | return debugfs_create_file(name, S_IFREG | S_IRUSR, parent, value, | |
239 | &blockcheck_fops); | |
240 | } | |
241 | ||
242 | static void ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats *stats) | |
243 | { | |
244 | if (stats) { | |
e581595e | 245 | debugfs_remove_recursive(stats->b_debug_dir); |
73be192b JB |
246 | stats->b_debug_dir = NULL; |
247 | } | |
248 | } | |
249 | ||
e581595e GKH |
250 | static void ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats *stats, |
251 | struct dentry *parent) | |
73be192b | 252 | { |
e581595e | 253 | stats->b_debug_dir = debugfs_create_dir("blockcheck", parent); |
73be192b | 254 | |
e581595e GKH |
255 | blockcheck_debugfs_create("blocks_checked", stats->b_debug_dir, |
256 | &stats->b_check_count); | |
73be192b | 257 | |
e581595e GKH |
258 | blockcheck_debugfs_create("checksums_failed", stats->b_debug_dir, |
259 | &stats->b_failure_count); | |
260 | ||
261 | blockcheck_debugfs_create("ecc_recoveries", stats->b_debug_dir, | |
262 | &stats->b_recover_count); | |
73be192b JB |
263 | } |
264 | #else | |
e581595e GKH |
265 | static inline void ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats *stats, |
266 | struct dentry *parent) | |
73be192b | 267 | { |
73be192b JB |
268 | } |
269 | ||
270 | static inline void ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats *stats) | |
271 | { | |
272 | } | |
273 | #endif /* CONFIG_DEBUG_FS */ | |
274 | ||
275 | /* Always-called wrappers for starting and stopping the debugfs files */ | |
e581595e GKH |
276 | void ocfs2_blockcheck_stats_debugfs_install(struct ocfs2_blockcheck_stats *stats, |
277 | struct dentry *parent) | |
73be192b | 278 | { |
e581595e | 279 | ocfs2_blockcheck_debug_install(stats, parent); |
73be192b JB |
280 | } |
281 | ||
282 | void ocfs2_blockcheck_stats_debugfs_remove(struct ocfs2_blockcheck_stats *stats) | |
283 | { | |
284 | ocfs2_blockcheck_debug_remove(stats); | |
285 | } | |
286 | ||
287 | static void ocfs2_blockcheck_inc_check(struct ocfs2_blockcheck_stats *stats) | |
288 | { | |
289 | u64 new_count; | |
290 | ||
291 | if (!stats) | |
292 | return; | |
293 | ||
294 | spin_lock(&stats->b_lock); | |
295 | stats->b_check_count++; | |
296 | new_count = stats->b_check_count; | |
297 | spin_unlock(&stats->b_lock); | |
298 | ||
299 | if (!new_count) | |
300 | mlog(ML_NOTICE, "Block check count has wrapped\n"); | |
301 | } | |
302 | ||
303 | static void ocfs2_blockcheck_inc_failure(struct ocfs2_blockcheck_stats *stats) | |
304 | { | |
305 | u64 new_count; | |
306 | ||
307 | if (!stats) | |
308 | return; | |
309 | ||
310 | spin_lock(&stats->b_lock); | |
311 | stats->b_failure_count++; | |
312 | new_count = stats->b_failure_count; | |
313 | spin_unlock(&stats->b_lock); | |
314 | ||
315 | if (!new_count) | |
316 | mlog(ML_NOTICE, "Checksum failure count has wrapped\n"); | |
317 | } | |
318 | ||
319 | static void ocfs2_blockcheck_inc_recover(struct ocfs2_blockcheck_stats *stats) | |
320 | { | |
321 | u64 new_count; | |
322 | ||
323 | if (!stats) | |
324 | return; | |
325 | ||
326 | spin_lock(&stats->b_lock); | |
327 | stats->b_recover_count++; | |
328 | new_count = stats->b_recover_count; | |
329 | spin_unlock(&stats->b_lock); | |
330 | ||
331 | if (!new_count) | |
332 | mlog(ML_NOTICE, "ECC recovery count has wrapped\n"); | |
333 | } | |
334 | ||
335 | ||
336 | ||
337 | /* | |
338 | * These are the low-level APIs for using the ocfs2_block_check structure. | |
339 | */ | |
340 | ||
70ad1ba7 JB |
341 | /* |
342 | * This function generates check information for a block. | |
343 | * data is the block to be checked. bc is a pointer to the | |
344 | * ocfs2_block_check structure describing the crc32 and the ecc. | |
345 | * | |
346 | * bc should be a pointer inside data, as the function will | |
347 | * take care of zeroing it before calculating the check information. If | |
348 | * bc does not point inside data, the caller must make sure any inline | |
349 | * ocfs2_block_check structures are zeroed. | |
350 | * | |
351 | * The data buffer must be in on-disk endian (little endian for ocfs2). | |
352 | * bc will be filled with little-endian values and will be ready to go to | |
353 | * disk. | |
354 | */ | |
355 | void ocfs2_block_check_compute(void *data, size_t blocksize, | |
356 | struct ocfs2_block_check *bc) | |
357 | { | |
358 | u32 crc; | |
359 | u32 ecc; | |
360 | ||
361 | memset(bc, 0, sizeof(struct ocfs2_block_check)); | |
362 | ||
363 | crc = crc32_le(~0, data, blocksize); | |
364 | ecc = ocfs2_hamming_encode_block(data, blocksize); | |
365 | ||
366 | /* | |
367 | * No ecc'd ocfs2 structure is larger than 4K, so ecc will be no | |
368 | * larger than 16 bits. | |
369 | */ | |
4be929be | 370 | BUG_ON(ecc > USHRT_MAX); |
70ad1ba7 JB |
371 | |
372 | bc->bc_crc32e = cpu_to_le32(crc); | |
373 | bc->bc_ecc = cpu_to_le16((u16)ecc); | |
374 | } | |
375 | ||
376 | /* | |
377 | * This function validates existing check information. Like _compute, | |
378 | * the function will take care of zeroing bc before calculating check codes. | |
379 | * If bc is not a pointer inside data, the caller must have zeroed any | |
380 | * inline ocfs2_block_check structures. | |
381 | * | |
382 | * Again, the data passed in should be the on-disk endian. | |
383 | */ | |
384 | int ocfs2_block_check_validate(void *data, size_t blocksize, | |
73be192b JB |
385 | struct ocfs2_block_check *bc, |
386 | struct ocfs2_blockcheck_stats *stats) | |
70ad1ba7 JB |
387 | { |
388 | int rc = 0; | |
1db5df98 AV |
389 | u32 bc_crc32e; |
390 | u16 bc_ecc; | |
70ad1ba7 JB |
391 | u32 crc, ecc; |
392 | ||
73be192b JB |
393 | ocfs2_blockcheck_inc_check(stats); |
394 | ||
1db5df98 AV |
395 | bc_crc32e = le32_to_cpu(bc->bc_crc32e); |
396 | bc_ecc = le16_to_cpu(bc->bc_ecc); | |
70ad1ba7 JB |
397 | |
398 | memset(bc, 0, sizeof(struct ocfs2_block_check)); | |
399 | ||
400 | /* Fast path - if the crc32 validates, we're good to go */ | |
401 | crc = crc32_le(~0, data, blocksize); | |
1db5df98 | 402 | if (crc == bc_crc32e) |
70ad1ba7 JB |
403 | goto out; |
404 | ||
73be192b | 405 | ocfs2_blockcheck_inc_failure(stats); |
d6b32bbb | 406 | mlog(ML_ERROR, |
dc696ace | 407 | "CRC32 failed: stored: 0x%x, computed 0x%x. Applying ECC.\n", |
1db5df98 | 408 | (unsigned int)bc_crc32e, (unsigned int)crc); |
d6b32bbb | 409 | |
70ad1ba7 JB |
410 | /* Ok, try ECC fixups */ |
411 | ecc = ocfs2_hamming_encode_block(data, blocksize); | |
1db5df98 | 412 | ocfs2_hamming_fix_block(data, blocksize, ecc ^ bc_ecc); |
70ad1ba7 JB |
413 | |
414 | /* And check the crc32 again */ | |
415 | crc = crc32_le(~0, data, blocksize); | |
1db5df98 | 416 | if (crc == bc_crc32e) { |
73be192b | 417 | ocfs2_blockcheck_inc_recover(stats); |
70ad1ba7 | 418 | goto out; |
73be192b | 419 | } |
70ad1ba7 | 420 | |
dc696ace | 421 | mlog(ML_ERROR, "Fixed CRC32 failed: stored: 0x%x, computed 0x%x\n", |
1db5df98 | 422 | (unsigned int)bc_crc32e, (unsigned int)crc); |
d6b32bbb | 423 | |
70ad1ba7 JB |
424 | rc = -EIO; |
425 | ||
426 | out: | |
1db5df98 AV |
427 | bc->bc_crc32e = cpu_to_le32(bc_crc32e); |
428 | bc->bc_ecc = cpu_to_le16(bc_ecc); | |
70ad1ba7 JB |
429 | |
430 | return rc; | |
431 | } | |
432 | ||
433 | /* | |
434 | * This function generates check information for a list of buffer_heads. | |
435 | * bhs is the blocks to be checked. bc is a pointer to the | |
436 | * ocfs2_block_check structure describing the crc32 and the ecc. | |
437 | * | |
438 | * bc should be a pointer inside data, as the function will | |
439 | * take care of zeroing it before calculating the check information. If | |
440 | * bc does not point inside data, the caller must make sure any inline | |
441 | * ocfs2_block_check structures are zeroed. | |
442 | * | |
443 | * The data buffer must be in on-disk endian (little endian for ocfs2). | |
444 | * bc will be filled with little-endian values and will be ready to go to | |
445 | * disk. | |
446 | */ | |
447 | void ocfs2_block_check_compute_bhs(struct buffer_head **bhs, int nr, | |
448 | struct ocfs2_block_check *bc) | |
449 | { | |
450 | int i; | |
451 | u32 crc, ecc; | |
452 | ||
453 | BUG_ON(nr < 0); | |
454 | ||
455 | if (!nr) | |
456 | return; | |
457 | ||
458 | memset(bc, 0, sizeof(struct ocfs2_block_check)); | |
459 | ||
460 | for (i = 0, crc = ~0, ecc = 0; i < nr; i++) { | |
461 | crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size); | |
462 | /* | |
463 | * The number of bits in a buffer is obviously b_size*8. | |
464 | * The offset of this buffer is b_size*i, so the bit offset | |
465 | * of this buffer is b_size*8*i. | |
466 | */ | |
467 | ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data, | |
468 | bhs[i]->b_size * 8, | |
469 | bhs[i]->b_size * 8 * i); | |
470 | } | |
471 | ||
472 | /* | |
473 | * No ecc'd ocfs2 structure is larger than 4K, so ecc will be no | |
474 | * larger than 16 bits. | |
475 | */ | |
4be929be | 476 | BUG_ON(ecc > USHRT_MAX); |
70ad1ba7 JB |
477 | |
478 | bc->bc_crc32e = cpu_to_le32(crc); | |
479 | bc->bc_ecc = cpu_to_le16((u16)ecc); | |
480 | } | |
481 | ||
482 | /* | |
483 | * This function validates existing check information on a list of | |
484 | * buffer_heads. Like _compute_bhs, the function will take care of | |
485 | * zeroing bc before calculating check codes. If bc is not a pointer | |
486 | * inside data, the caller must have zeroed any inline | |
487 | * ocfs2_block_check structures. | |
488 | * | |
489 | * Again, the data passed in should be the on-disk endian. | |
490 | */ | |
491 | int ocfs2_block_check_validate_bhs(struct buffer_head **bhs, int nr, | |
73be192b JB |
492 | struct ocfs2_block_check *bc, |
493 | struct ocfs2_blockcheck_stats *stats) | |
70ad1ba7 JB |
494 | { |
495 | int i, rc = 0; | |
1db5df98 AV |
496 | u32 bc_crc32e; |
497 | u16 bc_ecc; | |
70ad1ba7 JB |
498 | u32 crc, ecc, fix; |
499 | ||
500 | BUG_ON(nr < 0); | |
501 | ||
502 | if (!nr) | |
503 | return 0; | |
504 | ||
73be192b JB |
505 | ocfs2_blockcheck_inc_check(stats); |
506 | ||
1db5df98 AV |
507 | bc_crc32e = le32_to_cpu(bc->bc_crc32e); |
508 | bc_ecc = le16_to_cpu(bc->bc_ecc); | |
70ad1ba7 JB |
509 | |
510 | memset(bc, 0, sizeof(struct ocfs2_block_check)); | |
511 | ||
512 | /* Fast path - if the crc32 validates, we're good to go */ | |
513 | for (i = 0, crc = ~0; i < nr; i++) | |
514 | crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size); | |
1db5df98 | 515 | if (crc == bc_crc32e) |
70ad1ba7 JB |
516 | goto out; |
517 | ||
73be192b | 518 | ocfs2_blockcheck_inc_failure(stats); |
70ad1ba7 JB |
519 | mlog(ML_ERROR, |
520 | "CRC32 failed: stored: %u, computed %u. Applying ECC.\n", | |
1db5df98 | 521 | (unsigned int)bc_crc32e, (unsigned int)crc); |
70ad1ba7 JB |
522 | |
523 | /* Ok, try ECC fixups */ | |
524 | for (i = 0, ecc = 0; i < nr; i++) { | |
525 | /* | |
526 | * The number of bits in a buffer is obviously b_size*8. | |
527 | * The offset of this buffer is b_size*i, so the bit offset | |
528 | * of this buffer is b_size*8*i. | |
529 | */ | |
530 | ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data, | |
531 | bhs[i]->b_size * 8, | |
532 | bhs[i]->b_size * 8 * i); | |
533 | } | |
1db5df98 | 534 | fix = ecc ^ bc_ecc; |
70ad1ba7 JB |
535 | for (i = 0; i < nr; i++) { |
536 | /* | |
537 | * Try the fix against each buffer. It will only affect | |
538 | * one of them. | |
539 | */ | |
540 | ocfs2_hamming_fix(bhs[i]->b_data, bhs[i]->b_size * 8, | |
541 | bhs[i]->b_size * 8 * i, fix); | |
542 | } | |
543 | ||
544 | /* And check the crc32 again */ | |
545 | for (i = 0, crc = ~0; i < nr; i++) | |
546 | crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size); | |
1db5df98 | 547 | if (crc == bc_crc32e) { |
73be192b | 548 | ocfs2_blockcheck_inc_recover(stats); |
70ad1ba7 | 549 | goto out; |
73be192b | 550 | } |
70ad1ba7 JB |
551 | |
552 | mlog(ML_ERROR, "Fixed CRC32 failed: stored: %u, computed %u\n", | |
1db5df98 | 553 | (unsigned int)bc_crc32e, (unsigned int)crc); |
70ad1ba7 JB |
554 | |
555 | rc = -EIO; | |
556 | ||
557 | out: | |
1db5df98 AV |
558 | bc->bc_crc32e = cpu_to_le32(bc_crc32e); |
559 | bc->bc_ecc = cpu_to_le16(bc_ecc); | |
70ad1ba7 JB |
560 | |
561 | return rc; | |
562 | } | |
563 | ||
564 | /* | |
565 | * These are the main API. They check the superblock flag before | |
566 | * calling the underlying operations. | |
567 | * | |
568 | * They expect the buffer(s) to be in disk format. | |
569 | */ | |
570 | void ocfs2_compute_meta_ecc(struct super_block *sb, void *data, | |
571 | struct ocfs2_block_check *bc) | |
572 | { | |
573 | if (ocfs2_meta_ecc(OCFS2_SB(sb))) | |
574 | ocfs2_block_check_compute(data, sb->s_blocksize, bc); | |
575 | } | |
576 | ||
577 | int ocfs2_validate_meta_ecc(struct super_block *sb, void *data, | |
578 | struct ocfs2_block_check *bc) | |
579 | { | |
580 | int rc = 0; | |
73be192b | 581 | struct ocfs2_super *osb = OCFS2_SB(sb); |
70ad1ba7 | 582 | |
73be192b JB |
583 | if (ocfs2_meta_ecc(osb)) |
584 | rc = ocfs2_block_check_validate(data, sb->s_blocksize, bc, | |
585 | &osb->osb_ecc_stats); | |
70ad1ba7 JB |
586 | |
587 | return rc; | |
588 | } | |
589 | ||
590 | void ocfs2_compute_meta_ecc_bhs(struct super_block *sb, | |
591 | struct buffer_head **bhs, int nr, | |
592 | struct ocfs2_block_check *bc) | |
593 | { | |
594 | if (ocfs2_meta_ecc(OCFS2_SB(sb))) | |
595 | ocfs2_block_check_compute_bhs(bhs, nr, bc); | |
596 | } | |
597 | ||
598 | int ocfs2_validate_meta_ecc_bhs(struct super_block *sb, | |
599 | struct buffer_head **bhs, int nr, | |
600 | struct ocfs2_block_check *bc) | |
601 | { | |
602 | int rc = 0; | |
73be192b | 603 | struct ocfs2_super *osb = OCFS2_SB(sb); |
70ad1ba7 | 604 | |
73be192b JB |
605 | if (ocfs2_meta_ecc(osb)) |
606 | rc = ocfs2_block_check_validate_bhs(bhs, nr, bc, | |
607 | &osb->osb_ecc_stats); | |
70ad1ba7 JB |
608 | |
609 | return rc; | |
610 | } | |
611 |