Just a small program to print the fields of a super block.
Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de>
---
 Makefile           |   5 +-
 btrfs-show-super.c | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 205 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
index fe2b432..25ac6d6 100644
--- a/Makefile
+++ b/Makefile
@@ -20,7 +20,7 @@ RESTORE_LIBS=-lz
 
 progs = btrfsctl mkfs.btrfs btrfs-debug-tree btrfs-show btrfs-vol btrfsck \
 	btrfs btrfs-map-logical btrfs-image btrfs-zero-log btrfs-convert \
-	btrfs-find-root btrfs-restore btrfstune
+	btrfs-find-root btrfs-restore btrfstune btrfs-show-super
 
 # make C=1 to enable sparse
 ifdef C
@@ -73,6 +73,9 @@ btrfs-debug-tree: $(objects) debug-tree.o
 btrfs-zero-log: $(objects) btrfs-zero-log.o
 	$(CC) $(CFLAGS) -o btrfs-zero-log $(objects) btrfs-zero-log.o $(LDFLAGS)
$(LIBS)
 
+btrfs-show-super: $(objects) btrfs-show-super.o
+	$(CC) $(CFLAGS) -o btrfs-show-super $(objects) btrfs-show-super.o $(LDFLAGS)
$(LIBS)
+
 btrfs-select-super: $(objects) btrfs-select-super.o
 	$(CC) $(CFLAGS) -o btrfs-select-super $(objects) btrfs-select-super.o
$(LDFLAGS) $(LIBS)
 
diff --git a/btrfs-show-super.c b/btrfs-show-super.c
new file mode 100644
index 0000000..2e1a368
--- /dev/null
+++ b/btrfs-show-super.c
@@ -0,0 +1,201 @@
+/*
+ * Copyright (C) 2012 STRATO AG.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#define _XOPEN_SOURCE 500
+#define _GNU_SOURCE 1
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include "kerncompat.h"
+#include "ctree.h"
+#include "disk-io.h"
+#include "print-tree.h"
+#include "transaction.h"
+#include "list.h"
+#include "version.h"
+#include "utils.h"
+
+
+static void print_usage(void);
+static void dump_superblock(struct btrfs_super_block *sb);
+int main(int argc, char **argv);
+static void btrfs_print_id(char *prefix, u8 *id, size_t size);
+
+
+static void print_usage(void)
+{
+	fprintf(stderr, "usage: btrfs-show-super [-i super_mirror] dev\n");
+	fprintf(stderr, "\tThe super_mirror number is between 0 and %d.\n",
+		BTRFS_SUPER_MIRROR_MAX - 1);
+	fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
+}
+
+int main(int argc, char **argv)
+{
+	int opt;
+	int ret;
+	const char *filename;
+	struct btrfs_super_block sb;
+	int fd = -1;
+	u64 sb_bytenr = btrfs_sb_offset(0);
+
+	while ((opt = getopt(argc, argv, "i:")) != -1) {
+		switch(opt) {
+		case ''i'': {
+				int arg = atoi(optarg);
+
+				if (arg < 0 || arg >= BTRFS_SUPER_MIRROR_MAX) {
+					fprintf(stderr,
+						"Illegal super_mirror %d\n",
+						arg);
+					print_usage();
+					exit(1);
+				}
+				sb_bytenr = btrfs_sb_offset(arg);
+				break;
+			}
+		default:
+			print_usage();
+			exit(1);
+		}
+	}
+
+	if (argc != optind + 1) {
+		print_usage();
+		exit(1);
+	}
+
+	filename = argv[optind];
+	fd = open(filename, O_RDONLY, 0666);
+	if (fd < 0) {
+		fprintf(stderr, "Could not open %s\n", filename);
+		goto error;
+	}
+
+	ret = pread64(fd, &sb, sizeof(sb), sb_bytenr);
+	if (ret < sizeof(sb)) {
+		fprintf(stderr, "Failed to read the superblock on %s at %llu\n",
+			filename, (unsigned long long)sb_bytenr);
+		goto error;
+	}
+
+	dump_superblock(&sb);
+
+error:
+	if (fd != -1)
+		close(fd);
+
+	exit(1);
+}
+
+static void dump_superblock(struct btrfs_super_block *sb)
+{
+	printf("bytenr\t%llu\n", (unsigned long
long)btrfs_super_bytenr(sb));
+	printf("flags\t0x%llx\n", (unsigned long
long)btrfs_super_flags(sb));
+	printf("magic \t0x%llx (little endian)\n",
+	       (unsigned long long)sb->magic);
+	btrfs_print_id("fsid\t", sb->fsid, BTRFS_FSID_SIZE);
+	btrfs_print_id("label\t", (u8 *)sb->label, BTRFS_LABEL_SIZE);
+	printf("generation\t%llu\n",
+	       (unsigned long long)btrfs_super_generation(sb));
+	printf("root\t%llu\n", (unsigned long long)btrfs_super_root(sb));
+	printf("sys_array_size\t%llu\n",
+	       (unsigned long long)btrfs_super_sys_array_size(sb));
+	printf("chunk_root_generation\t%llu\n",
+	       (unsigned long long)btrfs_super_chunk_root_generation(sb));
+	printf("root_level\t%llu\n",
+	       (unsigned long long)btrfs_super_root_level(sb));
+	printf("chunk_root\t%llu\n",
+	       (unsigned long long)btrfs_super_chunk_root(sb));
+	printf("chunk_root_level\t%llu\n",
+	       (unsigned long long)btrfs_super_chunk_root_level(sb));
+	printf("log_root\t%llu\n",
+	       (unsigned long long)btrfs_super_log_root(sb));
+	printf("log_root_transid\t%llu\n",
+	       (unsigned long long)btrfs_super_log_root_transid(sb));
+	printf("log_root_level\t%llu\n",
+	       (unsigned long long)btrfs_super_log_root_level(sb));
+	printf("total_bytes\t%llu\n",
+	       (unsigned long long)btrfs_super_total_bytes(sb));
+	printf("bytes_used\t%llu\n",
+	       (unsigned long long)btrfs_super_bytes_used(sb));
+	printf("sectorsize\t%llu\n",
+	       (unsigned long long)btrfs_super_sectorsize(sb));
+	printf("nodesize\t%llu\n",
+	       (unsigned long long)btrfs_super_nodesize(sb));
+	printf("leafsize\t%llu\n",
+	       (unsigned long long)btrfs_super_leafsize(sb));
+	printf("stripesize\t%llu\n",
+	       (unsigned long long)btrfs_super_stripesize(sb));
+	printf("root_dir\t%llu\n",
+	       (unsigned long long)btrfs_super_root_dir(sb));
+	printf("num_devices\t%llu\n",
+	       (unsigned long long)btrfs_super_num_devices(sb));
+	printf("compat_flags\t0x%llx\n",
+	       (unsigned long long)btrfs_super_compat_flags(sb));
+	printf("compat_ro_flags\t0x%llx\n",
+	       (unsigned long long)btrfs_super_compat_ro_flags(sb));
+	printf("incompat_flags\t0x%llx\n",
+	       (unsigned long long)btrfs_super_incompat_flags(sb));
+	printf("csum_type\t%llu\n",
+	       (unsigned long long)btrfs_super_csum_type(sb));
+	printf("cache_generation\t%llu\n",
+	       (unsigned long long)btrfs_super_cache_generation(sb));
+
+	printf("dev_item.type\t%llu\n", (unsigned long long)
+	       btrfs_stack_device_type(&sb->dev_item));
+	printf("dev_item.total_bytes\t%llu\n", (unsigned long long)
+	       btrfs_stack_device_total_bytes(&sb->dev_item));
+	printf("dev_item.bytes_used\t%llu\n", (unsigned long long)
+	       btrfs_stack_device_bytes_used(&sb->dev_item));
+	printf("dev_item.io_align\t%u\n", (unsigned int)
+	       btrfs_stack_device_io_align(&sb->dev_item));
+	printf("dev_item.io_width\t%u\n", (unsigned int)
+	       btrfs_stack_device_io_width(&sb->dev_item));
+	printf("dev_item.sector_size\t%u\n", (unsigned int)
+	       btrfs_stack_device_sector_size(&sb->dev_item));
+	printf("dev_item.devid\t%llu\n",
+	       btrfs_stack_device_id(&sb->dev_item));
+	printf("dev_item.dev_group\t%u\n", (unsigned int)
+	       btrfs_stack_device_group(&sb->dev_item));
+	printf("dev_item.seek_speed\t%u\n", (unsigned int)
+	       btrfs_stack_device_seek_speed(&sb->dev_item));
+	printf("dev_item.bandwidth\t%u\n", (unsigned int)
+	       btrfs_stack_device_bandwidth(&sb->dev_item));
+	printf("dev_item.generation\t%llu\n", (unsigned long long)
+	       btrfs_stack_device_generation(&sb->dev_item));
+}
+
+static void btrfs_print_id(char *prefix, u8 *id, size_t size)
+{
+	int i;
+	char buf[3 * size];
+
+	for (i = 0; i < size; i++) {
+		buf[3 * i] = ((id[i] >> 4) < 10) ?
+			      (id[i] >> 4) + ''0'' :
+			      (id[i] >> 4) + ''A'' - 10;
+		buf[3 * i + 1] = (id[i] & 0xf) < 10 ?
+				  (id[i] & 0xf) + ''0'' :
+				  (id[i] & 0xf) + ''A'' - 10;
+		buf[3 * i + 2] = (i == size - 1) ? ''\0'' :
'':'';
+	}
+	printf("%s%s\n", prefix, buf);
+}
-- 
1.7.12.4
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs"
in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Goffredo Baroncelli
2012-Oct-23  19:45 UTC
Re: [PATCH] Btrfs-progs: add btrfs-show-super tool
Hi Stefan, On 2012-10-19 09:18, Stefan Behrens wrote:> Just a small program to print the fields of a super block. > > Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de> > ---[...]> + > + > +static void print_usage(void) > +{ > + fprintf(stderr, "usage: btrfs-show-super [-i super_mirror] dev\n"); > + fprintf(stderr, "\tThe super_mirror number is between 0 and %d.\n", > + BTRFS_SUPER_MIRROR_MAX - 1);What about adding a flag ''-a'' to show all the superblocks ?> + fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION); > +} > +[...]> +static void dump_superblock(struct btrfs_super_block *sb) > +{ > + printf("bytenr\t%llu\n", (unsigned long long)btrfs_super_bytenr(sb)); > + printf("flags\t0x%llx\n", (unsigned long long)btrfs_super_flags(sb)); > + printf("magic \t0x%llx (little endian)\n", > + (unsigned long long)sb->magic);I suggest to print if magic matches or not. The same if csum matches or not. This help to understand if the superblock is valid or not.> + btrfs_print_id("fsid\t", sb->fsid, BTRFS_FSID_SIZE);Please, use the uuid_unparse() function, so the output is like the other btrfs tools. Be aware that the libuuid is already linked.> + btrfs_print_id("label\t", (u8 *)sb->label, BTRFS_LABEL_SIZE);Does make sense to output the label as hex digits ? I prefer the ascii value, in order to avoid garbage I suggest something like: #include <ctype.h> char *p; for( p= sb->label ; *p ; p++ ) putchar(isprint(*p) ? *p : ''_'' );> + printf("generation\t%llu\n", > + (unsigned long long)btrfs_super_generation(sb)); > + printf("root\t%llu\n", (unsigned long long)btrfs_super_root(sb)); > + printf("sys_array_size\t%llu\n", > + (unsigned long long)btrfs_super_sys_array_size(sb));[...]> + printf("dev_item.generation\t%llu\n", (unsigned long long) > + btrfs_stack_device_generation(&sb->dev_item));Could you please add also the sb->dev_item.uuid and sb->dev_item.fsid fields ? Moreover I suggest to align every value to the right. For example a my test shows: bytenr 65536 flags 0x1 magic 0x4d5f53665248425f (little endian) fsid 1A:FD:C9:5D:3C:27:43:90:A2:C8:7A:F6:87:C8:82:B1 label 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 generation 4 root 4214784 sys_array_size 97 chunk_root_generation 2 root_level 0 chunk_root 139264 chunk_root_level 0 log_root 0 log_root_transid 0 log_root_level 0 total_bytes 1073741824 [...] I think that it could be more readable as bytenr 65536 flags 0x1 magic 0x4d5f53665248425f (little endian) generation 4 root 4214784 sys_array_size 97 chunk_root_generation 2 root_level 0 chunk_root 139264 chunk_root_level 0 log_root 0 log_root_transid 0 log_root_level 0 total_bytes 1073741824 In order to avoid to check every space, I suggest to change every printf from> + printf("dev_item.type\t%llu\n", (unsigned long long) > + btrfs_stack_device_type(&sb->dev_item));to printf("%-50s%20llu\n", "dev_item.type", (unsigned long long)btrfs_stack_device_type(&sb->dev_item)); BR G.Baroncelli -- gpg @keyserver.linux.it: Goffredo Baroncelli (kreijackATinwind.it> Key fingerprint BBF5 1610 0B64 DAC6 5F7D 17B2 0EDA 9B37 8B82 E0B5 -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Goffredo Baroncelli
2012-Oct-29  17:55 UTC
Re: [PATCH] Btrfs-progs: add btrfs-show-super tool
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 2012-10-29 12:00, Stefan Behrens wrote:> On Tue, 23 Oct 2012 21:45:08 +0200, Goffredo Baroncelli wrote: >> On 2012-10-19 09:18, Stefan Behrens wrote: >>> Just a small program to print the fields of a super block. >>> >>> Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de>[...]>> > > Hi Goffredo, > > All your comments are correct and are good suggestions for > improvements. Thank you! > > But I am not going to enhance this tool beyond to its current > rudimental state since I''m lazy and it works for me :) > > Such a low-level debug tool is not a candidate to ever be added to > the btrfs-progs IMO. Posting the sources to the mailing list was > just with the goal to use the Internet to create another backup of > the sources :)I think that it would be a great debug tool. Can I update your patch with my improvements ? G.Baroncelli> > Regards, Stefan . >- -- gpg @keyserver.linux.it: Goffredo Baroncelli (kreijackATinwind.it> Key fingerprint BBF5 1610 0B64 DAC6 5F7D 17B2 0EDA 9B37 8B82 E0B5 -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.19 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQIcBAEBAgAGBQJQjsMgAAoJEA7amzeLguC1p8kQAMQ2H8B3NT8l9DDo3HfQL5bq XKs8K3GUyYyl0gNYmrTtNwyHDnPffcz5FTTyIqBKXaiHmiMpCVDU6tKg5lD01ycP 61E39Zt6e2A0RK7iLW6QxXvsmx7yobqW7IMGvrZB6Kdnm8GEJ2Qhk4Xwx2yWmkFl k9cRrp2hdKNaJcheA+DtJm2HaJGj/9vLBSPq6e5fLEcn+alsQHgzp1+VGvwW7X5E OdoRlg2lRwG762mTC6Zh+W9+ArFIpBE+JGMSD9j06vH/SsnblcH5nPa5t0mVL0tQ jhrUra8I6L4Nz9ag0w40QlyHJdjflywTM5AcYoRSphHHemZzx1Po7pv/VplVlWGc /98merp0PPiaTR+0vzqq4bgw+kajSWT0PgKd9O+nLcSNa4DoJQKMct470r93KqKM lBVa8/6/HL5JEJk3S+wFZwJOriGZdEFVAi/V2fqpufEVi+aBU3buyO0QffBW3/Wu uP23kc57sPnhVt+4Zivmc2NyuiZ6EKSgzZEJSS+uWR87/PM80bp68g7Zt6Pa5yO4 E8QNkmGPKFMhlXTagq2R/+iyC9WUI4gKetAlnT5TTZ7gP6utIzS80buJ005CvATv BWw+R2HgmIMUb9Bb0zzYMgsLXCypxx2uYHraWGPY3VpfcntJKBi+uGbEuBm4EM5q PwTRjlBlGIgtCOixLQFj =ERTv -----END PGP SIGNATURE----- -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 10/29/2012 18:55, Goffredo Baroncelli wrote:> On 2012-10-29 12:00, Stefan Behrens wrote: >> All your comments are correct and are good suggestions for >> improvements. Thank you![...]> I think that it would be a great debug tool. Can I update your patch > with my improvements ?Great, thanks! -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html