Skip to content

Commit 168a2f7

Browse files
XidianGeneralkdave
authored andcommitted
btrfs: fix root ref counts in error handling in btrfs_get_root_ref
In btrfs_get_root_ref(), when btrfs_insert_fs_root() fails, btrfs_put_root() can happen for two reasons: - the root already exists in the tree, in that case it returns the reference obtained in btrfs_lookup_fs_root() - another error so the cleanup is done in the fail label Calling btrfs_put_root() unconditionally would lead to double decrement of the root reference possibly freeing it in the second case. Reported-by: TOTE Robot <oslab@tsinghua.edu.cn> Fixes: bc44d7c ("btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root") CC: stable@vger.kernel.org # 5.10+ Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 760e69c commit 168a2f7

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

fs/btrfs/disk-io.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,9 +1849,10 @@ static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
18491849

18501850
ret = btrfs_insert_fs_root(fs_info, root);
18511851
if (ret) {
1852-
btrfs_put_root(root);
1853-
if (ret == -EEXIST)
1852+
if (ret == -EEXIST) {
1853+
btrfs_put_root(root);
18541854
goto again;
1855+
}
18551856
goto fail;
18561857
}
18571858
return root;

0 commit comments

Comments
 (0)