2016-02-24 10:52:33 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2023-08-28 20:12:13 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <stdbool.h>
|
2016-02-24 10:52:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2016-03-15 15:31:58 +00:00
|
|
|
#include <getopt.h>
|
2023-08-28 20:12:13 +00:00
|
|
|
#include "kernel-shared/accessors.h"
|
|
|
|
#include "kernel-shared/uapi/btrfs_tree.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/ctree.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/disk-io.h"
|
2020-08-18 13:56:04 +00:00
|
|
|
#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"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/help.h"
|
2022-09-15 21:15:17 +00:00
|
|
|
#include "common/string-utils.h"
|
2022-09-15 11:59:39 +00:00
|
|
|
#include "common/messages.h"
|
2022-09-14 15:06:52 +00:00
|
|
|
#include "cmds/commands.h"
|
2016-02-24 10:52:33 +00:00
|
|
|
|
|
|
|
static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full,
|
|
|
|
int force)
|
|
|
|
{
|
2021-10-21 01:40:19 +00:00
|
|
|
struct btrfs_super_block sb;
|
2023-06-27 08:53:15 +00:00
|
|
|
struct stat st;
|
2016-02-24 10:52:33 +00:00
|
|
|
u64 ret;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-10-21 01:40:19 +00:00
|
|
|
ret = sbread(fd, &sb, sb_bytenr);
|
2016-02-24 10:52:33 +00:00
|
|
|
if (ret != BTRFS_SUPER_INFO_SIZE) {
|
|
|
|
/* check if the disk if too short for further superblock */
|
|
|
|
if (ret == 0 && errno == 0)
|
|
|
|
return 0;
|
|
|
|
|
2023-06-27 08:53:13 +00:00
|
|
|
error("failed to read the superblock on %s at %llu read %llu/%d bytes",
|
|
|
|
filename, sb_bytenr, ret, BTRFS_SUPER_INFO_SIZE);
|
2018-01-07 21:54:21 +00:00
|
|
|
error("error = '%m', errno = %d", errno);
|
2016-02-24 10:52:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-10-04 14:50:09 +00:00
|
|
|
pr_verbose(LOG_DEFAULT, "superblock: bytenr=%llu, device=%s\n", sb_bytenr, filename);
|
|
|
|
pr_verbose(LOG_DEFAULT, "---------------------------------------------------------\n");
|
2021-10-21 01:40:19 +00:00
|
|
|
if (btrfs_super_magic(&sb) != BTRFS_MAGIC && !force) {
|
2022-07-24 15:32:14 +00:00
|
|
|
error("bad magic on superblock on %s at %llu (use --force to dump it anyway)",
|
2022-09-27 23:13:00 +00:00
|
|
|
filename, sb_bytenr);
|
2022-07-24 15:32:14 +00:00
|
|
|
return 1;
|
2016-02-24 10:52:33 +00:00
|
|
|
}
|
2022-07-24 15:32:14 +00:00
|
|
|
btrfs_print_superblock(&sb, full);
|
2023-06-27 08:53:15 +00:00
|
|
|
putchar('\n');
|
2016-02-24 10:52:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-07 02:07:03 +00:00
|
|
|
static const char * const cmd_inspect_dump_super_usage[] = {
|
2016-02-24 10:52:33 +00:00
|
|
|
"btrfs inspect-internal dump-super [options] device [device...]",
|
2016-03-15 15:32:26 +00:00
|
|
|
"Dump superblock from a device in a textual form",
|
2019-06-21 13:36:04 +00:00
|
|
|
"",
|
2023-02-22 00:37:18 +00:00
|
|
|
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"),
|
2016-09-02 14:57:23 +00:00
|
|
|
"",
|
|
|
|
"Deprecated syntax:",
|
2023-02-22 00:37:18 +00:00
|
|
|
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"),
|
2016-02-24 10:52:33 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2018-03-07 02:24:46 +00:00
|
|
|
static int cmd_inspect_dump_super(const struct cmd_struct *cmd,
|
|
|
|
int argc, char **argv)
|
2016-02-24 10:52:33 +00:00
|
|
|
{
|
2022-10-05 18:02:13 +00:00
|
|
|
bool all = false;
|
|
|
|
bool full = false;
|
|
|
|
bool force = false;
|
2016-02-24 10:52:33 +00:00
|
|
|
char *filename;
|
|
|
|
int fd = -1;
|
|
|
|
int i;
|
|
|
|
u64 arg;
|
|
|
|
u64 sb_bytenr = btrfs_sb_offset(0);
|
|
|
|
|
2016-03-15 15:31:58 +00:00
|
|
|
while (1) {
|
|
|
|
int c;
|
2022-06-21 00:20:21 +00:00
|
|
|
enum { GETOPT_VAL_BYTENR = GETOPT_VAL_FIRST };
|
2016-03-15 15:31:58 +00:00
|
|
|
static const struct option long_options[] = {
|
2016-03-15 15:32:26 +00:00
|
|
|
{"all", no_argument, NULL, 'a'},
|
2017-08-24 05:25:29 +00:00
|
|
|
{"bytenr", required_argument, NULL, GETOPT_VAL_BYTENR },
|
2016-03-15 15:32:26 +00:00
|
|
|
{"full", no_argument, NULL, 'f'},
|
|
|
|
{"force", no_argument, NULL, 'F'},
|
2016-09-02 14:57:23 +00:00
|
|
|
{"super", required_argument, NULL, 's' },
|
2016-03-15 15:31:58 +00:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
c = getopt_long(argc, argv, "fFai:s:", long_options, NULL);
|
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
2016-02-24 10:52:33 +00:00
|
|
|
case 'i':
|
2016-09-02 14:57:23 +00:00
|
|
|
warning(
|
|
|
|
"option -i is deprecated, please use -s or --super");
|
2016-02-24 10:52:33 +00:00
|
|
|
arg = arg_strtou64(optarg);
|
|
|
|
if (arg >= BTRFS_SUPER_MIRROR_MAX) {
|
2016-03-02 14:47:19 +00:00
|
|
|
error("super mirror too big: %llu >= %d",
|
|
|
|
arg, BTRFS_SUPER_MIRROR_MAX);
|
2016-09-02 14:57:23 +00:00
|
|
|
return 1;
|
2016-02-24 10:52:33 +00:00
|
|
|
}
|
|
|
|
sb_bytenr = btrfs_sb_offset(arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
2022-10-05 18:02:13 +00:00
|
|
|
all = true;
|
2016-02-24 10:52:33 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2022-10-05 18:02:13 +00:00
|
|
|
full = true;
|
2016-02-24 10:52:33 +00:00
|
|
|
break;
|
|
|
|
case 'F':
|
2022-10-05 18:02:13 +00:00
|
|
|
force = true;
|
2016-02-24 10:52:33 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2016-09-02 14:57:23 +00:00
|
|
|
arg = arg_strtou64(optarg);
|
|
|
|
if (BTRFS_SUPER_MIRROR_MAX <= arg) {
|
|
|
|
warning(
|
|
|
|
"deprecated use of -s <bytenr> with %llu, assuming --bytenr",
|
2022-09-27 23:13:00 +00:00
|
|
|
arg);
|
2016-09-02 14:57:23 +00:00
|
|
|
sb_bytenr = arg;
|
|
|
|
} else {
|
|
|
|
sb_bytenr = btrfs_sb_offset(arg);
|
|
|
|
}
|
2022-10-05 18:02:13 +00:00
|
|
|
all = false;
|
2016-02-24 10:52:33 +00:00
|
|
|
break;
|
2017-08-24 05:25:29 +00:00
|
|
|
case GETOPT_VAL_BYTENR:
|
|
|
|
arg = arg_strtou64(optarg);
|
|
|
|
sb_bytenr = arg;
|
2022-10-05 18:02:13 +00:00
|
|
|
all = false;
|
2017-08-24 05:25:29 +00:00
|
|
|
break;
|
2016-02-24 10:52:33 +00:00
|
|
|
default:
|
2018-03-07 02:54:38 +00:00
|
|
|
usage_unknown_option(cmd, argv);
|
2016-02-24 10:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_argc_min(argc - optind, 1))
|
2019-03-04 13:49:15 +00:00
|
|
|
return 1;
|
2016-02-24 10:52:33 +00:00
|
|
|
|
|
|
|
for (i = optind; i < argc; i++) {
|
|
|
|
filename = argv[i];
|
2016-09-01 17:38:23 +00:00
|
|
|
fd = open(filename, O_RDONLY);
|
2016-02-24 10:52:33 +00:00
|
|
|
if (fd < 0) {
|
2018-01-07 21:54:21 +00:00
|
|
|
error("cannot open %s: %m", filename);
|
2023-06-27 08:53:14 +00:00
|
|
|
return 1;
|
2016-02-24 10:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-06-27 08:53:14 +00:00
|
|
|
return 1;
|
2016-02-24 10:52:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-24 15:32:14 +00:00
|
|
|
if (load_and_dump_sb(filename, fd, sb_bytenr, full, force)) {
|
|
|
|
close(fd);
|
2023-06-27 08:53:14 +00:00
|
|
|
return 1;
|
2022-07-24 15:32:14 +00:00
|
|
|
}
|
2016-02-24 10:52:33 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2023-06-27 08:53:14 +00:00
|
|
|
return 0;
|
2016-02-24 10:52:33 +00:00
|
|
|
}
|
2018-03-07 02:07:03 +00:00
|
|
|
DEFINE_SIMPLE_COMMAND(inspect_dump_super, "dump-super");
|