From ea2ec838ec4d123ed6ccf1bf20767fe9adbb97e0 Mon Sep 17 00:00:00 2001 From: Zach Brown Date: Thu, 28 Jun 2018 15:24:22 -0700 Subject: [PATCH] scoutfs-utils: use one super and verify its crc Signed-off-by: Zach Brown --- utils/src/crc.c | 13 ------------- utils/src/crc.h | 1 - utils/src/format.h | 20 +++++++------------- utils/src/mkfs.c | 37 +++++++++++++++++-------------------- utils/src/print.c | 44 ++++++++++++++++---------------------------- 5 files changed, 40 insertions(+), 75 deletions(-) diff --git a/utils/src/crc.c b/utils/src/crc.c index 1d027a32..38640fbc 100644 --- a/utils/src/crc.c +++ b/utils/src/crc.c @@ -37,16 +37,3 @@ 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; -} diff --git a/utils/src/crc.h b/utils/src/crc.h index 03ce2891..6878bf2f 100644 --- a/utils/src/crc.h +++ b/utils/src/crc.h @@ -8,6 +8,5 @@ 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_btree_block(struct scoutfs_btree_block *bt); #endif diff --git a/utils/src/format.h b/utils/src/format.h index 65c10b69..9addabf5 100644 --- a/utils/src/format.h +++ b/utils/src/format.h @@ -33,17 +33,15 @@ #define SCOUTFS_BLOCK_PAGE_ORDER (SCOUTFS_BLOCK_SHIFT - PAGE_SHIFT) /* - * The super blocks leave some room at the start of the first block for - * platform structures like boot loaders. + * The super block leaves some room before the first block for platform + * structures like boot loaders. */ -#define SCOUTFS_SUPER_BLKNO ((64 * 1024) >> SCOUTFS_BLOCK_SHIFT) -#define SCOUTFS_SUPER_NR 2 +#define SCOUTFS_SUPER_BLKNO ((64ULL * 1024) >> SCOUTFS_BLOCK_SHIFT) /* - * This header is found at the start of every block so that we can - * verify that it's what we were looking for. The crc and padding - * starts the block so that its calculation operations on a nice 64bit - * aligned region. + * This header is stored at the start of btree blocks and the super + * block for verification. The crc is calculated by zeroing the crc and + * padding so the buffer is large and aligned. */ struct scoutfs_block_header { __le32 crc; @@ -183,11 +181,7 @@ struct scoutfs_btree_item { } __packed; struct scoutfs_btree_block { - __le64 fsid; - __le64 blkno; - __le64 seq; - __le32 crc; - __le32 _pad; + struct scoutfs_block_header hdr; __le16 free_end; __le16 free_reclaim; __le16 nr_items; diff --git a/utils/src/mkfs.c b/utils/src/mkfs.c index 8002fc6b..c0dfcbf5 100644 --- a/utils/src/mkfs.c +++ b/utils/src/mkfs.c @@ -184,7 +184,6 @@ static int write_new_fs(char *path, int fd) u64 free_start; u64 free_len; int ret; - u64 i; gettimeofday(&tv, NULL); @@ -228,9 +227,8 @@ static int write_new_fs(char *path, int fd) super->total_blocks = cpu_to_le64(total_blocks); super->next_seg_seq = cpu_to_le64(2); - /* align the btree ring to the segment after the supers */ - blkno = round_up(SCOUTFS_SUPER_BLKNO + SCOUTFS_SUPER_NR, - SCOUTFS_SEGMENT_BLOCKS); + /* align the btree ring to the segment after the super */ + blkno = round_up(SCOUTFS_SUPER_BLKNO + 1, SCOUTFS_SEGMENT_BLOCKS); /* first usable segno follows manifest ring */ ring_blocks = calc_btree_ring_blocks(total_segs); first_segno = (blkno + ring_blocks) / SCOUTFS_SEGMENT_BLOCKS; @@ -249,9 +247,9 @@ static int write_new_fs(char *path, int fd) super->alloc_root.height = 1; memset(bt, 0, SCOUTFS_BLOCK_SIZE); - bt->fsid = super->hdr.fsid; - bt->blkno = cpu_to_le64(blkno); - bt->seq = cpu_to_le64(1); + bt->hdr.fsid = super->hdr.fsid; + bt->hdr.blkno = cpu_to_le64(blkno); + bt->hdr.seq = cpu_to_le64(1); bt->nr_items = cpu_to_le16(2); /* btree item allocated from the back of the block */ @@ -279,7 +277,8 @@ static int write_new_fs(char *path, int fd) ebk->major = cpu_to_be64(free_len); ebk->minor = cpu_to_be64(free_start + free_len - 1); - bt->crc = cpu_to_le32(crc_btree_block(bt)); + bt->hdr._pad = 0; + bt->hdr.crc = cpu_to_le32(crc_block(&bt->hdr)); ret = write_raw_block(fd, blkno, bt); if (ret) @@ -293,9 +292,9 @@ static int write_new_fs(char *path, int fd) super->manifest.level_counts[1] = cpu_to_le64(1); memset(bt, 0, SCOUTFS_BLOCK_SIZE); - bt->fsid = super->hdr.fsid; - bt->blkno = cpu_to_le64(blkno); - bt->seq = cpu_to_le64(1); + bt->hdr.fsid = super->hdr.fsid; + bt->hdr.blkno = cpu_to_le64(blkno); + bt->hdr.seq = cpu_to_le64(1); bt->nr_items = cpu_to_le16(1); /* btree item allocated from the back of the block */ @@ -323,7 +322,8 @@ static int write_new_fs(char *path, int fd) ino_key->ski_ino = cpu_to_le64(SCOUTFS_ROOT_INO); ino_key->sk_type = SCOUTFS_INODE_TYPE; - bt->crc = cpu_to_le32(crc_btree_block(bt)); + bt->hdr._pad = 0; + bt->hdr.crc = cpu_to_le32(crc_block(&bt->hdr)); ret = write_raw_block(fd, blkno, bt); if (ret) @@ -385,14 +385,11 @@ static int write_new_fs(char *path, int fd) goto out; } - /* write the two super blocks */ - for (i = 0; i < SCOUTFS_SUPER_NR; i++) { - super->hdr.seq = cpu_to_le64(i + 1); - ret = write_block(fd, SCOUTFS_SUPER_BLKNO + i, NULL, - &super->hdr); - if (ret) - goto out; - } + /* write the super block */ + super->hdr.seq = cpu_to_le64(1); + ret = write_block(fd, SCOUTFS_SUPER_BLKNO, NULL, &super->hdr); + if (ret) + goto out; if (fsync(fd)) { ret = -errno; diff --git a/utils/src/print.c b/utils/src/print.c index 7df3f46b..5b67f564 100644 --- a/utils/src/print.c +++ b/utils/src/print.c @@ -373,13 +373,13 @@ static int print_btree_block(int fd, struct scoutfs_super_block *super, if (bt->level == level) { printf("%s btree blkno %llu\n" - " fsid %llx blkno %llu seq %llu crc %08x \n" + " crc %08x fsid %llx seq %llu blkno %llu \n" " level %u free_end %u free_reclaim %u nr_items %u\n", which, le64_to_cpu(ref->blkno), - le64_to_cpu(bt->fsid), - le64_to_cpu(bt->blkno), - le64_to_cpu(bt->seq), - le32_to_cpu(bt->crc), + le32_to_cpu(bt->hdr.crc), + le64_to_cpu(bt->hdr.fsid), + le64_to_cpu(bt->hdr.seq), + le64_to_cpu(bt->hdr.blkno), bt->level, le16_to_cpu(bt->free_end), le16_to_cpu(bt->free_reclaim), @@ -490,33 +490,19 @@ static void print_super_block(struct scoutfs_super_block *super, u64 blkno) printf("\n"); } -static int print_super_blocks(int fd) +static int print_volume(int fd) { - struct scoutfs_super_block *super; - struct scoutfs_super_block recent = { .hdr.seq = 0 }; - unsigned long *seg_map; + struct scoutfs_super_block *super = NULL; + unsigned long *seg_map = NULL; u64 nr_segs; int ret = 0; int err; - int i; - int r = 0; - for (i = 0; i < SCOUTFS_SUPER_NR; i++) { - super = read_block(fd, SCOUTFS_SUPER_BLKNO + i); - if (!super) - return -ENOMEM; + super = read_block(fd, SCOUTFS_SUPER_BLKNO); + if (!super) + return -ENOMEM; - if (le64_to_cpu(super->hdr.seq) > le64_to_cpu(recent.hdr.seq)) { - memcpy(&recent, super, sizeof(recent)); - r = i; - } - - free(super); - } - - super = &recent; - - print_super_block(super, SCOUTFS_SUPER_BLKNO + r); + print_super_block(super, SCOUTFS_SUPER_BLKNO); nr_segs = le64_to_cpu(super->total_blocks) / SCOUTFS_SEGMENT_BLOCKS; seg_map = alloc_bits(nr_segs); @@ -524,7 +510,7 @@ static int print_super_blocks(int fd) ret = -ENOMEM; fprintf(stderr, "failed to alloc %llu seg map: %s (%d)\n", nr_segs, strerror(errno), errno); - return ret; + goto out; } ret = print_btree(fd, super, "alloc", &super->alloc_root, @@ -539,6 +525,8 @@ static int print_super_blocks(int fd) if (err && !ret) ret = err; +out: + free(super); free(seg_map); return ret; @@ -564,7 +552,7 @@ static int print_cmd(int argc, char **argv) return ret; } - ret = print_super_blocks(fd); + ret = print_volume(fd); close(fd); return ret; };