btrfs-progs/cmds/inspect-dump-super.c

205 lines
5.3 KiB
C
Raw Normal View History

/*
* 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.
*/
#include "kerncompat.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <getopt.h>
btrfs-progs: dump-super: fix read beyond device size On aarch64 systems with glibc 2.28, several btrfs-progs test cases are failing because the command 'btrfs inspect dump-super -a <dev>' reports an error when it attempts to read beyond the disk/file-image size. $ btrfs inspect dump-super -a /dev/vdb12 <snap> ERROR: Failed to read the superblock on /dev/vdb12 at 274877906944 And btrfs/184 also fails, as it uses -s 2 option to dump the last super block. $ ./check btrfs/184 FSTYP -- btrfs PLATFORM -- Linux/aarch64 a4k 6.4.0-rc7+ #7 SMP PREEMPT Sat Jun 24 02:47:24 EDT 2023 MKFS_OPTIONS -- /dev/vdb2 MOUNT_OPTIONS -- /dev/vdb2 /mnt/scratch btrfs/184 1s ... [failed, exit status 1]- output mismatch (see /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad) --- tests/btrfs/184.out 2020-03-03 00:26:40.172081468 -0500 +++ /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad 2023-06-24 05:54:40.868210737 -0400 @@ -1,2 +1,3 @@ QA output created by 184 -Silence is golden +Deleted dev superblocks not scratched +(see /Volumes/ws/xfstests-dev/results//btrfs/184.full for details) ... (Run 'diff -u /Volumes/ws/xfstests-dev/tests/btrfs/184.out /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad' to see the entire diff) Ran: btrfs/184 Failures: btrfs/184 Failed 1 of 1 tests This is because `pread()` behaves differently on aarch64 and sets `errno = 2` instead of the usual `errno = 0`. To fix check if the sb offset is beyond the device size or regular file size and skip the corresponding sbread(). Also, move putchar('\n') after a successful call to load_and_dump_sb() to the load_and_dump_sb() itself. Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: David Sterba <dsterba@suse.com>
2023-06-27 08:53:15 +00:00
#include <sys/stat.h>
#include "kernel-shared/ctree.h"
#include "kernel-shared/disk-io.h"
#include "kernel-shared/print-tree.h"
btrfs-progs: zoned: implement log-structured superblock Superblock (and its copies) is the only data structure in btrfs which has a fixed location on a device. Since we cannot overwrite in a sequential write required zone, we cannot place superblock in the zone. One easy solution is limiting superblock and copies to be placed only in conventional zones. However, this method has two downsides: one is reduced number of superblock copies. The location of the second copy of superblock is 256GB, which is in a sequential write required zone on typical devices in the market today. So, the number of superblock and copies is limited to be two. Second downside is that we cannot support devices which have no conventional zones at all. To solve these two problems, we employ superblock log writing. It uses two adjacent zones as a circular buffer to write updated superblocks. Once the first zone is filled up, start writing into the second one. Then, when both zones are filled up and before starting to write to the first zone again, reset the first zone. We can determine the position of the latest superblock by reading write pointer information from a device. One corner case is when both zones are full. For this situation, we read out the last superblock of each zone, and compare them to determine which zone is older. The following zones are reserved as the circular buffer on ZONED btrfs. - primary superblock: offset 0B (and the following zone) - first copy: offset 512G (and the following zone) - Second copy: offset 4T (4096G, and the following zone) If these reserved zones are conventional, superblock is written fixed at the start of the zone without logging. Currently, superblock reading/writing is done by pread/pwrite. This commit replace the call sites with sbread/sbwrite to wrap the functions. For zoned btrfs, btrfs_sb_io which is called from sbread/sbwrite reverses the IO position back to a mirror number, maps the mirror number into the superblock logging position, and do the IO. Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com> Signed-off-by: David Sterba <dsterba@suse.com>
2021-04-26 06:27:26 +00:00
#include "kernel-shared/zoned.h"
#include "common/help.h"
#include "common/string-utils.h"
#include "common/messages.h"
#include "cmds/commands.h"
static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
int force)
{
struct btrfs_super_block sb;
btrfs-progs: dump-super: fix read beyond device size On aarch64 systems with glibc 2.28, several btrfs-progs test cases are failing because the command 'btrfs inspect dump-super -a <dev>' reports an error when it attempts to read beyond the disk/file-image size. $ btrfs inspect dump-super -a /dev/vdb12 <snap> ERROR: Failed to read the superblock on /dev/vdb12 at 274877906944 And btrfs/184 also fails, as it uses -s 2 option to dump the last super block. $ ./check btrfs/184 FSTYP -- btrfs PLATFORM -- Linux/aarch64 a4k 6.4.0-rc7+ #7 SMP PREEMPT Sat Jun 24 02:47:24 EDT 2023 MKFS_OPTIONS -- /dev/vdb2 MOUNT_OPTIONS -- /dev/vdb2 /mnt/scratch btrfs/184 1s ... [failed, exit status 1]- output mismatch (see /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad) --- tests/btrfs/184.out 2020-03-03 00:26:40.172081468 -0500 +++ /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad 2023-06-24 05:54:40.868210737 -0400 @@ -1,2 +1,3 @@ QA output created by 184 -Silence is golden +Deleted dev superblocks not scratched +(see /Volumes/ws/xfstests-dev/results//btrfs/184.full for details) ... (Run 'diff -u /Volumes/ws/xfstests-dev/tests/btrfs/184.out /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad' to see the entire diff) Ran: btrfs/184 Failures: btrfs/184 Failed 1 of 1 tests This is because `pread()` behaves differently on aarch64 and sets `errno = 2` instead of the usual `errno = 0`. To fix check if the sb offset is beyond the device size or regular file size and skip the corresponding sbread(). Also, move putchar('\n') after a successful call to load_and_dump_sb() to the load_and_dump_sb() itself. Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: David Sterba <dsterba@suse.com>
2023-06-27 08:53:15 +00:00
struct stat st;
u64 ret;
btrfs-progs: dump-super: fix read beyond device size On aarch64 systems with glibc 2.28, several btrfs-progs test cases are failing because the command 'btrfs inspect dump-super -a <dev>' reports an error when it attempts to read beyond the disk/file-image size. $ btrfs inspect dump-super -a /dev/vdb12 <snap> ERROR: Failed to read the superblock on /dev/vdb12 at 274877906944 And btrfs/184 also fails, as it uses -s 2 option to dump the last super block. $ ./check btrfs/184 FSTYP -- btrfs PLATFORM -- Linux/aarch64 a4k 6.4.0-rc7+ #7 SMP PREEMPT Sat Jun 24 02:47:24 EDT 2023 MKFS_OPTIONS -- /dev/vdb2 MOUNT_OPTIONS -- /dev/vdb2 /mnt/scratch btrfs/184 1s ... [failed, exit status 1]- output mismatch (see /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad) --- tests/btrfs/184.out 2020-03-03 00:26:40.172081468 -0500 +++ /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad 2023-06-24 05:54:40.868210737 -0400 @@ -1,2 +1,3 @@ QA output created by 184 -Silence is golden +Deleted dev superblocks not scratched +(see /Volumes/ws/xfstests-dev/results//btrfs/184.full for details) ... (Run 'diff -u /Volumes/ws/xfstests-dev/tests/btrfs/184.out /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad' to see the entire diff) Ran: btrfs/184 Failures: btrfs/184 Failed 1 of 1 tests This is because `pread()` behaves differently on aarch64 and sets `errno = 2` instead of the usual `errno = 0`. To fix check if the sb offset is beyond the device size or regular file size and skip the corresponding sbread(). Also, move putchar('\n') after a successful call to load_and_dump_sb() to the load_and_dump_sb() itself. Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: David Sterba <dsterba@suse.com>
2023-06-27 08:53:15 +00:00
if (fstat(fd, &st) < 0) {
error("unable to stat %s to when loading superblock: %m", filename);
return 1;
}
if (S_ISBLK(st.st_mode) || S_ISREG(st.st_mode)) {
off_t last_byte;
last_byte = lseek(fd, 0, SEEK_END);
if (last_byte == -1) {
error("cannot read end of file %s: %m", filename);
return 1;
}
if (sb_bytenr > last_byte)
return 0;
}
ret = sbread(fd, &sb, sb_bytenr);
if (ret != BTRFS_SUPER_INFO_SIZE) {
/* check if the disk if too short for further superblock */
if (ret == 0 && errno == 0)
return 0;
error("failed to read the superblock on %s at %llu read %llu/%d bytes",
filename, sb_bytenr, ret, BTRFS_SUPER_INFO_SIZE);
error("error = '%m', errno = %d", errno);
return 1;
}
pr_verbose(LOG_DEFAULT, "superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
pr_verbose(LOG_DEFAULT, "---------------------------------------------------------\n");
if (btrfs_super_magic(&sb) != BTRFS_MAGIC && !force) {
error("bad magic on superblock on %s at %llu (use --force to dump it anyway)",
filename, sb_bytenr);
return 1;
}
btrfs_print_superblock(&sb, full);
btrfs-progs: dump-super: fix read beyond device size On aarch64 systems with glibc 2.28, several btrfs-progs test cases are failing because the command 'btrfs inspect dump-super -a <dev>' reports an error when it attempts to read beyond the disk/file-image size. $ btrfs inspect dump-super -a /dev/vdb12 <snap> ERROR: Failed to read the superblock on /dev/vdb12 at 274877906944 And btrfs/184 also fails, as it uses -s 2 option to dump the last super block. $ ./check btrfs/184 FSTYP -- btrfs PLATFORM -- Linux/aarch64 a4k 6.4.0-rc7+ #7 SMP PREEMPT Sat Jun 24 02:47:24 EDT 2023 MKFS_OPTIONS -- /dev/vdb2 MOUNT_OPTIONS -- /dev/vdb2 /mnt/scratch btrfs/184 1s ... [failed, exit status 1]- output mismatch (see /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad) --- tests/btrfs/184.out 2020-03-03 00:26:40.172081468 -0500 +++ /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad 2023-06-24 05:54:40.868210737 -0400 @@ -1,2 +1,3 @@ QA output created by 184 -Silence is golden +Deleted dev superblocks not scratched +(see /Volumes/ws/xfstests-dev/results//btrfs/184.full for details) ... (Run 'diff -u /Volumes/ws/xfstests-dev/tests/btrfs/184.out /Volumes/ws/xfstests-dev/results//btrfs/184.out.bad' to see the entire diff) Ran: btrfs/184 Failures: btrfs/184 Failed 1 of 1 tests This is because `pread()` behaves differently on aarch64 and sets `errno = 2` instead of the usual `errno = 0`. To fix check if the sb offset is beyond the device size or regular file size and skip the corresponding sbread(). Also, move putchar('\n') after a successful call to load_and_dump_sb() to the load_and_dump_sb() itself. Reviewed-by: Qu Wenruo <wqu@suse.com> Signed-off-by: Anand Jain <anand.jain@oracle.com> Signed-off-by: David Sterba <dsterba@suse.com>
2023-06-27 08:53:15 +00:00
putchar('\n');
return 0;
}
static const char * const cmd_inspect_dump_super_usage[] = {
"btrfs inspect-internal dump-super [options] device [device...]",
"Dump superblock from a device in a textual form",
"",
OPTLINE("-f|--full", "print full superblock information, backup roots etc."),
OPTLINE("-a|--all", "print information about all superblocks"),
OPTLINE("-s|--super <super>", "specify which copy to print out (values: 0, 1, 2)"),
OPTLINE("-F|--force", "attempt to dump superblocks with bad magic"),
OPTLINE("--bytenr <offset>", "specify alternate superblock offset"),
"",
"Deprecated syntax:",
OPTLINE("-s <bytenr>", "specify alternate superblock offset, values other than 0, 1, 2 "
"will be interpreted as --bytenr for backward compatibility, "
"option renamed for consistency with other tools (eg. check)"),
OPTLINE("-i <super>", "specify which copy to print out (values: 0, 1, 2), now moved to --super"),
NULL
};
static int cmd_inspect_dump_super(const struct cmd_struct *cmd,
int argc, char **argv)
{
bool all = false;
bool full = false;
bool force = false;
char *filename;
int fd = -1;
int i;
u64 arg;
u64 sb_bytenr = btrfs_sb_offset(0);
while (1) {
int c;
enum { GETOPT_VAL_BYTENR = GETOPT_VAL_FIRST };
static const struct option long_options[] = {
{"all", no_argument, NULL, 'a'},
{"bytenr", required_argument, NULL, GETOPT_VAL_BYTENR },
{"full", no_argument, NULL, 'f'},
{"force", no_argument, NULL, 'F'},
{"super", required_argument, NULL, 's' },
{NULL, 0, NULL, 0}
};
c = getopt_long(argc, argv, "fFai:s:", long_options, NULL);
if (c < 0)
break;
switch (c) {
case 'i':
warning(
"option -i is deprecated, please use -s or --super");
arg = arg_strtou64(optarg);
if (arg >= BTRFS_SUPER_MIRROR_MAX) {
error("super mirror too big: %llu >= %d",
arg, BTRFS_SUPER_MIRROR_MAX);
return 1;
}
sb_bytenr = btrfs_sb_offset(arg);
break;
case 'a':
all = true;
break;
case 'f':
full = true;
break;
case 'F':
force = true;
break;
case 's':
arg = arg_strtou64(optarg);
if (BTRFS_SUPER_MIRROR_MAX <= arg) {
warning(
"deprecated use of -s <bytenr> with %llu, assuming --bytenr",
arg);
sb_bytenr = arg;
} else {
sb_bytenr = btrfs_sb_offset(arg);
}
all = false;
break;
case GETOPT_VAL_BYTENR:
arg = arg_strtou64(optarg);
sb_bytenr = arg;
all = false;
break;
default:
usage_unknown_option(cmd, argv);
}
}
if (check_argc_min(argc - optind, 1))
return 1;
for (i = optind; i < argc; i++) {
filename = argv[i];
fd = open(filename, O_RDONLY);
if (fd < 0) {
error("cannot open %s: %m", filename);
return 1;
}
if (all) {
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, full, force)) {
close(fd);
return 1;
}
}
} else {
if (load_and_dump_sb(filename, fd, sb_bytenr, full, force)) {
close(fd);
return 1;
}
}
close(fd);
}
return 0;
}
DEFINE_SIMPLE_COMMAND(inspect_dump_super, "dump-super");