]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
Add a command to display a bcache superblock.
authorGabriel <g2p.code@gmail.com>
Sat, 9 Mar 2013 12:22:24 +0000 (13:22 +0100)
committerGabriel <g2p.code@gmail.com>
Sat, 9 Mar 2013 13:33:08 +0000 (14:33 +0100)
.gitignore
Makefile
README
bcache-super-show.c [new file with mode: 0644]

index c1743e269bb28531bd2588b030dc2a36f31f5806..e86f25622f937066fc5017ea9efffc992b0c790f 100644 (file)
@@ -1,3 +1,4 @@
+bcache-super-show
 make-bcache
 probe-bcache
 .*
index d15525b5396f818e730a9bd00440aae7a5d0d0c6..ce8c0c46a1140c69ddbf006cbaccb6585a879c0b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,21 +2,23 @@
 PREFIX=/usr
 CFLAGS+=-O2 -Wall -g
 
-all: make-bcache probe-bcache
+all: make-bcache probe-bcache bcache-super-show
 
-install: make-bcache probe-bcache
-       install -m0755 make-bcache      $(DESTDIR)${PREFIX}/sbin/
+install: make-bcache probe-bcache bcache-super-show
+       install -m0755 make-bcache bcache-super-show    $(DESTDIR)${PREFIX}/sbin/
        install -m0755 probe-bcache     $(DESTDIR)/sbin/
        install -m0644 61-bcache.rules  $(DESTDIR)/lib/udev/rules.d/
        -install -m0755 initramfs/script $(DESTDIR)/etc/initramfs-tools/scripts/init-premount/bcache
        -install -m0755 initramfs/hook  $(DESTDIR)/etc/initramfs-tools/hooks/bcache
-       install -m0644 *.8 $(DESTDIR)${PREFIX}/share/man/man8
+       install -m0644 -- *.8 $(DESTDIR)${PREFIX}/share/man/man8
 #      install -m0755 bcache-test $(DESTDIR)${PREFIX}/sbin/
 
 clean:
-       $(RM) -f make-bcache probe-bcache bcache-test *.o
+       $(RM) -f make-bcache probe-bcache bcache-super-show bcache-test *.o
 
 bcache-test: LDLIBS += -lm -lssl -lcrypto
 make-bcache: LDLIBS += -luuid
 make-bcache: bcache.o
 probe-bcache: LDLIBS += -luuid
+bcache-super-show: LDLIBS += -luuid
+bcache-super-show: bcache.o
diff --git a/README b/README
index 3aa525f3bc07c6ce8ef7142347c98143255631da..4a13db68cf47c73e7f44507aeed395c1265a38df 100644 (file)
--- a/README
+++ b/README
@@ -22,3 +22,6 @@ Only necessary until support for the bcache superblock is included
 in blkid; in the meantime, provides just enough functionality for a udev script
 to create the /dev/disk/by-uuid symlink. The arguments it does support are the
 same as for blkid.
+
+bcache-super-show
+Prints the bcache superblock of a cache device or a backing device.
diff --git a/bcache-super-show.c b/bcache-super-show.c
new file mode 100644 (file)
index 0000000..6f9f6a7
--- /dev/null
@@ -0,0 +1,145 @@
+#define _FILE_OFFSET_BITS      64
+#define __USE_FILE_OFFSET64
+#define _XOPEN_SOURCE 500
+
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <linux/fs.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <uuid/uuid.h>
+
+#include "bcache.h"
+
+
+static void usage()
+{
+       fprintf(stderr, "Usage: bcache-super-show [-f] <device>\n");
+}
+
+
+int main(int argc, char **argv)
+{
+       bool force_csum = false;
+       int o;
+       extern char *optarg;
+       struct cache_sb sb;
+       char uuid[40];
+       uint64_t expected_csum;
+
+       while ((o = getopt(argc, argv, "f")) != EOF)
+               switch (o) {
+                       case 'f':
+                               force_csum = 1;
+                               break;
+
+                       default:
+                               usage();
+                               exit(1);
+               }
+
+       argv += optind;
+       argc -= optind;
+
+       if (argc != 1) {
+               usage();
+               exit(1);
+       }
+
+       int fd = open(argv[0], O_RDONLY);
+       if (fd < 0) {
+               printf("Can't open dev %s: %s\n", argv[0], strerror(errno));
+               exit(2);
+       }
+
+       if (pread(fd, &sb, sizeof(sb), 4096) != sizeof(sb)) {
+               fprintf(stderr, "Couldn't read\n");
+               exit(2);
+       }
+
+       printf("sb.magic\t\t");
+       if (! memcmp(sb.magic, bcache_magic, 16)) {
+               printf("ok\n");
+       } else {
+               printf("bad magic\n");
+               fprintf(stderr, "Invalid superblock (bad magic)\n");
+               exit(2);
+       }
+
+       printf("sb.first_sector\t\t%" PRIu64, sb.offset);
+       if (sb.offset == SB_SECTOR) {
+               printf(" [match]\n");
+       } else {
+               printf(" [expected %ds]\n", SB_SECTOR);
+               fprintf(stderr, "Invalid superblock (bad sector)\n");
+               exit(2);
+       }
+
+       printf("sb.csum\t\t\t0x%" PRIx64, sb.csum);
+       expected_csum = csum_set(&sb);
+       if (sb.csum == expected_csum) {
+               printf(" [match]\n");
+       } else {
+               printf(" [expected %" PRIX64 "]\n", expected_csum);
+               if (! force_csum) {
+                       fprintf(stderr, "Corrupt superblock (bad csum)\n");
+                       exit(2);
+               }
+       }
+
+       printf("sb.version\t\t%" PRIu64, sb.version);
+       switch (sb.version) {
+               case 1:
+                       printf(" [backing device]\n");
+                       break;
+
+               case 2:
+                       printf(" [cache device]\n");
+                       break;
+
+               case 3:
+                       printf(" [backing device with offset]\n");
+                       // XXX Kernel side bcache.h says different, and implements neither
+                       return 0; //break;
+
+               default:
+                       printf(" [unknown]\n");
+                       // exit code?
+                       return 0;
+       }
+
+       putchar('\n');
+
+       uuid_unparse(sb.uuid, uuid);
+       printf("dev.uuid\t\t%s\n", uuid);
+
+       printf(
+                       "dev.sectors_per_block\t%u\n"
+                       "dev.sectors_per_bucket\t%u\n"
+                       "dev.bucket_count\t%ju\n"
+                       "dev.cache_count\t\t%u\n" // expect version == 2 ? 1 : 0
+                       "dev.data.first_bucket\t%u\n",
+                       sb.block_size,
+                       sb.bucket_size,
+                       sb.nbuckets,
+                       sb.nr_this_dev,
+                       sb.first_bucket);
+
+       printf("dev.data.first_sector\t%u\n", sb.bucket_size * sb.first_bucket);
+       putchar('\n');
+
+       uuid_unparse(sb.set_uuid, uuid);
+       printf("cset.uuid\t\t%s\n", uuid);
+
+       printf("cset.cache_count\t%u\n\n", sb.nr_in_set);
+
+       return 0;
+}