[RFC PATCH 2/4] struct_offset: print offset of each member of the on-disk data structure

Coly Li
2020-07-05T16:04:53Z

This is a helper small program to print out the offset in bytes of each
member of the on-disk data structure. Currently the member print lines
are coded manually, hope latter it can be more intelligent to avoid the
hard code.

Signed-off-by: Coly Li <colyli@suse.de>
---
Makefile | 4 +++-
struct_offset.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 65 insertions(+), 1 deletion(-)
create mode 100644 struct_offset.c

diff --git a/Makefile b/Makefile
index 2c326cf..b352d21 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ DRACUTLIBDIR=/lib/dracut
INSTALL=install
CFLAGS+=-O2 -Wall -g

-all: make-bcache probe-bcache bcache-super-show bcache-register bcache
+all: make-bcache probe-bcache bcache-super-show bcache-register bcache struct_offset

install: make-bcache probe-bcache bcache-super-show
$(INSTALL) -m0755 make-bcache bcache-super-show bcache $(DESTDIR)${PREFIX}/sbin/
@@ -22,6 +22,8 @@ clean:

bcache-test: LDLIBS += `pkg-config --libs openssl` -lm

+struct_offset: struct_offset.o
+
make-bcache: LDLIBS += `pkg-config --libs uuid blkid smartcols`
make-bcache: CFLAGS += `pkg-config --cflags uuid blkid smartcols`
make-bcache: make.o crc64.o lib.o zoned.o
diff --git a/struct_offset.c b/struct_offset.c
new file mode 100644
index 0000000..6061259
--- /dev/null
+++ b/struct_offset.c
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Author: Coly Li <colyli@suse.de>
+ *
+ * Print out offset of each member of on-disk structure
+ */
+
+#include <stdio.h>
+#include <stddef.h>
+#include <inttypes.h>
+#include <stdbool.h>
+
+#include "bcache.h"
+
+
+#define OFF_SB(m) offsetof(struct cache_sb, m)
+
+void print_cache_sb()
+{
+ printf(" struct cache_sb {:\n");
+ printf("/* %3.3lx */ uint64_t csum;\n", OFF_SB(csum));
+ printf("/* %3.3lx */ uint64_t offset;\n", OFF_SB(offset));
+ printf("/* %3.3lx */ uint64_t version;\n", OFF_SB(version));
+ printf("/* %3.3lx */ uint8_t magic[6];\n", OFF_SB(magic));
+ printf("/* %3.3lx */ uint8_t uuid[16];\n", OFF_SB(uuid));
+ printf(" union {;\n");
+ printf("/* %3.3lx */ uint8_t set_uuid;\n", OFF_SB(set_uuid));
+ printf("/* %3.3lx */ uint64_t set_magic;\n", OFF_SB(set_magic));
+ printf(" };\n");
+ printf("/* %3.3lx */ uint8_t label[%u];\n", OFF_SB(label),
+ SB_LABEL_SIZE);
+ printf("/* %3.3lx */ uint64_t flags;\n", OFF_SB(flags));
+ printf("/* %3.3lx */ uint64_t seq;\n", OFF_SB(seq));
+ printf("/* %3.3lx */ uint64_t pad[8];\n", OFF_SB(pad));
+ printf(" union {\n");
+ printf(" struct {\n");
+ printf("/* %3.3lx */ uint64_t nbuckets;\n", OFF_SB(nbuckets));
+ printf("/* %3.3lx */ uint16_t block_size;\n", OFF_SB(block_size));
+ printf("/* %3.3lx */ uint16_t bucket_size;\n", OFF_SB(bucket_size));
+ printf("/* %3.3lx */ uint16_t nr_in_set;\n", OFF_SB(nr_in_set));
+ printf("/* %3.3lx */ uint16_t nr_this_dev;\n", OFF_SB(nr_this_dev));
+ printf(" };\n");
+ printf(" struct {\n");
+ printf("/* %3.3lx */ uint64_t data_offset;\n", OFF_SB(data_offset));
+ printf(" };\n");
+ printf(" };\n");
+ printf("/* %3.3lx */ uint32_t last_mount;\n", OFF_SB(last_mount));
+ printf("/* %3.3lx */ uint16_t first_bucket;\n", OFF_SB(first_bucket));
+ printf(" union {\n");
+ printf("/* %3.3lx */ uint16_t njournal_buckets;\n", OFF_SB(njournal_buckets));
+ printf("/* %3.3lx */ uint16_t keys;\n", OFF_SB(keys));
+ printf(" };\n");
+ printf("/* %3.3lx */ uint64_t d[%u];\n", OFF_SB(d), SB_JOURNAL_BUCKETS);
+ printf("/* %3.3lx */ }\n", OFF_SB(d) + sizeof(uint64_t) * SB_JOURNAL_BUCKETS);
+}
+
+int main(int argc, char *argv[])
+{
+ print_cache_sb();
+ return 0;
+}
--
2.26.2