Skip to content

Commit 0497dfb

Browse files
boryaskdave
authored andcommitted
btrfs: codify pattern for adding block_group to bg_list
Similar to mark_bg_unused() and mark_bg_to_reclaim(), we have a few places that use bg_list with refcounting, mostly for retrying failures to reclaim/delete unused. These have custom logic for handling locking and refcounting the bg_list properly, but they actually all want to do the same thing, so pull that logic out into a helper. Unfortunately, mark_bg_unused() does still need the NEW flag to avoid prematurely marking stuff unused (even if refcount is fine, we don't want to mess with bg creation), so it cannot use the new helper. Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Boris Burkov <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 7cbce3c commit 0497dfb

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

fs/btrfs/block-group.c

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,32 @@ static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
14551455
return ret == 0;
14561456
}
14571457

1458+
/*
1459+
* Link the block_group to a list via bg_list.
1460+
*
1461+
* @bg: The block_group to link to the list.
1462+
* @list: The list to link it to.
1463+
*
1464+
* Use this rather than list_add_tail() directly to ensure proper respect
1465+
* to locking and refcounting.
1466+
*
1467+
* Returns: true if the bg was linked with a refcount bump and false otherwise.
1468+
*/
1469+
static bool btrfs_link_bg_list(struct btrfs_block_group *bg, struct list_head *list)
1470+
{
1471+
struct btrfs_fs_info *fs_info = bg->fs_info;
1472+
bool added = false;
1473+
1474+
spin_lock(&fs_info->unused_bgs_lock);
1475+
if (list_empty(&bg->bg_list)) {
1476+
btrfs_get_block_group(bg);
1477+
list_add_tail(&bg->bg_list, list);
1478+
added = true;
1479+
}
1480+
spin_unlock(&fs_info->unused_bgs_lock);
1481+
return added;
1482+
}
1483+
14581484
/*
14591485
* Process the unused_bgs list and remove any that don't have any allocated
14601486
* space inside of them.
@@ -1570,8 +1596,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
15701596
* drop under the "next" label for the
15711597
* fs_info->unused_bgs list.
15721598
*/
1573-
btrfs_get_block_group(block_group);
1574-
list_add_tail(&block_group->bg_list, &retry_list);
1599+
btrfs_link_bg_list(block_group, &retry_list);
15751600

15761601
trace_btrfs_skip_unused_block_group(block_group);
15771602
spin_unlock(&block_group->lock);
@@ -1968,20 +1993,8 @@ void btrfs_reclaim_bgs_work(struct work_struct *work)
19681993
spin_unlock(&space_info->lock);
19691994

19701995
next:
1971-
if (ret && !READ_ONCE(space_info->periodic_reclaim)) {
1972-
/* Refcount held by the reclaim_bgs list after splice. */
1973-
spin_lock(&fs_info->unused_bgs_lock);
1974-
/*
1975-
* This block group might be added to the unused list
1976-
* during the above process. Move it back to the
1977-
* reclaim list otherwise.
1978-
*/
1979-
if (list_empty(&bg->bg_list)) {
1980-
btrfs_get_block_group(bg);
1981-
list_add_tail(&bg->bg_list, &retry_list);
1982-
}
1983-
spin_unlock(&fs_info->unused_bgs_lock);
1984-
}
1996+
if (ret && !READ_ONCE(space_info->periodic_reclaim))
1997+
btrfs_link_bg_list(bg, &retry_list);
19851998
btrfs_put_block_group(bg);
19861999

19872000
mutex_unlock(&fs_info->reclaim_bgs_lock);
@@ -2021,13 +2034,8 @@ void btrfs_mark_bg_to_reclaim(struct btrfs_block_group *bg)
20212034
{
20222035
struct btrfs_fs_info *fs_info = bg->fs_info;
20232036

2024-
spin_lock(&fs_info->unused_bgs_lock);
2025-
if (list_empty(&bg->bg_list)) {
2026-
btrfs_get_block_group(bg);
2037+
if (btrfs_link_bg_list(bg, &fs_info->reclaim_bgs))
20272038
trace_btrfs_add_reclaim_block_group(bg);
2028-
list_add_tail(&bg->bg_list, &fs_info->reclaim_bgs);
2029-
}
2030-
spin_unlock(&fs_info->unused_bgs_lock);
20312039
}
20322040

20332041
static int read_bg_from_eb(struct btrfs_fs_info *fs_info, const struct btrfs_key *key,
@@ -2940,8 +2948,7 @@ struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *tran
29402948
}
29412949
#endif
29422950

2943-
btrfs_get_block_group(cache);
2944-
list_add_tail(&cache->bg_list, &trans->new_bgs);
2951+
btrfs_link_bg_list(cache, &trans->new_bgs);
29452952
btrfs_inc_delayed_refs_rsv_bg_inserts(fs_info);
29462953

29472954
set_avail_alloc_bits(fs_info, type);

0 commit comments

Comments
 (0)