]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - bcache-super-show.c
bcacheadm unregister devices
[bcachefs-tools-debian] / bcache-super-show.c
index 1607a677c10fbbfe4e2ff20fe2b9f8b431634d30..f645af68569f42baf445c704314602ecc72735c8 100644 (file)
@@ -1,7 +1,15 @@
+/*
+ * Author: Gabriel de Perthuis <g2p.code@gmail.com>
+ *
+ * GPLv2
+ */
+
+
 #define _FILE_OFFSET_BITS      64
 #define __USE_FILE_OFFSET64
 #define _XOPEN_SOURCE 500
 
+#include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
 
 #include "bcache.h"
 
-
-static void usage()
+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;
+       struct cache_sb sb_stack, *sb = &sb_stack;
+       size_t bytes = sizeof(*sb);
+       bool force_csum = false;
 
        while ((o = getopt(argc, argv, "f")) != EOF)
                switch (o) {
@@ -60,121 +65,25 @@ int main(int argc, char **argv)
                exit(2);
        }
 
-       if (pread(fd, &sb, sizeof(sb), SB_START) != sizeof(sb)) {
+       if (pread(fd, sb, bytes, SB_START) != bytes) {
                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);
-       }
+       if (sb->keys) {
+               bytes = sizeof(*sb) + sb->keys * sizeof(uint64_t);
+               sb = malloc(bytes);
 
-       printf("sb.csum\t\t\t%" 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");
+               if (pread(fd, sb, bytes, SB_START) != bytes) {
+                       fprintf(stderr, "Couldn't read\n");
                        exit(2);
                }
        }
 
-       printf("sb.version\t\t%" PRIu64, sb.version);
-       switch (sb.version) {
-               // These are handled the same by the kernel
-               case BCACHE_SB_VERSION_CDEV:
-               case BCACHE_SB_VERSION_CDEV_WITH_UUID:
-                       printf(" [cache device]\n");
-                       break;
-
-               // The second adds data offset support
-               case BCACHE_SB_VERSION_BDEV:
-               case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
-                       printf(" [backing device]\n");
-                       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",
-              sb.block_size,
-              sb.bucket_size);
-
-       if (!SB_IS_BDEV(&sb)) {
-               // total_sectors includes the superblock;
-               printf("dev.cache.first_sector\t%u\n"
-                      "dev.cache.cache_sectors\t%ju\n"
-                      "dev.cache.total_sectors\t%ju\n"
-                      "dev.cache.discard\t%s\n"
-                      "dev.cache.pos\t\t%u\n",
-                      sb.bucket_size * sb.first_bucket,
-                      sb.bucket_size * (sb.nbuckets - sb.first_bucket),
-                      sb.bucket_size * sb.nbuckets,
-                      CACHE_DISCARD(&sb) ? "yes" : "no",
-                      sb.nr_this_dev);
-       } else {
-               uint64_t first_sector;
-               if (sb.version == BCACHE_SB_VERSION_BDEV) {
-                       first_sector = BDEV_DATA_START_DEFAULT;
-               } else {
-                       if (sb.keys == 1 || sb.d[0]) {
-                               fprintf(stderr,
-                                       "Possible experimental format detected, bailing\n");
-                               exit(3);
-                       }
-                       first_sector = sb.data_offset;
-               }
-               printf("dev.data.first_sector\t%ju\n"
-                      "dev.data.cache_mode\t%ju",
-                      first_sector,
-                      BDEV_CACHE_MODE(&sb));
-
-               switch (BDEV_CACHE_MODE(&sb)) {
-                       case CACHE_MODE_WRITETHROUGH:
-                               printf(" [writethrough]\n");
-                               break;
-                       case CACHE_MODE_WRITEBACK:
-                               printf(" [writeback]\n");
-                               break;
-                       case CACHE_MODE_WRITEAROUND:
-                               printf(" [writearound]\n");
-                               break;
-                       case CACHE_MODE_NONE:
-                               printf(" [no caching]\n");
-                               break;
-                       default:
-                               putchar('\n');
-               }
-       }
-       putchar('\n');
-
-       uuid_unparse(sb.set_uuid, uuid);
-       printf("cset.uuid\t\t%s\n", uuid);
+       if (!SB_IS_BDEV(sb))
+               show_super_cache(sb, force_csum);
+       else
+               show_super_backingdev(sb, force_csum);
 
        return 0;
 }