blockcheck.c 13 KB

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