blob: 863a5316030b9f6a6a016523508e786457840f66 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Masahiro Yamadafa60ce22021-05-06 18:06:44 -07002/*
Joel Becker70ad1ba2008-10-16 17:54:25 -07003 * blockcheck.c
4 *
5 * Checksum and ECC codes for the OCFS2 userspace library.
6 *
7 * Copyright (C) 2006, 2008 Oracle. All rights reserved.
Joel Becker70ad1ba2008-10-16 17:54:25 -07008 */
9
10#include <linux/kernel.h>
11#include <linux/types.h>
12#include <linux/crc32.h>
13#include <linux/buffer_head.h>
14#include <linux/bitops.h>
Joel Becker73be1922009-01-06 14:57:08 -080015#include <linux/debugfs.h>
16#include <linux/module.h>
17#include <linux/fs.h>
Joel Becker70ad1ba2008-10-16 17:54:25 -070018#include <asm/byteorder.h>
19
Joel Beckerd6b32bb2008-10-17 14:55:01 -070020#include <cluster/masklog.h>
21
Joel Becker70ad1ba2008-10-16 17:54:25 -070022#include "ocfs2.h"
23
24#include "blockcheck.h"
25
26
Joel Becker70ad1ba2008-10-16 17:54:25 -070027/*
28 * We use the following conventions:
29 *
30 * d = # data bits
31 * p = # parity bits
32 * c = # total code bits (d + p)
33 */
Joel Becker70ad1ba2008-10-16 17:54:25 -070034
Joel Becker7bb458a2008-12-15 18:24:33 -080035
36/*
Joel Becker70ad1ba2008-10-16 17:54:25 -070037 * Calculate the bit offset in the hamming code buffer based on the bit's
38 * offset in the data buffer. Since the hamming code reserves all
39 * power-of-two bits for parity, the data bit number and the code bit
Uwe Kleine-Königbf48aab2009-10-28 20:11:03 +010040 * number are offset by all the parity bits beforehand.
Joel Becker70ad1ba2008-10-16 17:54:25 -070041 *
42 * Recall that bit numbers in hamming code are 1-based. This function
43 * takes the 0-based data bit from the caller.
44 *
45 * An example. Take bit 1 of the data buffer. 1 is a power of two (2^0),
46 * so it's a parity bit. 2 is a power of two (2^1), so it's a parity bit.
47 * 3 is not a power of two. So bit 1 of the data buffer ends up as bit 3
48 * in the code buffer.
Joel Becker58896c42008-12-16 13:54:40 -080049 *
50 * The caller can pass in *p if it wants to keep track of the most recent
51 * number of parity bits added. This allows the function to start the
52 * calculation at the last place.
Joel Becker70ad1ba2008-10-16 17:54:25 -070053 */
Joel Becker58896c42008-12-16 13:54:40 -080054static unsigned int calc_code_bit(unsigned int i, unsigned int *p_cache)
Joel Becker70ad1ba2008-10-16 17:54:25 -070055{
Joel Becker58896c42008-12-16 13:54:40 -080056 unsigned int b, p = 0;
Joel Becker70ad1ba2008-10-16 17:54:25 -070057
58 /*
59 * Data bits are 0-based, but we're talking code bits, which
60 * are 1-based.
61 */
62 b = i + 1;
63
Joel Becker58896c42008-12-16 13:54:40 -080064 /* Use the cache if it is there */
65 if (p_cache)
66 p = *p_cache;
Joel Becker7bb458a2008-12-15 18:24:33 -080067 b += p;
68
69 /*
Joel Becker70ad1ba2008-10-16 17:54:25 -070070 * For every power of two below our bit number, bump our bit.
71 *
Joel Becker58896c42008-12-16 13:54:40 -080072 * We compare with (b + 1) because we have to compare with what b
Joel Becker70ad1ba2008-10-16 17:54:25 -070073 * would be _if_ it were bumped up by the parity bit. Capice?
Joel Becker7bb458a2008-12-15 18:24:33 -080074 *
Joel Becker58896c42008-12-16 13:54:40 -080075 * p is set above.
Joel Becker70ad1ba2008-10-16 17:54:25 -070076 */
Joel Becker58896c42008-12-16 13:54:40 -080077 for (; (1 << p) < (b + 1); p++)
Joel Becker70ad1ba2008-10-16 17:54:25 -070078 b++;
79
Joel Becker58896c42008-12-16 13:54:40 -080080 if (p_cache)
81 *p_cache = p;
82
Joel Becker70ad1ba2008-10-16 17:54:25 -070083 return b;
84}
85
86/*
87 * This is the low level encoder function. It can be called across
88 * multiple hunks just like the crc32 code. 'd' is the number of bits
89 * _in_this_hunk_. nr is the bit offset of this hunk. So, if you had
90 * two 512B buffers, you would do it like so:
91 *
92 * parity = ocfs2_hamming_encode(0, buf1, 512 * 8, 0);
93 * parity = ocfs2_hamming_encode(parity, buf2, 512 * 8, 512 * 8);
94 *
95 * If you just have one buffer, use ocfs2_hamming_encode_block().
96 */
97u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d, unsigned int nr)
98{
Joel Becker58896c42008-12-16 13:54:40 -080099 unsigned int i, b, p = 0;
Joel Becker70ad1ba2008-10-16 17:54:25 -0700100
Joel Beckere798b3f82008-12-15 17:13:48 -0800101 BUG_ON(!d);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700102
103 /*
104 * b is the hamming code bit number. Hamming code specifies a
105 * 1-based array, but C uses 0-based. So 'i' is for C, and 'b' is
106 * for the algorithm.
107 *
108 * The i++ in the for loop is so that the start offset passed
109 * to ocfs2_find_next_bit_set() is one greater than the previously
110 * found bit.
111 */
112 for (i = 0; (i = ocfs2_find_next_bit(data, d, i)) < d; i++)
113 {
114 /*
115 * i is the offset in this hunk, nr + i is the total bit
116 * offset.
117 */
Joel Becker58896c42008-12-16 13:54:40 -0800118 b = calc_code_bit(nr + i, &p);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700119
Joel Beckere798b3f82008-12-15 17:13:48 -0800120 /*
121 * Data bits in the resultant code are checked by
122 * parity bits that are part of the bit number
123 * representation. Huh?
124 *
Alexander A. Klimov4510a5a2020-08-06 23:18:06 -0700125 * <wikipedia href="https://en.wikipedia.org/wiki/Hamming_code">
Joel Beckere798b3f82008-12-15 17:13:48 -0800126 * In other words, the parity bit at position 2^k
127 * checks bits in positions having bit k set in
128 * their binary representation. Conversely, for
129 * instance, bit 13, i.e. 1101(2), is checked by
130 * bits 1000(2) = 8, 0100(2)=4 and 0001(2) = 1.
131 * </wikipedia>
132 *
133 * Note that 'k' is the _code_ bit number. 'b' in
134 * our loop.
135 */
136 parity ^= b;
Joel Becker70ad1ba2008-10-16 17:54:25 -0700137 }
138
139 /* While the data buffer was treated as little endian, the
140 * return value is in host endian. */
141 return parity;
142}
143
144u32 ocfs2_hamming_encode_block(void *data, unsigned int blocksize)
145{
146 return ocfs2_hamming_encode(0, data, blocksize * 8, 0);
147}
148
149/*
150 * Like ocfs2_hamming_encode(), this can handle hunks. nr is the bit
151 * offset of the current hunk. If bit to be fixed is not part of the
152 * current hunk, this does nothing.
153 *
154 * If you only have one hunk, use ocfs2_hamming_fix_block().
155 */
156void ocfs2_hamming_fix(void *data, unsigned int d, unsigned int nr,
157 unsigned int fix)
158{
Joel Becker70ad1ba2008-10-16 17:54:25 -0700159 unsigned int i, b;
160
Joel Beckere798b3f82008-12-15 17:13:48 -0800161 BUG_ON(!d);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700162
163 /*
164 * If the bit to fix has an hweight of 1, it's a parity bit. One
165 * busted parity bit is its own error. Nothing to do here.
166 */
167 if (hweight32(fix) == 1)
168 return;
169
170 /*
171 * nr + d is the bit right past the data hunk we're looking at.
172 * If fix after that, nothing to do
173 */
Joel Becker58896c42008-12-16 13:54:40 -0800174 if (fix >= calc_code_bit(nr + d, NULL))
Joel Becker70ad1ba2008-10-16 17:54:25 -0700175 return;
176
177 /*
178 * nr is the offset in the data hunk we're starting at. Let's
179 * start b at the offset in the code buffer. See hamming_encode()
180 * for a more detailed description of 'b'.
181 */
Joel Becker58896c42008-12-16 13:54:40 -0800182 b = calc_code_bit(nr, NULL);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700183 /* If the fix is before this hunk, nothing to do */
184 if (fix < b)
185 return;
186
187 for (i = 0; i < d; i++, b++)
188 {
189 /* Skip past parity bits */
190 while (hweight32(b) == 1)
191 b++;
192
193 /*
194 * i is the offset in this data hunk.
195 * nr + i is the offset in the total data buffer.
196 * b is the offset in the total code buffer.
197 *
198 * Thus, when b == fix, bit i in the current hunk needs
199 * fixing.
200 */
201 if (b == fix)
202 {
203 if (ocfs2_test_bit(i, data))
204 ocfs2_clear_bit(i, data);
205 else
206 ocfs2_set_bit(i, data);
207 break;
208 }
209 }
210}
211
212void ocfs2_hamming_fix_block(void *data, unsigned int blocksize,
213 unsigned int fix)
214{
215 ocfs2_hamming_fix(data, blocksize * 8, 0, fix);
216}
217
Joel Becker73be1922009-01-06 14:57:08 -0800218
219/*
220 * Debugfs handling.
221 */
222
223#ifdef CONFIG_DEBUG_FS
224
225static int blockcheck_u64_get(void *data, u64 *val)
226{
227 *val = *(u64 *)data;
228 return 0;
229}
Yang Li16348522021-04-29 22:54:02 -0700230DEFINE_DEBUGFS_ATTRIBUTE(blockcheck_fops, blockcheck_u64_get, NULL, "%llu\n");
Joel Becker73be1922009-01-06 14:57:08 -0800231
Joel Becker73be1922009-01-06 14:57:08 -0800232static void ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats *stats)
233{
234 if (stats) {
Greg Kroah-Hartmane5815952019-07-11 20:53:12 -0700235 debugfs_remove_recursive(stats->b_debug_dir);
Joel Becker73be1922009-01-06 14:57:08 -0800236 stats->b_debug_dir = NULL;
237 }
238}
239
Greg Kroah-Hartmane5815952019-07-11 20:53:12 -0700240static void ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats *stats,
241 struct dentry *parent)
Joel Becker73be1922009-01-06 14:57:08 -0800242{
Greg Kroah-Hartman5e7a3ed2019-09-23 15:33:15 -0700243 struct dentry *dir;
Joel Becker73be1922009-01-06 14:57:08 -0800244
Greg Kroah-Hartman5e7a3ed2019-09-23 15:33:15 -0700245 dir = debugfs_create_dir("blockcheck", parent);
246 stats->b_debug_dir = dir;
Joel Becker73be1922009-01-06 14:57:08 -0800247
Greg Kroah-Hartman5e7a3ed2019-09-23 15:33:15 -0700248 debugfs_create_file("blocks_checked", S_IFREG | S_IRUSR, dir,
249 &stats->b_check_count, &blockcheck_fops);
Joel Becker73be1922009-01-06 14:57:08 -0800250
Greg Kroah-Hartman5e7a3ed2019-09-23 15:33:15 -0700251 debugfs_create_file("checksums_failed", S_IFREG | S_IRUSR, dir,
252 &stats->b_failure_count, &blockcheck_fops);
253
254 debugfs_create_file("ecc_recoveries", S_IFREG | S_IRUSR, dir,
255 &stats->b_recover_count, &blockcheck_fops);
256
Joel Becker73be1922009-01-06 14:57:08 -0800257}
258#else
Greg Kroah-Hartmane5815952019-07-11 20:53:12 -0700259static inline void ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats *stats,
260 struct dentry *parent)
Joel Becker73be1922009-01-06 14:57:08 -0800261{
Joel Becker73be1922009-01-06 14:57:08 -0800262}
263
264static inline void ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats *stats)
265{
266}
267#endif /* CONFIG_DEBUG_FS */
268
269/* Always-called wrappers for starting and stopping the debugfs files */
Greg Kroah-Hartmane5815952019-07-11 20:53:12 -0700270void ocfs2_blockcheck_stats_debugfs_install(struct ocfs2_blockcheck_stats *stats,
271 struct dentry *parent)
Joel Becker73be1922009-01-06 14:57:08 -0800272{
Greg Kroah-Hartmane5815952019-07-11 20:53:12 -0700273 ocfs2_blockcheck_debug_install(stats, parent);
Joel Becker73be1922009-01-06 14:57:08 -0800274}
275
276void ocfs2_blockcheck_stats_debugfs_remove(struct ocfs2_blockcheck_stats *stats)
277{
278 ocfs2_blockcheck_debug_remove(stats);
279}
280
281static void ocfs2_blockcheck_inc_check(struct ocfs2_blockcheck_stats *stats)
282{
283 u64 new_count;
284
285 if (!stats)
286 return;
287
288 spin_lock(&stats->b_lock);
289 stats->b_check_count++;
290 new_count = stats->b_check_count;
291 spin_unlock(&stats->b_lock);
292
293 if (!new_count)
294 mlog(ML_NOTICE, "Block check count has wrapped\n");
295}
296
297static void ocfs2_blockcheck_inc_failure(struct ocfs2_blockcheck_stats *stats)
298{
299 u64 new_count;
300
301 if (!stats)
302 return;
303
304 spin_lock(&stats->b_lock);
305 stats->b_failure_count++;
306 new_count = stats->b_failure_count;
307 spin_unlock(&stats->b_lock);
308
309 if (!new_count)
310 mlog(ML_NOTICE, "Checksum failure count has wrapped\n");
311}
312
313static void ocfs2_blockcheck_inc_recover(struct ocfs2_blockcheck_stats *stats)
314{
315 u64 new_count;
316
317 if (!stats)
318 return;
319
320 spin_lock(&stats->b_lock);
321 stats->b_recover_count++;
322 new_count = stats->b_recover_count;
323 spin_unlock(&stats->b_lock);
324
325 if (!new_count)
326 mlog(ML_NOTICE, "ECC recovery count has wrapped\n");
327}
328
329
330
331/*
332 * These are the low-level APIs for using the ocfs2_block_check structure.
333 */
334
Joel Becker70ad1ba2008-10-16 17:54:25 -0700335/*
336 * This function generates check information for a block.
337 * data is the block to be checked. bc is a pointer to the
338 * ocfs2_block_check structure describing the crc32 and the ecc.
339 *
340 * bc should be a pointer inside data, as the function will
341 * take care of zeroing it before calculating the check information. If
342 * bc does not point inside data, the caller must make sure any inline
343 * ocfs2_block_check structures are zeroed.
344 *
345 * The data buffer must be in on-disk endian (little endian for ocfs2).
346 * bc will be filled with little-endian values and will be ready to go to
347 * disk.
348 */
349void ocfs2_block_check_compute(void *data, size_t blocksize,
350 struct ocfs2_block_check *bc)
351{
352 u32 crc;
353 u32 ecc;
354
355 memset(bc, 0, sizeof(struct ocfs2_block_check));
356
357 crc = crc32_le(~0, data, blocksize);
358 ecc = ocfs2_hamming_encode_block(data, blocksize);
359
360 /*
361 * No ecc'd ocfs2 structure is larger than 4K, so ecc will be no
362 * larger than 16 bits.
363 */
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700364 BUG_ON(ecc > USHRT_MAX);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700365
366 bc->bc_crc32e = cpu_to_le32(crc);
367 bc->bc_ecc = cpu_to_le16((u16)ecc);
368}
369
370/*
371 * This function validates existing check information. Like _compute,
372 * the function will take care of zeroing bc before calculating check codes.
373 * If bc is not a pointer inside data, the caller must have zeroed any
374 * inline ocfs2_block_check structures.
375 *
376 * Again, the data passed in should be the on-disk endian.
377 */
378int ocfs2_block_check_validate(void *data, size_t blocksize,
Joel Becker73be1922009-01-06 14:57:08 -0800379 struct ocfs2_block_check *bc,
380 struct ocfs2_blockcheck_stats *stats)
Joel Becker70ad1ba2008-10-16 17:54:25 -0700381{
382 int rc = 0;
Al Viro1db5df92012-04-12 19:58:53 -0400383 u32 bc_crc32e;
384 u16 bc_ecc;
Joel Becker70ad1ba2008-10-16 17:54:25 -0700385 u32 crc, ecc;
386
Joel Becker73be1922009-01-06 14:57:08 -0800387 ocfs2_blockcheck_inc_check(stats);
388
Al Viro1db5df92012-04-12 19:58:53 -0400389 bc_crc32e = le32_to_cpu(bc->bc_crc32e);
390 bc_ecc = le16_to_cpu(bc->bc_ecc);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700391
392 memset(bc, 0, sizeof(struct ocfs2_block_check));
393
394 /* Fast path - if the crc32 validates, we're good to go */
395 crc = crc32_le(~0, data, blocksize);
Al Viro1db5df92012-04-12 19:58:53 -0400396 if (crc == bc_crc32e)
Joel Becker70ad1ba2008-10-16 17:54:25 -0700397 goto out;
398
Joel Becker73be1922009-01-06 14:57:08 -0800399 ocfs2_blockcheck_inc_failure(stats);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700400 mlog(ML_ERROR,
Sunil Mushrandc696ac2010-08-12 16:24:25 -0700401 "CRC32 failed: stored: 0x%x, computed 0x%x. Applying ECC.\n",
Al Viro1db5df92012-04-12 19:58:53 -0400402 (unsigned int)bc_crc32e, (unsigned int)crc);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700403
Joel Becker70ad1ba2008-10-16 17:54:25 -0700404 /* Ok, try ECC fixups */
405 ecc = ocfs2_hamming_encode_block(data, blocksize);
Al Viro1db5df92012-04-12 19:58:53 -0400406 ocfs2_hamming_fix_block(data, blocksize, ecc ^ bc_ecc);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700407
408 /* And check the crc32 again */
409 crc = crc32_le(~0, data, blocksize);
Al Viro1db5df92012-04-12 19:58:53 -0400410 if (crc == bc_crc32e) {
Joel Becker73be1922009-01-06 14:57:08 -0800411 ocfs2_blockcheck_inc_recover(stats);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700412 goto out;
Joel Becker73be1922009-01-06 14:57:08 -0800413 }
Joel Becker70ad1ba2008-10-16 17:54:25 -0700414
Sunil Mushrandc696ac2010-08-12 16:24:25 -0700415 mlog(ML_ERROR, "Fixed CRC32 failed: stored: 0x%x, computed 0x%x\n",
Al Viro1db5df92012-04-12 19:58:53 -0400416 (unsigned int)bc_crc32e, (unsigned int)crc);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700417
Joel Becker70ad1ba2008-10-16 17:54:25 -0700418 rc = -EIO;
419
420out:
Al Viro1db5df92012-04-12 19:58:53 -0400421 bc->bc_crc32e = cpu_to_le32(bc_crc32e);
422 bc->bc_ecc = cpu_to_le16(bc_ecc);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700423
424 return rc;
425}
426
427/*
428 * This function generates check information for a list of buffer_heads.
429 * bhs is the blocks to be checked. bc is a pointer to the
430 * ocfs2_block_check structure describing the crc32 and the ecc.
431 *
432 * bc should be a pointer inside data, as the function will
433 * take care of zeroing it before calculating the check information. If
434 * bc does not point inside data, the caller must make sure any inline
435 * ocfs2_block_check structures are zeroed.
436 *
437 * The data buffer must be in on-disk endian (little endian for ocfs2).
438 * bc will be filled with little-endian values and will be ready to go to
439 * disk.
440 */
441void ocfs2_block_check_compute_bhs(struct buffer_head **bhs, int nr,
442 struct ocfs2_block_check *bc)
443{
444 int i;
445 u32 crc, ecc;
446
447 BUG_ON(nr < 0);
448
449 if (!nr)
450 return;
451
452 memset(bc, 0, sizeof(struct ocfs2_block_check));
453
454 for (i = 0, crc = ~0, ecc = 0; i < nr; i++) {
455 crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
456 /*
457 * The number of bits in a buffer is obviously b_size*8.
458 * The offset of this buffer is b_size*i, so the bit offset
459 * of this buffer is b_size*8*i.
460 */
461 ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data,
462 bhs[i]->b_size * 8,
463 bhs[i]->b_size * 8 * i);
464 }
465
466 /*
467 * No ecc'd ocfs2 structure is larger than 4K, so ecc will be no
468 * larger than 16 bits.
469 */
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700470 BUG_ON(ecc > USHRT_MAX);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700471
472 bc->bc_crc32e = cpu_to_le32(crc);
473 bc->bc_ecc = cpu_to_le16((u16)ecc);
474}
475
476/*
477 * This function validates existing check information on a list of
478 * buffer_heads. Like _compute_bhs, the function will take care of
479 * zeroing bc before calculating check codes. If bc is not a pointer
480 * inside data, the caller must have zeroed any inline
481 * ocfs2_block_check structures.
482 *
483 * Again, the data passed in should be the on-disk endian.
484 */
485int ocfs2_block_check_validate_bhs(struct buffer_head **bhs, int nr,
Joel Becker73be1922009-01-06 14:57:08 -0800486 struct ocfs2_block_check *bc,
487 struct ocfs2_blockcheck_stats *stats)
Joel Becker70ad1ba2008-10-16 17:54:25 -0700488{
489 int i, rc = 0;
Al Viro1db5df92012-04-12 19:58:53 -0400490 u32 bc_crc32e;
491 u16 bc_ecc;
Joel Becker70ad1ba2008-10-16 17:54:25 -0700492 u32 crc, ecc, fix;
493
494 BUG_ON(nr < 0);
495
496 if (!nr)
497 return 0;
498
Joel Becker73be1922009-01-06 14:57:08 -0800499 ocfs2_blockcheck_inc_check(stats);
500
Al Viro1db5df92012-04-12 19:58:53 -0400501 bc_crc32e = le32_to_cpu(bc->bc_crc32e);
502 bc_ecc = le16_to_cpu(bc->bc_ecc);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700503
504 memset(bc, 0, sizeof(struct ocfs2_block_check));
505
506 /* Fast path - if the crc32 validates, we're good to go */
507 for (i = 0, crc = ~0; i < nr; i++)
508 crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
Al Viro1db5df92012-04-12 19:58:53 -0400509 if (crc == bc_crc32e)
Joel Becker70ad1ba2008-10-16 17:54:25 -0700510 goto out;
511
Joel Becker73be1922009-01-06 14:57:08 -0800512 ocfs2_blockcheck_inc_failure(stats);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700513 mlog(ML_ERROR,
514 "CRC32 failed: stored: %u, computed %u. Applying ECC.\n",
Al Viro1db5df92012-04-12 19:58:53 -0400515 (unsigned int)bc_crc32e, (unsigned int)crc);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700516
517 /* Ok, try ECC fixups */
518 for (i = 0, ecc = 0; i < nr; i++) {
519 /*
520 * The number of bits in a buffer is obviously b_size*8.
521 * The offset of this buffer is b_size*i, so the bit offset
522 * of this buffer is b_size*8*i.
523 */
524 ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data,
525 bhs[i]->b_size * 8,
526 bhs[i]->b_size * 8 * i);
527 }
Al Viro1db5df92012-04-12 19:58:53 -0400528 fix = ecc ^ bc_ecc;
Joel Becker70ad1ba2008-10-16 17:54:25 -0700529 for (i = 0; i < nr; i++) {
530 /*
531 * Try the fix against each buffer. It will only affect
532 * one of them.
533 */
534 ocfs2_hamming_fix(bhs[i]->b_data, bhs[i]->b_size * 8,
535 bhs[i]->b_size * 8 * i, fix);
536 }
537
538 /* And check the crc32 again */
539 for (i = 0, crc = ~0; i < nr; i++)
540 crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
Al Viro1db5df92012-04-12 19:58:53 -0400541 if (crc == bc_crc32e) {
Joel Becker73be1922009-01-06 14:57:08 -0800542 ocfs2_blockcheck_inc_recover(stats);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700543 goto out;
Joel Becker73be1922009-01-06 14:57:08 -0800544 }
Joel Becker70ad1ba2008-10-16 17:54:25 -0700545
546 mlog(ML_ERROR, "Fixed CRC32 failed: stored: %u, computed %u\n",
Al Viro1db5df92012-04-12 19:58:53 -0400547 (unsigned int)bc_crc32e, (unsigned int)crc);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700548
549 rc = -EIO;
550
551out:
Al Viro1db5df92012-04-12 19:58:53 -0400552 bc->bc_crc32e = cpu_to_le32(bc_crc32e);
553 bc->bc_ecc = cpu_to_le16(bc_ecc);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700554
555 return rc;
556}
557
558/*
559 * These are the main API. They check the superblock flag before
560 * calling the underlying operations.
561 *
562 * They expect the buffer(s) to be in disk format.
563 */
564void ocfs2_compute_meta_ecc(struct super_block *sb, void *data,
565 struct ocfs2_block_check *bc)
566{
567 if (ocfs2_meta_ecc(OCFS2_SB(sb)))
568 ocfs2_block_check_compute(data, sb->s_blocksize, bc);
569}
570
571int ocfs2_validate_meta_ecc(struct super_block *sb, void *data,
572 struct ocfs2_block_check *bc)
573{
574 int rc = 0;
Joel Becker73be1922009-01-06 14:57:08 -0800575 struct ocfs2_super *osb = OCFS2_SB(sb);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700576
Joel Becker73be1922009-01-06 14:57:08 -0800577 if (ocfs2_meta_ecc(osb))
578 rc = ocfs2_block_check_validate(data, sb->s_blocksize, bc,
579 &osb->osb_ecc_stats);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700580
581 return rc;
582}
583
584void ocfs2_compute_meta_ecc_bhs(struct super_block *sb,
585 struct buffer_head **bhs, int nr,
586 struct ocfs2_block_check *bc)
587{
588 if (ocfs2_meta_ecc(OCFS2_SB(sb)))
589 ocfs2_block_check_compute_bhs(bhs, nr, bc);
590}
591
592int ocfs2_validate_meta_ecc_bhs(struct super_block *sb,
593 struct buffer_head **bhs, int nr,
594 struct ocfs2_block_check *bc)
595{
596 int rc = 0;
Joel Becker73be1922009-01-06 14:57:08 -0800597 struct ocfs2_super *osb = OCFS2_SB(sb);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700598
Joel Becker73be1922009-01-06 14:57:08 -0800599 if (ocfs2_meta_ecc(osb))
600 rc = ocfs2_block_check_validate_bhs(bhs, nr, bc,
601 &osb->osb_ecc_stats);
Joel Becker70ad1ba2008-10-16 17:54:25 -0700602
603 return rc;
604}
605