]> git.hungrycats.org Git - linux/commitdiff
btrfs: push __setup_root into btrfs_alloc_root
authorJosef Bacik <josef@toxicpanda.com>
Tue, 26 Nov 2019 19:24:59 +0000 (11:24 -0800)
committerJosef Bacik <josef@toxicpanda.com>
Fri, 6 Dec 2019 16:34:48 +0000 (08:34 -0800)
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.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
fs/btrfs/disk-io.c

index 8783d86f487dbd95512ba752902059f209ecb273..ee0ddd4f45f0fc6728ed90dc3c1d15cee09a41d8 100644 (file)
@@ -1132,6 +1132,7 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
                         u64 objectid)
 {
        bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
+       root->fs_info = fs_info;
        root->node = NULL;
        root->commit_root = NULL;
        root->state = 0;
@@ -1200,11 +1201,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,
-               gfp_t flags)
+                                          u64 objectid, gfp_t flags)
 {
        struct btrfs_root *root = kzalloc(sizeof(*root), flags);
        if (root)
-               root->fs_info = fs_info;
+               __setup_root(root, fs_info, objectid);
        return root;
 }
 
@@ -1217,12 +1218,11 @@ struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
        if (!fs_info)
                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)
                return ERR_PTR(-ENOMEM);
 
        /* 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;
 
        return root;
@@ -1246,12 +1246,11 @@ struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
         * context to avoid deadlock if reclaim happens.
         */
        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);
        if (!root)
                return ERR_PTR(-ENOMEM);
 
-       __setup_root(root, fs_info, objectid);
        root->root_key.objectid = objectid;
        root->root_key.type = BTRFS_ROOT_ITEM_KEY;
        root->root_key.offset = 0;
@@ -1311,12 +1310,10 @@ static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
        struct btrfs_root *root;
        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)
                return ERR_PTR(-ENOMEM);
 
-       __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID);
-
        root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
        root->root_key.type = BTRFS_ROOT_ITEM_KEY;
        root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
@@ -1403,14 +1400,12 @@ static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
        if (!path)
                return ERR_PTR(-ENOMEM);
 
-       root = btrfs_alloc_root(fs_info, GFP_NOFS);
+       root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS);
        if (!root) {
                ret = -ENOMEM;
                goto alloc_fail;
        }
 
-       __setup_root(root, fs_info, key->objectid);
-
        ret = btrfs_find_root(tree_root, key, path,
                              &root->root_item, &root->root_key);
        if (ret) {
@@ -2205,12 +2200,11 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
                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)
                return -ENOMEM;
 
-       __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
-
        log_tree_root->node = read_tree_block(fs_info, bytenr,
                                              fs_info->generation + 1,
                                              level, NULL);
@@ -2642,8 +2636,10 @@ int __cold open_ctree(struct super_block *sb,
        int clear_free_space_tree = 0;
        int level;
 
-       tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
-       chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
+       tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info,
+                                       BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL);
+       chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info,
+                                       BTRFS_CHUNK_TREE_OBJECTID, GFP_KERNEL);
        if (!tree_root || !chunk_root) {
                err = -ENOMEM;
                goto fail;
@@ -2821,8 +2817,6 @@ int __cold open_ctree(struct super_block *sb,
                goto fail_alloc;
        }
 
-       __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
-
        invalidate_bdev(fs_devices->latest_bdev);
 
        /*
@@ -3018,8 +3012,6 @@ int __cold open_ctree(struct super_block *sb,
        generation = btrfs_super_chunk_root_generation(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,
                                           btrfs_super_chunk_root(disk_super),
                                           generation, level, NULL);