xref: /openbmc/linux/fs/xfs/libxfs/xfs_cksum.h (revision 498495dba268b20e8eadd7fe93c140c68b6cc9d2)
1*b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
284be0ffcSDave Chinner #ifndef _XFS_CKSUM_H
384be0ffcSDave Chinner #define _XFS_CKSUM_H 1
484be0ffcSDave Chinner 
5c8ce540dSDarrick J. Wong #define XFS_CRC_SEED	(~(uint32_t)0)
684be0ffcSDave Chinner 
784be0ffcSDave Chinner /*
884be0ffcSDave Chinner  * Calculate the intermediate checksum for a buffer that has the CRC field
984be0ffcSDave Chinner  * inside it.  The offset of the 32bit crc fields is passed as the
10cae028dfSDave Chinner  * cksum_offset parameter. We do not modify the buffer during verification,
11cae028dfSDave Chinner  * hence we have to split the CRC calculation across the cksum_offset.
1284be0ffcSDave Chinner  */
13c8ce540dSDarrick J. Wong static inline uint32_t
xfs_start_cksum_safe(char * buffer,size_t length,unsigned long cksum_offset)14cae028dfSDave Chinner xfs_start_cksum_safe(char *buffer, size_t length, unsigned long cksum_offset)
1584be0ffcSDave Chinner {
16c8ce540dSDarrick J. Wong 	uint32_t zero = 0;
17c8ce540dSDarrick J. Wong 	uint32_t crc;
1884be0ffcSDave Chinner 
1984be0ffcSDave Chinner 	/* Calculate CRC up to the checksum. */
2084be0ffcSDave Chinner 	crc = crc32c(XFS_CRC_SEED, buffer, cksum_offset);
2184be0ffcSDave Chinner 
2284be0ffcSDave Chinner 	/* Skip checksum field */
2384be0ffcSDave Chinner 	crc = crc32c(crc, &zero, sizeof(__u32));
2484be0ffcSDave Chinner 
2584be0ffcSDave Chinner 	/* Calculate the rest of the CRC. */
2684be0ffcSDave Chinner 	return crc32c(crc, &buffer[cksum_offset + sizeof(__be32)],
2784be0ffcSDave Chinner 		      length - (cksum_offset + sizeof(__be32)));
2884be0ffcSDave Chinner }
2984be0ffcSDave Chinner 
3084be0ffcSDave Chinner /*
31cae028dfSDave Chinner  * Fast CRC method where the buffer is modified. Callers must have exclusive
32cae028dfSDave Chinner  * access to the buffer while the calculation takes place.
33cae028dfSDave Chinner  */
34c8ce540dSDarrick J. Wong static inline uint32_t
xfs_start_cksum_update(char * buffer,size_t length,unsigned long cksum_offset)35cae028dfSDave Chinner xfs_start_cksum_update(char *buffer, size_t length, unsigned long cksum_offset)
36cae028dfSDave Chinner {
37cae028dfSDave Chinner 	/* zero the CRC field */
38cae028dfSDave Chinner 	*(__le32 *)(buffer + cksum_offset) = 0;
39cae028dfSDave Chinner 
40cae028dfSDave Chinner 	/* single pass CRC calculation for the entire buffer */
41cae028dfSDave Chinner 	return crc32c(XFS_CRC_SEED, buffer, length);
42cae028dfSDave Chinner }
43cae028dfSDave Chinner 
44cae028dfSDave Chinner /*
4584be0ffcSDave Chinner  * Convert the intermediate checksum to the final ondisk format.
4684be0ffcSDave Chinner  *
4784be0ffcSDave Chinner  * The CRC32c calculation uses LE format even on BE machines, but returns the
4884be0ffcSDave Chinner  * result in host endian format. Hence we need to byte swap it back to LE format
4984be0ffcSDave Chinner  * so that it is consistent on disk.
5084be0ffcSDave Chinner  */
5184be0ffcSDave Chinner static inline __le32
xfs_end_cksum(uint32_t crc)52c8ce540dSDarrick J. Wong xfs_end_cksum(uint32_t crc)
5384be0ffcSDave Chinner {
5484be0ffcSDave Chinner 	return ~cpu_to_le32(crc);
5584be0ffcSDave Chinner }
5684be0ffcSDave Chinner 
5784be0ffcSDave Chinner /*
5884be0ffcSDave Chinner  * Helper to generate the checksum for a buffer.
59cae028dfSDave Chinner  *
60cae028dfSDave Chinner  * This modifies the buffer temporarily - callers must have exclusive
61cae028dfSDave Chinner  * access to the buffer while the calculation takes place.
6284be0ffcSDave Chinner  */
6384be0ffcSDave Chinner static inline void
xfs_update_cksum(char * buffer,size_t length,unsigned long cksum_offset)6484be0ffcSDave Chinner xfs_update_cksum(char *buffer, size_t length, unsigned long cksum_offset)
6584be0ffcSDave Chinner {
66c8ce540dSDarrick J. Wong 	uint32_t crc = xfs_start_cksum_update(buffer, length, cksum_offset);
6784be0ffcSDave Chinner 
6884be0ffcSDave Chinner 	*(__le32 *)(buffer + cksum_offset) = xfs_end_cksum(crc);
6984be0ffcSDave Chinner }
7084be0ffcSDave Chinner 
7184be0ffcSDave Chinner /*
7284be0ffcSDave Chinner  * Helper to verify the checksum for a buffer.
7384be0ffcSDave Chinner  */
7484be0ffcSDave Chinner static inline int
xfs_verify_cksum(char * buffer,size_t length,unsigned long cksum_offset)7584be0ffcSDave Chinner xfs_verify_cksum(char *buffer, size_t length, unsigned long cksum_offset)
7684be0ffcSDave Chinner {
77c8ce540dSDarrick J. Wong 	uint32_t crc = xfs_start_cksum_safe(buffer, length, cksum_offset);
7884be0ffcSDave Chinner 
7984be0ffcSDave Chinner 	return *(__le32 *)(buffer + cksum_offset) == xfs_end_cksum(crc);
8084be0ffcSDave Chinner }
8184be0ffcSDave Chinner 
8284be0ffcSDave Chinner #endif /* _XFS_CKSUM_H */
83