Files
scoutfs/utils/src/crc.c
Zach Brown 6c37e3dee0 scoutfs-utils: add btree ring storage
Manifest entries and segment allocation bitmap regions are now stored in
btree items instead of the ring log.  This lets us work with them
incrementally and share them between nodes.

Signed-off-by: Zach Brown <zab@versity.com>
2017-07-17 13:43:37 -07:00

53 lines
1.0 KiB
C

#include "crc.h"
#include "util.h"
#include "format.h"
u32 crc32c(u32 crc, const void *data, unsigned int len)
{
while (len >= 8) {
crc = __builtin_ia32_crc32di(crc, *(u64 *)data);
len -= 8;
data += 8;
}
if (len & 4) {
crc = __builtin_ia32_crc32si(crc, *(u32 *)data);
data += 4;
}
if (len & 2) {
crc = __builtin_ia32_crc32hi(crc, *(u16 *)data);
data += 2;
}
if (len & 1)
crc = __builtin_ia32_crc32qi(crc, *(u8 *)data);
return crc;
}
/* A simple hack to get reasonably solid 64bit hash values */
u64 crc32c_64(u32 crc, const void *data, unsigned int len)
{
unsigned int half = (len + 1) / 2;
return ((u64)crc32c(crc, data, half) << 32) |
crc32c(~crc, data + len - half, half);
}
u32 crc_block(struct scoutfs_block_header *hdr)
{
return crc32c(~0, (char *)hdr + sizeof(hdr->crc),
SCOUTFS_BLOCK_SIZE - sizeof(hdr->crc));
}
u32 crc_btree_block(struct scoutfs_btree_block *bt)
{
__le32 old;
u32 crc;
old = bt->crc;
bt->crc = 0;
crc = crc32c(~0, bt, SCOUTFS_BLOCK_SIZE);
bt->crc = old;
return crc;
}