aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fs/ocfs2/blockcheck.c61
1 files changed, 19 insertions, 42 deletions
diff --git a/fs/ocfs2/blockcheck.c b/fs/ocfs2/blockcheck.c
index f102ec939c90..2a947c44e594 100644
--- a/fs/ocfs2/blockcheck.c
+++ b/fs/ocfs2/blockcheck.c
@@ -41,34 +41,6 @@
41 41
42 42
43/* 43/*
44 * Find the log base 2 of 32-bit v.
45 *
46 * Algorithm found on http://graphics.stanford.edu/~seander/bithacks.html,
47 * by Sean Eron Anderson. Code on the page is in the public domain unless
48 * otherwise noted.
49 *
50 * This particular algorithm is credited to Eric Cole.
51 */
52static int find_highest_bit_set(unsigned int v)
53{
54
55 static const int MultiplyDeBruijnBitPosition[32] =
56 {
57 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
58 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
59 };
60
61 v |= v >> 1; /* first round down to power of 2 */
62 v |= v >> 2;
63 v |= v >> 4;
64 v |= v >> 8;
65 v |= v >> 16;
66 v = (v >> 1) + 1;
67
68 return MultiplyDeBruijnBitPosition[(u32)(v * 0x077CB531UL) >> 27];
69}
70
71/*
72 * Calculate the bit offset in the hamming code buffer based on the bit's 44 * Calculate the bit offset in the hamming code buffer based on the bit's
73 * offset in the data buffer. Since the hamming code reserves all 45 * offset in the data buffer. Since the hamming code reserves all
74 * power-of-two bits for parity, the data bit number and the code bit 46 * power-of-two bits for parity, the data bit number and the code bit
@@ -81,10 +53,14 @@ static int find_highest_bit_set(unsigned int v)
81 * so it's a parity bit. 2 is a power of two (2^1), so it's a parity bit. 53 * so it's a parity bit. 2 is a power of two (2^1), so it's a parity bit.
82 * 3 is not a power of two. So bit 1 of the data buffer ends up as bit 3 54 * 3 is not a power of two. So bit 1 of the data buffer ends up as bit 3
83 * in the code buffer. 55 * in the code buffer.
56 *
57 * The caller can pass in *p if it wants to keep track of the most recent
58 * number of parity bits added. This allows the function to start the
59 * calculation at the last place.
84 */ 60 */
85static unsigned int calc_code_bit(unsigned int i) 61static unsigned int calc_code_bit(unsigned int i, unsigned int *p_cache)
86{ 62{
87 unsigned int b, p; 63 unsigned int b, p = 0;
88 64
89 /* 65 /*
90 * Data bits are 0-based, but we're talking code bits, which 66 * Data bits are 0-based, but we're talking code bits, which
@@ -92,24 +68,25 @@ static unsigned int calc_code_bit(unsigned int i)
92 */ 68 */
93 b = i + 1; 69 b = i + 1;
94 70
95 /* 71 /* Use the cache if it is there */
96 * As a cheat, we know that all bits below b's highest bit must be 72 if (p_cache)
97 * parity bits, so we can start there. 73 p = *p_cache;
98 */
99 p = find_highest_bit_set(b);
100 b += p; 74 b += p;
101 75
102 /* 76 /*
103 * For every power of two below our bit number, bump our bit. 77 * For every power of two below our bit number, bump our bit.
104 * 78 *
105 * We compare with (b + 1) becuase we have to compare with what b 79 * We compare with (b + 1) because we have to compare with what b
106 * would be _if_ it were bumped up by the parity bit. Capice? 80 * would be _if_ it were bumped up by the parity bit. Capice?
107 * 81 *
108 * We start p at 2^p because of the cheat above. 82 * p is set above.
109 */ 83 */
110 for (p = (1 << p); p < (b + 1); p <<= 1) 84 for (; (1 << p) < (b + 1); p++)
111 b++; 85 b++;
112 86
87 if (p_cache)
88 *p_cache = p;
89
113 return b; 90 return b;
114} 91}
115 92
@@ -126,7 +103,7 @@ static unsigned int calc_code_bit(unsigned int i)
126 */ 103 */
127u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d, unsigned int nr) 104u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d, unsigned int nr)
128{ 105{
129 unsigned int i, b; 106 unsigned int i, b, p = 0;
130 107
131 BUG_ON(!d); 108 BUG_ON(!d);
132 109
@@ -145,7 +122,7 @@ u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d, unsigned int nr
145 * i is the offset in this hunk, nr + i is the total bit 122 * i is the offset in this hunk, nr + i is the total bit
146 * offset. 123 * offset.
147 */ 124 */
148 b = calc_code_bit(nr + i); 125 b = calc_code_bit(nr + i, &p);
149 126
150 /* 127 /*
151 * Data bits in the resultant code are checked by 128 * Data bits in the resultant code are checked by
@@ -201,7 +178,7 @@ void ocfs2_hamming_fix(void *data, unsigned int d, unsigned int nr,
201 * nr + d is the bit right past the data hunk we're looking at. 178 * nr + d is the bit right past the data hunk we're looking at.
202 * If fix after that, nothing to do 179 * If fix after that, nothing to do
203 */ 180 */
204 if (fix >= calc_code_bit(nr + d)) 181 if (fix >= calc_code_bit(nr + d, NULL))
205 return; 182 return;
206 183
207 /* 184 /*
@@ -209,7 +186,7 @@ void ocfs2_hamming_fix(void *data, unsigned int d, unsigned int nr,
209 * start b at the offset in the code buffer. See hamming_encode() 186 * start b at the offset in the code buffer. See hamming_encode()
210 * for a more detailed description of 'b'. 187 * for a more detailed description of 'b'.
211 */ 188 */
212 b = calc_code_bit(nr); 189 b = calc_code_bit(nr, NULL);
213 /* If the fix is before this hunk, nothing to do */ 190 /* If the fix is before this hunk, nothing to do */
214 if (fix < b) 191 if (fix < b)
215 return; 192 return;