blockcheck.c 13 KB

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