blockcheck.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * blockcheck.c
  4. *
  5. * Checksum and ECC codes for the OCFS2 userspace library.
  6. *
  7. * Copyright (C) 2006, 2008 Oracle. All rights reserved.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/crc32.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/bitops.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/module.h>
  16. #include <linux/fs.h>
  17. #include <asm/byteorder.h>
  18. #include <cluster/masklog.h>
  19. #include "ocfs2.h"
  20. #include "blockcheck.h"
  21. /*
  22. * We use the following conventions:
  23. *
  24. * d = # data bits
  25. * p = # parity bits
  26. * c = # total code bits (d + p)
  27. */
  28. /*
  29. * Calculate the bit offset in the hamming code buffer based on the bit's
  30. * offset in the data buffer. Since the hamming code reserves all
  31. * power-of-two bits for parity, the data bit number and the code bit
  32. * number are offset by all the parity bits beforehand.
  33. *
  34. * Recall that bit numbers in hamming code are 1-based. This function
  35. * takes the 0-based data bit from the caller.
  36. *
  37. * An example. Take bit 1 of the data buffer. 1 is a power of two (2^0),
  38. * so it's a parity bit. 2 is a power of two (2^1), so it's a parity bit.
  39. * 3 is not a power of two. So bit 1 of the data buffer ends up as bit 3
  40. * in the code buffer.
  41. *
  42. * The caller can pass in *p if it wants to keep track of the most recent
  43. * number of parity bits added. This allows the function to start the
  44. * calculation at the last place.
  45. */
  46. static unsigned int calc_code_bit(unsigned int i, unsigned int *p_cache)
  47. {
  48. unsigned int b, p = 0;
  49. /*
  50. * Data bits are 0-based, but we're talking code bits, which
  51. * are 1-based.
  52. */
  53. b = i + 1;
  54. /* Use the cache if it is there */
  55. if (p_cache)
  56. p = *p_cache;
  57. b += p;
  58. /*
  59. * For every power of two below our bit number, bump our bit.
  60. *
  61. * We compare with (b + 1) because we have to compare with what b
  62. * would be _if_ it were bumped up by the parity bit. Capice?
  63. *
  64. * p is set above.
  65. */
  66. for (; (1 << p) < (b + 1); p++)
  67. b++;
  68. if (p_cache)
  69. *p_cache = p;
  70. return b;
  71. }
  72. /*
  73. * This is the low level encoder function. It can be called across
  74. * multiple hunks just like the crc32 code. 'd' is the number of bits
  75. * _in_this_hunk_. nr is the bit offset of this hunk. So, if you had
  76. * two 512B buffers, you would do it like so:
  77. *
  78. * parity = ocfs2_hamming_encode(0, buf1, 512 * 8, 0);
  79. * parity = ocfs2_hamming_encode(parity, buf2, 512 * 8, 512 * 8);
  80. *
  81. * If you just have one buffer, use ocfs2_hamming_encode_block().
  82. */
  83. u32 ocfs2_hamming_encode(u32 parity, void *data, unsigned int d, unsigned int nr)
  84. {
  85. unsigned int i, b, p = 0;
  86. BUG_ON(!d);
  87. /*
  88. * b is the hamming code bit number. Hamming code specifies a
  89. * 1-based array, but C uses 0-based. So 'i' is for C, and 'b' is
  90. * for the algorithm.
  91. *
  92. * The i++ in the for loop is so that the start offset passed
  93. * to ocfs2_find_next_bit_set() is one greater than the previously
  94. * found bit.
  95. */
  96. for (i = 0; (i = ocfs2_find_next_bit(data, d, i)) < d; i++)
  97. {
  98. /*
  99. * i is the offset in this hunk, nr + i is the total bit
  100. * offset.
  101. */
  102. b = calc_code_bit(nr + i, &p);
  103. /*
  104. * Data bits in the resultant code are checked by
  105. * parity bits that are part of the bit number
  106. * representation. Huh?
  107. *
  108. * <wikipedia href="https://en.wikipedia.org/wiki/Hamming_code">
  109. * In other words, the parity bit at position 2^k
  110. * checks bits in positions having bit k set in
  111. * their binary representation. Conversely, for
  112. * instance, bit 13, i.e. 1101(2), is checked by
  113. * bits 1000(2) = 8, 0100(2)=4 and 0001(2) = 1.
  114. * </wikipedia>
  115. *
  116. * Note that 'k' is the _code_ bit number. 'b' in
  117. * our loop.
  118. */
  119. parity ^= b;
  120. }
  121. /* While the data buffer was treated as little endian, the
  122. * return value is in host endian. */
  123. return parity;
  124. }
  125. u32 ocfs2_hamming_encode_block(void *data, unsigned int blocksize)
  126. {
  127. return ocfs2_hamming_encode(0, data, blocksize * 8, 0);
  128. }
  129. /*
  130. * Like ocfs2_hamming_encode(), this can handle hunks. nr is the bit
  131. * offset of the current hunk. If bit to be fixed is not part of the
  132. * current hunk, this does nothing.
  133. *
  134. * If you only have one hunk, use ocfs2_hamming_fix_block().
  135. */
  136. void ocfs2_hamming_fix(void *data, unsigned int d, unsigned int nr,
  137. unsigned int fix)
  138. {
  139. unsigned int i, b;
  140. BUG_ON(!d);
  141. /*
  142. * If the bit to fix has an hweight of 1, it's a parity bit. One
  143. * busted parity bit is its own error. Nothing to do here.
  144. */
  145. if (hweight32(fix) == 1)
  146. return;
  147. /*
  148. * nr + d is the bit right past the data hunk we're looking at.
  149. * If fix after that, nothing to do
  150. */
  151. if (fix >= calc_code_bit(nr + d, NULL))
  152. return;
  153. /*
  154. * nr is the offset in the data hunk we're starting at. Let's
  155. * start b at the offset in the code buffer. See hamming_encode()
  156. * for a more detailed description of 'b'.
  157. */
  158. b = calc_code_bit(nr, NULL);
  159. /* If the fix is before this hunk, nothing to do */
  160. if (fix < b)
  161. return;
  162. for (i = 0; i < d; i++, b++)
  163. {
  164. /* Skip past parity bits */
  165. while (hweight32(b) == 1)
  166. b++;
  167. /*
  168. * i is the offset in this data hunk.
  169. * nr + i is the offset in the total data buffer.
  170. * b is the offset in the total code buffer.
  171. *
  172. * Thus, when b == fix, bit i in the current hunk needs
  173. * fixing.
  174. */
  175. if (b == fix)
  176. {
  177. if (ocfs2_test_bit(i, data))
  178. ocfs2_clear_bit(i, data);
  179. else
  180. ocfs2_set_bit(i, data);
  181. break;
  182. }
  183. }
  184. }
  185. void ocfs2_hamming_fix_block(void *data, unsigned int blocksize,
  186. unsigned int fix)
  187. {
  188. ocfs2_hamming_fix(data, blocksize * 8, 0, fix);
  189. }
  190. /*
  191. * Debugfs handling.
  192. */
  193. #ifdef CONFIG_DEBUG_FS
  194. static int blockcheck_u64_get(void *data, u64 *val)
  195. {
  196. *val = *(u64 *)data;
  197. return 0;
  198. }
  199. DEFINE_DEBUGFS_ATTRIBUTE(blockcheck_fops, blockcheck_u64_get, NULL, "%llu\n");
  200. static void ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats *stats)
  201. {
  202. if (stats) {
  203. debugfs_remove_recursive(stats->b_debug_dir);
  204. stats->b_debug_dir = NULL;
  205. }
  206. }
  207. static void ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats *stats,
  208. struct dentry *parent)
  209. {
  210. struct dentry *dir;
  211. dir = debugfs_create_dir("blockcheck", parent);
  212. stats->b_debug_dir = dir;
  213. debugfs_create_file("blocks_checked", S_IFREG | S_IRUSR, dir,
  214. &stats->b_check_count, &blockcheck_fops);
  215. debugfs_create_file("checksums_failed", S_IFREG | S_IRUSR, dir,
  216. &stats->b_failure_count, &blockcheck_fops);
  217. debugfs_create_file("ecc_recoveries", S_IFREG | S_IRUSR, dir,
  218. &stats->b_recover_count, &blockcheck_fops);
  219. }
  220. #else
  221. static inline void ocfs2_blockcheck_debug_install(struct ocfs2_blockcheck_stats *stats,
  222. struct dentry *parent)
  223. {
  224. }
  225. static inline void ocfs2_blockcheck_debug_remove(struct ocfs2_blockcheck_stats *stats)
  226. {
  227. }
  228. #endif /* CONFIG_DEBUG_FS */
  229. /* Always-called wrappers for starting and stopping the debugfs files */
  230. void ocfs2_blockcheck_stats_debugfs_install(struct ocfs2_blockcheck_stats *stats,
  231. struct dentry *parent)
  232. {
  233. ocfs2_blockcheck_debug_install(stats, parent);
  234. }
  235. void ocfs2_blockcheck_stats_debugfs_remove(struct ocfs2_blockcheck_stats *stats)
  236. {
  237. ocfs2_blockcheck_debug_remove(stats);
  238. }
  239. static void ocfs2_blockcheck_inc_check(struct ocfs2_blockcheck_stats *stats)
  240. {
  241. u64 new_count;
  242. if (!stats)
  243. return;
  244. spin_lock(&stats->b_lock);
  245. stats->b_check_count++;
  246. new_count = stats->b_check_count;
  247. spin_unlock(&stats->b_lock);
  248. if (!new_count)
  249. mlog(ML_NOTICE, "Block check count has wrapped\n");
  250. }
  251. static void ocfs2_blockcheck_inc_failure(struct ocfs2_blockcheck_stats *stats)
  252. {
  253. u64 new_count;
  254. if (!stats)
  255. return;
  256. spin_lock(&stats->b_lock);
  257. stats->b_failure_count++;
  258. new_count = stats->b_failure_count;
  259. spin_unlock(&stats->b_lock);
  260. if (!new_count)
  261. mlog(ML_NOTICE, "Checksum failure count has wrapped\n");
  262. }
  263. static void ocfs2_blockcheck_inc_recover(struct ocfs2_blockcheck_stats *stats)
  264. {
  265. u64 new_count;
  266. if (!stats)
  267. return;
  268. spin_lock(&stats->b_lock);
  269. stats->b_recover_count++;
  270. new_count = stats->b_recover_count;
  271. spin_unlock(&stats->b_lock);
  272. if (!new_count)
  273. mlog(ML_NOTICE, "ECC recovery count has wrapped\n");
  274. }
  275. /*
  276. * These are the low-level APIs for using the ocfs2_block_check structure.
  277. */
  278. /*
  279. * This function generates check information for a block.
  280. * data is the block 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(void *data, size_t blocksize,
  293. struct ocfs2_block_check *bc)
  294. {
  295. u32 crc;
  296. u32 ecc;
  297. memset(bc, 0, sizeof(struct ocfs2_block_check));
  298. crc = crc32_le(~0, data, blocksize);
  299. ecc = ocfs2_hamming_encode_block(data, blocksize);
  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 > USHRT_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. Like _compute,
  310. * the function will take care of zeroing bc before calculating check codes.
  311. * If bc is not a pointer inside data, the caller must have zeroed any
  312. * inline ocfs2_block_check structures.
  313. *
  314. * Again, the data passed in should be the on-disk endian.
  315. */
  316. int ocfs2_block_check_validate(void *data, size_t blocksize,
  317. struct ocfs2_block_check *bc,
  318. struct ocfs2_blockcheck_stats *stats)
  319. {
  320. int rc = 0;
  321. u32 bc_crc32e;
  322. u16 bc_ecc;
  323. u32 crc, ecc;
  324. ocfs2_blockcheck_inc_check(stats);
  325. bc_crc32e = le32_to_cpu(bc->bc_crc32e);
  326. bc_ecc = le16_to_cpu(bc->bc_ecc);
  327. memset(bc, 0, sizeof(struct ocfs2_block_check));
  328. /* Fast path - if the crc32 validates, we're good to go */
  329. crc = crc32_le(~0, data, blocksize);
  330. if (crc == bc_crc32e)
  331. goto out;
  332. ocfs2_blockcheck_inc_failure(stats);
  333. mlog(ML_ERROR,
  334. "CRC32 failed: stored: 0x%x, computed 0x%x. Applying ECC.\n",
  335. (unsigned int)bc_crc32e, (unsigned int)crc);
  336. /* Ok, try ECC fixups */
  337. ecc = ocfs2_hamming_encode_block(data, blocksize);
  338. ocfs2_hamming_fix_block(data, blocksize, ecc ^ bc_ecc);
  339. /* And check the crc32 again */
  340. crc = crc32_le(~0, data, blocksize);
  341. if (crc == bc_crc32e) {
  342. ocfs2_blockcheck_inc_recover(stats);
  343. goto out;
  344. }
  345. mlog(ML_ERROR, "Fixed CRC32 failed: stored: 0x%x, computed 0x%x\n",
  346. (unsigned int)bc_crc32e, (unsigned int)crc);
  347. rc = -EIO;
  348. out:
  349. bc->bc_crc32e = cpu_to_le32(bc_crc32e);
  350. bc->bc_ecc = cpu_to_le16(bc_ecc);
  351. return rc;
  352. }
  353. /*
  354. * This function generates check information for a list of buffer_heads.
  355. * bhs is the blocks to be checked. bc is a pointer to the
  356. * ocfs2_block_check structure describing the crc32 and the ecc.
  357. *
  358. * bc should be a pointer inside data, as the function will
  359. * take care of zeroing it before calculating the check information. If
  360. * bc does not point inside data, the caller must make sure any inline
  361. * ocfs2_block_check structures are zeroed.
  362. *
  363. * The data buffer must be in on-disk endian (little endian for ocfs2).
  364. * bc will be filled with little-endian values and will be ready to go to
  365. * disk.
  366. */
  367. void ocfs2_block_check_compute_bhs(struct buffer_head **bhs, int nr,
  368. struct ocfs2_block_check *bc)
  369. {
  370. int i;
  371. u32 crc, ecc;
  372. BUG_ON(nr < 0);
  373. if (!nr)
  374. return;
  375. memset(bc, 0, sizeof(struct ocfs2_block_check));
  376. for (i = 0, crc = ~0, ecc = 0; i < nr; i++) {
  377. crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
  378. /*
  379. * The number of bits in a buffer is obviously b_size*8.
  380. * The offset of this buffer is b_size*i, so the bit offset
  381. * of this buffer is b_size*8*i.
  382. */
  383. ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data,
  384. bhs[i]->b_size * 8,
  385. bhs[i]->b_size * 8 * i);
  386. }
  387. /*
  388. * No ecc'd ocfs2 structure is larger than 4K, so ecc will be no
  389. * larger than 16 bits.
  390. */
  391. BUG_ON(ecc > USHRT_MAX);
  392. bc->bc_crc32e = cpu_to_le32(crc);
  393. bc->bc_ecc = cpu_to_le16((u16)ecc);
  394. }
  395. /*
  396. * This function validates existing check information on a list of
  397. * buffer_heads. Like _compute_bhs, the function will take care of
  398. * zeroing bc before calculating check codes. If bc is not a pointer
  399. * inside data, the caller must have zeroed any inline
  400. * ocfs2_block_check structures.
  401. *
  402. * Again, the data passed in should be the on-disk endian.
  403. */
  404. int ocfs2_block_check_validate_bhs(struct buffer_head **bhs, int nr,
  405. struct ocfs2_block_check *bc,
  406. struct ocfs2_blockcheck_stats *stats)
  407. {
  408. int i, rc = 0;
  409. u32 bc_crc32e;
  410. u16 bc_ecc;
  411. u32 crc, ecc, fix;
  412. BUG_ON(nr < 0);
  413. if (!nr)
  414. return 0;
  415. ocfs2_blockcheck_inc_check(stats);
  416. bc_crc32e = le32_to_cpu(bc->bc_crc32e);
  417. bc_ecc = le16_to_cpu(bc->bc_ecc);
  418. memset(bc, 0, sizeof(struct ocfs2_block_check));
  419. /* Fast path - if the crc32 validates, we're good to go */
  420. for (i = 0, crc = ~0; i < nr; i++)
  421. crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
  422. if (crc == bc_crc32e)
  423. goto out;
  424. ocfs2_blockcheck_inc_failure(stats);
  425. mlog(ML_ERROR,
  426. "CRC32 failed: stored: %u, computed %u. Applying ECC.\n",
  427. (unsigned int)bc_crc32e, (unsigned int)crc);
  428. /* Ok, try ECC fixups */
  429. for (i = 0, ecc = 0; i < nr; i++) {
  430. /*
  431. * The number of bits in a buffer is obviously b_size*8.
  432. * The offset of this buffer is b_size*i, so the bit offset
  433. * of this buffer is b_size*8*i.
  434. */
  435. ecc = (u16)ocfs2_hamming_encode(ecc, bhs[i]->b_data,
  436. bhs[i]->b_size * 8,
  437. bhs[i]->b_size * 8 * i);
  438. }
  439. fix = ecc ^ bc_ecc;
  440. for (i = 0; i < nr; i++) {
  441. /*
  442. * Try the fix against each buffer. It will only affect
  443. * one of them.
  444. */
  445. ocfs2_hamming_fix(bhs[i]->b_data, bhs[i]->b_size * 8,
  446. bhs[i]->b_size * 8 * i, fix);
  447. }
  448. /* And check the crc32 again */
  449. for (i = 0, crc = ~0; i < nr; i++)
  450. crc = crc32_le(crc, bhs[i]->b_data, bhs[i]->b_size);
  451. if (crc == bc_crc32e) {
  452. ocfs2_blockcheck_inc_recover(stats);
  453. goto out;
  454. }
  455. mlog(ML_ERROR, "Fixed CRC32 failed: stored: %u, computed %u\n",
  456. (unsigned int)bc_crc32e, (unsigned int)crc);
  457. rc = -EIO;
  458. out:
  459. bc->bc_crc32e = cpu_to_le32(bc_crc32e);
  460. bc->bc_ecc = cpu_to_le16(bc_ecc);
  461. return rc;
  462. }
  463. /*
  464. * These are the main API. They check the superblock flag before
  465. * calling the underlying operations.
  466. *
  467. * They expect the buffer(s) to be in disk format.
  468. */
  469. void ocfs2_compute_meta_ecc(struct super_block *sb, void *data,
  470. struct ocfs2_block_check *bc)
  471. {
  472. if (ocfs2_meta_ecc(OCFS2_SB(sb)))
  473. ocfs2_block_check_compute(data, sb->s_blocksize, bc);
  474. }
  475. int ocfs2_validate_meta_ecc(struct super_block *sb, void *data,
  476. struct ocfs2_block_check *bc)
  477. {
  478. int rc = 0;
  479. struct ocfs2_super *osb = OCFS2_SB(sb);
  480. if (ocfs2_meta_ecc(osb))
  481. rc = ocfs2_block_check_validate(data, sb->s_blocksize, bc,
  482. &osb->osb_ecc_stats);
  483. return rc;
  484. }
  485. void ocfs2_compute_meta_ecc_bhs(struct super_block *sb,
  486. struct buffer_head **bhs, int nr,
  487. struct ocfs2_block_check *bc)
  488. {
  489. if (ocfs2_meta_ecc(OCFS2_SB(sb)))
  490. ocfs2_block_check_compute_bhs(bhs, nr, bc);
  491. }
  492. int ocfs2_validate_meta_ecc_bhs(struct super_block *sb,
  493. struct buffer_head **bhs, int nr,
  494. struct ocfs2_block_check *bc)
  495. {
  496. int rc = 0;
  497. struct ocfs2_super *osb = OCFS2_SB(sb);
  498. if (ocfs2_meta_ecc(osb))
  499. rc = ocfs2_block_check_validate_bhs(bhs, nr, bc,
  500. &osb->osb_ecc_stats);
  501. return rc;
  502. }