Skip to content

Commit ded9c77

Browse files
fdmananakdave
authored andcommitted
btrfs: check BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE at __add_block_group_free_space()
Every caller of __add_block_group_free_space() is checking if the flag BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE is set before calling it. This is duplicate code and it's prone to some mistake in case we add more callers in the future. So move the check for that flag into the start of __add_block_group_free_space(), and, as a consequence, the path allocation from add_block_group_free_space() is moved into __add_block_group_free_space(), to preserve the behaviour of allocating a path only if the flag BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE is set. Reviewed-by: Boris Burkov <[email protected]> Signed-off-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 9d4f4e7 commit ded9c77

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

fs/btrfs/free-space-tree.c

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,9 @@ int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
816816
u32 flags;
817817
int ret;
818818

819-
if (test_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &block_group->runtime_flags)) {
820-
ret = __add_block_group_free_space(trans, block_group, path);
821-
if (ret)
822-
return ret;
823-
}
819+
ret = __add_block_group_free_space(trans, block_group, path);
820+
if (ret)
821+
return ret;
824822

825823
info = search_free_space_info(NULL, block_group, path, 0);
826824
if (IS_ERR(info))
@@ -1011,11 +1009,9 @@ int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
10111009
u32 flags;
10121010
int ret;
10131011

1014-
if (test_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &block_group->runtime_flags)) {
1015-
ret = __add_block_group_free_space(trans, block_group, path);
1016-
if (ret)
1017-
return ret;
1018-
}
1012+
ret = __add_block_group_free_space(trans, block_group, path);
1013+
if (ret)
1014+
return ret;
10191015

10201016
info = search_free_space_info(NULL, block_group, path, 0);
10211017
if (IS_ERR(info))
@@ -1403,9 +1399,12 @@ static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
14031399
struct btrfs_block_group *block_group,
14041400
struct btrfs_path *path)
14051401
{
1402+
bool own_path = false;
14061403
int ret;
14071404

1408-
clear_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &block_group->runtime_flags);
1405+
if (!test_and_clear_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE,
1406+
&block_group->runtime_flags))
1407+
return 0;
14091408

14101409
/*
14111410
* While rebuilding the free space tree we may allocate new metadata
@@ -1430,44 +1429,43 @@ static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
14301429
*/
14311430
set_bit(BLOCK_GROUP_FLAG_FREE_SPACE_ADDED, &block_group->runtime_flags);
14321431

1432+
if (!path) {
1433+
path = btrfs_alloc_path();
1434+
if (!path) {
1435+
btrfs_abort_transaction(trans, -ENOMEM);
1436+
return -ENOMEM;
1437+
}
1438+
own_path = true;
1439+
}
1440+
14331441
ret = add_new_free_space_info(trans, block_group, path);
14341442
if (ret) {
14351443
btrfs_abort_transaction(trans, ret);
1436-
return ret;
1444+
goto out;
14371445
}
14381446

14391447
ret = __add_to_free_space_tree(trans, block_group, path,
14401448
block_group->start, block_group->length);
14411449
if (ret)
14421450
btrfs_abort_transaction(trans, ret);
14431451

1444-
return 0;
1452+
out:
1453+
if (own_path)
1454+
btrfs_free_path(path);
1455+
1456+
return ret;
14451457
}
14461458

14471459
int add_block_group_free_space(struct btrfs_trans_handle *trans,
14481460
struct btrfs_block_group *block_group)
14491461
{
1450-
struct btrfs_fs_info *fs_info = trans->fs_info;
1451-
struct btrfs_path *path = NULL;
1452-
int ret = 0;
1462+
int ret;
14531463

1454-
if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1464+
if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
14551465
return 0;
14561466

14571467
mutex_lock(&block_group->free_space_lock);
1458-
if (!test_bit(BLOCK_GROUP_FLAG_NEEDS_FREE_SPACE, &block_group->runtime_flags))
1459-
goto out;
1460-
1461-
path = btrfs_alloc_path();
1462-
if (!path) {
1463-
ret = -ENOMEM;
1464-
btrfs_abort_transaction(trans, ret);
1465-
goto out;
1466-
}
1467-
1468-
ret = __add_block_group_free_space(trans, block_group, path);
1469-
out:
1470-
btrfs_free_path(path);
1468+
ret = __add_block_group_free_space(trans, block_group, NULL);
14711469
mutex_unlock(&block_group->free_space_lock);
14721470
return ret;
14731471
}

0 commit comments

Comments
 (0)