Skip to content

Commit 4785e24

Browse files
josefbacikkdave
authored andcommitted
btrfs: don't take an extra root ref at allocation time
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 <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent dc9492c commit 4785e24

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

fs/btrfs/disk-io.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,22 +1601,11 @@ struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
16011601
if (ret == 0)
16021602
set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
16031603

1604-
/*
1605-
* All roots have two refs on them at all times, one for the mounted fs,
1606-
* and one for being in the radix tree. This way we only free the root
1607-
* when we are unmounting or deleting the subvolume. We get one ref
1608-
* from __setup_root, one for inserting it into the radix tree, and then
1609-
* we have the third for returning it, and the caller will put it when
1610-
* it's done with the root.
1611-
*/
1612-
btrfs_grab_root(root);
16131604
ret = btrfs_insert_fs_root(fs_info, root);
16141605
if (ret) {
16151606
btrfs_put_root(root);
1616-
if (ret == -EEXIST) {
1617-
btrfs_put_root(root);
1607+
if (ret == -EEXIST)
16181608
goto again;
1619-
}
16201609
goto fail;
16211610
}
16221611
return root;
@@ -3904,11 +3893,13 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
39043893
void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
39053894
struct btrfs_root *root)
39063895
{
3896+
bool drop_ref = false;
3897+
39073898
spin_lock(&fs_info->fs_roots_radix_lock);
39083899
radix_tree_delete(&fs_info->fs_roots_radix,
39093900
(unsigned long)root->root_key.objectid);
39103901
if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state))
3911-
btrfs_put_root(root);
3902+
drop_ref = true;
39123903
spin_unlock(&fs_info->fs_roots_radix_lock);
39133904

39143905
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,
39303921
iput(root->ino_cache_inode);
39313922
root->ino_cache_inode = NULL;
39323923
}
3933-
btrfs_put_root(root);
3924+
if (drop_ref)
3925+
btrfs_put_root(root);
39343926
}
39353927

39363928
int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)

fs/btrfs/tests/qgroup-tests.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
507507
test_err("couldn't insert fs root %d", ret);
508508
goto out;
509509
}
510+
btrfs_put_root(tmp_root);
510511

511512
tmp_root = btrfs_alloc_dummy_root(fs_info);
512513
if (IS_ERR(tmp_root)) {
@@ -521,6 +522,7 @@ int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
521522
test_err("couldn't insert fs root %d", ret);
522523
goto out;
523524
}
525+
btrfs_put_root(tmp_root);
524526

525527
test_msg("running qgroup tests");
526528
ret = test_no_shared_qgroup(root, sectorsize, nodesize);

0 commit comments

Comments
 (0)