From 7abf5c1e2ba038e0e8327a477271334269016676 Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Thu, 16 Aug 2018 13:57:01 -0700 Subject: [PATCH] scoutfs-utils: calculate segment crc in mkfs Signed-off-by: Zach Brown --- utils/src/crc.c | 9 +++++++++ utils/src/crc.h | 1 + utils/src/format.h | 3 +++ utils/src/mkfs.c | 1 + 4 files changed, 14 insertions(+) diff --git a/utils/src/crc.c b/utils/src/crc.c index 38640fbc..714afa90 100644 --- a/utils/src/crc.c +++ b/utils/src/crc.c @@ -37,3 +37,12 @@ u32 crc_block(struct scoutfs_block_header *hdr) return crc32c(~0, (char *)hdr + sizeof(hdr->crc), SCOUTFS_BLOCK_SIZE - sizeof(hdr->crc)); } + +u32 crc_segment(struct scoutfs_segment_block *sblk) +{ + u32 off = offsetof(struct scoutfs_segment_block, _padding) + + sizeof(sblk->_padding); + + return crc32c(~0, (char *)sblk + off, + le32_to_cpu(sblk->total_bytes) - off); +} diff --git a/utils/src/crc.h b/utils/src/crc.h index 6878bf2f..a928bf0a 100644 --- a/utils/src/crc.h +++ b/utils/src/crc.h @@ -8,5 +8,6 @@ u32 crc32c(u32 crc, const void *data, unsigned int len); u64 crc32c_64(u32 crc, const void *data, unsigned int len); u32 crc_block(struct scoutfs_block_header *hdr); +u32 crc_segment(struct scoutfs_segment_block *seg); #endif diff --git a/utils/src/format.h b/utils/src/format.h index 6f2d46f8..5a01f57b 100644 --- a/utils/src/format.h +++ b/utils/src/format.h @@ -271,6 +271,9 @@ struct scoutfs_segment_item { /* * Each large segment starts with a segment block that describes the * rest of the blocks that make up the segment. + * + * The crc covers the initial total_bytes of the segment but starts + * after the padding. */ struct scoutfs_segment_block { __le32 crc; diff --git a/utils/src/mkfs.c b/utils/src/mkfs.c index c0dfcbf5..678114ef 100644 --- a/utils/src/mkfs.c +++ b/utils/src/mkfs.c @@ -377,6 +377,7 @@ static int write_new_fs(char *path, int fd) item = (void *)(inode + 1); sblk->total_bytes = cpu_to_le32((long)item - (long)sblk); + sblk->crc = cpu_to_le32(crc_segment(sblk)); ret = pwrite(fd, sblk, SCOUTFS_SEGMENT_SIZE, first_segno << SCOUTFS_SEGMENT_SHIFT);