btrfs-progs: image: Verify the superblock before restore

This patch will export disk-io.c::check_super() as btrfs_check_super()
and use it in btrfs-image for extra verification.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
master
Qu Wenruo 2019-06-06 19:06:06 +08:00 committed by David Sterba
parent 686e86d82d
commit ab5079c19a
3 changed files with 9 additions and 3 deletions

View File

@ -1347,7 +1347,7 @@ struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
* - number of devices - something sane * - number of devices - something sane
* - sys array size - maximum * - sys array size - maximum
*/ */
static int check_super(struct btrfs_super_block *sb, unsigned sbflags) int btrfs_check_super(struct btrfs_super_block *sb, unsigned sbflags)
{ {
u8 result[BTRFS_CSUM_SIZE]; u8 result[BTRFS_CSUM_SIZE];
u32 crc; u32 crc;
@ -1547,7 +1547,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
if (btrfs_super_bytenr(buf) != sb_bytenr) if (btrfs_super_bytenr(buf) != sb_bytenr)
return -EIO; return -EIO;
ret = check_super(buf, sbflags); ret = btrfs_check_super(buf, sbflags);
if (ret < 0) if (ret < 0)
return ret; return ret;
memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE); memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
@ -1572,7 +1572,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
/* if magic is NULL, the device was removed */ /* if magic is NULL, the device was removed */
if (btrfs_super_magic(buf) == 0 && i == 0) if (btrfs_super_magic(buf) == 0 && i == 0)
break; break;
if (check_super(buf, sbflags)) if (btrfs_check_super(buf, sbflags))
continue; continue;
if (!fsid_is_initialized) { if (!fsid_is_initialized) {

View File

@ -171,6 +171,7 @@ static inline int close_ctree(struct btrfs_root *root)
int write_all_supers(struct btrfs_fs_info *fs_info); int write_all_supers(struct btrfs_fs_info *fs_info);
int write_ctree_super(struct btrfs_trans_handle *trans); int write_ctree_super(struct btrfs_trans_handle *trans);
int btrfs_check_super(struct btrfs_super_block *sb, unsigned sbflags);
int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr, int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
unsigned sbflags); unsigned sbflags);
int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *bh, int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *bh,

View File

@ -1983,6 +1983,11 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,
pthread_mutex_lock(&mdres->mutex); pthread_mutex_lock(&mdres->mutex);
super = (struct btrfs_super_block *)buffer; super = (struct btrfs_super_block *)buffer;
ret = btrfs_check_super(super, 0);
if (ret < 0) {
error("invalid superblock");
return ret;
}
chunk_root_bytenr = btrfs_super_chunk_root(super); chunk_root_bytenr = btrfs_super_chunk_root(super);
mdres->nodesize = btrfs_super_nodesize(super); mdres->nodesize = btrfs_super_nodesize(super);
if (btrfs_super_incompat_flags(super) & if (btrfs_super_incompat_flags(super) &