Skip to contentSkip to conversation

btrfs: minor bs != ps cases fixes for free space tree enforcing

PATCH In review Qu Wenruo 2 participants 4 messages Dec 18, 2025 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs: minor bs != ps cases fixes for free space tree enforcing

I'm update btrfs/131 to remove the v1 cache usage since v1 cache is already marked deprecated.

With that update, I can run the test on bs < ps and bs > ps cases, the formal failed due to the following reason:

- Free space cache is always enforced for bs < ps case Even if 'nospace_cache' mount option is provided. This is fixed by the first patch

And during tests I also exposed a minor problem for bs > ps cases:

- v1 cache mount is rejected for bs > ps cases
That's because we lack the automatic free space tree enforcing for bs > ps cases.
This is fixed by the second patch.

Qu Wenruo (2):
btrfs: only enforce free space tree if v1 cache is required for bs <
ps cases
btrfs: forcing free space tree for bs > ps cases

 fs/btrfs/super.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
Signature · 1 line
2.52.0
author
#1 lore raw

PATCH 1/2 btrfs: only enforce free space tree if v1 cache is required for bs < ps cases

[BUG]
Since the introduction of btrfs bs < ps support, v1 cache is never on the plan due to its hard coded PAGE_SIZE usage, and the future plan to properly deprecate it.

However for bs < ps cases, even if 'nospace_cache,clear_cache' mount option is specified, it's never respected and free space tree is always enabled:

mkfs.btrfs -f -O ^bgt,fst $dev
mount $dev $mnt -o clear_cache,nospace_cache
umount $mnt
btrfs ins dump-super $dev
...
compat_ro_flags 0x3
( FREE_SPACE_TREE |
FREE_SPACE_TREE_VALID )
...

This means a different behavior compared to bs >= ps cases.

[CAUSE]
The forcing usage of v2 space cache is done inside btrfs_set_free_space_cache_settings(), however it never checks if we're even using space cache but always enabling v2 cache.

[FIX]
Instead unconditionally enable v2 cache, only forcing v2 cache if the old v1 cache is required.

Now v2 space cache can be properly disabled on bs < ps cases:

mkfs.btrfs -f -O ^bgt,fst $dev
mount $dev $mnt -o clear_cache,nospace_cache
umount $mnt
btrfs ins dump-super $dev
...
compat_ro_flags 0x0
...

Fixes 9f73f1aef98b ("btrfs: force v2 space cache usage for subpage mount")
---
 fs/btrfs/super.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
fs/btrfs/super.c +5 -7
index a37b71091014..09c38becf20b 100644
@@ -736,14 +736,12 @@ bool btrfs_check_options(const struct btrfs_fs_info *info,
  */
 void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info)
 {
-	if (fs_info->sectorsize < PAGE_SIZE) {
+	if (fs_info->sectorsize < PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
+		btrfs_info(fs_info,
+			   "forcing free space tree for sector size %u with page size %lu",
+			   fs_info->sectorsize, PAGE_SIZE);
 		btrfs_clear_opt(fs_info->mount_opt, SPACE_CACHE);
-		if (!btrfs_test_opt(fs_info, FREE_SPACE_TREE)) {
-			btrfs_info(fs_info,
-				   "forcing free space tree for sector size %u with page size %lu",
-				   fs_info->sectorsize, PAGE_SIZE);
-			btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
-		}
+		btrfs_set_opt(fs_info->mount_opt, FREE_SPACE_TREE);
 	}
 
 	/*
Signature · 1 line
2.52.0
author
#2 lore raw

PATCH 2/2 btrfs: forcing free space tree for bs > ps cases

In reply to Qu Wenruo

[BUG]
Currently we only enforcing free space tree for bs < ps cases, but with the recently added bs > ps support, we lack the free space tree enforcing, causing explicit v1 cache mount option to fail on bs > ps cases:

 # mount -o space_cache=v1 /dev/test/scratch1  /mnt/btrfs/
 mount: /mnt/btrfs: wrong fs type, bad option, bad superblock on /dev/mapper/test-scratch1, missing codepage or helper program, or other error.
        dmesg(1) may have more information after failed mount system call.
 # dmesg -t | tail -n7
 BTRFS: device fsid ac14a6fa-4ec9-449e-aec9-7d1777bfdc06 devid 1 transid 11 /dev/mapper/test-scratch1 (253:3) scanned by mount (2849)
 BTRFS info (device dm-3): first mount of filesystem ac14a6fa-4ec9-449e-aec9-7d1777bfdc06
 BTRFS info (device dm-3): using crc32c checksum algorithm
 BTRFS warning (device dm-3): support for block size 8192 with page size 4096 is experimental, some features may be missing
 BTRFS warning (device dm-3): space cache v1 is being deprecated and will be removed in a future release, please use -o space_cache=v2
 BTRFS warning (device dm-3): v1 space cache is not supported for page size 4096 with sectorsize 8192
 BTRFS error (device dm-3): open_ctree failed: -22

[FIX]
Just enable the same free space tree for bs > ps cases, aligning the behavior to bs < ps cases.

---
 fs/btrfs/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/super.c +1 -1
index 09c38becf20b..0a931555e6dc 100644
@@ -736,7 +736,7 @@ bool btrfs_check_options(const struct btrfs_fs_info *info,
  */
 void btrfs_set_free_space_cache_settings(struct btrfs_fs_info *fs_info)
 {
-	if (fs_info->sectorsize < PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
+	if (fs_info->sectorsize != PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
 		btrfs_info(fs_info,
 			   "forcing free space tree for sector size %u with page size %lu",
 			   fs_info->sectorsize, PAGE_SIZE);
Signature · 1 line
2.52.0
Reviewed-by on the v1 series
#3 lore raw
In reply to Qu Wenruo
Quoted 18 lines · Qu Wenruo

I'm update btrfs/131 to remove the v1 cache usage since v1 cache is already marked deprecated.

With that update, I can run the test on bs < ps and bs > ps cases, the formal failed due to the following reason:

- Free space cache is always enforced for bs < ps case Even if 'nospace_cache' mount option is provided. This is fixed by the first patch

And during tests I also exposed a minor problem for bs > ps cases:

- v1 cache mount is rejected for bs > ps cases
That's because we lack the automatic free space tree enforcing for bs > ps cases.
This is fixed by the second patch.

Quoted 4 lines

Qu Wenruo (2):
btrfs: only enforce free space tree if v1 cache is required for bs <
ps cases
btrfs: forcing free space tree for bs > ps cases

Looks good, thanks.

Quoted 8 lines
 fs/btrfs/super.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

--
2.52.0