blockcheck.c 13 KB

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