Skip to content

Commit 0752841

Browse files
naotakdave
authored andcommitted
btrfs: zoned: fix data relocation block group reservation
btrfs_zoned_reserve_data_reloc_bg() is called on mount and at that point, all data block groups belong to the primary data space_info. So, we don't find anything in the data relocation space_info. Also, the condition "bg->used > 0" can select a block group with full of zone_unusable bytes for the candidate. As we cannot allocate from the block group, it is useless to reserve it as the data relocation block group. Furthermore, because of the space_info separation, we need to migrate the selected block group to the data relocation space_info. If not, the extent allocator cannot use the block group to do the allocation. This commit fixes these three issues. Fixes: e606ff9 ("btrfs: zoned: reserve data_reloc block group on mount") Signed-off-by: Naohiro Aota <[email protected]> Reviewed-by: Johannes Thumshirn <[email protected]>
1 parent 50e2361 commit 0752841

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

fs/btrfs/zoned.c

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "accessors.h"
1818
#include "bio.h"
1919
#include "transaction.h"
20+
#include "sysfs.h"
2021

2122
/* Maximum number of zones to report per blkdev_report_zones() call */
2223
#define BTRFS_REPORT_NR_ZONES 4096
@@ -2525,12 +2526,12 @@ void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
25252526
void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
25262527
{
25272528
struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
2528-
struct btrfs_space_info *space_info = data_sinfo->sub_group[0];
2529+
struct btrfs_space_info *space_info = data_sinfo;
25292530
struct btrfs_trans_handle *trans;
25302531
struct btrfs_block_group *bg;
25312532
struct list_head *bg_list;
25322533
u64 alloc_flags;
2533-
bool initial = false;
2534+
bool first = true;
25342535
bool did_chunk_alloc = false;
25352536
int index;
25362537
int ret;
@@ -2544,21 +2545,52 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
25442545
if (sb_rdonly(fs_info->sb))
25452546
return;
25462547

2547-
ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC);
25482548
alloc_flags = btrfs_get_alloc_profile(fs_info, space_info->flags);
25492549
index = btrfs_bg_flags_to_raid_index(alloc_flags);
25502550

2551-
bg_list = &data_sinfo->block_groups[index];
2551+
/* Scan the data space_info to find empty block groups. Take the second one. */
25522552
again:
2553+
bg_list = &space_info->block_groups[index];
25532554
list_for_each_entry(bg, bg_list, list) {
2554-
if (bg->used > 0)
2555+
if (bg->alloc_offset != 0)
25552556
continue;
25562557

2557-
if (!initial) {
2558-
initial = true;
2558+
if (first) {
2559+
first = false;
25592560
continue;
25602561
}
25612562

2563+
if (space_info == data_sinfo) {
2564+
/* Migrate the block group to the data relocation space_info. */
2565+
struct btrfs_space_info *reloc_sinfo = data_sinfo->sub_group[0];
2566+
int factor;
2567+
2568+
ASSERT(reloc_sinfo->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC);
2569+
factor = btrfs_bg_type_to_factor(bg->flags);
2570+
2571+
down_write(&space_info->groups_sem);
2572+
list_del_init(&bg->list);
2573+
/* We can assume this as we choose the second empty one. */
2574+
ASSERT(!list_empty(&space_info->block_groups[index]));
2575+
up_write(&space_info->groups_sem);
2576+
2577+
spin_lock(&space_info->lock);
2578+
space_info->total_bytes -= bg->length;
2579+
space_info->disk_total -= bg->length * factor;
2580+
/* There is no allocation ever happened. */
2581+
ASSERT(bg->used == 0);
2582+
ASSERT(bg->zone_unusable == 0);
2583+
/* No super block in a block group on the zoned setup. */
2584+
ASSERT(bg->bytes_super == 0);
2585+
spin_unlock(&space_info->lock);
2586+
2587+
bg->space_info = reloc_sinfo;
2588+
if (reloc_sinfo->block_group_kobjs[index] == NULL)
2589+
btrfs_sysfs_add_block_group_type(bg);
2590+
2591+
btrfs_add_bg_to_space_info(fs_info, bg);
2592+
}
2593+
25622594
fs_info->data_reloc_bg = bg->start;
25632595
set_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC, &bg->runtime_flags);
25642596
btrfs_zone_activate(bg);
@@ -2573,11 +2605,18 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
25732605
if (IS_ERR(trans))
25742606
return;
25752607

2608+
/* Allocate new BG in the data relocation space_info. */
2609+
space_info = data_sinfo->sub_group[0];
2610+
ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC);
25762611
ret = btrfs_chunk_alloc(trans, space_info, alloc_flags, CHUNK_ALLOC_FORCE);
25772612
btrfs_end_transaction(trans);
25782613
if (ret == 1) {
2614+
/*
2615+
* We allocated a new block group in the data relocation space_info. We
2616+
* can take that one.
2617+
*/
2618+
first = false;
25792619
did_chunk_alloc = true;
2580-
bg_list = &space_info->block_groups[index];
25812620
goto again;
25822621
}
25832622
}

0 commit comments

Comments
 (0)