Commit 96dfcb46 authored by Josef Bacik's avatar Josef Bacik Committed by David Sterba

btrfs: push __setup_root into btrfs_alloc_root

There's no reason to not init the root at alloc time, and with later
patches it actually causes problems if we error out mounting the fs
before the tree_root is init'ed because we expect it to have a valid ref
count.  Fix this by pushing __setup_root into btrfs_alloc_root.
Reviewed-by: default avatarNikolay Borisov <nborisov@suse.com>
Signed-off-by: default avatarJosef Bacik <josef@toxicpanda.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 3f1c64ce
...@@ -1130,6 +1130,7 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, ...@@ -1130,6 +1130,7 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
u64 objectid) u64 objectid)
{ {
bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state); bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
root->fs_info = fs_info;
root->node = NULL; root->node = NULL;
root->commit_root = NULL; root->commit_root = NULL;
root->state = 0; root->state = 0;
...@@ -1198,11 +1199,11 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info, ...@@ -1198,11 +1199,11 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
} }
static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info, static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
gfp_t flags) u64 objectid, gfp_t flags)
{ {
struct btrfs_root *root = kzalloc(sizeof(*root), flags); struct btrfs_root *root = kzalloc(sizeof(*root), flags);
if (root) if (root)
root->fs_info = fs_info; __setup_root(root, fs_info, objectid);
return root; return root;
} }
...@@ -1215,12 +1216,11 @@ struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info) ...@@ -1215,12 +1216,11 @@ struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
if (!fs_info) if (!fs_info)
return ERR_PTR(-EINVAL); return ERR_PTR(-EINVAL);
root = btrfs_alloc_root(fs_info, GFP_KERNEL); root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL);
if (!root) if (!root)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
/* We don't use the stripesize in selftest, set it as sectorsize */ /* We don't use the stripesize in selftest, set it as sectorsize */
__setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
root->alloc_bytenr = 0; root->alloc_bytenr = 0;
return root; return root;
...@@ -1244,12 +1244,11 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, ...@@ -1244,12 +1244,11 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
* context to avoid deadlock if reclaim happens. * context to avoid deadlock if reclaim happens.
*/ */
nofs_flag = memalloc_nofs_save(); nofs_flag = memalloc_nofs_save();
root = btrfs_alloc_root(fs_info, GFP_KERNEL); root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL);
memalloc_nofs_restore(nofs_flag); memalloc_nofs_restore(nofs_flag);
if (!root) if (!root)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
__setup_root(root, fs_info, objectid);
root->root_key.objectid = objectid; root->root_key.objectid = objectid;
root->root_key.type = BTRFS_ROOT_ITEM_KEY; root->root_key.type = BTRFS_ROOT_ITEM_KEY;
root->root_key.offset = 0; root->root_key.offset = 0;
...@@ -1309,12 +1308,10 @@ static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans, ...@@ -1309,12 +1308,10 @@ static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
struct btrfs_root *root; struct btrfs_root *root;
struct extent_buffer *leaf; struct extent_buffer *leaf;
root = btrfs_alloc_root(fs_info, GFP_NOFS); root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS);
if (!root) if (!root)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
__setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID);
root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID; root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
root->root_key.type = BTRFS_ROOT_ITEM_KEY; root->root_key.type = BTRFS_ROOT_ITEM_KEY;
root->root_key.offset = BTRFS_TREE_LOG_OBJECTID; root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
...@@ -1401,14 +1398,12 @@ static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, ...@@ -1401,14 +1398,12 @@ static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
if (!path) if (!path)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
root = btrfs_alloc_root(fs_info, GFP_NOFS); root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS);
if (!root) { if (!root) {
ret = -ENOMEM; ret = -ENOMEM;
goto alloc_fail; goto alloc_fail;
} }
__setup_root(root, fs_info, key->objectid);
ret = btrfs_find_root(tree_root, key, path, ret = btrfs_find_root(tree_root, key, path,
&root->root_item, &root->root_key); &root->root_item, &root->root_key);
if (ret) { if (ret) {
...@@ -2208,12 +2203,11 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info, ...@@ -2208,12 +2203,11 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
return -EIO; return -EIO;
} }
log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID,
GFP_KERNEL);
if (!log_tree_root) if (!log_tree_root)
return -ENOMEM; return -ENOMEM;
__setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
log_tree_root->node = read_tree_block(fs_info, bytenr, log_tree_root->node = read_tree_block(fs_info, bytenr,
fs_info->generation + 1, fs_info->generation + 1,
level, NULL); level, NULL);
...@@ -2645,8 +2639,12 @@ int __cold open_ctree(struct super_block *sb, ...@@ -2645,8 +2639,12 @@ int __cold open_ctree(struct super_block *sb,
int clear_free_space_tree = 0; int clear_free_space_tree = 0;
int level; int level;
tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL); tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID,
chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL); GFP_KERNEL);
fs_info->tree_root = tree_root;
chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID,
GFP_KERNEL);
fs_info->chunk_root = chunk_root;
if (!tree_root || !chunk_root) { if (!tree_root || !chunk_root) {
err = -ENOMEM; err = -ENOMEM;
goto fail; goto fail;
...@@ -2824,8 +2822,6 @@ int __cold open_ctree(struct super_block *sb, ...@@ -2824,8 +2822,6 @@ int __cold open_ctree(struct super_block *sb,
goto fail_alloc; goto fail_alloc;
} }
__setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
invalidate_bdev(fs_devices->latest_bdev); invalidate_bdev(fs_devices->latest_bdev);
/* /*
...@@ -3021,8 +3017,6 @@ int __cold open_ctree(struct super_block *sb, ...@@ -3021,8 +3017,6 @@ int __cold open_ctree(struct super_block *sb,
generation = btrfs_super_chunk_root_generation(disk_super); generation = btrfs_super_chunk_root_generation(disk_super);
level = btrfs_super_chunk_root_level(disk_super); level = btrfs_super_chunk_root_level(disk_super);
__setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
chunk_root->node = read_tree_block(fs_info, chunk_root->node = read_tree_block(fs_info,
btrfs_super_chunk_root(disk_super), btrfs_super_chunk_root(disk_super),
generation, level, NULL); generation, level, NULL);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment