Blame view

fs/ocfs2/blockcheck.c 16.3 KB
70ad1ba7b   Joel Becker   ocfs2: Add the un...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  /* -*- mode: c; c-basic-offset: 8; -*-
   * vim: noexpandtab sw=8 ts=8 sts=0:
   *
   * blockcheck.c
   *
   * Checksum and ECC codes for the OCFS2 userspace library.
   *
   * Copyright (C) 2006, 2008 Oracle.  All rights reserved.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public
   * License, version 2, as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   */
  
  #include <linux/kernel.h>
  #include <linux/types.h>
  #include <linux/crc32.h>
  #include <linux/buffer_head.h>
  #include <linux/bitops.h>
73be192b1   Joel Becker   ocfs2: Add statis...
25
26
27
  #include <linux/debugfs.h>
  #include <linux/module.h>
  #include <linux/fs.h>
70ad1ba7b   Joel Becker   ocfs2: Add the un...
28
  #include <asm/byteorder.h>
d6b32bbb3   Joel Becker   ocfs2: block read...
29
  #include <cluster/masklog.h>
70ad1ba7b   Joel Becker   ocfs2: Add the un...
30
31
32
  #include "ocfs2.h"
  
  #include "blockcheck.h"
70ad1ba7b   Joel Becker   ocfs2: Add the un...
33
34
35
36
37
38
39
  /*
   * We use the following conventions:
   *
   * d = # data bits
   * p = # parity bits
   * c = # total code bits (d + p)
   */
70ad1ba7b   Joel Becker   ocfs2: Add the un...
40

7bb458a58   Joel Becker   ocfs2: Another ha...
41
42
  
  /*
70ad1ba7b   Joel Becker   ocfs2: Add the un...
43
44
45
   * Calculate the bit offset in the hamming code buffer based on the bit's
   * offset in the data buffer.  Since the hamming code reserves all
   * power-of-two bits for parity, the data bit number and the code bit
bf48aabb8   Uwe Kleine-König   tree-wide: fix ty...
46
   * number are offset by all the parity bits beforehand.
70ad1ba7b   Joel Becker   ocfs2: Add the un...
47
48
49
50
51
52
53
54
   *
   * Recall that bit numbers in hamming code are 1-based.  This function
   * takes the 0-based data bit from the caller.
   *
   * An example.  Take bit 1 of the data buffer.  1 is a power of two (2^0),
   * so it's a parity bit.  2 is a power of two (2^1), so it's a parity bit.
   * 3 is not a power of two.  So bit 1 of the data buffer ends up as bit 3
   * in the code buffer.
58896c4d0   Joel Becker   ocfs2: One more h...
55
56
57
58
   *
   * The caller can pass in *p if it wants to keep track of the most recent
   * number of parity bits added.  This allows the function to start the
   * calculation at the last place.
70ad1ba7b   Joel Becker   ocfs2: Add the un...
59
   */
58896c4d0   Joel Becker   ocfs2: One more h...
60
  static unsigned int calc_code_bit(unsigned int i, unsigned int *p_cache)
70ad1ba7b   Joel Becker   ocfs2: Add the un...
61
  {
58896c4d0   Joel Becker   ocfs2: One more h...
62
  	unsigned int b, p = 0;
70ad1ba7b   Joel Becker   ocfs2: Add the un...
63
64
65
66
67
68
  
  	/*
  	 * Data bits are 0-based, but we're talking code bits, which
  	 * are 1-based.
  	 */
  	b = i + 1;
58896c4d0   Joel Becker   ocfs2: One more h...
69
70
71
  	/* Use the cache if it is there */
  	if (p_cache)
  		p = *p_cache;
7bb458a58   Joel Becker   ocfs2: Another ha...
72
73
74
          b += p;
  
  	/*
70ad1ba7b   Joel Becker   ocfs2: Add the un...
75
76
  	 * For every power of two below our bit number, bump our bit.
  	 *
58896c4d0   Joel Becker   ocfs2: One more h...
77
  	 * We compare with (b + 1) because we have to compare with what b
70ad1ba7b   Joel Becker   ocfs2: Add the un...
78
  	 * would be _if_ it were bumped up by the parity bit.  Capice?
7bb458a58   Joel Becker   ocfs2: Another ha...
79
  	 *
58896c4d0   Joel Becker   ocfs2: One more h...
80
  	 * p is set above.
70ad1ba7b   Joel Becker   ocfs2: Add the un...
81
  	 */
58896c4d0   Joel Becker   ocfs2: One more h...
82
  	for (; (1 << p) < (b + 1); p++)
70ad1ba7b   Joel Becker   ocfs2: Add the un...
83
  		b++;
58896c4d0   Joel Becker   ocfs2: One more h...
84
85
  	if (p_cache)
  		*p_cache = p;
70ad1ba7b   Joel Becker   ocfs2: Add the un...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  	return b;
  }
  
  /*
   * This is the low level encoder function.  It can be called across
   * multiple hunks just like the crc32 code.  'd' is the number of bits
   * _in_this_hunk_.  nr is the bit offset of this hunk.  So, if you had
   * two 512B buffers, you would do it like so:
   *
   * parity = ocfs2_hamming_encode(0, buf1, 512 * 8, 0);
   * parity = ocfs2_hamming_encode(parity, buf2, 512 * 8, 512 * 8);
   *
   * If you just have one buffer, use ocfs2_hamming_encode_block().
   */
  u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d, unsigned int nr)
  {
58896c4d0   Joel Becker   ocfs2: One more h...
102
  	unsigned int i, b, p = 0;
70ad1ba7b   Joel Becker   ocfs2: Add the un...
103

e798b3f8a   Joel Becker   ocfs2: Don't hand...
104
  	BUG_ON(!d);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  
  	/*
  	 * b is the hamming code bit number.  Hamming code specifies a
  	 * 1-based array, but C uses 0-based.  So 'i' is for C, and 'b' is
  	 * for the algorithm.
  	 *
  	 * The i++ in the for loop is so that the start offset passed
  	 * to ocfs2_find_next_bit_set() is one greater than the previously
  	 * found bit.
  	 */
  	for (i = 0; (i = ocfs2_find_next_bit(data, d, i)) < d; i++)
  	{
  		/*
  		 * i is the offset in this hunk, nr + i is the total bit
  		 * offset.
  		 */
58896c4d0   Joel Becker   ocfs2: One more h...
121
  		b = calc_code_bit(nr + i, &p);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
122

e798b3f8a   Joel Becker   ocfs2: Don't hand...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  		/*
  		 * Data bits in the resultant code are checked by
  		 * parity bits that are part of the bit number
  		 * representation.  Huh?
  		 *
  		 * <wikipedia href="http://en.wikipedia.org/wiki/Hamming_code">
  		 * In other words, the parity bit at position 2^k
  		 * checks bits in positions having bit k set in
  		 * their binary representation.  Conversely, for
  		 * instance, bit 13, i.e. 1101(2), is checked by
  		 * bits 1000(2) = 8, 0100(2)=4 and 0001(2) = 1.
  		 * </wikipedia>
  		 *
  		 * Note that 'k' is the _code_ bit number.  'b' in
  		 * our loop.
  		 */
  		parity ^= b;
70ad1ba7b   Joel Becker   ocfs2: Add the un...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  	}
  
  	/* While the data buffer was treated as little endian, the
  	 * return value is in host endian. */
  	return parity;
  }
  
  u32 ocfs2_hamming_encode_block(void *data, unsigned int blocksize)
  {
  	return ocfs2_hamming_encode(0, data, blocksize * 8, 0);
  }
  
  /*
   * Like ocfs2_hamming_encode(), this can handle hunks.  nr is the bit
   * offset of the current hunk.  If bit to be fixed is not part of the
   * current hunk, this does nothing.
   *
   * If you only have one hunk, use ocfs2_hamming_fix_block().
   */
  void ocfs2_hamming_fix(void *data, unsigned int d, unsigned int nr,
  		       unsigned int fix)
  {
70ad1ba7b   Joel Becker   ocfs2: Add the un...
162
  	unsigned int i, b;
e798b3f8a   Joel Becker   ocfs2: Don't hand...
163
  	BUG_ON(!d);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
164
165
166
167
168
169
170
171
172
173
174
175
  
  	/*
  	 * If the bit to fix has an hweight of 1, it's a parity bit.  One
  	 * busted parity bit is its own error.  Nothing to do here.
  	 */
  	if (hweight32(fix) == 1)
  		return;
  
  	/*
  	 * nr + d is the bit right past the data hunk we're looking at.
  	 * If fix after that, nothing to do
  	 */
58896c4d0   Joel Becker   ocfs2: One more h...
176
  	if (fix >= calc_code_bit(nr + d, NULL))
70ad1ba7b   Joel Becker   ocfs2: Add the un...
177
178
179
180
181
182
183
  		return;
  
  	/*
  	 * nr is the offset in the data hunk we're starting at.  Let's
  	 * start b at the offset in the code buffer.  See hamming_encode()
  	 * for a more detailed description of 'b'.
  	 */
58896c4d0   Joel Becker   ocfs2: One more h...
184
  	b = calc_code_bit(nr, NULL);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  	/* If the fix is before this hunk, nothing to do */
  	if (fix < b)
  		return;
  
  	for (i = 0; i < d; i++, b++)
  	{
  		/* Skip past parity bits */
  		while (hweight32(b) == 1)
  			b++;
  
  		/*
  		 * i is the offset in this data hunk.
  		 * nr + i is the offset in the total data buffer.
  		 * b is the offset in the total code buffer.
  		 *
  		 * Thus, when b == fix, bit i in the current hunk needs
  		 * fixing.
  		 */
  		if (b == fix)
  		{
  			if (ocfs2_test_bit(i, data))
  				ocfs2_clear_bit(i, data);
  			else
  				ocfs2_set_bit(i, data);
  			break;
  		}
  	}
  }
  
  void ocfs2_hamming_fix_block(void *data, unsigned int blocksize,
  			     unsigned int fix)
  {
  	ocfs2_hamming_fix(data, blocksize * 8, 0, fix);
  }
73be192b1   Joel Becker   ocfs2: Add statis...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  
  /*
   * Debugfs handling.
   */
  
  #ifdef CONFIG_DEBUG_FS
  
  static int blockcheck_u64_get(void *data, u64 *val)
  {
  	*val = *(u64 *)data;
  	return 0;
  }
  DEFINE_SIMPLE_ATTRIBUTE(blockcheck_fops, blockcheck_u64_get, NULL, "%llu
  ");
  
  static struct dentry *blockcheck_debugfs_create(const char *name,
  						struct dentry *parent,
  						u64 *value)
  {
  	return debugfs_create_file(name, S_IFREG | S_IRUSR, parent, value,
  				   &blockcheck_fops);
  }
  
  static void ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats *stats)
  {
  	if (stats) {
  		debugfs_remove(stats->b_debug_check);
  		stats->b_debug_check = NULL;
  		debugfs_remove(stats->b_debug_failure);
  		stats->b_debug_failure = NULL;
  		debugfs_remove(stats->b_debug_recover);
  		stats->b_debug_recover = NULL;
  		debugfs_remove(stats->b_debug_dir);
  		stats->b_debug_dir = NULL;
  	}
  }
  
  static int ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats *stats,
  					  struct dentry *parent)
  {
  	int rc = -EINVAL;
  
  	if (!stats)
  		goto out;
  
  	stats->b_debug_dir = debugfs_create_dir("blockcheck", parent);
  	if (!stats->b_debug_dir)
  		goto out;
  
  	stats->b_debug_check =
  		blockcheck_debugfs_create("blocks_checked",
  					  stats->b_debug_dir,
  					  &stats->b_check_count);
  
  	stats->b_debug_failure =
  		blockcheck_debugfs_create("checksums_failed",
  					  stats->b_debug_dir,
  					  &stats->b_failure_count);
  
  	stats->b_debug_recover =
  		blockcheck_debugfs_create("ecc_recoveries",
  					  stats->b_debug_dir,
  					  &stats->b_recover_count);
  	if (stats->b_debug_check && stats->b_debug_failure &&
  	    stats->b_debug_recover)
  		rc = 0;
  
  out:
  	if (rc)
  		ocfs2_blockcheck_debug_remove(stats);
  	return rc;
  }
  #else
  static inline int ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats *stats,
  						 struct dentry *parent)
  {
  	return 0;
  }
  
  static inline void ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats *stats)
  {
  }
  #endif  /* CONFIG_DEBUG_FS */
  
  /* Always-called wrappers for starting and stopping the debugfs files */
  int ocfs2_blockcheck_stats_debugfs_install(struct ocfs2_blockcheck_stats *stats,
  					   struct dentry *parent)
  {
  	return ocfs2_blockcheck_debug_install(stats, parent);
  }
  
  void ocfs2_blockcheck_stats_debugfs_remove(struct ocfs2_blockcheck_stats *stats)
  {
  	ocfs2_blockcheck_debug_remove(stats);
  }
  
  static void ocfs2_blockcheck_inc_check(struct ocfs2_blockcheck_stats *stats)
  {
  	u64 new_count;
  
  	if (!stats)
  		return;
  
  	spin_lock(&stats->b_lock);
  	stats->b_check_count++;
  	new_count = stats->b_check_count;
  	spin_unlock(&stats->b_lock);
  
  	if (!new_count)
  		mlog(ML_NOTICE, "Block check count has wrapped
  ");
  }
  
  static void ocfs2_blockcheck_inc_failure(struct ocfs2_blockcheck_stats *stats)
  {
  	u64 new_count;
  
  	if (!stats)
  		return;
  
  	spin_lock(&stats->b_lock);
  	stats->b_failure_count++;
  	new_count = stats->b_failure_count;
  	spin_unlock(&stats->b_lock);
  
  	if (!new_count)
  		mlog(ML_NOTICE, "Checksum failure count has wrapped
  ");
  }
  
  static void ocfs2_blockcheck_inc_recover(struct ocfs2_blockcheck_stats *stats)
  {
  	u64 new_count;
  
  	if (!stats)
  		return;
  
  	spin_lock(&stats->b_lock);
  	stats->b_recover_count++;
  	new_count = stats->b_recover_count;
  	spin_unlock(&stats->b_lock);
  
  	if (!new_count)
  		mlog(ML_NOTICE, "ECC recovery count has wrapped
  ");
  }
  
  
  
  /*
   * These are the low-level APIs for using the ocfs2_block_check structure.
   */
70ad1ba7b   Joel Becker   ocfs2: Add the un...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
  /*
   * This function generates check information for a block.
   * data is the block to be checked.  bc is a pointer to the
   * ocfs2_block_check structure describing the crc32 and the ecc.
   *
   * bc should be a pointer inside data, as the function will
   * take care of zeroing it before calculating the check information.  If
   * bc does not point inside data, the caller must make sure any inline
   * ocfs2_block_check structures are zeroed.
   *
   * The data buffer must be in on-disk endian (little endian for ocfs2).
   * bc will be filled with little-endian values and will be ready to go to
   * disk.
   */
  void ocfs2_block_check_compute(void *data, size_t blocksize,
  			       struct ocfs2_block_check *bc)
  {
  	u32 crc;
  	u32 ecc;
  
  	memset(bc, 0, sizeof(struct ocfs2_block_check));
  
  	crc = crc32_le(~0, data, blocksize);
  	ecc = ocfs2_hamming_encode_block(data, blocksize);
  
  	/*
  	 * No ecc'd ocfs2 structure is larger than 4K, so ecc will be no
  	 * larger than 16 bits.
  	 */
4be929be3   Alexey Dobriyan   kernel-wide: repl...
400
  	BUG_ON(ecc > USHRT_MAX);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
401
402
403
404
405
406
407
408
409
410
411
412
413
414
  
  	bc->bc_crc32e = cpu_to_le32(crc);
  	bc->bc_ecc = cpu_to_le16((u16)ecc);
  }
  
  /*
   * This function validates existing check information.  Like _compute,
   * the function will take care of zeroing bc before calculating check codes.
   * If bc is not a pointer inside data, the caller must have zeroed any
   * inline ocfs2_block_check structures.
   *
   * Again, the data passed in should be the on-disk endian.
   */
  int ocfs2_block_check_validate(void *data, size_t blocksize,
73be192b1   Joel Becker   ocfs2: Add statis...
415
416
  			       struct ocfs2_block_check *bc,
  			       struct ocfs2_blockcheck_stats *stats)
70ad1ba7b   Joel Becker   ocfs2: Add the un...
417
418
  {
  	int rc = 0;
1db5df98f   Al Viro   ocfs2: kill endia...
419
420
  	u32 bc_crc32e;
  	u16 bc_ecc;
70ad1ba7b   Joel Becker   ocfs2: Add the un...
421
  	u32 crc, ecc;
73be192b1   Joel Becker   ocfs2: Add statis...
422
  	ocfs2_blockcheck_inc_check(stats);
1db5df98f   Al Viro   ocfs2: kill endia...
423
424
  	bc_crc32e = le32_to_cpu(bc->bc_crc32e);
  	bc_ecc = le16_to_cpu(bc->bc_ecc);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
425
426
427
428
429
  
  	memset(bc, 0, sizeof(struct ocfs2_block_check));
  
  	/* Fast path - if the crc32 validates, we're good to go */
  	crc = crc32_le(~0, data, blocksize);
1db5df98f   Al Viro   ocfs2: kill endia...
430
  	if (crc == bc_crc32e)
70ad1ba7b   Joel Becker   ocfs2: Add the un...
431
  		goto out;
73be192b1   Joel Becker   ocfs2: Add statis...
432
  	ocfs2_blockcheck_inc_failure(stats);
d6b32bbb3   Joel Becker   ocfs2: block read...
433
  	mlog(ML_ERROR,
dc696aced   Sunil Mushran   ocfs2: Fix metaec...
434
435
  	     "CRC32 failed: stored: 0x%x, computed 0x%x. Applying ECC.
  ",
1db5df98f   Al Viro   ocfs2: kill endia...
436
  	     (unsigned int)bc_crc32e, (unsigned int)crc);
d6b32bbb3   Joel Becker   ocfs2: block read...
437

70ad1ba7b   Joel Becker   ocfs2: Add the un...
438
439
  	/* Ok, try ECC fixups */
  	ecc = ocfs2_hamming_encode_block(data, blocksize);
1db5df98f   Al Viro   ocfs2: kill endia...
440
  	ocfs2_hamming_fix_block(data, blocksize, ecc ^ bc_ecc);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
441
442
443
  
  	/* And check the crc32 again */
  	crc = crc32_le(~0, data, blocksize);
1db5df98f   Al Viro   ocfs2: kill endia...
444
  	if (crc == bc_crc32e) {
73be192b1   Joel Becker   ocfs2: Add statis...
445
  		ocfs2_blockcheck_inc_recover(stats);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
446
  		goto out;
73be192b1   Joel Becker   ocfs2: Add statis...
447
  	}
70ad1ba7b   Joel Becker   ocfs2: Add the un...
448

dc696aced   Sunil Mushran   ocfs2: Fix metaec...
449
450
  	mlog(ML_ERROR, "Fixed CRC32 failed: stored: 0x%x, computed 0x%x
  ",
1db5df98f   Al Viro   ocfs2: kill endia...
451
  	     (unsigned int)bc_crc32e, (unsigned int)crc);
d6b32bbb3   Joel Becker   ocfs2: block read...
452

70ad1ba7b   Joel Becker   ocfs2: Add the un...
453
454
455
  	rc = -EIO;
  
  out:
1db5df98f   Al Viro   ocfs2: kill endia...
456
457
  	bc->bc_crc32e = cpu_to_le32(bc_crc32e);
  	bc->bc_ecc = cpu_to_le16(bc_ecc);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
  
  	return rc;
  }
  
  /*
   * This function generates check information for a list of buffer_heads.
   * bhs is the blocks to be checked.  bc is a pointer to the
   * ocfs2_block_check structure describing the crc32 and the ecc.
   *
   * bc should be a pointer inside data, as the function will
   * take care of zeroing it before calculating the check information.  If
   * bc does not point inside data, the caller must make sure any inline
   * ocfs2_block_check structures are zeroed.
   *
   * The data buffer must be in on-disk endian (little endian for ocfs2).
   * bc will be filled with little-endian values and will be ready to go to
   * disk.
   */
  void ocfs2_block_check_compute_bhs(struct buffer_head **bhs, int nr,
  				   struct ocfs2_block_check *bc)
  {
  	int i;
  	u32 crc, ecc;
  
  	BUG_ON(nr < 0);
  
  	if (!nr)
  		return;
  
  	memset(bc, 0, sizeof(struct ocfs2_block_check));
  
  	for (i = 0, crc = ~0, ecc = 0; i < nr; i++) {
  		crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
  		/*
  		 * The number of bits in a buffer is obviously b_size*8.
  		 * The offset of this buffer is b_size*i, so the bit offset
  		 * of this buffer is b_size*8*i.
  		 */
  		ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data,
  						bhs[i]->b_size * 8,
  						bhs[i]->b_size * 8 * i);
  	}
  
  	/*
  	 * No ecc'd ocfs2 structure is larger than 4K, so ecc will be no
  	 * larger than 16 bits.
  	 */
4be929be3   Alexey Dobriyan   kernel-wide: repl...
505
  	BUG_ON(ecc > USHRT_MAX);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
  
  	bc->bc_crc32e = cpu_to_le32(crc);
  	bc->bc_ecc = cpu_to_le16((u16)ecc);
  }
  
  /*
   * This function validates existing check information on a list of
   * buffer_heads.  Like _compute_bhs, the function will take care of
   * zeroing bc before calculating check codes.  If bc is not a pointer
   * inside data, the caller must have zeroed any inline
   * ocfs2_block_check structures.
   *
   * Again, the data passed in should be the on-disk endian.
   */
  int ocfs2_block_check_validate_bhs(struct buffer_head **bhs, int nr,
73be192b1   Joel Becker   ocfs2: Add statis...
521
522
  				   struct ocfs2_block_check *bc,
  				   struct ocfs2_blockcheck_stats *stats)
70ad1ba7b   Joel Becker   ocfs2: Add the un...
523
524
  {
  	int i, rc = 0;
1db5df98f   Al Viro   ocfs2: kill endia...
525
526
  	u32 bc_crc32e;
  	u16 bc_ecc;
70ad1ba7b   Joel Becker   ocfs2: Add the un...
527
528
529
530
531
532
  	u32 crc, ecc, fix;
  
  	BUG_ON(nr < 0);
  
  	if (!nr)
  		return 0;
73be192b1   Joel Becker   ocfs2: Add statis...
533
  	ocfs2_blockcheck_inc_check(stats);
1db5df98f   Al Viro   ocfs2: kill endia...
534
535
  	bc_crc32e = le32_to_cpu(bc->bc_crc32e);
  	bc_ecc = le16_to_cpu(bc->bc_ecc);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
536
537
538
539
540
541
  
  	memset(bc, 0, sizeof(struct ocfs2_block_check));
  
  	/* Fast path - if the crc32 validates, we're good to go */
  	for (i = 0, crc = ~0; i < nr; i++)
  		crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
1db5df98f   Al Viro   ocfs2: kill endia...
542
  	if (crc == bc_crc32e)
70ad1ba7b   Joel Becker   ocfs2: Add the un...
543
  		goto out;
73be192b1   Joel Becker   ocfs2: Add statis...
544
  	ocfs2_blockcheck_inc_failure(stats);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
545
546
547
  	mlog(ML_ERROR,
  	     "CRC32 failed: stored: %u, computed %u.  Applying ECC.
  ",
1db5df98f   Al Viro   ocfs2: kill endia...
548
  	     (unsigned int)bc_crc32e, (unsigned int)crc);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
549
550
551
552
553
554
555
556
557
558
559
560
  
  	/* Ok, try ECC fixups */
  	for (i = 0, ecc = 0; i < nr; i++) {
  		/*
  		 * The number of bits in a buffer is obviously b_size*8.
  		 * The offset of this buffer is b_size*i, so the bit offset
  		 * of this buffer is b_size*8*i.
  		 */
  		ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data,
  						bhs[i]->b_size * 8,
  						bhs[i]->b_size * 8 * i);
  	}
1db5df98f   Al Viro   ocfs2: kill endia...
561
  	fix = ecc ^ bc_ecc;
70ad1ba7b   Joel Becker   ocfs2: Add the un...
562
563
564
565
566
567
568
569
570
571
572
573
  	for (i = 0; i < nr; i++) {
  		/*
  		 * Try the fix against each buffer.  It will only affect
  		 * one of them.
  		 */
  		ocfs2_hamming_fix(bhs[i]->b_data, bhs[i]->b_size * 8,
  				  bhs[i]->b_size * 8 * i, fix);
  	}
  
  	/* And check the crc32 again */
  	for (i = 0, crc = ~0; i < nr; i++)
  		crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
1db5df98f   Al Viro   ocfs2: kill endia...
574
  	if (crc == bc_crc32e) {
73be192b1   Joel Becker   ocfs2: Add statis...
575
  		ocfs2_blockcheck_inc_recover(stats);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
576
  		goto out;
73be192b1   Joel Becker   ocfs2: Add statis...
577
  	}
70ad1ba7b   Joel Becker   ocfs2: Add the un...
578
579
580
  
  	mlog(ML_ERROR, "Fixed CRC32 failed: stored: %u, computed %u
  ",
1db5df98f   Al Viro   ocfs2: kill endia...
581
  	     (unsigned int)bc_crc32e, (unsigned int)crc);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
582
583
584
585
  
  	rc = -EIO;
  
  out:
1db5df98f   Al Viro   ocfs2: kill endia...
586
587
  	bc->bc_crc32e = cpu_to_le32(bc_crc32e);
  	bc->bc_ecc = cpu_to_le16(bc_ecc);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
  
  	return rc;
  }
  
  /*
   * These are the main API.  They check the superblock flag before
   * calling the underlying operations.
   *
   * They expect the buffer(s) to be in disk format.
   */
  void ocfs2_compute_meta_ecc(struct super_block *sb, void *data,
  			    struct ocfs2_block_check *bc)
  {
  	if (ocfs2_meta_ecc(OCFS2_SB(sb)))
  		ocfs2_block_check_compute(data, sb->s_blocksize, bc);
  }
  
  int ocfs2_validate_meta_ecc(struct super_block *sb, void *data,
  			    struct ocfs2_block_check *bc)
  {
  	int rc = 0;
73be192b1   Joel Becker   ocfs2: Add statis...
609
  	struct ocfs2_super *osb = OCFS2_SB(sb);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
610

73be192b1   Joel Becker   ocfs2: Add statis...
611
612
613
  	if (ocfs2_meta_ecc(osb))
  		rc = ocfs2_block_check_validate(data, sb->s_blocksize, bc,
  						&osb->osb_ecc_stats);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
  
  	return rc;
  }
  
  void ocfs2_compute_meta_ecc_bhs(struct super_block *sb,
  				struct buffer_head **bhs, int nr,
  				struct ocfs2_block_check *bc)
  {
  	if (ocfs2_meta_ecc(OCFS2_SB(sb)))
  		ocfs2_block_check_compute_bhs(bhs, nr, bc);
  }
  
  int ocfs2_validate_meta_ecc_bhs(struct super_block *sb,
  				struct buffer_head **bhs, int nr,
  				struct ocfs2_block_check *bc)
  {
  	int rc = 0;
73be192b1   Joel Becker   ocfs2: Add statis...
631
  	struct ocfs2_super *osb = OCFS2_SB(sb);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
632

73be192b1   Joel Becker   ocfs2: Add statis...
633
634
635
  	if (ocfs2_meta_ecc(osb))
  		rc = ocfs2_block_check_validate_bhs(bhs, nr, bc,
  						    &osb->osb_ecc_stats);
70ad1ba7b   Joel Becker   ocfs2: Add the un...
636
637
638
  
  	return rc;
  }