xref: /openbmc/linux/fs/ocfs2/blockcheck.c (revision fa60ce2c)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2*fa60ce2cSMasahiro Yamada /*
370ad1ba7SJoel Becker  * blockcheck.c
470ad1ba7SJoel Becker  *
570ad1ba7SJoel Becker  * Checksum and ECC codes for the OCFS2 userspace library.
670ad1ba7SJoel Becker  *
770ad1ba7SJoel Becker  * Copyright (C) 2006, 2008 Oracle.  All rights reserved.
870ad1ba7SJoel Becker  */
970ad1ba7SJoel Becker 
1070ad1ba7SJoel Becker #include <linux/kernel.h>
1170ad1ba7SJoel Becker #include <linux/types.h>
1270ad1ba7SJoel Becker #include <linux/crc32.h>
1370ad1ba7SJoel Becker #include <linux/buffer_head.h>
1470ad1ba7SJoel Becker #include <linux/bitops.h>
1573be192bSJoel Becker #include <linux/debugfs.h>
1673be192bSJoel Becker #include <linux/module.h>
1773be192bSJoel Becker #include <linux/fs.h>
1870ad1ba7SJoel Becker #include <asm/byteorder.h>
1970ad1ba7SJoel Becker 
20d6b32bbbSJoel Becker #include <cluster/masklog.h>
21d6b32bbbSJoel Becker 
2270ad1ba7SJoel Becker #include "ocfs2.h"
2370ad1ba7SJoel Becker 
2470ad1ba7SJoel Becker #include "blockcheck.h"
2570ad1ba7SJoel Becker 
2670ad1ba7SJoel Becker 
2770ad1ba7SJoel Becker /*
2870ad1ba7SJoel Becker  * We use the following conventions:
2970ad1ba7SJoel Becker  *
3070ad1ba7SJoel Becker  * d = # data bits
3170ad1ba7SJoel Becker  * p = # parity bits
3270ad1ba7SJoel Becker  * c = # total code bits (d + p)
3370ad1ba7SJoel Becker  */
3470ad1ba7SJoel Becker 
357bb458a5SJoel Becker 
367bb458a5SJoel Becker /*
3770ad1ba7SJoel Becker  * Calculate the bit offset in the hamming code buffer based on the bit's
3870ad1ba7SJoel Becker  * offset in the data buffer.  Since the hamming code reserves all
3970ad1ba7SJoel Becker  * power-of-two bits for parity, the data bit number and the code bit
40bf48aabbSUwe Kleine-König  * number are offset by all the parity bits beforehand.
4170ad1ba7SJoel Becker  *
4270ad1ba7SJoel Becker  * Recall that bit numbers in hamming code are 1-based.  This function
4370ad1ba7SJoel Becker  * takes the 0-based data bit from the caller.
4470ad1ba7SJoel Becker  *
4570ad1ba7SJoel Becker  * An example.  Take bit 1 of the data buffer.  1 is a power of two (2^0),
4670ad1ba7SJoel Becker  * so it's a parity bit.  2 is a power of two (2^1), so it's a parity bit.
4770ad1ba7SJoel Becker  * 3 is not a power of two.  So bit 1 of the data buffer ends up as bit 3
4870ad1ba7SJoel Becker  * in the code buffer.
4958896c4dSJoel Becker  *
5058896c4dSJoel Becker  * The caller can pass in *p if it wants to keep track of the most recent
5158896c4dSJoel Becker  * number of parity bits added.  This allows the function to start the
5258896c4dSJoel Becker  * calculation at the last place.
5370ad1ba7SJoel Becker  */
calc_code_bit(unsigned int i,unsigned int * p_cache)5458896c4dSJoel Becker static unsigned int calc_code_bit(unsigned int i, unsigned int *p_cache)
5570ad1ba7SJoel Becker {
5658896c4dSJoel Becker 	unsigned int b, p = 0;
5770ad1ba7SJoel Becker 
5870ad1ba7SJoel Becker 	/*
5970ad1ba7SJoel Becker 	 * Data bits are 0-based, but we're talking code bits, which
6070ad1ba7SJoel Becker 	 * are 1-based.
6170ad1ba7SJoel Becker 	 */
6270ad1ba7SJoel Becker 	b = i + 1;
6370ad1ba7SJoel Becker 
6458896c4dSJoel Becker 	/* Use the cache if it is there */
6558896c4dSJoel Becker 	if (p_cache)
6658896c4dSJoel Becker 		p = *p_cache;
677bb458a5SJoel Becker         b += p;
687bb458a5SJoel Becker 
697bb458a5SJoel Becker 	/*
7070ad1ba7SJoel Becker 	 * For every power of two below our bit number, bump our bit.
7170ad1ba7SJoel Becker 	 *
7258896c4dSJoel Becker 	 * We compare with (b + 1) because we have to compare with what b
7370ad1ba7SJoel Becker 	 * would be _if_ it were bumped up by the parity bit.  Capice?
747bb458a5SJoel Becker 	 *
7558896c4dSJoel Becker 	 * p is set above.
7670ad1ba7SJoel Becker 	 */
7758896c4dSJoel Becker 	for (; (1 << p) < (b + 1); p++)
7870ad1ba7SJoel Becker 		b++;
7970ad1ba7SJoel Becker 
8058896c4dSJoel Becker 	if (p_cache)
8158896c4dSJoel Becker 		*p_cache = p;
8258896c4dSJoel Becker 
8370ad1ba7SJoel Becker 	return b;
8470ad1ba7SJoel Becker }
8570ad1ba7SJoel Becker 
8670ad1ba7SJoel Becker /*
8770ad1ba7SJoel Becker  * This is the low level encoder function.  It can be called across
8870ad1ba7SJoel Becker  * multiple hunks just like the crc32 code.  'd' is the number of bits
8970ad1ba7SJoel Becker  * _in_this_hunk_.  nr is the bit offset of this hunk.  So, if you had
9070ad1ba7SJoel Becker  * two 512B buffers, you would do it like so:
9170ad1ba7SJoel Becker  *
9270ad1ba7SJoel Becker  * parity = ocfs2_hamming_encode(0, buf1, 512 * 8, 0);
9370ad1ba7SJoel Becker  * parity = ocfs2_hamming_encode(parity, buf2, 512 * 8, 512 * 8);
9470ad1ba7SJoel Becker  *
9570ad1ba7SJoel Becker  * If you just have one buffer, use ocfs2_hamming_encode_block().
9670ad1ba7SJoel Becker  */
ocfs2_hamming_encode(u32 parity,void * data,unsigned int d,unsigned int nr)9770ad1ba7SJoel Becker u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d, unsigned int nr)
9870ad1ba7SJoel Becker {
9958896c4dSJoel Becker 	unsigned int i, b, p = 0;
10070ad1ba7SJoel Becker 
101e798b3f8SJoel Becker 	BUG_ON(!d);
10270ad1ba7SJoel Becker 
10370ad1ba7SJoel Becker 	/*
10470ad1ba7SJoel Becker 	 * b is the hamming code bit number.  Hamming code specifies a
10570ad1ba7SJoel Becker 	 * 1-based array, but C uses 0-based.  So 'i' is for C, and 'b' is
10670ad1ba7SJoel Becker 	 * for the algorithm.
10770ad1ba7SJoel Becker 	 *
10870ad1ba7SJoel Becker 	 * The i++ in the for loop is so that the start offset passed
10970ad1ba7SJoel Becker 	 * to ocfs2_find_next_bit_set() is one greater than the previously
11070ad1ba7SJoel Becker 	 * found bit.
11170ad1ba7SJoel Becker 	 */
11270ad1ba7SJoel Becker 	for (i = 0; (i = ocfs2_find_next_bit(data, d, i)) < d; i++)
11370ad1ba7SJoel Becker 	{
11470ad1ba7SJoel Becker 		/*
11570ad1ba7SJoel Becker 		 * i is the offset in this hunk, nr + i is the total bit
11670ad1ba7SJoel Becker 		 * offset.
11770ad1ba7SJoel Becker 		 */
11858896c4dSJoel Becker 		b = calc_code_bit(nr + i, &p);
11970ad1ba7SJoel Becker 
12070ad1ba7SJoel Becker 		/*
12170ad1ba7SJoel Becker 		 * Data bits in the resultant code are checked by
12270ad1ba7SJoel Becker 		 * parity bits that are part of the bit number
12370ad1ba7SJoel Becker 		 * representation.  Huh?
12470ad1ba7SJoel Becker 		 *
1254510a5a9SAlexander A. Klimov 		 * <wikipedia href="https://en.wikipedia.org/wiki/Hamming_code">
12670ad1ba7SJoel Becker 		 * In other words, the parity bit at position 2^k
12770ad1ba7SJoel Becker 		 * checks bits in positions having bit k set in
12870ad1ba7SJoel Becker 		 * their binary representation.  Conversely, for
12970ad1ba7SJoel Becker 		 * instance, bit 13, i.e. 1101(2), is checked by
13070ad1ba7SJoel Becker 		 * bits 1000(2) = 8, 0100(2)=4 and 0001(2) = 1.
13170ad1ba7SJoel Becker 		 * </wikipedia>
13270ad1ba7SJoel Becker 		 *
13370ad1ba7SJoel Becker 		 * Note that 'k' is the _code_ bit number.  'b' in
13470ad1ba7SJoel Becker 		 * our loop.
13570ad1ba7SJoel Becker 		 */
136e798b3f8SJoel Becker 		parity ^= b;
13770ad1ba7SJoel Becker 	}
13870ad1ba7SJoel Becker 
13970ad1ba7SJoel Becker 	/* While the data buffer was treated as little endian, the
14070ad1ba7SJoel Becker 	 * return value is in host endian. */
14170ad1ba7SJoel Becker 	return parity;
14270ad1ba7SJoel Becker }
14370ad1ba7SJoel Becker 
ocfs2_hamming_encode_block(void * data,unsigned int blocksize)14470ad1ba7SJoel Becker u32 ocfs2_hamming_encode_block(void *data, unsigned int blocksize)
14570ad1ba7SJoel Becker {
14670ad1ba7SJoel Becker 	return ocfs2_hamming_encode(0, data, blocksize * 8, 0);
14770ad1ba7SJoel Becker }
14870ad1ba7SJoel Becker 
14970ad1ba7SJoel Becker /*
15070ad1ba7SJoel Becker  * Like ocfs2_hamming_encode(), this can handle hunks.  nr is the bit
15170ad1ba7SJoel Becker  * offset of the current hunk.  If bit to be fixed is not part of the
15270ad1ba7SJoel Becker  * current hunk, this does nothing.
15370ad1ba7SJoel Becker  *
15470ad1ba7SJoel Becker  * If you only have one hunk, use ocfs2_hamming_fix_block().
15570ad1ba7SJoel Becker  */
ocfs2_hamming_fix(void * data,unsigned int d,unsigned int nr,unsigned int fix)15670ad1ba7SJoel Becker void ocfs2_hamming_fix(void *data, unsigned int d, unsigned int nr,
15770ad1ba7SJoel Becker 		       unsigned int fix)
15870ad1ba7SJoel Becker {
15970ad1ba7SJoel Becker 	unsigned int i, b;
16070ad1ba7SJoel Becker 
161e798b3f8SJoel Becker 	BUG_ON(!d);
16270ad1ba7SJoel Becker 
16370ad1ba7SJoel Becker 	/*
16470ad1ba7SJoel Becker 	 * If the bit to fix has an hweight of 1, it's a parity bit.  One
16570ad1ba7SJoel Becker 	 * busted parity bit is its own error.  Nothing to do here.
16670ad1ba7SJoel Becker 	 */
16770ad1ba7SJoel Becker 	if (hweight32(fix) == 1)
16870ad1ba7SJoel Becker 		return;
16970ad1ba7SJoel Becker 
17070ad1ba7SJoel Becker 	/*
17170ad1ba7SJoel Becker 	 * nr + d is the bit right past the data hunk we're looking at.
17270ad1ba7SJoel Becker 	 * If fix after that, nothing to do
17370ad1ba7SJoel Becker 	 */
17458896c4dSJoel Becker 	if (fix >= calc_code_bit(nr + d, NULL))
17570ad1ba7SJoel Becker 		return;
17670ad1ba7SJoel Becker 
17770ad1ba7SJoel Becker 	/*
17870ad1ba7SJoel Becker 	 * nr is the offset in the data hunk we're starting at.  Let's
17970ad1ba7SJoel Becker 	 * start b at the offset in the code buffer.  See hamming_encode()
18070ad1ba7SJoel Becker 	 * for a more detailed description of 'b'.
18170ad1ba7SJoel Becker 	 */
18258896c4dSJoel Becker 	b = calc_code_bit(nr, NULL);
18370ad1ba7SJoel Becker 	/* If the fix is before this hunk, nothing to do */
18470ad1ba7SJoel Becker 	if (fix < b)
18570ad1ba7SJoel Becker 		return;
18670ad1ba7SJoel Becker 
18770ad1ba7SJoel Becker 	for (i = 0; i < d; i++, b++)
18870ad1ba7SJoel Becker 	{
18970ad1ba7SJoel Becker 		/* Skip past parity bits */
19070ad1ba7SJoel Becker 		while (hweight32(b) == 1)
19170ad1ba7SJoel Becker 			b++;
19270ad1ba7SJoel Becker 
19370ad1ba7SJoel Becker 		/*
19470ad1ba7SJoel Becker 		 * i is the offset in this data hunk.
19570ad1ba7SJoel Becker 		 * nr + i is the offset in the total data buffer.
19670ad1ba7SJoel Becker 		 * b is the offset in the total code buffer.
19770ad1ba7SJoel Becker 		 *
19870ad1ba7SJoel Becker 		 * Thus, when b == fix, bit i in the current hunk needs
19970ad1ba7SJoel Becker 		 * fixing.
20070ad1ba7SJoel Becker 		 */
20170ad1ba7SJoel Becker 		if (b == fix)
20270ad1ba7SJoel Becker 		{
20370ad1ba7SJoel Becker 			if (ocfs2_test_bit(i, data))
20470ad1ba7SJoel Becker 				ocfs2_clear_bit(i, data);
20570ad1ba7SJoel Becker 			else
20670ad1ba7SJoel Becker 				ocfs2_set_bit(i, data);
20770ad1ba7SJoel Becker 			break;
20870ad1ba7SJoel Becker 		}
20970ad1ba7SJoel Becker 	}
21070ad1ba7SJoel Becker }
21170ad1ba7SJoel Becker 
ocfs2_hamming_fix_block(void * data,unsigned int blocksize,unsigned int fix)21270ad1ba7SJoel Becker void ocfs2_hamming_fix_block(void *data, unsigned int blocksize,
21370ad1ba7SJoel Becker 			     unsigned int fix)
21470ad1ba7SJoel Becker {
21570ad1ba7SJoel Becker 	ocfs2_hamming_fix(data, blocksize * 8, 0, fix);
21670ad1ba7SJoel Becker }
21770ad1ba7SJoel Becker 
21873be192bSJoel Becker 
21973be192bSJoel Becker /*
22073be192bSJoel Becker  * Debugfs handling.
22173be192bSJoel Becker  */
22273be192bSJoel Becker 
22373be192bSJoel Becker #ifdef CONFIG_DEBUG_FS
22473be192bSJoel Becker 
blockcheck_u64_get(void * data,u64 * val)22573be192bSJoel Becker static int blockcheck_u64_get(void *data, u64 *val)
22673be192bSJoel Becker {
22773be192bSJoel Becker 	*val = *(u64 *)data;
22873be192bSJoel Becker 	return 0;
22973be192bSJoel Becker }
2301634852dSYang Li DEFINE_DEBUGFS_ATTRIBUTE(blockcheck_fops, blockcheck_u64_get, NULL, "%llu\n");
23173be192bSJoel Becker 
ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats * stats)23273be192bSJoel Becker static void ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats *stats)
23373be192bSJoel Becker {
23473be192bSJoel Becker 	if (stats) {
235e581595eSGreg Kroah-Hartman 		debugfs_remove_recursive(stats->b_debug_dir);
23673be192bSJoel Becker 		stats->b_debug_dir = NULL;
23773be192bSJoel Becker 	}
23873be192bSJoel Becker }
23973be192bSJoel Becker 
ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats * stats,struct dentry * parent)240e581595eSGreg Kroah-Hartman static void ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats *stats,
24173be192bSJoel Becker 					   struct dentry *parent)
24273be192bSJoel Becker {
2435e7a3ed9SGreg Kroah-Hartman 	struct dentry *dir;
24473be192bSJoel Becker 
2455e7a3ed9SGreg Kroah-Hartman 	dir = debugfs_create_dir("blockcheck", parent);
2465e7a3ed9SGreg Kroah-Hartman 	stats->b_debug_dir = dir;
24773be192bSJoel Becker 
2485e7a3ed9SGreg Kroah-Hartman 	debugfs_create_file("blocks_checked", S_IFREG | S_IRUSR, dir,
2495e7a3ed9SGreg Kroah-Hartman 			    &stats->b_check_count, &blockcheck_fops);
25073be192bSJoel Becker 
2515e7a3ed9SGreg Kroah-Hartman 	debugfs_create_file("checksums_failed", S_IFREG | S_IRUSR, dir,
2525e7a3ed9SGreg Kroah-Hartman 			    &stats->b_failure_count, &blockcheck_fops);
2535e7a3ed9SGreg Kroah-Hartman 
2545e7a3ed9SGreg Kroah-Hartman 	debugfs_create_file("ecc_recoveries", S_IFREG | S_IRUSR, dir,
2555e7a3ed9SGreg Kroah-Hartman 			    &stats->b_recover_count, &blockcheck_fops);
2565e7a3ed9SGreg Kroah-Hartman 
25773be192bSJoel Becker }
25873be192bSJoel Becker #else
ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats * stats,struct dentry * parent)259e581595eSGreg Kroah-Hartman static inline void ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats *stats,
26073be192bSJoel Becker 						  struct dentry *parent)
26173be192bSJoel Becker {
26273be192bSJoel Becker }
26373be192bSJoel Becker 
ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats * stats)26473be192bSJoel Becker static inline void ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats *stats)
26573be192bSJoel Becker {
26673be192bSJoel Becker }
26773be192bSJoel Becker #endif  /* CONFIG_DEBUG_FS */
26873be192bSJoel Becker 
26973be192bSJoel Becker /* Always-called wrappers for starting and stopping the debugfs files */
ocfs2_blockcheck_stats_debugfs_install(struct ocfs2_blockcheck_stats * stats,struct dentry * parent)270e581595eSGreg Kroah-Hartman void ocfs2_blockcheck_stats_debugfs_install(struct ocfs2_blockcheck_stats *stats,
27173be192bSJoel Becker 					    struct dentry *parent)
27273be192bSJoel Becker {
273e581595eSGreg Kroah-Hartman 	ocfs2_blockcheck_debug_install(stats, parent);
27473be192bSJoel Becker }
27573be192bSJoel Becker 
ocfs2_blockcheck_stats_debugfs_remove(struct ocfs2_blockcheck_stats * stats)27673be192bSJoel Becker void ocfs2_blockcheck_stats_debugfs_remove(struct ocfs2_blockcheck_stats *stats)
27773be192bSJoel Becker {
27873be192bSJoel Becker 	ocfs2_blockcheck_debug_remove(stats);
27973be192bSJoel Becker }
28073be192bSJoel Becker 
ocfs2_blockcheck_inc_check(struct ocfs2_blockcheck_stats * stats)28173be192bSJoel Becker static void ocfs2_blockcheck_inc_check(struct ocfs2_blockcheck_stats *stats)
28273be192bSJoel Becker {
28373be192bSJoel Becker 	u64 new_count;
28473be192bSJoel Becker 
28573be192bSJoel Becker 	if (!stats)
28673be192bSJoel Becker 		return;
28773be192bSJoel Becker 
28873be192bSJoel Becker 	spin_lock(&stats->b_lock);
28973be192bSJoel Becker 	stats->b_check_count++;
29073be192bSJoel Becker 	new_count = stats->b_check_count;
29173be192bSJoel Becker 	spin_unlock(&stats->b_lock);
29273be192bSJoel Becker 
29373be192bSJoel Becker 	if (!new_count)
29473be192bSJoel Becker 		mlog(ML_NOTICE, "Block check count has wrapped\n");
29573be192bSJoel Becker }
29673be192bSJoel Becker 
ocfs2_blockcheck_inc_failure(struct ocfs2_blockcheck_stats * stats)29773be192bSJoel Becker static void ocfs2_blockcheck_inc_failure(struct ocfs2_blockcheck_stats *stats)
29873be192bSJoel Becker {
29973be192bSJoel Becker 	u64 new_count;
30073be192bSJoel Becker 
30173be192bSJoel Becker 	if (!stats)
30273be192bSJoel Becker 		return;
30373be192bSJoel Becker 
30473be192bSJoel Becker 	spin_lock(&stats->b_lock);
30573be192bSJoel Becker 	stats->b_failure_count++;
30673be192bSJoel Becker 	new_count = stats->b_failure_count;
30773be192bSJoel Becker 	spin_unlock(&stats->b_lock);
30873be192bSJoel Becker 
30973be192bSJoel Becker 	if (!new_count)
31073be192bSJoel Becker 		mlog(ML_NOTICE, "Checksum failure count has wrapped\n");
31173be192bSJoel Becker }
31273be192bSJoel Becker 
ocfs2_blockcheck_inc_recover(struct ocfs2_blockcheck_stats * stats)31373be192bSJoel Becker static void ocfs2_blockcheck_inc_recover(struct ocfs2_blockcheck_stats *stats)
31473be192bSJoel Becker {
31573be192bSJoel Becker 	u64 new_count;
31673be192bSJoel Becker 
31773be192bSJoel Becker 	if (!stats)
31873be192bSJoel Becker 		return;
31973be192bSJoel Becker 
32073be192bSJoel Becker 	spin_lock(&stats->b_lock);
32173be192bSJoel Becker 	stats->b_recover_count++;
32273be192bSJoel Becker 	new_count = stats->b_recover_count;
32373be192bSJoel Becker 	spin_unlock(&stats->b_lock);
32473be192bSJoel Becker 
32573be192bSJoel Becker 	if (!new_count)
32673be192bSJoel Becker 		mlog(ML_NOTICE, "ECC recovery count has wrapped\n");
32773be192bSJoel Becker }
32873be192bSJoel Becker 
32973be192bSJoel Becker 
33073be192bSJoel Becker 
33173be192bSJoel Becker /*
33273be192bSJoel Becker  * These are the low-level APIs for using the ocfs2_block_check structure.
33373be192bSJoel Becker  */
33473be192bSJoel Becker 
33570ad1ba7SJoel Becker /*
33670ad1ba7SJoel Becker  * This function generates check information for a block.
33770ad1ba7SJoel Becker  * data is the block to be checked.  bc is a pointer to the
33870ad1ba7SJoel Becker  * ocfs2_block_check structure describing the crc32 and the ecc.
33970ad1ba7SJoel Becker  *
34070ad1ba7SJoel Becker  * bc should be a pointer inside data, as the function will
34170ad1ba7SJoel Becker  * take care of zeroing it before calculating the check information.  If
34270ad1ba7SJoel Becker  * bc does not point inside data, the caller must make sure any inline
34370ad1ba7SJoel Becker  * ocfs2_block_check structures are zeroed.
34470ad1ba7SJoel Becker  *
34570ad1ba7SJoel Becker  * The data buffer must be in on-disk endian (little endian for ocfs2).
34670ad1ba7SJoel Becker  * bc will be filled with little-endian values and will be ready to go to
34770ad1ba7SJoel Becker  * disk.
34870ad1ba7SJoel Becker  */
ocfs2_block_check_compute(void * data,size_t blocksize,struct ocfs2_block_check * bc)34970ad1ba7SJoel Becker void ocfs2_block_check_compute(void *data, size_t blocksize,
35070ad1ba7SJoel Becker 			       struct ocfs2_block_check *bc)
35170ad1ba7SJoel Becker {
35270ad1ba7SJoel Becker 	u32 crc;
35370ad1ba7SJoel Becker 	u32 ecc;
35470ad1ba7SJoel Becker 
35570ad1ba7SJoel Becker 	memset(bc, 0, sizeof(struct ocfs2_block_check));
35670ad1ba7SJoel Becker 
35770ad1ba7SJoel Becker 	crc = crc32_le(~0, data, blocksize);
35870ad1ba7SJoel Becker 	ecc = ocfs2_hamming_encode_block(data, blocksize);
35970ad1ba7SJoel Becker 
36070ad1ba7SJoel Becker 	/*
36170ad1ba7SJoel Becker 	 * No ecc'd ocfs2 structure is larger than 4K, so ecc will be no
36270ad1ba7SJoel Becker 	 * larger than 16 bits.
36370ad1ba7SJoel Becker 	 */
3644be929beSAlexey Dobriyan 	BUG_ON(ecc > USHRT_MAX);
36570ad1ba7SJoel Becker 
36670ad1ba7SJoel Becker 	bc->bc_crc32e = cpu_to_le32(crc);
36770ad1ba7SJoel Becker 	bc->bc_ecc = cpu_to_le16((u16)ecc);
36870ad1ba7SJoel Becker }
36970ad1ba7SJoel Becker 
37070ad1ba7SJoel Becker /*
37170ad1ba7SJoel Becker  * This function validates existing check information.  Like _compute,
37270ad1ba7SJoel Becker  * the function will take care of zeroing bc before calculating check codes.
37370ad1ba7SJoel Becker  * If bc is not a pointer inside data, the caller must have zeroed any
37470ad1ba7SJoel Becker  * inline ocfs2_block_check structures.
37570ad1ba7SJoel Becker  *
37670ad1ba7SJoel Becker  * Again, the data passed in should be the on-disk endian.
37770ad1ba7SJoel Becker  */
ocfs2_block_check_validate(void * data,size_t blocksize,struct ocfs2_block_check * bc,struct ocfs2_blockcheck_stats * stats)37870ad1ba7SJoel Becker int ocfs2_block_check_validate(void *data, size_t blocksize,
37973be192bSJoel Becker 			       struct ocfs2_block_check *bc,
38073be192bSJoel Becker 			       struct ocfs2_blockcheck_stats *stats)
38170ad1ba7SJoel Becker {
38270ad1ba7SJoel Becker 	int rc = 0;
3831db5df98SAl Viro 	u32 bc_crc32e;
3841db5df98SAl Viro 	u16 bc_ecc;
38570ad1ba7SJoel Becker 	u32 crc, ecc;
38670ad1ba7SJoel Becker 
38773be192bSJoel Becker 	ocfs2_blockcheck_inc_check(stats);
38873be192bSJoel Becker 
3891db5df98SAl Viro 	bc_crc32e = le32_to_cpu(bc->bc_crc32e);
3901db5df98SAl Viro 	bc_ecc = le16_to_cpu(bc->bc_ecc);
39170ad1ba7SJoel Becker 
39270ad1ba7SJoel Becker 	memset(bc, 0, sizeof(struct ocfs2_block_check));
39370ad1ba7SJoel Becker 
39470ad1ba7SJoel Becker 	/* Fast path - if the crc32 validates, we're good to go */
39570ad1ba7SJoel Becker 	crc = crc32_le(~0, data, blocksize);
3961db5df98SAl Viro 	if (crc == bc_crc32e)
39770ad1ba7SJoel Becker 		goto out;
39870ad1ba7SJoel Becker 
39973be192bSJoel Becker 	ocfs2_blockcheck_inc_failure(stats);
400d6b32bbbSJoel Becker 	mlog(ML_ERROR,
401dc696aceSSunil Mushran 	     "CRC32 failed: stored: 0x%x, computed 0x%x. Applying ECC.\n",
4021db5df98SAl Viro 	     (unsigned int)bc_crc32e, (unsigned int)crc);
403d6b32bbbSJoel Becker 
40470ad1ba7SJoel Becker 	/* Ok, try ECC fixups */
40570ad1ba7SJoel Becker 	ecc = ocfs2_hamming_encode_block(data, blocksize);
4061db5df98SAl Viro 	ocfs2_hamming_fix_block(data, blocksize, ecc ^ bc_ecc);
40770ad1ba7SJoel Becker 
40870ad1ba7SJoel Becker 	/* And check the crc32 again */
40970ad1ba7SJoel Becker 	crc = crc32_le(~0, data, blocksize);
4101db5df98SAl Viro 	if (crc == bc_crc32e) {
41173be192bSJoel Becker 		ocfs2_blockcheck_inc_recover(stats);
41270ad1ba7SJoel Becker 		goto out;
41373be192bSJoel Becker 	}
41470ad1ba7SJoel Becker 
415dc696aceSSunil Mushran 	mlog(ML_ERROR, "Fixed CRC32 failed: stored: 0x%x, computed 0x%x\n",
4161db5df98SAl Viro 	     (unsigned int)bc_crc32e, (unsigned int)crc);
417d6b32bbbSJoel Becker 
41870ad1ba7SJoel Becker 	rc = -EIO;
41970ad1ba7SJoel Becker 
42070ad1ba7SJoel Becker out:
4211db5df98SAl Viro 	bc->bc_crc32e = cpu_to_le32(bc_crc32e);
4221db5df98SAl Viro 	bc->bc_ecc = cpu_to_le16(bc_ecc);
42370ad1ba7SJoel Becker 
42470ad1ba7SJoel Becker 	return rc;
42570ad1ba7SJoel Becker }
42670ad1ba7SJoel Becker 
42770ad1ba7SJoel Becker /*
42870ad1ba7SJoel Becker  * This function generates check information for a list of buffer_heads.
42970ad1ba7SJoel Becker  * bhs is the blocks to be checked.  bc is a pointer to the
43070ad1ba7SJoel Becker  * ocfs2_block_check structure describing the crc32 and the ecc.
43170ad1ba7SJoel Becker  *
43270ad1ba7SJoel Becker  * bc should be a pointer inside data, as the function will
43370ad1ba7SJoel Becker  * take care of zeroing it before calculating the check information.  If
43470ad1ba7SJoel Becker  * bc does not point inside data, the caller must make sure any inline
43570ad1ba7SJoel Becker  * ocfs2_block_check structures are zeroed.
43670ad1ba7SJoel Becker  *
43770ad1ba7SJoel Becker  * The data buffer must be in on-disk endian (little endian for ocfs2).
43870ad1ba7SJoel Becker  * bc will be filled with little-endian values and will be ready to go to
43970ad1ba7SJoel Becker  * disk.
44070ad1ba7SJoel Becker  */
ocfs2_block_check_compute_bhs(struct buffer_head ** bhs,int nr,struct ocfs2_block_check * bc)44170ad1ba7SJoel Becker void ocfs2_block_check_compute_bhs(struct buffer_head **bhs, int nr,
44270ad1ba7SJoel Becker 				   struct ocfs2_block_check *bc)
44370ad1ba7SJoel Becker {
44470ad1ba7SJoel Becker 	int i;
44570ad1ba7SJoel Becker 	u32 crc, ecc;
44670ad1ba7SJoel Becker 
44770ad1ba7SJoel Becker 	BUG_ON(nr < 0);
44870ad1ba7SJoel Becker 
44970ad1ba7SJoel Becker 	if (!nr)
45070ad1ba7SJoel Becker 		return;
45170ad1ba7SJoel Becker 
45270ad1ba7SJoel Becker 	memset(bc, 0, sizeof(struct ocfs2_block_check));
45370ad1ba7SJoel Becker 
45470ad1ba7SJoel Becker 	for (i = 0, crc = ~0, ecc = 0; i < nr; i++) {
45570ad1ba7SJoel Becker 		crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
45670ad1ba7SJoel Becker 		/*
45770ad1ba7SJoel Becker 		 * The number of bits in a buffer is obviously b_size*8.
45870ad1ba7SJoel Becker 		 * The offset of this buffer is b_size*i, so the bit offset
45970ad1ba7SJoel Becker 		 * of this buffer is b_size*8*i.
46070ad1ba7SJoel Becker 		 */
46170ad1ba7SJoel Becker 		ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data,
46270ad1ba7SJoel Becker 						bhs[i]->b_size * 8,
46370ad1ba7SJoel Becker 						bhs[i]->b_size * 8 * i);
46470ad1ba7SJoel Becker 	}
46570ad1ba7SJoel Becker 
46670ad1ba7SJoel Becker 	/*
46770ad1ba7SJoel Becker 	 * No ecc'd ocfs2 structure is larger than 4K, so ecc will be no
46870ad1ba7SJoel Becker 	 * larger than 16 bits.
46970ad1ba7SJoel Becker 	 */
4704be929beSAlexey Dobriyan 	BUG_ON(ecc > USHRT_MAX);
47170ad1ba7SJoel Becker 
47270ad1ba7SJoel Becker 	bc->bc_crc32e = cpu_to_le32(crc);
47370ad1ba7SJoel Becker 	bc->bc_ecc = cpu_to_le16((u16)ecc);
47470ad1ba7SJoel Becker }
47570ad1ba7SJoel Becker 
47670ad1ba7SJoel Becker /*
47770ad1ba7SJoel Becker  * This function validates existing check information on a list of
47870ad1ba7SJoel Becker  * buffer_heads.  Like _compute_bhs, the function will take care of
47970ad1ba7SJoel Becker  * zeroing bc before calculating check codes.  If bc is not a pointer
48070ad1ba7SJoel Becker  * inside data, the caller must have zeroed any inline
48170ad1ba7SJoel Becker  * ocfs2_block_check structures.
48270ad1ba7SJoel Becker  *
48370ad1ba7SJoel Becker  * Again, the data passed in should be the on-disk endian.
48470ad1ba7SJoel Becker  */
ocfs2_block_check_validate_bhs(struct buffer_head ** bhs,int nr,struct ocfs2_block_check * bc,struct ocfs2_blockcheck_stats * stats)48570ad1ba7SJoel Becker int ocfs2_block_check_validate_bhs(struct buffer_head **bhs, int nr,
48673be192bSJoel Becker 				   struct ocfs2_block_check *bc,
48773be192bSJoel Becker 				   struct ocfs2_blockcheck_stats *stats)
48870ad1ba7SJoel Becker {
48970ad1ba7SJoel Becker 	int i, rc = 0;
4901db5df98SAl Viro 	u32 bc_crc32e;
4911db5df98SAl Viro 	u16 bc_ecc;
49270ad1ba7SJoel Becker 	u32 crc, ecc, fix;
49370ad1ba7SJoel Becker 
49470ad1ba7SJoel Becker 	BUG_ON(nr < 0);
49570ad1ba7SJoel Becker 
49670ad1ba7SJoel Becker 	if (!nr)
49770ad1ba7SJoel Becker 		return 0;
49870ad1ba7SJoel Becker 
49973be192bSJoel Becker 	ocfs2_blockcheck_inc_check(stats);
50073be192bSJoel Becker 
5011db5df98SAl Viro 	bc_crc32e = le32_to_cpu(bc->bc_crc32e);
5021db5df98SAl Viro 	bc_ecc = le16_to_cpu(bc->bc_ecc);
50370ad1ba7SJoel Becker 
50470ad1ba7SJoel Becker 	memset(bc, 0, sizeof(struct ocfs2_block_check));
50570ad1ba7SJoel Becker 
50670ad1ba7SJoel Becker 	/* Fast path - if the crc32 validates, we're good to go */
50770ad1ba7SJoel Becker 	for (i = 0, crc = ~0; i < nr; i++)
50870ad1ba7SJoel Becker 		crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
5091db5df98SAl Viro 	if (crc == bc_crc32e)
51070ad1ba7SJoel Becker 		goto out;
51170ad1ba7SJoel Becker 
51273be192bSJoel Becker 	ocfs2_blockcheck_inc_failure(stats);
51370ad1ba7SJoel Becker 	mlog(ML_ERROR,
51470ad1ba7SJoel Becker 	     "CRC32 failed: stored: %u, computed %u.  Applying ECC.\n",
5151db5df98SAl Viro 	     (unsigned int)bc_crc32e, (unsigned int)crc);
51670ad1ba7SJoel Becker 
51770ad1ba7SJoel Becker 	/* Ok, try ECC fixups */
51870ad1ba7SJoel Becker 	for (i = 0, ecc = 0; i < nr; i++) {
51970ad1ba7SJoel Becker 		/*
52070ad1ba7SJoel Becker 		 * The number of bits in a buffer is obviously b_size*8.
52170ad1ba7SJoel Becker 		 * The offset of this buffer is b_size*i, so the bit offset
52270ad1ba7SJoel Becker 		 * of this buffer is b_size*8*i.
52370ad1ba7SJoel Becker 		 */
52470ad1ba7SJoel Becker 		ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data,
52570ad1ba7SJoel Becker 						bhs[i]->b_size * 8,
52670ad1ba7SJoel Becker 						bhs[i]->b_size * 8 * i);
52770ad1ba7SJoel Becker 	}
5281db5df98SAl Viro 	fix = ecc ^ bc_ecc;
52970ad1ba7SJoel Becker 	for (i = 0; i < nr; i++) {
53070ad1ba7SJoel Becker 		/*
53170ad1ba7SJoel Becker 		 * Try the fix against each buffer.  It will only affect
53270ad1ba7SJoel Becker 		 * one of them.
53370ad1ba7SJoel Becker 		 */
53470ad1ba7SJoel Becker 		ocfs2_hamming_fix(bhs[i]->b_data, bhs[i]->b_size * 8,
53570ad1ba7SJoel Becker 				  bhs[i]->b_size * 8 * i, fix);
53670ad1ba7SJoel Becker 	}
53770ad1ba7SJoel Becker 
53870ad1ba7SJoel Becker 	/* And check the crc32 again */
53970ad1ba7SJoel Becker 	for (i = 0, crc = ~0; i < nr; i++)
54070ad1ba7SJoel Becker 		crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
5411db5df98SAl Viro 	if (crc == bc_crc32e) {
54273be192bSJoel Becker 		ocfs2_blockcheck_inc_recover(stats);
54370ad1ba7SJoel Becker 		goto out;
54473be192bSJoel Becker 	}
54570ad1ba7SJoel Becker 
54670ad1ba7SJoel Becker 	mlog(ML_ERROR, "Fixed CRC32 failed: stored: %u, computed %u\n",
5471db5df98SAl Viro 	     (unsigned int)bc_crc32e, (unsigned int)crc);
54870ad1ba7SJoel Becker 
54970ad1ba7SJoel Becker 	rc = -EIO;
55070ad1ba7SJoel Becker 
55170ad1ba7SJoel Becker out:
5521db5df98SAl Viro 	bc->bc_crc32e = cpu_to_le32(bc_crc32e);
5531db5df98SAl Viro 	bc->bc_ecc = cpu_to_le16(bc_ecc);
55470ad1ba7SJoel Becker 
55570ad1ba7SJoel Becker 	return rc;
55670ad1ba7SJoel Becker }
55770ad1ba7SJoel Becker 
55870ad1ba7SJoel Becker /*
55970ad1ba7SJoel Becker  * These are the main API.  They check the superblock flag before
56070ad1ba7SJoel Becker  * calling the underlying operations.
56170ad1ba7SJoel Becker  *
56270ad1ba7SJoel Becker  * They expect the buffer(s) to be in disk format.
56370ad1ba7SJoel Becker  */
ocfs2_compute_meta_ecc(struct super_block * sb,void * data,struct ocfs2_block_check * bc)56470ad1ba7SJoel Becker void ocfs2_compute_meta_ecc(struct super_block *sb, void *data,
56570ad1ba7SJoel Becker 			    struct ocfs2_block_check *bc)
56670ad1ba7SJoel Becker {
56770ad1ba7SJoel Becker 	if (ocfs2_meta_ecc(OCFS2_SB(sb)))
56870ad1ba7SJoel Becker 		ocfs2_block_check_compute(data, sb->s_blocksize, bc);
56970ad1ba7SJoel Becker }
57070ad1ba7SJoel Becker 
ocfs2_validate_meta_ecc(struct super_block * sb,void * data,struct ocfs2_block_check * bc)57170ad1ba7SJoel Becker int ocfs2_validate_meta_ecc(struct super_block *sb, void *data,
57270ad1ba7SJoel Becker 			    struct ocfs2_block_check *bc)
57370ad1ba7SJoel Becker {
57470ad1ba7SJoel Becker 	int rc = 0;
57573be192bSJoel Becker 	struct ocfs2_super *osb = OCFS2_SB(sb);
57670ad1ba7SJoel Becker 
57773be192bSJoel Becker 	if (ocfs2_meta_ecc(osb))
57873be192bSJoel Becker 		rc = ocfs2_block_check_validate(data, sb->s_blocksize, bc,
57973be192bSJoel Becker 						&osb->osb_ecc_stats);
58070ad1ba7SJoel Becker 
58170ad1ba7SJoel Becker 	return rc;
58270ad1ba7SJoel Becker }
58370ad1ba7SJoel Becker 
ocfs2_compute_meta_ecc_bhs(struct super_block * sb,struct buffer_head ** bhs,int nr,struct ocfs2_block_check * bc)58470ad1ba7SJoel Becker void ocfs2_compute_meta_ecc_bhs(struct super_block *sb,
58570ad1ba7SJoel Becker 				struct buffer_head **bhs, int nr,
58670ad1ba7SJoel Becker 				struct ocfs2_block_check *bc)
58770ad1ba7SJoel Becker {
58870ad1ba7SJoel Becker 	if (ocfs2_meta_ecc(OCFS2_SB(sb)))
58970ad1ba7SJoel Becker 		ocfs2_block_check_compute_bhs(bhs, nr, bc);
59070ad1ba7SJoel Becker }
59170ad1ba7SJoel Becker 
ocfs2_validate_meta_ecc_bhs(struct super_block * sb,struct buffer_head ** bhs,int nr,struct ocfs2_block_check * bc)59270ad1ba7SJoel Becker int ocfs2_validate_meta_ecc_bhs(struct super_block *sb,
59370ad1ba7SJoel Becker 				struct buffer_head **bhs, int nr,
59470ad1ba7SJoel Becker 				struct ocfs2_block_check *bc)
59570ad1ba7SJoel Becker {
59670ad1ba7SJoel Becker 	int rc = 0;
59773be192bSJoel Becker 	struct ocfs2_super *osb = OCFS2_SB(sb);
59870ad1ba7SJoel Becker 
59973be192bSJoel Becker 	if (ocfs2_meta_ecc(osb))
60073be192bSJoel Becker 		rc = ocfs2_block_check_validate_bhs(bhs, nr, bc,
60173be192bSJoel Becker 						    &osb->osb_ecc_stats);
60270ad1ba7SJoel Becker 
60370ad1ba7SJoel Becker 	return rc;
60470ad1ba7SJoel Becker }
60570ad1ba7SJoel Becker 
606