Skip to content

Commit bd67644

Browse files
josefbacikkdave
authored andcommitted
btrfs: abstract out loading the tree root
We're going to be adding more roots that need to be loaded from the super block, so abstract out the code to read the tree_root from the super block, and use this helper for the chunk root as well. This will make it simpler to load the new trees in the future. Signed-off-by: Josef Bacik <josef@toxicpanda.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent c2fa821 commit bd67644

File tree

1 file changed

+46
-35
lines changed

1 file changed

+46
-35
lines changed

fs/btrfs/disk-io.c

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,6 +2930,45 @@ static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
29302930
return ret;
29312931
}
29322932

2933+
static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level)
2934+
{
2935+
int ret = 0;
2936+
2937+
root->node = read_tree_block(root->fs_info, bytenr,
2938+
root->root_key.objectid, gen, level, NULL);
2939+
if (IS_ERR(root->node)) {
2940+
ret = PTR_ERR(root->node);
2941+
root->node = NULL;
2942+
} else if (!extent_buffer_uptodate(root->node)) {
2943+
free_extent_buffer(root->node);
2944+
root->node = NULL;
2945+
ret = -EIO;
2946+
}
2947+
2948+
if (ret)
2949+
return ret;
2950+
2951+
btrfs_set_root_node(&root->root_item, root->node);
2952+
root->commit_root = btrfs_root_node(root);
2953+
btrfs_set_root_refs(&root->root_item, 1);
2954+
return ret;
2955+
}
2956+
2957+
static int load_important_roots(struct btrfs_fs_info *fs_info)
2958+
{
2959+
struct btrfs_super_block *sb = fs_info->super_copy;
2960+
u64 gen, bytenr;
2961+
int level, ret;
2962+
2963+
bytenr = btrfs_super_root(sb);
2964+
gen = btrfs_super_generation(sb);
2965+
level = btrfs_super_root_level(sb);
2966+
ret = load_super_root(fs_info->tree_root, bytenr, gen, level);
2967+
if (ret)
2968+
btrfs_warn(fs_info, "couldn't read tree root");
2969+
return ret;
2970+
}
2971+
29332972
static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
29342973
{
29352974
int backup_index = find_newest_super_backup(fs_info);
@@ -2940,9 +2979,6 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
29402979
int i;
29412980

29422981
for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
2943-
u64 generation;
2944-
int level;
2945-
29462982
if (handle_error) {
29472983
if (!IS_ERR(tree_root->node))
29482984
free_extent_buffer(tree_root->node);
@@ -2967,29 +3003,13 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
29673003
if (ret < 0)
29683004
return ret;
29693005
}
2970-
generation = btrfs_super_generation(sb);
2971-
level = btrfs_super_root_level(sb);
2972-
tree_root->node = read_tree_block(fs_info, btrfs_super_root(sb),
2973-
BTRFS_ROOT_TREE_OBJECTID,
2974-
generation, level, NULL);
2975-
if (IS_ERR(tree_root->node)) {
2976-
handle_error = true;
2977-
ret = PTR_ERR(tree_root->node);
2978-
tree_root->node = NULL;
2979-
btrfs_warn(fs_info, "couldn't read tree root");
2980-
continue;
29813006

2982-
} else if (!extent_buffer_uptodate(tree_root->node)) {
3007+
ret = load_important_roots(fs_info);
3008+
if (ret) {
29833009
handle_error = true;
2984-
ret = -EIO;
2985-
btrfs_warn(fs_info, "error while reading tree root");
29863010
continue;
29873011
}
29883012

2989-
btrfs_set_root_node(&tree_root->root_item, tree_root->node);
2990-
tree_root->commit_root = btrfs_root_node(tree_root);
2991-
btrfs_set_root_refs(&tree_root->root_item, 1);
2992-
29933013
/*
29943014
* No need to hold btrfs_root::objectid_mutex since the fs
29953015
* hasn't been fully initialised and we are the only user
@@ -3009,8 +3029,8 @@ static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
30093029
}
30103030

30113031
/* All successful */
3012-
fs_info->generation = generation;
3013-
fs_info->last_trans_committed = generation;
3032+
fs_info->generation = btrfs_header_generation(tree_root->node);
3033+
fs_info->last_trans_committed = fs_info->generation;
30143034
fs_info->last_reloc_trans = 0;
30153035

30163036
/* Always begin writing backup roots after the one being used */
@@ -3594,21 +3614,12 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
35943614

35953615
generation = btrfs_super_chunk_root_generation(disk_super);
35963616
level = btrfs_super_chunk_root_level(disk_super);
3597-
3598-
chunk_root->node = read_tree_block(fs_info,
3599-
btrfs_super_chunk_root(disk_super),
3600-
BTRFS_CHUNK_TREE_OBJECTID,
3601-
generation, level, NULL);
3602-
if (IS_ERR(chunk_root->node) ||
3603-
!extent_buffer_uptodate(chunk_root->node)) {
3617+
ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super),
3618+
generation, level);
3619+
if (ret) {
36043620
btrfs_err(fs_info, "failed to read chunk root");
3605-
if (!IS_ERR(chunk_root->node))
3606-
free_extent_buffer(chunk_root->node);
3607-
chunk_root->node = NULL;
36083621
goto fail_tree_roots;
36093622
}
3610-
btrfs_set_root_node(&chunk_root->root_item, chunk_root->node);
3611-
chunk_root->commit_root = btrfs_root_node(chunk_root);
36123623

36133624
read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
36143625
offsetof(struct btrfs_header, chunk_tree_uuid),

0 commit comments

Comments
 (0)