blockcheck.c 13 KB

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