Skip to contentSkip to conversation

btrfs: zoned: handle a failed zone finish on one device

PATCH New Miroslav Cimerman 1 participant 4 messages Nov 22 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: zoned: handle a failed zone finish on one device

When ZONE_FINISH fails on one device, btrfs can lose a completed write or turn read-only, even if the profile tolerates the failed device.

1/3: since 7.3, a failed zone finish fails the ordered extent whose completion started it. The data is written, but the file keeps the old data in the range of that extent.

2/3: since 6.18, one failed device of a RAID1 metadata block group turns the filesystem read-only. The patch tolerates the failure when the zones are full. A partly written block group still stops at the first error. Finishing its other zones could make the next mount fail.

3/3: relocation hits a WARN_ON() when a device fails ZONE_FINISH.

Tested in QEMU with KASAN on for-next 65101fe10b30, with two zoned null_blk devices (1 GiB, 16 MiB zones, zone capacity 12 or 16 MiB). A test-only module parameter fails the next ZONE_FINISH on one device. Each RAID1 case ran with a failure on each device:

for-next with the series

  RAID1 or single data, an  last 1 MiB reads     all new data
  O_DIRECT overwrite fills  old data, first
  the block group           read gets EIO

RAID1 metadata fills forced read-only stays writable,
a block group remount works

relocation, RAID1 or WARN_ON() two warning
single data messages

The relocated block groups are partly written, so 2/3 does not tolerate the failure there.

The btrfs self-tests pass. Degraded-mount tests and parallel writers show the same failures as on for-next. These are the bugs fixed by my pending series [1], [2] and [3]. fstests was not run.

Found with FuzzyRAID, a fault-injection tester for RAID that is not public at the moment. It ran the series on an older for-next with my other pending zoned fixes. Its runs showed no lost write and nothing new. The analysis, the fixes and the tests were done with the help of an LLM. I reviewed everything.

[1] https://lore.kernel.org/linux-btrfs/[email protected]/
[2] https://lore.kernel.org/linux-btrfs/[email protected]/
[3] https://lore.kernel.org/linux-btrfs/[email protected]/

Miroslav Cimerman (3):
btrfs: zoned: do not fail an ordered extent on a zone finish error
btrfs: zoned: tolerate a failed zone finish on a full block group
btrfs: zoned: do not WARN when zones fail to finish before relocation

 fs/btrfs/relocation.c |  5 +++-
 fs/btrfs/zoned.c      | 59 +++++++++++++++++++++++++++++++++++++------
 2 files changed, 55 insertions(+), 9 deletions(-)

base-commit: 65101fe10b3008186b813b628ee3a611b0e70ce6

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/3 btrfs: zoned: do not fail an ordered extent on a zone finish error

When an ordered extent ends at the zone capacity of its block group, btrfs_zone_finish_endio() finishes the zones of the block group. Since commit c428b763f29b ("btrfs: zoned: propagate do_zone_finish() error in btrfs_zone_finish_endio()"), it returns the error of do_zone_finish(). btrfs_finish_one_ordered() then fails the ordered extent. It skips the file extent item that points the file to the new data. It also frees the new extent.

However, the data of the ordered extent is already written. Only the ZONE_FINISH command failed, for example on one device of a RAID1 block group. The write is still lost:

O_DIRECT overwrite, the last extent ends at the zone capacity write and sync return 0
ZONE_FINISH fails on one device btrfs_finish_one_ordered(): no file extent item, extent freed first O_DIRECT read of the file: -EIO (writeback error of the mapping) later reads, also after a remount: the old data of that extent

do_zone_finish() marks the block group as finished before it sends ZONE_FINISH, so nothing writes to these zones anymore. Report the error and let the ordered extent complete, as before that commit.

Fixes c428b763f29b ("btrfs: zoned: propagate do_zone_finish() error in btrfs_zone_finish_endio()")
---
 fs/btrfs/zoned.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
fs/btrfs/zoned.c +9 -2
index e6ce6f05ad75..45bd05d8edb4 100644
@@ -2783,7 +2783,7 @@ int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 leng
 {
 	struct btrfs_block_group *block_group;
 	u64 min_alloc_bytes;
-	int ret = 0;
+	int ret;
 
 	if (!btrfs_is_zoned(fs_info))
 		return 0;
@@ -2803,11 +2803,18 @@ int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical, u64 leng
 	    block_group->start + block_group->zone_capacity)
 		goto out;
 
+	/*
+	 * The data of the ordered extent is written. A failure to finish the
+	 * zones does not change that, so only report it.
+	 */
 	ret = do_zone_finish(block_group, true);
+	if (ret)
+		btrfs_warn(fs_info, "zoned: failed to finish zones of block group %llu: %d",
+			   block_group->start, ret);
 
 out:
 	btrfs_put_block_group(block_group);
-	return ret;
+	return 0;
 }
 
 static void btrfs_zone_finish_endio_workfn(struct work_struct *work)
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/3 btrfs: zoned: tolerate a failed zone finish on a full block group

In reply to Miroslav Cimerman

do_zone_finish() sends ZONE_FINISH to the zone of each stripe of a block group. It stops at the first error and returns it, even if the profile tolerates the failed device. Since commit 3d16abf6c88a ("btrfs: zoned:
directly call do_zone_finish() from btrfs_zone_finish_endio_workfn()"), btrfs_zone_finish_endio_workfn() handles that error with btrfs_handle_fs_error(). So one failed device of a RAID1 metadata block group turns the filesystem read-only.

When the block group is allocated up to the zone capacity and everything is written, the zones are already full on the devices. A failed ZONE_FINISH does not change them. In this case, finish the zones of all stripes. Like a write, fail only if more devices failed than the profile tolerates. Count a missing device toward the limit. The failed device keeps the zone counted as active until the zone is reset or the filesystem is remounted. For metadata, the active zone reservation comes back only at the next mount.

A partly written block group keeps the current behavior. It stops at the first error and returns it. Finishing the zones after a failed one would leave more zones at different write pointers. btrfs_load_block_group_raid1() refuses such a block group at the next mount.

Fixes 3d16abf6c88a ("btrfs: zoned: directly call do_zone_finish() from btrfs_zone_finish_endio_workfn()")
CC [email protected] # 6.18+: a18a6b93a284: btrfs: zoned: finish active block group cleanup if call_zone_finish() fails
---
 fs/btrfs/zoned.c | 48 ++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 42 insertions(+), 6 deletions(-)
fs/btrfs/zoned.c +42 -6
index 45bd05d8edb4..d18dc4f4dc33 100644
@@ -2540,9 +2540,6 @@ static int call_zone_finish(struct btrfs_block_group *block_group,
 	bool cleared;
 	int ret;
 
-	if (!device->bdev)
-		return 0;
-
 	if (zinfo->max_active_zones == 0)
 		return 0;
 
@@ -2573,6 +2570,9 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
 	const bool is_metadata = (block_group->flags &
 			(BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
 	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
+	enum btrfs_raid_types index;
+	bool zones_full;
+	int failed = 0;
 	int ret = 0;
 	int i;
 
@@ -2639,6 +2639,13 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
 	}
 
 	clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
+	/*
+	 * If the block group is allocated up to the zone capacity,
+	 * everything is written and the zones should be full already. A
+	 * truncated or failed write at the end is not detected here. That
+	 * would need a zone report from every device.
+	 */
+	zones_full = (block_group->alloc_offset == block_group->zone_capacity);
 	block_group->alloc_offset = block_group->zone_capacity;
 	if (block_group->flags & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM))
 		block_group->meta_write_pointer = block_group->start +
@@ -2651,12 +2658,41 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
 	down_read(&dev_replace->rwsem);
 	map = block_group->physical_map;
 	for (i = 0; i < map->num_stripes; i++) {
-		ret = call_zone_finish(block_group, &map->stripes[i]);
-		if (ret)
-			break;
+		struct btrfs_io_stripe *stripe = &map->stripes[i];
+		int err;
+
+		if (!stripe->dev->bdev) {
+			failed++;
+			continue;
+		}
+
+		err = call_zone_finish(block_group, stripe);
+		if (err) {
+			btrfs_warn(fs_info,
+		"zoned: failed to finish zone at physical %llu on devid %llu: %d",
+				   stripe->physical, stripe->dev->devid, err);
+			failed++;
+			ret = err;
+			/*
+			 * Do not finish more zones of a partly written block
+			 * group. They would end at a different write pointer
+			 * than the failed one.
+			 */
+			if (!zones_full)
+				break;
+		}
 	}
 	up_read(&dev_replace->rwsem);
 
+	/*
+	 * Zones that are already full do not change with a failed finish.
+	 * Like a write, fail only if more devices failed than the profile
+	 * tolerates. A missing device counts toward the limit.
+	 */
+	index = btrfs_bg_flags_to_raid_index(map->type);
+	if (zones_full && failed <= btrfs_raid_array[index].tolerated_failures)
+		ret = 0;
+
 	if (!ret && !fully_written)
 		btrfs_dec_block_group_ro(block_group);
 
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/3 btrfs: zoned: do not WARN when zones fail to finish before relocation

In reply to Miroslav Cimerman

btrfs_relocate_block_group() finishes the zones of the block group before it relocates it. Any error other than -EAGAIN hits a WARN_ON(). However, btrfs_zone_finish() also fails when a device fails ZONE_FINISH. A device error is not a kernel bug, and relocation continues either way.

Print a warning instead.

Fixes 7ae9bd18032e ("btrfs: zoned: finish relocating block group")
---
 fs/btrfs/relocation.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/btrfs/relocation.c +4 -1
index c7b5a655e89f..b60d9a0c5c1d 100644
@@ -5538,7 +5538,10 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
 	btrfs_wait_ordered_roots(fs_info, U64_MAX, rc->block_group);
 
 	ret = btrfs_zone_finish(rc->block_group);
-	WARN_ON(ret && ret != -EAGAIN);
+	if (ret && ret != -EAGAIN)
+		btrfs_warn(fs_info,
+			   "zoned: failed to finish zones of block group %llu before relocation: %d",
+			   rc->block_group->start, ret);
 
 	if (should_relocate_using_remap_tree(bg)) {
 		if (bg->remap_bytes != 0) {
Signature · 1 line
2.53.0