From patchwork Wed May 14 02:03:04 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sandeen X-Patchwork-Id: 4172101 X-Patchwork-Delegate: dave@jikos.cz Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 91F439F23C for ; Wed, 14 May 2014 02:03:11 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9B957202EB for ; Wed, 14 May 2014 02:03:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 515AD202E5 for ; Wed, 14 May 2014 02:03:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752327AbaENCDG (ORCPT ); Tue, 13 May 2014 22:03:06 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54347 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751871AbaENCDD (ORCPT ); Tue, 13 May 2014 22:03:03 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s4E233OT006116 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 13 May 2014 22:03:03 -0400 Received: from liberator.sandeen.net (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id s4E232Sx007399 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO) for ; Tue, 13 May 2014 22:03:02 -0400 Message-ID: <5372CED8.7030404@redhat.com> Date: Tue, 13 May 2014 21:03:04 -0500 From: Eric Sandeen User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: linux-btrfs Subject: [PATCH] btrfs-show-super: don't try to print not-superblocks X-Enigmail-Version: 1.6 X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- -- 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 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\fP" 5 -specify which mirror to print out. \fI\fP is between 0 and 2. if several '\fB-i \fI\fP'\fR are given, only the last one is valid. +Specify which mirror to print out. \fI\fP is between 0 and 2. If several '\fB-i \fI\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.