diff --git a/cmds/inspect-dump-super.c b/cmds/inspect-dump-super.c index 58f9ba5f..e724428c 100644 --- a/cmds/inspect-dump-super.c +++ b/cmds/inspect-dump-super.c @@ -50,11 +50,11 @@ static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full, printf("superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename); printf("---------------------------------------------------------\n"); if (btrfs_super_magic(&sb) != BTRFS_MAGIC && !force) { - error("bad magic on superblock on %s at %llu", + error("bad magic on superblock on %s at %llu (use --force to dump it anyway)", filename, (unsigned long long)sb_bytenr); - } else { - btrfs_print_superblock(&sb, full); + return 1; } + btrfs_print_superblock(&sb, full); return 0; } @@ -177,7 +177,11 @@ static int cmd_inspect_dump_super(const struct cmd_struct *cmd, putchar('\n'); } } else { - load_and_dump_sb(filename, fd, sb_bytenr, full, force); + if (load_and_dump_sb(filename, fd, sb_bytenr, full, force)) { + close(fd); + ret = 1; + goto out; + } putchar('\n'); } close(fd); diff --git a/tests/misc-tests/015-dump-super-garbage/test.sh b/tests/misc-tests/015-dump-super-garbage/test.sh index b3469459..1cafaee9 100755 --- a/tests/misc-tests/015-dump-super-garbage/test.sh +++ b/tests/misc-tests/015-dump-super-garbage/test.sh @@ -6,9 +6,14 @@ source "$TEST_TOP/common" check_prereq btrfs -run_check "$TOP/btrfs" inspect-internal dump-super /dev/urandom -run_check "$TOP/btrfs" inspect-internal dump-super -a /dev/urandom -run_check "$TOP/btrfs" inspect-internal dump-super -fa /dev/urandom +run_mustfail "attempt to print bad superblock without force" \ + "$TOP/btrfs" inspect-internal dump-super /dev/urandom +run_mustfail "attempt to print bad superblock without force" \ + "$TOP/btrfs" inspect-internal dump-super -a /dev/urandom +run_mustfail "attempt to print bad superblock without force" \ + "$TOP/btrfs" inspect-internal dump-super -fa /dev/urandom + +# All forced, no failure run_check "$TOP/btrfs" inspect-internal dump-super -Ffa /dev/urandom run_check "$TOP/btrfs" inspect-internal dump-super -Ffa /dev/urandom run_check "$TOP/btrfs" inspect-internal dump-super -Ffa /dev/urandom