Skip to contentSkip to conversation

btrfs: zoned: fix removing of unwritten non-data block group

PATCH Superseded Naohiro Aota 3 participants 5 messages Jun 30, 2025 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover btrfs: zoned: fix removing of unwritten non-data block group

There are some reports of "unable to find chunk map for logical 2147483648 length 16384" error message appears in the dmesg. This means some IOs are occurring after a block group is removed.

The IOs are coming from unused dirty tree nodes. When a metadata tree node is cleaned on the zoned setup, we keep that node still dirty and write it out not to create a write hole. When user operation e.g, fsync() is fast enough, we can have a block grup full of "unused dirty" tree nodes. Such block group has its used count == 0, making it "unused" block group and candidate for removal.

Removing the block group keeps the dirty nodes unsent until next sync or umount operation. At that time, such nodes will cause the "unable to find chunk map" failures.

This sympton can happen relatively easy on SMR, whose zone size == 256MB, but it is hidden coincidently hitting a check in do_zone_finish(). Hitting the check remove the block group from the unused list and let it never reclaimed, which itself is a problem.

Fix them adding an extra check in the BG removal process and put a block group in the retry list when btrfs_zone_finish() returns -EAGAIN.

Naohiro Aota (2):
btrfs: zoned: do not remove unwritten non-data block group
btrfs: zoned: requeue to unused block group list if zone finish failed

 fs/btrfs/block-group.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
Signature · 1 line
2.50.0
#1 lore raw

PATCH 1/2 btrfs: zoned: do not remove unwritten non-data block group

There are some reports of "unable to find chunk map for logical 2147483648 length 16384" error message appears in the dmesg. This means some IOs are occurring after a block group is removed.

When a metadata tree node is cleaned on the zoned setup, we keep that node still dirty and write it out not to create a write hole. However, this can make a block group's used bytes == 0 while there is dirty region left.

Such unused block group is moved into the unused_bg list and processed for the removal. When the removal succeeds, the block group is removed from the transaction->dirty_bgs list, so the unused dirty nodes in the block group are not sent at the transaction commit time. It will be written at some later time e.g, sync or umount, and causes the "unable to find chunk map" errors.

This can happen relatively easier on SMR whose zone size is 256MB. However, calling do_zone_finish() on such block group returns -EAGAIN and keep that block group intact, which is why the issue is hidden until now.

Fixes afba2bc036b0 ("btrfs: zoned: implement active zone tracking")
---
 fs/btrfs/block-group.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
fs/btrfs/block-group.c +17 -2
index 00e567a4cd16..d9afaddeeab0 100644
@@ -34,6 +34,14 @@ int btrfs_should_fragment_free_space(const struct btrfs_block_group *block_group
 }
 #endif
 
+static inline bool has_unwritten_metadata(struct btrfs_block_group *block_group)
+{
+	return btrfs_is_zoned(block_group->fs_info) &&
+		(block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)) &&
+		block_group->start + block_group->alloc_offset >
+			block_group->meta_write_pointer;
+}
+
 /*
  * Return target flags in extended format or 0 if restripe for this chunk_type
  * is not in progress
@@ -1244,6 +1252,12 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
 		goto out;
 
 	spin_lock(&block_group->lock);
+	/*
+	 * Hitting this WARN means we removed a block group with an unwritten
+	 * region. It will cause "unable to find chunk map for logical" errors.
+	 */
+	WARN_ON(has_unwritten_metadata(block_group));
+
 	set_bit(BLOCK_GROUP_FLAG_REMOVED, &block_group->runtime_flags);
 
 	/*
@@ -1586,8 +1600,9 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
 		 * needing to allocate extents from the block group.
 		 */
 		used = btrfs_space_info_used(space_info, true);
-		if (space_info->total_bytes - block_group->length < used &&
-		    block_group->zone_unusable < block_group->length) {
+		if ((space_info->total_bytes - block_group->length < used &&
+		     block_group->zone_unusable < block_group->length) ||
+		    has_unwritten_metadata(block_group)) {
 			/*
 			 * Add a reference for the list, compensate for the ref
 			 * drop under the "next" label for the
Signature · 1 line
2.50.0
#2 lore raw

Re: [PATCH 1/2] btrfs: zoned: do not remove unwritten non-data block group

Quoted 32 lines · Naohiro Aota

There are some reports of "unable to find chunk map for logical 2147483648 length 16384" error message appears in the dmesg. This means some IOs are occurring after a block group is removed.

When a metadata tree node is cleaned on the zoned setup, we keep that node still dirty and write it out not to create a write hole. However, this can make a block group's used bytes == 0 while there is dirty region left.

Such unused block group is moved into the unused_bg list and processed for the removal. When the removal succeeds, the block group is removed from the transaction->dirty_bgs list, so the unused dirty nodes in the block group are not sent at the transaction commit time. It will be written at some later time e.g, sync or umount, and causes the "unable to find chunk map" errors.

This can happen relatively easier on SMR whose zone size is 256MB. However, calling do_zone_finish() on such block group returns -EAGAIN and keep that block group intact, which is why the issue is hidden until now.

Fixes afba2bc036b0 ("btrfs: zoned: implement active zone tracking")
---
 fs/btrfs/block-group.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)
fs/btrfs/block-group.c +0 -0
index 00e567a4cd16..d9afaddeeab0 100644

fs/btrfs/block-group.c int btrfs_should_fragment_free_space(const struct btrfs_block_group *block_group · line 42

Quoted 9 lines
fs/btrfs/block-group.c +6 -0
@@ -34,6 +34,14 @@ int btrfs_should_fragment_free_space(const struct btrfs_block_group *block_group
 }
 #endif
 
+static inline bool has_unwritten_metadata(struct btrfs_block_group *block_group)
+{
+	return btrfs_is_zoned(block_group->fs_info) &&
+		(block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM)) &&
+		block_group->start + block_group->alloc_offset >
+			block_group->meta_write_pointer;

Could you please untangle the condition, like split it to a sequence of ifs, where the first one would be if (!zoned) which makes it easy to skip the rest if one is not considering the zoned mode.

fs/btrfs/block-group.c int btrfs_remove_block_group(struct btrfs_trans_handle *trans, · line 1259

Quoted 13 lines
fs/btrfs/block-group.c +6 -0
+
 /*
  * Return target flags in extended format or 0 if restripe for this chunk_type
  * is not in progress
@@ -1244,6 +1252,12 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
 		goto out;
 
 	spin_lock(&block_group->lock);
+	/*
+	 * Hitting this WARN means we removed a block group with an unwritten
+	 * region. It will cause "unable to find chunk map for logical" errors.
+	 */
+	WARN_ON(has_unwritten_metadata(block_group));

Please add a message to the warning, we have too many plain WARN_ONs and it takes some extra steps to identify the problem.

#3 lore raw

PATCH 2/2 btrfs: zoned: requeue to unused block group list if zone finish failed

In reply to Naohiro Aota

btrfs_zone_finish() can fail for several reason. If it is -EAGAIN, we need to try it again later. So, put the block group to the retry list properly.

Failed to do so will keep the removable block group intact until remount and can causes unnecessary ENOSPC.

Fixes 74e91b12b115 ("btrfs: zoned: zone finish unused block group")
---
 fs/btrfs/block-group.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/btrfs/block-group.c +3 -1
index d9afaddeeab0..4eef86dac6e3 100644
@@ -1631,8 +1631,10 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
 		ret = btrfs_zone_finish(block_group);
 		if (ret < 0) {
 			btrfs_dec_block_group_ro(block_group);
-			if (ret == -EAGAIN)
+			if (ret == -EAGAIN) {
+				btrfs_link_bg_list(block_group, &retry_list);
 				ret = 0;
+			}
 			goto next;
 		}
 
Signature · 1 line
2.50.0