[PATCH v5 11/28] btrfs: make unmirroed BGs readonly only if we have at least one writable BG

Naohiro Aota
2019-12-04T08:19:51Z

If the btrfs volume has mirrored block groups, it unconditionally makes
un-mirrored block groups read only. When we have mirrored block groups, but
don't have writable block groups, this will drop all writable block groups.
So, check if we have at least one writable mirrored block group before
setting un-mirrored block groups read only.

This change is necessary to handle e.g. xfstests btrfs/124 case.

When we mount degraded RAID1 FS and write to it, and then re-mount with
full device, the write pointers of corresponding zones of written block
group differ. We mark such block group as "wp_broken" and make it read
only. In this situation, we only have read only RAID1 block groups because
of "wp_broken" and un-mirrored block groups are also marked read only,
because we have RAID1 block groups. As a result, all the block groups are
now read only, so that we cannot even start the rebalance to fix the
situation.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
---
fs/btrfs/block-group.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 5c04422f6f5a..b286359f3876 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -1813,6 +1813,27 @@ static int read_one_block_group(struct btrfs_fs_info *info,
return ret;
}

+/*
+ * have_mirrored_block_group - check if we have at least one writable
+ * mirrored Block Group
+ */
+static bool have_mirrored_block_group(struct btrfs_space_info *space_info)
+{
+ struct btrfs_block_group *block_group;
+ int i;
+
+ for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
+ if (i == BTRFS_RAID_RAID0 || i == BTRFS_RAID_SINGLE)
+ continue;
+ list_for_each_entry(block_group, &space_info->block_groups[i],
+ list) {
+ if (!block_group->ro)
+ return true;
+ }
+ }
+ return false;
+}
+
int btrfs_read_block_groups(struct btrfs_fs_info *info)
{
struct btrfs_path *path;
@@ -1861,6 +1882,10 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
BTRFS_BLOCK_GROUP_RAID56_MASK |
BTRFS_BLOCK_GROUP_DUP)))
continue;
+
+ if (!have_mirrored_block_group(space_info))
+ continue;
+
/*
* Avoid allocating from un-mirrored block group if there are
* mirrored block groups.
--
2.24.0