diff mbox series

[v3,1/3] btrfs: enhance unsupported compat RO flags handling

Message ID 1b3011f4b1bf4e60479568fcd3e090ea8b68d253.1660021230.git.wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs: separate BLOCK_GROUP_TREE feature from extent-tree-v2 | expand

Commit Message

Qu Wenruo Aug. 9, 2022, 5:02 a.m. UTC
Currently there are two corner cases not handling compat RO flags
correctly:

- Remount
  We can still mount the fs RO with compat RO flags, then remount it RW.
  We should not allow any write into a fs with unsupported RO flags.

- Still try to search block group items
  In fact, behavior/on-disk format change to extent tree should not
  need a full incompat flag.

  And since we can ensure fs with unsupported RO flags never got any
  writes (with above case fixed), then we can even skip block group
  items search at mount time.

This patch will enhance the unsupported RO compat flags by:

- Reject RW remount if there is unsupported RO compat flags

- Go dummy block group items directly for unsupported RO compat flags
  In fact, only changes to chunk/subvolume/root/csum trees should go
  incompat flags.

The latter part should allow future change to extent tree to be compat
RO flags.

Thus this patch also needs to be backported to all stable trees.

Cc: stable@vger.kernel.org
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/block-group.c | 11 ++++++++++-
 fs/btrfs/super.c       |  9 +++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

Comments

David Sterba Sept. 5, 2022, 2:53 p.m. UTC | #1
On Tue, Aug 09, 2022 at 01:02:16PM +0800, Qu Wenruo wrote:
> Currently there are two corner cases not handling compat RO flags
> correctly:
> 
> - Remount
>   We can still mount the fs RO with compat RO flags, then remount it RW.
>   We should not allow any write into a fs with unsupported RO flags.
> 
> - Still try to search block group items
>   In fact, behavior/on-disk format change to extent tree should not
>   need a full incompat flag.
> 
>   And since we can ensure fs with unsupported RO flags never got any
>   writes (with above case fixed), then we can even skip block group
>   items search at mount time.
> 
> This patch will enhance the unsupported RO compat flags by:
> 
> - Reject RW remount if there is unsupported RO compat flags
> 
> - Go dummy block group items directly for unsupported RO compat flags
>   In fact, only changes to chunk/subvolume/root/csum trees should go
>   incompat flags.
> 
> The latter part should allow future change to extent tree to be compat
> RO flags.
> 
> Thus this patch also needs to be backported to all stable trees.
> 
> Cc: stable@vger.kernel.org

This applies cleanly only to 5.19, anything else would need a separate
backport. I'm planning to send this patch among 6.0 fixes so this
should give us time to get it to older stable kernels before the block
group tree is released.
Qu Wenruo Sept. 5, 2022, 10:36 p.m. UTC | #2
On 2022/9/5 22:53, David Sterba wrote:
> On Tue, Aug 09, 2022 at 01:02:16PM +0800, Qu Wenruo wrote:
>> Currently there are two corner cases not handling compat RO flags
>> correctly:
>>
>> - Remount
>>    We can still mount the fs RO with compat RO flags, then remount it RW.
>>    We should not allow any write into a fs with unsupported RO flags.
>>
>> - Still try to search block group items
>>    In fact, behavior/on-disk format change to extent tree should not
>>    need a full incompat flag.
>>
>>    And since we can ensure fs with unsupported RO flags never got any
>>    writes (with above case fixed), then we can even skip block group
>>    items search at mount time.
>>
>> This patch will enhance the unsupported RO compat flags by:
>>
>> - Reject RW remount if there is unsupported RO compat flags
>>
>> - Go dummy block group items directly for unsupported RO compat flags
>>    In fact, only changes to chunk/subvolume/root/csum trees should go
>>    incompat flags.
>>
>> The latter part should allow future change to extent tree to be compat
>> RO flags.
>>
>> Thus this patch also needs to be backported to all stable trees.
>>
>> Cc: stable@vger.kernel.org
>
> This applies cleanly only to 5.19, anything else would need a separate
> backport. I'm planning to send this patch among 6.0 fixes so this
> should give us time to get it to older stable kernels before the block
> group tree is released.

No problem, in fact I (normally) only start backports when I got the
failed to apply message from Greg.

So I'd go the regular backport cycle for it.

Thanks,
Qu
diff mbox series

Patch

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 38a740d5297c..4a78136bcd17 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -2186,7 +2186,16 @@  int btrfs_read_block_groups(struct btrfs_fs_info *info)
 	int need_clear = 0;
 	u64 cache_gen;
 
-	if (!root)
+	/*
+	 * Either no extent root (with ibadroots rescue option) or we have
+	 * unsupporter RO options. The fs can never be mounted RW, so no
+	 * need to waste time search block group items.
+	 *
+	 * This also allows new extent tree related changes to be RO compat,
+	 * no need for a full incompat flag.
+	 */
+	if (!root || (btrfs_super_compat_ro_flags(info->super_copy) &
+		      ~BTRFS_FEATURE_COMPAT_RO_SUPP))
 		return fill_dummy_bgs(info);
 
 	key.objectid = 0;
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 4c7089b1681b..7d3213e67fb5 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -2110,6 +2110,15 @@  static int btrfs_remount(struct super_block *sb, int *flags, char *data)
 			ret = -EINVAL;
 			goto restore;
 		}
+		if (btrfs_super_compat_ro_flags(fs_info->super_copy) &
+		    ~BTRFS_FEATURE_COMPAT_RO_SUPP) {
+			btrfs_err(fs_info,
+		"can not remount read-write due to unsupported optional flags 0x%llx",
+				btrfs_super_compat_ro_flags(fs_info->super_copy) &
+				~BTRFS_FEATURE_COMPAT_RO_SUPP);
+			ret = -EINVAL;
+			goto restore;
+		}
 		if (fs_info->fs_devices->rw_devices == 0) {
 			ret = -EACCES;
 			goto restore;