Skip to content

Commit daa0fde

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: e606ff985ec7 ("btrfs: zoned: reserve data_reloc block group on mount") Reviewed-by: Johannes Thumshirn <[email protected]> Signed-off-by: Naohiro Aota <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent f0ba0e7 commit daa0fde

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
@@ -2519,12 +2520,12 @@ void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
25192520
void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
25202521
{
25212522
struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
2522-
struct btrfs_space_info *space_info = data_sinfo->sub_group[0];
2523+
struct btrfs_space_info *space_info = data_sinfo;
25232524
struct btrfs_trans_handle *trans;
25242525
struct btrfs_block_group *bg;
25252526
struct list_head *bg_list;
25262527
u64 alloc_flags;
2527-
bool initial = false;
2528+
bool first = true;
25282529
bool did_chunk_alloc = false;
25292530
int index;
25302531
int ret;
@@ -2538,21 +2539,52 @@ void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info)
25382539
if (sb_rdonly(fs_info->sb))
25392540
return;
25402541

2541-
ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC);
25422542
alloc_flags = btrfs_get_alloc_profile(fs_info, space_info->flags);
25432543
index = btrfs_bg_flags_to_raid_index(alloc_flags);
25442544

2545-
bg_list = &data_sinfo->block_groups[index];
2545+
/* Scan the data space_info to find empty block groups. Take the second one. */
25462546
again:
2547+
bg_list = &space_info->block_groups[index];
25472548
list_for_each_entry(bg, bg_list, list) {
2548-
if (bg->used > 0)
2549+
if (bg->alloc_offset != 0)
25492550
continue;
25502551

2551-
if (!initial) {
2552-
initial = true;
2552+
if (first) {
2553+
first = false;
25532554
continue;
25542555
}
25552556

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

2602+
/* Allocate new BG in the data relocation space_info. */
2603+
space_info = data_sinfo->sub_group[0];
2604+
ASSERT(space_info->subgroup_id == BTRFS_SUB_GROUP_DATA_RELOC);
25702605
ret = btrfs_chunk_alloc(trans, space_info, alloc_flags, CHUNK_ALLOC_FORCE);
25712606
btrfs_end_transaction(trans);
25722607
if (ret == 1) {
2608+
/*
2609+
* We allocated a new block group in the data relocation space_info. We
2610+
* can take that one.
2611+
*/
2612+
first = false;
25732613
did_chunk_alloc = true;
2574-
bg_list = &space_info->block_groups[index];
25752614
goto again;
25762615
}
25772616
}

0 commit comments

Comments
 (0)