Re: [PATCH 04/20] btrfs-progs: add a helper for setting up a root node
From: Qu Wenruo <hidden>
Date: 2021-11-06 00:18:40
On 2021/11/6 04:28, Josef Bacik wrote:
quoted hunk ↗ jump to hunk
We use this pattern in a few places, and will use it more with different roots in the future. Extract out this helper to read the root nodes. Signed-off-by: Josef Bacik <josef@toxicpanda.com> --- kernel-shared/disk-io.c | 58 +++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 28 deletions(-)diff --git a/kernel-shared/disk-io.c b/kernel-shared/disk-io.c index 3b3c4523..1f940221 100644 --- a/kernel-shared/disk-io.c +++ b/kernel-shared/disk-io.c@@ -578,12 +578,23 @@ void btrfs_setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, root->root_key.objectid = objectid; } +static int read_root_node(struct btrfs_fs_info *fs_info, + struct btrfs_root *root, u64 bytenr, u64 gen)
Can we add "int level" to bring proper level check for this function and read_tree_block()? Thanks, Qu
quoted hunk ↗ jump to hunk
+{ + root->node = read_tree_block(fs_info, bytenr, gen); > + if (!extent_buffer_uptodate(root->node)) { + free_extent_buffer(root->node); + root->node = NULL; + return -EIO; + } + return 0; +} + static int find_and_setup_root(struct btrfs_root *tree_root, struct btrfs_fs_info *fs_info, u64 objectid, struct btrfs_root *root) { int ret; - u64 generation; btrfs_setup_root(root, fs_info, objectid); ret = btrfs_find_last_root(tree_root, objectid,@@ -591,13 +602,9 @@ static int find_and_setup_root(struct btrfs_root *tree_root, if (ret) return ret; - generation = btrfs_root_generation(&root->root_item); - root->node = read_tree_block(fs_info, - btrfs_root_bytenr(&root->root_item), generation); - if (!extent_buffer_uptodate(root->node)) - return -EIO; - - return 0; + return read_root_node(fs_info, root, + btrfs_root_bytenr(&root->root_item), + btrfs_root_generation(&root->root_item)); } static int find_and_setup_log_root(struct btrfs_root *tree_root,@@ -606,6 +613,7 @@ static int find_and_setup_log_root(struct btrfs_root *tree_root, { u64 blocknr = btrfs_super_log_root(disk_super); struct btrfs_root *log_root = malloc(sizeof(struct btrfs_root)); + int ret; if (!log_root) return -ENOMEM;@@ -615,20 +623,15 @@ static int find_and_setup_log_root(struct btrfs_root *tree_root, return 0; } - btrfs_setup_root(log_root, fs_info, - BTRFS_TREE_LOG_OBJECTID); - - log_root->node = read_tree_block(fs_info, blocknr, - btrfs_super_generation(disk_super) + 1); - - fs_info->log_root_tree = log_root; - - if (!extent_buffer_uptodate(log_root->node)) { - free_extent_buffer(log_root->node); + btrfs_setup_root(log_root, fs_info, BTRFS_TREE_LOG_OBJECTID); + ret = read_root_node(fs_info, log_root, blocknr, + btrfs_super_generation(disk_super) + 1); + if (ret) { free(log_root); fs_info->log_root_tree = NULL; - return -EIO; + return ret; } + fs_info->log_root_tree = log_root; return 0; }@@ -704,9 +707,9 @@ out: return ERR_PTR(ret); } generation = btrfs_root_generation(&root->root_item); - root->node = read_tree_block(fs_info, - btrfs_root_bytenr(&root->root_item), generation); - if (!extent_buffer_uptodate(root->node)) { + ret = read_root_node(fs_info, root, + btrfs_root_bytenr(&root->root_item), generation); + if (ret) { free(root); return ERR_PTR(-EIO); }@@ -970,8 +973,8 @@ int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr, generation = btrfs_backup_tree_root_gen(backup); } - root->node = read_tree_block(fs_info, root_tree_bytenr, generation); - if (!extent_buffer_uptodate(root->node)) { + ret = read_root_node(fs_info, root, root_tree_bytenr, generation); + if (ret) { fprintf(stderr, "Couldn't read tree root\n"); return -EIO; }@@ -1179,10 +1182,9 @@ int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info, else generation = 0; - fs_info->chunk_root->node = read_tree_block(fs_info, - chunk_root_bytenr, - generation); - if (!extent_buffer_uptodate(fs_info->chunk_root->node)) { + ret = read_root_node(fs_info, fs_info->chunk_root, chunk_root_bytenr, + generation); + if (ret) { if (fs_info->ignore_chunk_tree_error) { warning("cannot read chunk root, continue anyway"); fs_info->chunk_root = NULL;