]> git.hungrycats.org Git - linux/commitdiff
btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root
authorJosef Bacik <josef@toxicpanda.com>
Wed, 20 Nov 2019 20:49:19 +0000 (12:49 -0800)
committerJosef Bacik <josef@toxicpanda.com>
Fri, 6 Dec 2019 16:34:48 +0000 (08:34 -0800)
Now that all callers of btrfs_get_fs_root are subsequently calling
btrfs_grab_fs_root and handling dropping the ref when they are done
appropriately, go ahead and push btrfs_grab_fs_root up into
btrfs_get_fs_root.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
14 files changed:
fs/btrfs/backref.c
fs/btrfs/disk-io.c
fs/btrfs/export.c
fs/btrfs/file.c
fs/btrfs/inode.c
fs/btrfs/ioctl.c
fs/btrfs/relocation.c
fs/btrfs/root-tree.c
fs/btrfs/scrub.c
fs/btrfs/send.c
fs/btrfs/super.c
fs/btrfs/transaction.c
fs/btrfs/tree-log.c
fs/btrfs/volumes.c

index 193747b6e1f92d4be9d39d7d2e4add40447383a5..b69154d7252990efb10e5b8dd6c9bfbcbf56875c 100644 (file)
@@ -527,12 +527,6 @@ static int resolve_indirect_ref(struct btrfs_fs_info *fs_info,
                goto out_free;
        }
 
-       if (!btrfs_grab_fs_root(root)) {
-               srcu_read_unlock(&fs_info->subvol_srcu, index);
-               ret = -ENOENT;
-               goto out_free;
-       }
-
        if (btrfs_is_testing(fs_info)) {
                srcu_read_unlock(&fs_info->subvol_srcu, index);
                ret = -ENOENT;
index b7a2c570c438c5280d36ec973b71ba103df1c2fd..64e9df8e64fd20e68c8699c98ee69d6fdacb823f 100644 (file)
@@ -1498,6 +1498,8 @@ static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
        spin_lock(&fs_info->fs_roots_radix_lock);
        root = radix_tree_lookup(&fs_info->fs_roots_radix,
                                 (unsigned long)root_id);
+       if (root)
+               root = btrfs_grab_fs_root(root);
        spin_unlock(&fs_info->fs_roots_radix_lock);
        return root;
 }
@@ -1554,29 +1556,31 @@ struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
        int ret;
 
        if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
-               return fs_info->tree_root;
+               return btrfs_grab_fs_root(fs_info->tree_root);
        if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
-               return fs_info->extent_root;
+               return btrfs_grab_fs_root(fs_info->extent_root);
        if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
-               return fs_info->chunk_root;
+               return btrfs_grab_fs_root(fs_info->chunk_root);
        if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
-               return fs_info->dev_root;
+               return btrfs_grab_fs_root(fs_info->dev_root);
        if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
-               return fs_info->csum_root;
+               return btrfs_grab_fs_root(fs_info->csum_root);
        if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
-               return fs_info->quota_root ? fs_info->quota_root :
-                                            ERR_PTR(-ENOENT);
+               return btrfs_grab_fs_root(fs_info->quota_root) ?
+                       fs_info->quota_root : ERR_PTR(-ENOENT);
        if (location->objectid == BTRFS_UUID_TREE_OBJECTID)
-               return fs_info->uuid_root ? fs_info->uuid_root :
-                                           ERR_PTR(-ENOENT);
+               return btrfs_grab_fs_root(fs_info->uuid_root) ?
+                       fs_info->uuid_root : ERR_PTR(-ENOENT);
        if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
-               return fs_info->free_space_root ? fs_info->free_space_root :
-                                                 ERR_PTR(-ENOENT);
+               return btrfs_grab_fs_root(fs_info->free_space_root) ?
+                       fs_info->free_space_root : ERR_PTR(-ENOENT);
 again:
        root = btrfs_lookup_fs_root(fs_info, location->objectid);
        if (root) {
-               if (check_ref && btrfs_root_refs(&root->root_item) == 0)
+               if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
+                       btrfs_put_fs_root(root);
                        return ERR_PTR(-ENOENT);
+               }
                return root;
        }
 
@@ -1609,8 +1613,18 @@ again:
        if (ret == 0)
                set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
 
+       /*
+        * All roots have two refs on them at all times, one for the mounted fs,
+        * and one for being in the radix tree.  This way we only free the root
+        * when we are unmounting or deleting the subvolume.  We get one ref
+        * from __setup_root, one for inserting it into the radix tree, and then
+        * we have the third for returning it, and the caller will put it when
+        * it's done with the root.
+        */
+       btrfs_grab_fs_root(root);
        ret = btrfs_insert_fs_root(fs_info, root);
        if (ret) {
+               btrfs_put_fs_root(root);
                if (ret == -EEXIST) {
                        btrfs_free_fs_root(root);
                        goto again;
@@ -3207,13 +3221,6 @@ int __cold open_ctree(struct super_block *sb,
                goto fail_qgroup;
        }
 
-       if (!btrfs_grab_fs_root(fs_info->fs_root)) {
-               fs_info->fs_root = NULL;
-               err = -ENOENT;
-               btrfs_warn(fs_info, "failed to grab a ref on the fs tree");
-               goto fail_qgroup;
-       }
-
        if (sb_rdonly(sb))
                return 0;
 
index eba6c6d27bade16b0a5afce5addb8d0a31312704..f07c2300ade223e9d56ae5d235ef77e7106851af 100644 (file)
@@ -82,10 +82,6 @@ static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
                err = PTR_ERR(root);
                goto fail;
        }
-       if (!btrfs_grab_fs_root(root)) {
-               err = -ENOENT;
-               goto fail;
-       }
 
        key.objectid = objectid;
        key.type = BTRFS_INODE_ITEM_KEY;
index f8e16f44a97069cee9a7ac62d9bb5429e6640281..062580a1721f3b74ae30705b052dd8a093b9bab4 100644 (file)
@@ -292,10 +292,6 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
                ret = PTR_ERR(inode_root);
                goto cleanup;
        }
-       if (!btrfs_grab_fs_root(inode_root)) {
-               ret = -ENOENT;
-               goto cleanup;
-       }
 
        key.objectid = defrag->ino;
        key.type = BTRFS_INODE_ITEM_KEY;
index 5fa39ac234724005315132ea7bd3f57eca46d7d9..bc961ca6995f421f98199b90a67aef8e8e554481 100644 (file)
@@ -2523,8 +2523,6 @@ static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
                         inum, offset, root_id);
                return PTR_ERR(root);
        }
-       if (!btrfs_grab_fs_root(root))
-               return 0;
 
        key.objectid = inum;
        key.type = BTRFS_EXTENT_DATA_KEY;
@@ -2711,9 +2709,6 @@ static noinline int relink_extent_backref(struct btrfs_path *path,
                return PTR_ERR(root);
        }
 
-       if (!btrfs_grab_fs_root(root))
-               return 0;
-
        if (btrfs_root_readonly(root)) {
                srcu_read_unlock(&fs_info->subvol_srcu, index);
                return 0;
@@ -5689,10 +5684,6 @@ static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
                err = PTR_ERR(new_root);
                goto out;
        }
-       if (!btrfs_grab_fs_root(new_root)) {
-               err = -ENOENT;
-               goto out;
-       }
 
        *sub_root = new_root;
        location->objectid = btrfs_root_dirid(&new_root->root_item);
index d5a994ab9602d925d4988168563a6f26158d0db5..7df2576aa2e348e3c7e7c4bcec6f15db326d8036 100644 (file)
@@ -672,11 +672,6 @@ static noinline int create_subvol(struct inode *dir,
                btrfs_abort_transaction(trans, ret);
                goto fail;
        }
-       if (!btrfs_grab_fs_root(new_root)) {
-               ret = -ENOENT;
-               btrfs_abort_transaction(trans, ret);
-               goto fail;
-       }
 
        btrfs_record_root_in_trans(trans, new_root);
 
@@ -2191,10 +2186,6 @@ static noinline int search_ioctl(struct inode *inode,
                        btrfs_free_path(path);
                        return PTR_ERR(root);
                }
-               if (!btrfs_grab_fs_root(root)) {
-                       btrfs_free_path(path);
-                       return -ENOENT;
-               }
        }
 
        key.objectid = sk->min_objectid;
@@ -2332,11 +2323,6 @@ static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
                root = NULL;
                goto out;
        }
-       if (!btrfs_grab_fs_root(root)) {
-               ret = -ENOENT;
-               root = NULL;
-               goto out;
-       }
 
        key.objectid = dirid;
        key.type = BTRFS_INODE_REF_KEY;
@@ -2433,10 +2419,6 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
                        ret = PTR_ERR(root);
                        goto out;
                }
-               if (!btrfs_grab_fs_root(root)) {
-                       ret = -ENOENT;
-                       goto out;
-               }
 
                key.objectid = dirid;
                key.type = BTRFS_INODE_REF_KEY;
@@ -2686,10 +2668,6 @@ static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
                ret = PTR_ERR(root);
                goto out_free;
        }
-       if (!btrfs_grab_fs_root(root)) {
-               ret = -ENOENT;
-               goto out;
-       }
        root_item = &root->root_item;
 
        subvol_info->treeid = key.objectid;
@@ -4025,10 +4003,6 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
                ret = PTR_ERR(new_root);
                goto out;
        }
-       if (!btrfs_grab_fs_root(root)) {
-               ret = -ENOENT;
-               goto out;
-       }
        if (!is_fstree(new_root->root_key.objectid)) {
                ret = -ENOENT;
                goto out_free;
index 086c8126ebf2ac5158121c5a10198190613cd95d..f6d63d6cf22d9156b60ae4eb5657061c87535600 100644 (file)
@@ -581,7 +581,6 @@ static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
                                        u64 root_objectid)
 {
        struct btrfs_key key;
-       struct btrfs_root *root;
 
        key.objectid = root_objectid;
        key.type = BTRFS_ROOT_ITEM_KEY;
@@ -590,12 +589,7 @@ static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
        else
                key.offset = (u64)-1;
 
-       root = btrfs_get_fs_root(fs_info, &key, false);
-       if (IS_ERR(root))
-               return root;
-       if (!btrfs_grab_fs_root(root))
-               return ERR_PTR(-ENOENT);
-       return root;
+       return btrfs_get_fs_root(fs_info, &key, false);
 }
 
 static noinline_for_stack
index cac2407cc00323b2974538dceddc7ec27e26b786..2c94b5318b402026f820dcdd396aef4c9178c7c8 100644 (file)
@@ -257,8 +257,6 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
 
                root = btrfs_get_fs_root(fs_info, &root_key, false);
                err = PTR_ERR_OR_ZERO(root);
-               if (!err && !btrfs_grab_fs_root(root))
-                       err = -ENOENT;
                if (err && err != -ENOENT) {
                        break;
                } else if (err == -ENOENT) {
index 904e860c509c5172d01eae8ea6c31824e2d10021..0c8b60055c2e430f13d4faa9987eb78e1169973d 100644 (file)
@@ -657,10 +657,6 @@ static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
                ret = PTR_ERR(local_root);
                goto err;
        }
-       if (!btrfs_grab_fs_root(local_root)) {
-               ret = -ENOENT;
-               goto err;
-       }
 
        /*
         * this makes the path point to (inum INODE_ITEM ioff)
index a82201e6979ec14edd94d626f25c3b8f2a7c23b7..6520ad8f4e81ee5b3f71b5c8bdfc6ed3b5770ee6 100644 (file)
@@ -7206,11 +7206,6 @@ long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
                                ret = PTR_ERR(clone_root);
                                goto out;
                        }
-                       if (!btrfs_grab_fs_root(clone_root)) {
-                               srcu_read_unlock(&fs_info->subvol_srcu, index);
-                               ret = -ENOENT;
-                               goto out;
-                       }
                        spin_lock(&clone_root->root_item_lock);
                        if (!btrfs_root_readonly(clone_root) ||
                            btrfs_root_dead(clone_root)) {
@@ -7252,12 +7247,6 @@ long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
                        ret = PTR_ERR(sctx->parent_root);
                        goto out;
                }
-               if (!btrfs_grab_fs_root(sctx->parent_root)) {
-                       srcu_read_unlock(&fs_info->subvol_srcu, index);
-                       ret = -ENOENT;
-                       sctx->parent_root = ERR_PTR(ret);
-                       goto out;
-               }
 
                spin_lock(&sctx->parent_root->root_item_lock);
                sctx->parent_root->send_in_progress++;
index cc411304e2c91528d04b0c5c4267a3a207cfca19..80fea16cf34cadd08c0b6cc3757403932db77cf4 100644 (file)
@@ -1082,10 +1082,6 @@ static char *get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
                        ret = PTR_ERR(fs_root);
                        goto err;
                }
-               if (!btrfs_grab_fs_root(fs_root)) {
-                       ret = -ENOENT;
-                       goto err;
-               }
 
                /*
                 * Walk up the filesystem tree by inode refs until we hit the
index 7008def3391be58db189180eaacb5e4c87c63f65..e194d3e4e3a9a5196dc1d90a37b80eca471feb9e 100644 (file)
@@ -1637,12 +1637,6 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
                btrfs_abort_transaction(trans, ret);
                goto fail;
        }
-       if (!btrfs_grab_fs_root(pending->snap)) {
-               ret = -ENOENT;
-               pending->snap = NULL;
-               btrfs_abort_transaction(trans, ret);
-               goto fail;
-       }
 
        ret = btrfs_reloc_post_snapshot(trans, pending);
        if (ret) {
index f2c26e07eedbcf8edd105ff10066832e48bd07ba..32ee8698a87f6284bc02f9df1547a294dac58cfd 100644 (file)
@@ -6292,10 +6292,6 @@ again:
                tmp_key.offset = (u64)-1;
 
                wc.replay_dest = btrfs_get_fs_root(fs_info, &tmp_key, true);
-               if (!IS_ERR(wc.replay_dest)) {
-                       if (!btrfs_grab_fs_root(wc.replay_dest))
-                               wc.replay_dest = ERR_PTR(-ENOENT);
-               }
                if (IS_ERR(wc.replay_dest)) {
                        ret = PTR_ERR(wc.replay_dest);
 
index 47ac3277ee08cee7f038956327f7a7416913d130..d261a39b7aa6b54c0cff0e333216e4ad69073e3a 100644 (file)
@@ -4379,10 +4379,6 @@ static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
                        ret = 1;
                goto out;
        }
-       if (!btrfs_grab_fs_root(subvol_root)) {
-               ret = 1;
-               goto out;
-       }
 
        switch (type) {
        case BTRFS_UUID_KEY_SUBVOL: