All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Sandeen <sandeen@redhat.com>
To: linux-btrfs <linux-btrfs@vger.kernel.org>
Subject: [PATCH] btrfs-show-super: don't try to print not-superblocks
Date: Tue, 13 May 2014 21:03:04 -0500	[thread overview]
Message-ID: <5372CED8.7030404@redhat.com> (raw)

If we point btrfs-show-super at a not-btrfs-device and
try to print all superblocks, bad things are apt to happen:

superblock: bytenr=274877906944, device=/dev/sdc2
---------------------------------------------------------
btrfs-show-super: ctree.h:1984: btrfs_super_csum_size: Assertion `!(t >= (sizeof(btrfs_csum_sizes) / sizeof((btrfs_csum_sizes)[0])))' failed.
csum			0xAborted

Don't try to print superblocks that don't look like superblocks,
and add an "-f" (force) option to try anyway, if the user
really wants to give it a shot.

Fix some spelling & capitalization while we're at it.

The manpage says that if any problem happens, 1 will
be returned, but that's already not true today LOL, so
I didn't bother to make it true when we detect bad
sb magic, either...

I figure it's worth continuing and trying all superblocks
in case just one has a corrupt magic.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---

diff --git a/btrfs-show-super.c b/btrfs-show-super.c
index d4df0ac..023f099 100644
--- a/btrfs-show-super.c
+++ b/btrfs-show-super.c
@@ -40,16 +40,17 @@
 static void print_usage(void);
 static void dump_superblock(struct btrfs_super_block *sb);
 int main(int argc, char **argv);
-static int load_and_dump_sb(char *, int fd, u64 sb_bytenr);
+static int load_and_dump_sb(char *, int fd, u64 sb_bytenr, int force);
 
 
 static void print_usage(void)
 {
 	fprintf(stderr,
-		"usage: btrfs-show-super [-i super_mirror|-a] dev [dev..]\n");
+		"usage: btrfs-show-super [-i super_mirror|-a] [-f] dev [dev..]\n");
 	fprintf(stderr, "\tThe super_mirror number is between 0 and %d.\n",
 		BTRFS_SUPER_MIRROR_MAX - 1);
-	fprintf(stderr, "\tIf -a is passed all the superblocks are showed.\n");
+	fprintf(stderr, "\tIf -a is passed all the superblocks are shown.\n");
+	fprintf(stderr, "\tPass -f to attempt to dump superblocks with bad magic.\n");
 	fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
 }
 
@@ -60,10 +61,11 @@ int main(int argc, char **argv)
 	char *filename;
 	int fd = -1;
 	int i;
+	int force = 0;
 	u64 arg;
 	u64 sb_bytenr = btrfs_sb_offset(0);
 
-	while ((opt = getopt(argc, argv, "ai:")) != -1) {
+	while ((opt = getopt(argc, argv, "afi:")) != -1) {
 		switch (opt) {
 		case 'i':
 			arg = arg_strtou64(optarg);
@@ -81,6 +83,10 @@ int main(int argc, char **argv)
 			all = 1;
 			break;
 
+		case 'f':
+			force = 1;
+			break;
+
 		default:
 			print_usage();
 			exit(1);
@@ -104,7 +110,8 @@ int main(int argc, char **argv)
 			int idx;
 			for (idx = 0; idx < BTRFS_SUPER_MIRROR_MAX; idx++) {
 				sb_bytenr = btrfs_sb_offset(idx);
-				if (load_and_dump_sb(filename, fd, sb_bytenr)) {
+				if (load_and_dump_sb(filename, fd,
+						     sb_bytenr, force)) {
 					close(fd);
 					exit(1);
 				}
@@ -112,7 +119,7 @@ int main(int argc, char **argv)
 				putchar('\n');
 			}
 		} else {
-			load_and_dump_sb(filename, fd, sb_bytenr);
+			load_and_dump_sb(filename, fd, sb_bytenr, force);
 			putchar('\n');
 		}
 		close(fd);
@@ -121,7 +128,7 @@ int main(int argc, char **argv)
 	exit(0);
 }
 
-static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr)
+static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int force)
 {
 	u8 super_block_data[BTRFS_SUPER_INFO_SIZE];
 	struct btrfs_super_block *sb;
@@ -146,7 +153,12 @@ static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr)
 	}
 	printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
 	printf("---------------------------------------------------------\n");
-	dump_superblock(sb);
+	if (btrfs_super_magic(sb) != BTRFS_MAGIC && !force) {
+		fprintf(stderr,
+		    "ERROR: bad magic on superblock on %s at %llu\n",
+		    filename, (unsigned long long)sb_bytenr);
+	} else
+		dump_superblock(sb);
 	return 0;
 }
 
diff --git a/man/btrfs-show-super.8.in b/man/btrfs-show-super.8.in
index 3fe8bde..582649a 100644
--- a/man/btrfs-show-super.8.in
+++ b/man/btrfs-show-super.8.in
@@ -10,9 +10,11 @@ first superblock will be printed out.
 
 \fIOptions\fP
 .IP "\fB-a\fP" 5
-print all the superblock information, if this option is given, '\fB-i\fP' option will be ignored.
+Print all the superblock information, if this option is given, '\fB-i\fP' option will be ignored.
+.IP "\fB-f\fP" 5
+Attempt to print the superblock even if no superblock magic is found.  May end badly.
 .IP "\fB-i \fI<super_mirror>\fP" 5
-specify which mirror to print out. \fI<super_mirror>\fP is between 0 and 2. if several '\fB-i \fI<super_mirror>\fP'\fR are given, only the last one is valid.
+Specify which mirror to print out. \fI<super_mirror>\fP is between 0 and 2.  If several '\fB-i \fI<super_mirror>\fP'\fR are given, only the last one is valid.
 
 .SH EXIT CODE
 \fBbtrfs-show-super\fR will return 0 exit code if no error happened.


             reply	other threads:[~2014-05-14  2:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-14  2:03 Eric Sandeen [this message]
2014-05-28 16:07 ` [PATCH] btrfs-show-super: don't try to print not-superblocks David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5372CED8.7030404@redhat.com \
    --to=sandeen@redhat.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.