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