btrfs: don't take an extra root ref at allocation time
authorJosef Bacik <josef@toxicpanda.com>
Fri, 14 Feb 2020 21:11:45 +0000 (16:11 -0500)
committerDavid Sterba <dsterba@suse.com>
Mon, 23 Mar 2020 16:01:59 +0000 (17:01 +0100)
Now that all the users of roots take references for them we can drop the
extra root ref we've been taking.  Before we had roots at 2 refs for the
life of the file system, one for the radix tree, and one simply for
existing.  Now that we have proper ref accounting in all places that use
roots we can drop this extra ref simply for existing as we no longer
need it.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/disk-io.c
fs/btrfs/tests/qgroup-tests.c

index 831755a867ce3f261dfbdbaebd0d0ef578c70b6d..3a6d35e1edb723cc2a3a89cb35fde1596bcfbd59 100644 (file)
@@ -1601,22 +1601,11 @@ 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_root(root);
        ret = btrfs_insert_fs_root(fs_info, root);
        if (ret) {
                btrfs_put_root(root);
-               if (ret == -EEXIST) {
-                       btrfs_put_root(root);
+               if (ret == -EEXIST)
                        goto again;
-               }
                goto fail;
        }
        return root;
@@ -3904,11 +3893,13 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
 void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
                                  struct btrfs_root *root)
 {
+       bool drop_ref = false;
+
        spin_lock(&fs_info->fs_roots_radix_lock);
        radix_tree_delete(&fs_info->fs_roots_radix,
                          (unsigned long)root->root_key.objectid);
        if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state))
-               btrfs_put_root(root);
+               drop_ref = true;
        spin_unlock(&fs_info->fs_roots_radix_lock);
 
        if (btrfs_root_refs(&root->root_item) == 0)
@@ -3930,7 +3921,8 @@ void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
                iput(root->ino_cache_inode);
                root->ino_cache_inode = NULL;
        }
-       btrfs_put_root(root);
+       if (drop_ref)
+               btrfs_put_root(root);
 }
 
 int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
index ac035a6fa003dda0d9f4820f905e0a58df26d1f1..ce1ca8e73c2d1a8801e149fac57c8d771763ed1b 100644 (file)
@@ -507,6 +507,7 @@ int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
                test_err("couldn't insert fs root %d", ret);
                goto out;
        }
+       btrfs_put_root(tmp_root);
 
        tmp_root = btrfs_alloc_dummy_root(fs_info);
        if (IS_ERR(tmp_root)) {
@@ -521,6 +522,7 @@ int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
                test_err("couldn't insert fs root %d", ret);
                goto out;
        }
+       btrfs_put_root(tmp_root);
 
        test_msg("running qgroup tests");
        ret = test_no_shared_qgroup(root, sectorsize, nodesize);