blockcheck.c 12 KB

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