mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Johannes Thumshirn <johannes.thumshirn@wdc.com>
To: socketpair@gmail.com
Cc: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
	 Naohiro Aota <naohiro.aota@wdc.com>,
	linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] btrfs: zoned: reset leaked zones on read-write mount
Date: Tue, 17 Nov 2026 12:46:39 +0100	[thread overview]
Message-ID: <avw98epZjlOLcu8g@mayhem.fritz.box> (raw)
In-Reply-To: <20261102-b4-btrfs-zoned-reset-leaked-zones-v3-1-7979bda654ff@gmail.com>


On Mon, Nov 02, 2026 at 08:35:04PM +0500, Mark Korenberg wrote:
> If a transaction commit is aborted after the zones of a new block group
> were written but before the superblock was, the chunk item and the dev
> extent are never persisted and the zones leak. The chunk allocator skips
> non-empty zones, so they are never reused, open ones keep counting
> against the active zone limit, and btrfs check reports them as
> unreferenced.

Sorry for the delay and thanks for picking this up.

Please mention that this needs btrfs-progs v7.3 or later, older versions
don't check for leaked zones.

[...]

> Testing: virtme-ng VM, zoned null_blk with 64 MiB zones,
> CONFIG_PROVE_LOCKING, this patch plus debug logging of every decision:
> - not zoned filesystem: nothing done
> - fresh filesystem: the first mount resets zone 3, which mkfs.btrfs
>   (btrfs-progs 7.1) leaves written outside any chunk; nothing to do on
>   the next mount, btrfs check clean

That one is fixed in btrfs-progs v7.3 by commit dcc41ad6 ("btrfs-progs:
zoned: refresh stale cached zone state in btrfs_reset_dev_zone()").
mkfs didn't reset the zone of a removed temporary chunk because the
cached zone state was stale.

[...]

> +	/*
> +	 * Chunks are only removed under reclaim_bgs_lock, and their zones must
> +	 * not be reset before the removal is committed. A relocating repair,
> +	 * started by a read error during mount, may already have removed one in
> +	 * the running transaction, so commit it before looking for leaked zones.
> +	 */
> +	mutex_lock(&fs_info->reclaim_bgs_lock);
> +	ret = btrfs_commit_current_transaction(fs_info->tree_root);

On a filesystem without free active zones, this commit can be the one
that aborts with -EAGAIN, before the reset could free anything. In my
tests there was never a running transaction at this point, so it didn't
matter. Can you skip the commit when nothing is running, or add a
sentence to the changelog why it is safe here?

With that
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>

Btw, I tested v3 on top of for-next (146946479b45) and told Claude to write an
fstests [1] case for it (zloop with dm-flakey on top, power failure after direct
I/O to new zones). It fails on for-next and passes with your patch, I'll send
it to fstests once you've send a v4.


Thanks,
	Johannes


[1]

diff --git a/tests/generic/815 b/tests/generic/815
new file mode 100755
index 00000000..f5969e1a
--- /dev/null
+++ b/tests/generic/815
@@ -0,0 +1,78 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Western Digital Corporation.  All Rights Reserved.
+#
+# FS QA Test 815
+#
+# Simulate a power failure on a zoned device after data has been written to
+# newly allocated zones, but before the allocation itself was committed. The
+# zones written that way are not referenced by the filesystem. Check that the
+# filesystem is consistent after the next read-write mount, i.e. that it
+# doesn't leave these zones written and active.
+#
+. ./common/preamble
+_begin_fstest auto quick zone log
+
+_cleanup()
+{
+	cd /
+	[ -n "$mnt" ] && _unmount $mnt 2>/dev/null
+	[ -n "$flakey" ] && _cleanup_flakey_dev $flakey
+	_destroy_zloop $zloop
+	rm -r -f $tmp.*
+}
+
+. ./common/zoned
+. ./common/dmflakey
+
+_fixed_by_fs_commit btrfs XXXXXXXXXXXX \
+	"btrfs: zoned: reset leaked zones on read-write mount"
+
+_require_scratch_size $((4 * 1024 * 1024)) # kB
+_require_block_device $SCRATCH_DEV
+_require_zloop
+_require_dm_target flakey
+_require_command "$BLKZONE_PROG" blkzone
+
+_scratch_mkfs > /dev/null 2>&1
+_scratch_mount
+
+mnt=$SCRATCH_MNT/mnt
+mkdir -p $mnt
+zloop=$(_create_zloop $SCRATCH_MNT/zloop 64 4)
+
+flakey=zflakey-test.$seq
+_init_flakey_dev $flakey $zloop
+dev=/dev/mapper/$flakey
+
+_try_mkfs_dev $dev >> $seqres.full 2>&1 || \
+	_notrun "cannot mkfs zoned filesystem"
+_mount $dev $mnt
+sync
+
+# Direct I/O puts the data on the device right away, but the allocation of
+# the zones it went to is only persisted with the next commit.
+for i in $(seq 4); do
+	$XFS_IO_PROG -f -d -c "pwrite -q -b 1m 0 96m" $mnt/file$i >> $seqres.full 2>&1
+done
+
+echo "zones before power failure:" >> $seqres.full
+$BLKZONE_PROG report $zloop | grep -cvE "\((em|nw)\)" >> $seqres.full
+
+_load_flakey_dev $flakey $zloop $FLAKEY_DROP_WRITES
+_unmount $mnt
+_load_flakey_dev $flakey $zloop $FLAKEY_ALLOW_WRITES
+
+# The read-write mount is expected to clean up after the power failure.
+_mount $dev $mnt
+_unmount $mnt
+
+echo "zones after recovery:" >> $seqres.full
+$BLKZONE_PROG report $zloop | grep -cvE "\((em|nw)\)" >> $seqres.full
+
+_check_dev_fs $dev
+
+echo "Silence is golden"
+
+# success, all done
+_exit 0
diff --git a/tests/generic/815.out b/tests/generic/815.out
new file mode 100644
index 00000000..6ea462f3
--- /dev/null
+++ b/tests/generic/815.out
@@ -0,0 +1,2 @@
+QA output created by 815
+Silence is golden
diff --git a/common/dmflakey b/common/dmflakey
index 6ac666f5..9bba8958 100644
--- a/common/dmflakey
+++ b/common/dmflakey
@@ -12,6 +12,47 @@ export FLAKEY_NAME="flakey-test.$seq"
 export FLAKEY_RTNAME="flakey-rttest.$seq"
 export FLAKEY_LOGNAME="flakey-logtest.$seq"
 
+_flakey_table()
+{
+	local size=`blockdev --getsz $1`
+
+	case "$2" in
+	"$FLAKEY_DROP_WRITES")
+		echo "0 $size flakey $1 0 0 180 1 drop_writes"
+		;;
+	"$FLAKEY_ERROR_WRITES")
+		echo "0 $size flakey $1 0 0 180 1 error_writes"
+		;;
+	*)
+		echo "0 $size flakey $1 0 180 0"
+		;;
+	esac
+}
+
+_init_flakey_dev()
+{
+	_dmsetup_create $1 --table "`_flakey_table $2`" || \
+		_fatal "failed to create flakey device $1"
+}
+
+_load_flakey_dev()
+{
+	local suspend_opt="--nolockfs"
+
+	[ $# -gt 3 ] && [ $4 -eq 1 ] && suspend_opt=""
+
+	$DMSETUP_PROG suspend $suspend_opt $1 || _fatal "failed to suspend $1"
+	_flakey_table $2 $3 | $DMSETUP_PROG load $1 || \
+		_fatal "failed to load table into $1"
+	$DMSETUP_PROG resume $1 || _fatal "failed to resume $1"
+}
+
+_cleanup_flakey_dev()
+{
+	$DMSETUP_PROG resume $1 &> /dev/null
+	_dmsetup_remove $1
+}
+
 _init_flakey()
 {
 	# Scratch device
@@ -24,12 +65,10 @@ _init_flakey()
 		# Already set up; recreate tables
 		local backing_dev="$NON_FLAKEY_DEV"
 	fi
-	local BLK_DEV_SIZE=`blockdev --getsz $backing_dev`
-	FLAKEY_TABLE="0 $BLK_DEV_SIZE flakey $backing_dev 0 180 0"
-	FLAKEY_TABLE_DROP="0 $BLK_DEV_SIZE flakey $backing_dev 0 0 180 1 drop_writes"
-	FLAKEY_TABLE_ERROR="0 $BLK_DEV_SIZE flakey $backing_dev 0 0 180 1 error_writes"
-	_dmsetup_create $FLAKEY_NAME --table "$FLAKEY_TABLE" || \
-		_fatal "failed to create flakey device"
+	FLAKEY_TABLE=`_flakey_table $backing_dev`
+	FLAKEY_TABLE_DROP=`_flakey_table $backing_dev $FLAKEY_DROP_WRITES`
+	FLAKEY_TABLE_ERROR=`_flakey_table $backing_dev $FLAKEY_ERROR_WRITES`
+	_init_flakey_dev $FLAKEY_NAME $backing_dev
 
 	# Realtime device
 	if [ -n "$SCRATCH_RTDEV" ]; then
@@ -42,12 +81,10 @@ _init_flakey()
 			# Already set up; recreate tables
 			local backing_dev="$NON_FLAKEY_RTDEV"
 		fi
-		local BLK_DEV_SIZE=`blockdev --getsz $backing_dev`
-		FLAKEY_RTTABLE="0 $BLK_DEV_SIZE flakey $backing_dev 0 180 0"
-		FLAKEY_RTTABLE_DROP="0 $BLK_DEV_SIZE flakey $backing_dev 0 0 180 1 drop_writes"
-		FLAKEY_RTTABLE_ERROR="0 $BLK_DEV_SIZE flakey $backing_dev 0 0 180 1 error_writes"
-		_dmsetup_create $FLAKEY_RTNAME --table "$FLAKEY_RTTABLE" || \
-			_fatal "failed to create flakey rt device"
+		FLAKEY_RTTABLE=`_flakey_table $backing_dev`
+		FLAKEY_RTTABLE_DROP=`_flakey_table $backing_dev $FLAKEY_DROP_WRITES`
+		FLAKEY_RTTABLE_ERROR=`_flakey_table $backing_dev $FLAKEY_ERROR_WRITES`
+		_init_flakey_dev $FLAKEY_RTNAME $backing_dev
 	fi
 
 	# External log device
@@ -61,12 +98,10 @@ _init_flakey()
 			# Already set up; recreate tables
 			local backing_dev="$NON_FLAKEY_LOGDEV"
 		fi
-		local BLK_DEV_SIZE=`blockdev --getsz $backing_dev`
-		FLAKEY_LOGTABLE="0 $BLK_DEV_SIZE flakey $backing_dev 0 180 0"
-		FLAKEY_LOGTABLE_DROP="0 $BLK_DEV_SIZE flakey $backing_dev 0 0 180 1 drop_writes"
-		FLAKEY_LOGTABLE_ERROR="0 $BLK_DEV_SIZE flakey $backing_dev 0 0 180 1 error_writes"
-		_dmsetup_create $FLAKEY_LOGNAME --table "$FLAKEY_LOGTABLE" || \
-			_fatal "failed to create flakey log device"
+		FLAKEY_LOGTABLE=`_flakey_table $backing_dev`
+		FLAKEY_LOGTABLE_DROP=`_flakey_table $backing_dev $FLAKEY_DROP_WRITES`
+		FLAKEY_LOGTABLE_ERROR=`_flakey_table $backing_dev $FLAKEY_ERROR_WRITES`
+		_init_flakey_dev $FLAKEY_LOGNAME $backing_dev
 	fi
 }
 
@@ -80,9 +115,9 @@ _cleanup_flakey()
 
 	_unmount $SCRATCH_MNT > /dev/null 2>&1
 
-	test -n "$NON_FLAKEY_DEV" && _dmsetup_remove $FLAKEY_NAME
-	test -n "$NON_FLAKEY_LOGDEV" && _dmsetup_remove $FLAKEY_LOGNAME
-	test -n "$NON_FLAKEY_RTDEV" && _dmsetup_remove $FLAKEY_RTNAME
+	test -n "$NON_FLAKEY_DEV" && _cleanup_flakey_dev $FLAKEY_NAME
+	test -n "$NON_FLAKEY_LOGDEV" && _cleanup_flakey_dev $FLAKEY_LOGNAME
+	test -n "$NON_FLAKEY_RTDEV" && _cleanup_flakey_dev $FLAKEY_RTNAME
 
 	SCRATCH_DEV="$NON_FLAKEY_DEV"
 	unset NON_FLAKEY_DEV

      parent reply	other threads:[~2026-11-17 11:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02 15:35 Mark Korenberg via B4 Relay
2026-11-17  6:54 ` Mark Korenberg
2026-11-17 11:46 ` Johannes Thumshirn [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=avw98epZjlOLcu8g@mayhem.fritz.box \
    --to=johannes.thumshirn@wdc.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    --cc=naohiro.aota@wdc.com \
    --cc=socketpair@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®