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