[PATCH v2 04/14] btrfs-progs: image: Verify the superblock before restore

WenRuo Qu
2019-07-02T10:24:05Z

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>
---
disk-io.c | 6 +++---
disk-io.h | 1 +
image/main.c | 5 +++++
3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/disk-io.c b/disk-io.c
index 151eb3b5a278..ffe4a8c58060 100644
--- a/disk-io.c
+++ b/disk-io.c
@@ -1347,7 +1347,7 @@ struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
* - number of devices - something sane
* - 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];
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)
return -EIO;

- ret = check_super(buf, sbflags);
+ ret = btrfs_check_super(buf, sbflags);
if (ret < 0)
return ret;
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 (btrfs_super_magic(buf) == 0 && i == 0)
break;
- if (check_super(buf, sbflags))
+ if (btrfs_check_super(buf, sbflags))
continue;

if (!fsid_is_initialized) {
diff --git a/disk-io.h b/disk-io.h
index ddf3a3803ed5..c97aa2344ac9 100644
--- a/disk-io.h
+++ b/disk-io.h
@@ -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_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,
unsigned sbflags);
int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *bh,
diff --git a/image/main.c b/image/main.c
index c45d506812b2..f155794cfc19 100644
--- a/image/main.c
+++ b/image/main.c
@@ -1983,6 +1983,11 @@ static int build_chunk_tree(struct mdrestore_struct *mdres,

pthread_mutex_lock(&mdres->mutex);
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);
mdres->nodesize = btrfs_super_nodesize(super);
if (btrfs_super_incompat_flags(super) &
--
2.22.0