mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3 0/6] btrfs: data integrity fixes
@ 2026-11-20 20:51 Josef Bacik
  2026-11-20 20:51 ` [PATCH v3 1/6] btrfs: don't drop the error of the last split bio Josef Bacik
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Josef Bacik @ 2026-11-20 20:51 UTC (permalink / raw)
  To: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	Qu Wenruo, Boris Burkov, Filipe Manana
  Cc: linux-btrfs, linux-kernel, stable, Zygo Blaxell, Josef Bacik

v1: https://lore.kernel.org/all/20261030-b4-btrfs-data-integrity-v1-0-f4d4196731e6@toxicpanda.com/
v2: https://lore.kernel.org/all/20261103-b4-btrfs-data-integrity-v2-0-577dc2690061@toxicpanda.com/

v2->v3:
- Rebase onto for-next-20261109.  Encoded writes now always get a full
  fsync (900bbc210986 "btrfs: skip fast sync for inodes with encoded
  writes"), so the fsync fix no longer handles them.
- Patch 3 of v2 is now two patches, and goes back to an inode flag
  instead of checking the collected ordered extents for errors, which
  missed truncated ordered extents (Sashiko, Filipe):
  - Patch 3 renames BTRFS_INODE_COW_WRITE_ERROR to
    BTRFS_INODE_INCOMPLETE_COW_WRITE and also sets it for a write that
    fails before a bio is submitted and for a truncated ordered extent,
    such as a direct IO write that stops short.  It is set under the
    ordered_tree_lock, before BTRFS_ORDERED_IO_DONE.
  - Patch 4 makes a fast fsync wait for BTRFS_ORDERED_IO_DONE on the
    ordered extents it collected, direct IO included, before it checks
    the flag.
- Patch 1: drop the bi_status store that nothing reads anymore.
- Patch 2: use btrfs_folio_clamp_finish_io() for the cleanup.
- Patch 6 (patch 5 in v2): don't count the replace target in
  btrfs_ioctl_fitrim(), and Fixes: 929be17a9b49, which made the trim of
  a device depend on its alloc_state; before it the replace target was
  never trimmed.
- Stable: patch 1 6.6+ and patch 2 5.15+, as for the commits they fix;
  patches 3 and 4 6.10+, and patch 4 depends on patch 3.

v1->v2:
- Rework patch 3, now "btrfs: wait for ordered extent IO in fast fsync".
  BTRFS_INODE_COW_WRITE_ERROR is set after the end IO handler clears
  writeback, so a fast fsync could still miss it, for plain write errors
  as well.  Fast fsync now waits for BTRFS_ORDERED_IO_DONE on the ordered
  extents it collected and checks BTRFS_ORDERED_IOERR, and the inode flag
  is gone (Sashiko, Qu, Filipe).
- Patch 4: use DEBUG_WARN() instead of ASSERT() for the check that can no
  longer be true (Filipe).
- Collect tags.

--- Original email (v1) ---

Hello,

I've been doing some model checking of our trickier paths with Claude's
help, and these are the fixes for the bugs it turned up that actually
lose data, or tell userspace that something is safe when it isn't.
Every one of them was reproduced in a VM on an unpatched kernel and goes
away with the fix, though the fsync and log sync ones needed a delay
injected to hit the window.

The worst one is the fstrim one.  If you run fstrim while a device
replace is running we'll discard everything that has already been copied
to the target device, because the target's alloc_state is empty until
the replace finishes, so the whole device looks unallocated.  The
replace then finishes happily, the source device goes away, and you're
left with zeroes where your metadata used to be.  This has been around
since FITRIM learned to trim unallocated space back in 4.3.

The split bio one is similarly nasty.  If the last piece of a split bio
to complete is one of the split bios and it failed, we drop the error on
the floor, and a buffered read returns success with whatever was sitting
in the page.

The rest are in our error handling.  A delalloc failure in the middle of
a folio leaves blocks dirty that no longer have an ordered extent, a
fast fsync after an error that happened before we submitted the bio can
log extents that were never written (which log replay then happily puts
in place of the data that was there before), and btrfs_sync_log() can
return success for a log transaction that never got committed.

I have a cleanup series for btrfs_sync_log coming, as I had to spend a
lot of time grokking that fix, which is a good indication that the code
is too complicated.

They're all tagged for stable, and independent of each other except
that patch 4 needs patch 3.  This is against for-next.  Thanks,

Josef

---
Changes in v3:
- Split patch 3 in two and set an inode flag for failed and truncated
  writes (Sashiko, Filipe); fast fsync waits for IO_DONE first.
- Patch 1: drop the unread bi_status store.
- Patch 2: use btrfs_folio_clamp_finish_io().
- Patch 6: don't count the replace target in btrfs_ioctl_fitrim(),
  Fixes: 929be17a9b49.
- Stable: patch 1 6.6+, patch 2 5.15+, patches 3 and 4 6.10+.
- Rebase onto for-next-20261109.
- Link to v2: https://patch.msgid.link/20261103-b4-btrfs-data-integrity-v2-0-577dc2690061@toxicpanda.com

Changes in v2:
- Link to v1: https://patch.msgid.link/20261030-b4-btrfs-data-integrity-v1-0-f4d4196731e6@toxicpanda.com

---
Josef Bacik (6):
      btrfs: don't drop the error of the last split bio
      btrfs: clear dirty blocks after a delalloc failure
      btrfs: make fsync wait for all incomplete COW writes
      btrfs: wait for ordered extent IO in fast fsync
      btrfs: don't report success for a skipped log sync
      btrfs: don't trim the target of a device replace

 fs/btrfs/bio.c          | 10 ++++---
 fs/btrfs/btrfs_inode.h  | 24 ++++++++++------
 fs/btrfs/extent-tree.c  |  8 ++++++
 fs/btrfs/extent_io.c    | 15 ++++++----
 fs/btrfs/file.c         | 74 ++++++++++++++++++++++++++++++++++++++-----------
 fs/btrfs/ioctl.c        |  3 ++
 fs/btrfs/ordered-data.c | 52 +++++++++++++---------------------
 fs/btrfs/tree-log.c     | 20 +++++++++++--
 8 files changed, 139 insertions(+), 67 deletions(-)
---
base-commit: 3aefd67f08f8ed59ed0fa90619b25856df415031
change-id: 20261029-b4-btrfs-data-integrity-eb753b57300f


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 1/6] btrfs: don't drop the error of the last split bio
  2026-11-20 20:51 [PATCH v3 0/6] btrfs: data integrity fixes Josef Bacik
@ 2026-11-20 20:51 ` Josef Bacik
  2026-11-20 20:51 ` [PATCH v3 2/6] btrfs: clear dirty blocks after a delalloc failure Josef Bacik
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Josef Bacik @ 2026-11-20 20:51 UTC (permalink / raw)
  To: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	Qu Wenruo, Boris Burkov, Filipe Manana
  Cc: linux-btrfs, linux-kernel, stable, Zygo Blaxell, Josef Bacik

When a btrfs_bio had to be split, btrfs_bio_end_io() saves the first error
of any of its parts in the status field of the original btrfs_bio. The
part that completes last is supposed to load it into bi_status of the
original bio before calling the end_io handler, but that is only done if
that last part was successful:

	if (atomic_dec_and_test(&bbio->pending_ios)) {
		/* Load split bio's error which might be set above. */
		if (status == BLK_STS_OK)
			bbio->bio.bi_status = READ_ONCE(bbio->status);

If the last part failed, the assumption is that bi_status was set at the
top of the function. That is true if the last part is what is left of the
original bio. If it is a split bio, the assignment was done to the split
bio, which was freed right after. If the original bio has completed before
without an error, its bi_status is BLK_STS_OK and stays like that:

  original bio (tail)                 split bio (head)

  btrfs_bio_end_io(orig, OK)
    orig->bio.bi_status = OK
    pending_ios 2 -> 1
                                      btrfs_bio_end_io(split, IOERR)
                                        split->bio.bi_status = IOERR
                                        bio_put(split)
                                        cmpxchg(&orig->status, OK, IOERR)
                                        pending_ios 1 -> 0
                                        orig->end_io(), bi_status is OK

The end_io handler covers the whole range, so for a buffered read all the
folios are marked uptodate, including the ones that were never filled, and
read(2) returns their content instead of -EIO. For a write the ordered
extent is completed as if all the data had reached the device.

This can be reproduced with raid0 data on two devices, one of them slower
than the other and with a bad block, by reading a file whose last stripe
is on the fast device.

The saved status has the first error of all the parts, or BLK_STS_OK if
there was none, so always use it, and drop the store into bi_status at
the top of the function, which nothing reads anymore.

Fixes: d48e1dea3931 ("btrfs: fix error propagation of split bios")
CC: stable@vger.kernel.org # 6.6+
Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Tested-by: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/bio.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/bio.c b/fs/btrfs/bio.c
index 2e12a6d7e16f..b24cba595107 100644
--- a/fs/btrfs/bio.c
+++ b/fs/btrfs/bio.c
@@ -107,7 +107,6 @@ void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)
 	/* Make sure we're already in task context. */
 	ASSERT(in_task());
 
-	bbio->bio.bi_status = status;
 	if (bbio->bio.bi_pool == &btrfs_clone_bioset) {
 		struct btrfs_bio *orig_bbio = bbio->private;
 
@@ -127,9 +126,12 @@ void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)
 		cmpxchg(&bbio->status, BLK_STS_OK, status);
 
 	if (atomic_dec_and_test(&bbio->pending_ios)) {
-		/* Load split bio's error which might be set above. */
-		if (status == BLK_STS_OK)
-			bbio->bio.bi_status = READ_ONCE(bbio->status);
+		/*
+		 * Load the first error, if any. It may come from a split bio,
+		 * including the one we were called for, in which case nothing
+		 * stored it in the original bio yet.
+		 */
+		bbio->bio.bi_status = READ_ONCE(bbio->status);
 
 		if (bbio_has_ordered_extent(bbio)) {
 			struct btrfs_ordered_extent *ordered = bbio->ordered;

-- 
2.55.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 2/6] btrfs: clear dirty blocks after a delalloc failure
  2026-11-20 20:51 [PATCH v3 0/6] btrfs: data integrity fixes Josef Bacik
  2026-11-20 20:51 ` [PATCH v3 1/6] btrfs: don't drop the error of the last split bio Josef Bacik
@ 2026-11-20 20:51 ` Josef Bacik
  2026-11-20 20:51 ` [PATCH v3 3/6] btrfs: make fsync wait for all incomplete COW writes Josef Bacik
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Josef Bacik @ 2026-11-20 20:51 UTC (permalink / raw)
  To: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	Qu Wenruo, Boris Burkov, Filipe Manana
  Cc: linux-btrfs, linux-kernel, stable, Josef Bacik

If writepage_delalloc() runs the first delalloc ranges of a folio and
then btrfs_run_delalloc_range() fails, the blocks that already got an
ordered extent are finished with an error through
btrfs_mark_ordered_io_finished(), but their dirty bits are left set. The
next writeback then submits blocks whose ordered extent is gone, or is
about to be completed a second time.

Fix by clearing the dirty bits and cycling the writeback state for those
blocks, as submit_write_sector() does for a block it fails to submit.

Fixes: 72dad8e377af ("btrfs: fix double accounting race when btrfs_run_delalloc_range() failed")
CC: stable@vger.kernel.org # 5.15+
Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/extent_io.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index b2739838c646..036d8c0f04ae 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -1746,6 +1746,14 @@ static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
 			u64 start = page_start + (start_bit << fs_info->sectorsize_bits);
 			u32 len = (end_bit - start_bit) << fs_info->sectorsize_bits;
 
+			/*
+			 * The blocks are not going to be submitted, so they
+			 * must not stay dirty. Otherwise they are written back
+			 * later without an ordered extent, or worse, with this
+			 * one if it is still around, and invalidating the folio
+			 * would account for them again.
+			 */
+			btrfs_folio_clamp_finish_io(fs_info, folio, start, len);
 			btrfs_mark_ordered_io_finished(inode, start, len, false);
 		}
 		return ret;

-- 
2.55.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 3/6] btrfs: make fsync wait for all incomplete COW writes
  2026-11-20 20:51 [PATCH v3 0/6] btrfs: data integrity fixes Josef Bacik
  2026-11-20 20:51 ` [PATCH v3 1/6] btrfs: don't drop the error of the last split bio Josef Bacik
  2026-11-20 20:51 ` [PATCH v3 2/6] btrfs: clear dirty blocks after a delalloc failure Josef Bacik
@ 2026-11-20 20:51 ` Josef Bacik
  2026-11-20 20:51 ` [PATCH v3 4/6] btrfs: wait for ordered extent IO in fast fsync Josef Bacik
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Josef Bacik @ 2026-11-20 20:51 UTC (permalink / raw)
  To: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	Qu Wenruo, Boris Burkov, Filipe Manana
  Cc: linux-btrfs, linux-kernel, stable, Josef Bacik

If a COW write fails, or its ordered extent is truncated, the extent
maps of the part that was not written still point to the newly
allocated extent until the ordered extent completes. If a fast fsync
logs those extent maps first, replaying the log after a crash replaces
the file's old data with an extent that was never written.

BTRFS_INODE_COW_WRITE_ERROR is meant to make a fast fsync wait for the
ordered extents to complete in that case, but only
btrfs_finish_ordered_extent() sets it. A write that fails before a bio
is submitted, in writepage_delalloc() or submit_write_sector(), finishes
its ordered extent through btrfs_mark_ordered_io_finished(), which does
not set the flag. And since commit 66ff4d366e7e ("btrfs: fix false IO
failure after falling back to buffered write"), a direct IO write that
stops short truncates the rest of its ordered extent and finishes it as
uptodate, so the flag is not set either.

Fix this by setting the flag, renamed to
BTRFS_INODE_INCOMPLETE_COW_WRITE, in can_finish_ordered_extent() when a
write fails, and in btrfs_mark_ordered_extent_truncated(). Convert the
open-coded truncation in extent_writepage_io() to that helper, so that
every truncation sets the flag. Both places hold the inode's
ordered_tree_lock and run before BTRFS_ORDERED_IO_DONE is set.

The flag stays set until a fast fsync clears it, and that fsync then
waits for all ordered extents to complete, even if the failed or
truncated one completed long before. A direct IO write that stops short
because its buffer was not faulted in costs the next fast fsync one
such wait.

Fixes: f13e01b89daf ("btrfs: ensure fast fsync waits for ordered extents after a write failure")
Fixes: 66ff4d366e7e ("btrfs: fix false IO failure after falling back to buffered write")
CC: stable@vger.kernel.org # 6.10+
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/btrfs_inode.h  | 24 ++++++++++++++++--------
 fs/btrfs/extent_io.c    |  7 ++-----
 fs/btrfs/file.c         | 17 ++++++++---------
 fs/btrfs/ordered-data.c | 46 ++++++++++++++--------------------------------
 4 files changed, 40 insertions(+), 54 deletions(-)

diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h
index fbcf8ca05fb7..cd75c1a17e45 100644
--- a/fs/btrfs/btrfs_inode.h
+++ b/fs/btrfs/btrfs_inode.h
@@ -90,15 +90,23 @@ enum {
 	/* Set when there are no capabilities in XATTs for the inode. */
 	BTRFS_INODE_NO_CAP_XATTR,
 	/*
-	 * Set if an error happened when doing a COW write before submitting a
-	 * bio or during writeback. Used for both buffered writes and direct IO
-	 * writes. This is to signal a fast fsync that it has to wait for
-	 * ordered extents to complete and therefore not log extent maps that
-	 * point to unwritten extents (when an ordered extent completes and it
-	 * has the BTRFS_ORDERED_IOERR flag set, it drops extent maps in its
-	 * range).
+	 * Set when a COW write failed, before submitting a bio or during
+	 * writeback, or when a COW ordered extent was truncated. Used for both
+	 * buffered writes and direct IO writes.
+	 *
+	 * The extent maps of what was not written point to blocks that won't be
+	 * written until the ordered extent completes and
+	 * btrfs_finish_one_ordered() drops them. Completion runs in a work
+	 * queue, queued once the IO of the write is finished, and a fast fsync
+	 * does not wait for it, to reduce latency. If a fast fsync logged
+	 * those extent maps, replaying the log would put unwritten extents in
+	 * place of the file's data, so this tells it to wait for the ordered
+	 * extents to complete.
+	 *
+	 * It is set under the inode's ordered_tree_lock, before
+	 * BTRFS_ORDERED_IO_DONE is set.
 	 */
-	BTRFS_INODE_COW_WRITE_ERROR,
+	BTRFS_INODE_INCOMPLETE_COW_WRITE,
 	/*
 	 * Set by reflink while it holds the source's VFS inode lock shared.
 	 * Direct IO writes also take that lock shared; one that finds this bit
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 036d8c0f04ae..58951a89bfc6 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -1963,11 +1963,8 @@ static noinline_for_stack int extent_writepage_io(struct btrfs_inode *inode,
 			 * there must be an ordered extent.
 			 */
 			ASSERT(ordered != NULL);
-			spin_lock(&inode->ordered_tree_lock);
-			set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
-			ordered->truncated_len = min(ordered->truncated_len,
-						     cur - ordered->file_offset);
-			spin_unlock(&inode->ordered_tree_lock);
+			btrfs_mark_ordered_extent_truncated(ordered,
+							    cur - ordered->file_offset);
 			btrfs_put_ordered_extent(ordered);
 
 			btrfs_mark_ordered_io_finished(inode, cur, fs_info->sectorsize, true);
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 6f525ce42a2d..4208587abff1 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -1638,7 +1638,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 	 */
 	if (full_sync || btrfs_is_zoned(fs_info)) {
 		ret = btrfs_wait_ordered_range(inode, start, len);
-		clear_bit(BTRFS_INODE_COW_WRITE_ERROR, &inode->runtime_flags);
+		clear_bit(BTRFS_INODE_INCOMPLETE_COW_WRITE, &inode->runtime_flags);
 	} else {
 		/*
 		 * Get our ordered extents as soon as possible to avoid doing
@@ -1651,15 +1651,14 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 			goto out_release_extents;
 
 		/*
-		 * Check and clear the BTRFS_INODE_COW_WRITE_ERROR now after
-		 * starting and waiting for writeback, because for buffered IO
-		 * it may have been set during the end IO callback
-		 * (end_bbio_data_write() -> btrfs_finish_ordered_extent()) in
-		 * case an error happened and we need to wait for ordered
-		 * extents to complete so that any extent maps that point to
-		 * unwritten locations are dropped and we don't log them.
+		 * Check and clear BTRFS_INODE_INCOMPLETE_COW_WRITE now after
+		 * starting and waiting for writeback, because it may have been
+		 * set while we waited, and if a write failed or was truncated
+		 * we need to wait for ordered extents to complete so that any
+		 * extent maps that point to unwritten locations are dropped and
+		 * we don't log them.
 		 */
-		if (test_and_clear_bit(BTRFS_INODE_COW_WRITE_ERROR, &inode->runtime_flags))
+		if (test_and_clear_bit(BTRFS_INODE_INCOMPLETE_COW_WRITE, &inode->runtime_flags))
 			ret = btrfs_wait_ordered_range(inode, start, len);
 	}
 
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index df74c75d6c29..1a4e3e34ec11 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -367,6 +367,9 @@ void btrfs_mark_ordered_extent_truncated(struct btrfs_ordered_extent *ordered,
 	spin_lock(&inode->ordered_tree_lock);
 	set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
 	ordered->truncated_len = min(ordered->truncated_len, truncate_len);
+	/* As for a failed write, see can_finish_ordered_extent(). */
+	if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
+		set_bit(BTRFS_INODE_INCOMPLETE_COW_WRITE, &inode->runtime_flags);
 	spin_unlock(&inode->ordered_tree_lock);
 }
 
@@ -398,8 +401,18 @@ static bool can_finish_ordered_extent(struct btrfs_ordered_extent *ordered,
 		ordered->bytes_left -= len;
 	}
 
-	if (!uptodate)
+	if (!uptodate) {
 		btrfs_mark_ordered_extent_error(ordered);
+		/*
+		 * The extent maps of a failed COW write point to blocks that
+		 * won't be written until the ordered extent completes and
+		 * drops them. Make a fast fsync wait for that, before
+		 * BTRFS_ORDERED_IO_DONE is set.
+		 */
+		if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
+			set_bit(BTRFS_INODE_INCOMPLETE_COW_WRITE,
+				&inode->runtime_flags);
+	}
 
 	if (ordered->bytes_left)
 		return false;
@@ -436,37 +449,6 @@ void btrfs_finish_ordered_extent(struct btrfs_ordered_extent *ordered,
 					uptodate);
 	spin_unlock(&inode->ordered_tree_lock);
 
-	/*
-	 * If this is a COW write it means we created new extent maps for the
-	 * range and they point to unwritten locations if we got an error either
-	 * before submitting a bio or during IO.
-	 *
-	 * We have marked the ordered extent with BTRFS_ORDERED_IOERR, and we
-	 * are queuing its completion below. During completion, at
-	 * btrfs_finish_one_ordered(), we will drop the extent maps for the
-	 * unwritten extents.
-	 *
-	 * However because completion runs in a work queue we can end up having
-	 * a fast fsync running before that. In the case of direct IO, once we
-	 * unlock the inode the fsync might start, and we queue the completion
-	 * before unlocking the inode. In the case of buffered IO when writeback
-	 * finishes (end_bbio_data_write()) we queue the completion, so if the
-	 * writeback was triggered by a fast fsync, the fsync might start
-	 * logging before ordered extent completion runs in the work queue.
-	 *
-	 * The fast fsync will log file extent items based on the extent maps it
-	 * finds, so if by the time it collects extent maps the ordered extent
-	 * completion didn't happen yet, it will log file extent items that
-	 * point to unwritten extents, resulting in a corruption if a crash
-	 * happens and the log tree is replayed. Note that a fast fsync does not
-	 * wait for completion of ordered extents in order to reduce latency.
-	 *
-	 * Set a flag in the inode so that the next fast fsync will wait for
-	 * ordered extents to complete before starting to log.
-	 */
-	if (!uptodate && !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
-		set_bit(BTRFS_INODE_COW_WRITE_ERROR, &inode->runtime_flags);
-
 	if (ret)
 		btrfs_queue_ordered_fn(ordered);
 }

-- 
2.55.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 4/6] btrfs: wait for ordered extent IO in fast fsync
  2026-11-20 20:51 [PATCH v3 0/6] btrfs: data integrity fixes Josef Bacik
                   ` (2 preceding siblings ...)
  2026-11-20 20:51 ` [PATCH v3 3/6] btrfs: make fsync wait for all incomplete COW writes Josef Bacik
@ 2026-11-20 20:51 ` Josef Bacik
  2026-11-20 20:51 ` [PATCH v3 5/6] btrfs: don't report success for a skipped log sync Josef Bacik
  2026-11-20 20:51 ` [PATCH v3 6/6] btrfs: don't trim the target of a device replace Josef Bacik
  5 siblings, 0 replies; 7+ messages in thread
From: Josef Bacik @ 2026-11-20 20:51 UTC (permalink / raw)
  To: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	Qu Wenruo, Boris Burkov, Filipe Manana
  Cc: linux-btrfs, linux-kernel, stable, Josef Bacik

A fast fsync waits for writeback and then checks whether a write failed
or was truncated, in which case it waits for the ordered extents to
complete. The check can come too early, because end_bbio_data_write()
clears writeback before it finishes the ordered extent, which is where
a failure is recorded:

  end_bbio_data_write()            fsync

  btrfs_folio_clear_writeback()
                                   filemap_fdatawait_range()
                                   BTRFS_INODE_INCOMPLETE_COW_WRITE clear
                                   btrfs_log_changed_extents()
  btrfs_finish_ordered_extent()
    set BTRFS_INODE_INCOMPLETE_COW_WRITE

A direct IO write has no writeback for the fsync to wait for at all, so
the failure of an asynchronous direct IO write still in flight is missed
too.

The buffered case comes from commit a11d6912fdd9 ("btrfs: remove folio
parameter from ordered io related functions"), which moved the call to
btrfs_finish_ordered_extent() after the loop that clears writeback. The
direct IO case has been there since commit f13e01b89daf ("btrfs: ensure
fast fsync waits for ordered extents after a write failure") added the
flag.

Fix by having a fast fsync wait for BTRFS_ORDERED_IO_DONE on the ordered
extents it collected for logging before it checks. Since "btrfs: make
fsync wait for all incomplete COW writes", a failure or truncation is
recorded before that bit is set, and a barrier in
can_finish_ordered_extent() now orders the two, pairing with the fully
ordered test_and_clear_bit() of the flag in fsync. For a buffered write
the bit is set right after writeback is cleared. The writeback of a
buffered write that is not done yet is started first, and fsync returns
the error if that fails, since the rest of the ordered extent may then
not be submitted.

This depends on "btrfs: make fsync wait for all incomplete COW writes".

Fixes: a11d6912fdd9 ("btrfs: remove folio parameter from ordered io related functions")
Fixes: f13e01b89daf ("btrfs: ensure fast fsync waits for ordered extents after a write failure")
CC: stable@vger.kernel.org # 6.10+
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/file.c         | 67 ++++++++++++++++++++++++++++++++++++++++---------
 fs/btrfs/ordered-data.c |  6 +++++
 2 files changed, 61 insertions(+), 12 deletions(-)

diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 4208587abff1..ee09c4ecbd4c 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -1512,8 +1512,8 @@ static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx)
 	 * If we are doing a fast fsync we can not bail out if the inode's
 	 * last_trans is <= then the last committed transaction, because we only
 	 * update the last_trans of the inode during ordered extent completion,
-	 * and for a fast fsync we don't wait for that, we only wait for the
-	 * writeback to complete.
+	 * and for a fast fsync we don't wait for that, we only wait for the IO
+	 * of the writes to complete.
 	 */
 	if (inode->last_trans <= btrfs_get_last_trans_committed(fs_info) &&
 	    (test_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags) ||
@@ -1523,6 +1523,41 @@ static inline bool skip_inode_logging(const struct btrfs_log_ctx *ctx)
 	return false;
 }
 
+/*
+ * Wait for the IO of the ordered extents collected for logging to be accounted,
+ * starting IO on the dirty folios of a buffered write that is not done yet.
+ *
+ * Waiting for writeback is not enough: the end IO handler clears writeback
+ * before it finishes the ordered extent, and a direct IO write has no
+ * writeback. A failed or truncated write sets BTRFS_INODE_INCOMPLETE_COW_WRITE
+ * before BTRFS_ORDERED_IO_DONE is set, see can_finish_ordered_extent().
+ *
+ * If starting IO fails, the rest of the ordered extent may never be submitted,
+ * so return the error instead of waiting.
+ */
+static int wait_ordered_io_done(struct btrfs_inode *inode,
+				struct list_head *ordered_extents)
+{
+	struct btrfs_ordered_extent *ordered;
+	int ret;
+
+	list_for_each_entry(ordered, ordered_extents, log_list) {
+		if (test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags))
+			continue;
+		if (!test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags)) {
+			ret = btrfs_fdatawrite_range(inode, ordered->file_offset,
+						     ordered->file_offset +
+						     ordered->num_bytes - 1);
+			if (ret)
+				return ret;
+		}
+		wait_event(ordered->wait,
+			   test_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags));
+	}
+
+	return 0;
+}
+
 /*
  * fsync call for both files and directories.  This logs the inode into
  * the tree log instead of forcing full commits whenever possible.
@@ -1626,11 +1661,11 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 	 * IO of a lower priority task while holding a transaction open.
 	 *
 	 * For a full fsync we wait for the ordered extents to complete while
-	 * for a fast fsync we wait just for writeback to complete, and then
-	 * attach the ordered extents to the transaction so that a transaction
-	 * commit waits for their completion, to avoid data loss if we fsync,
-	 * the current transaction commits before the ordered extents complete
-	 * and a power failure happens right after that.
+	 * for a fast fsync we wait just for the IO of the writes to complete,
+	 * and then attach the ordered extents to the transaction so that a
+	 * transaction commit waits for their completion, to avoid data loss if
+	 * we fsync, the current transaction commits before the ordered extents
+	 * complete and a power failure happens right after that.
 	 *
 	 * For zoned filesystem, if a write IO uses a ZONE_APPEND command, the
 	 * logical address recorded in the ordered extent may change. We need
@@ -1650,13 +1685,21 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
 		if (ret)
 			goto out_release_extents;
 
+		ret = wait_ordered_io_done(inode, &ctx.ordered_extents);
+		if (ret)
+			goto out_release_extents;
+
 		/*
 		 * Check and clear BTRFS_INODE_INCOMPLETE_COW_WRITE now after
-		 * starting and waiting for writeback, because it may have been
-		 * set while we waited, and if a write failed or was truncated
-		 * we need to wait for ordered extents to complete so that any
-		 * extent maps that point to unwritten locations are dropped and
-		 * we don't log them.
+		 * waiting for the IO of the ordered extents, because it may
+		 * have been set while we waited, and if a write failed or was
+		 * truncated we need to wait for ordered extents to complete so
+		 * that any extent maps that point to unwritten locations are
+		 * dropped and we don't log them.
+		 *
+		 * test_and_clear_bit() is fully ordered, which pairs with the
+		 * barrier before BTRFS_ORDERED_IO_DONE is set in
+		 * can_finish_ordered_extent().
 		 */
 		if (test_and_clear_bit(BTRFS_INODE_INCOMPLETE_COW_WRITE, &inode->runtime_flags))
 			ret = btrfs_wait_ordered_range(inode, start, len);
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index 1a4e3e34ec11..728e73baf15e 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -420,7 +420,13 @@ static bool can_finish_ordered_extent(struct btrfs_ordered_extent *ordered,
 	/*
 	 * All the IO of the ordered extent is finished, we need to queue
 	 * the finish_func to be executed.
+	 *
+	 * Order BTRFS_INODE_INCOMPLETE_COW_WRITE, if set above, before
+	 * BTRFS_ORDERED_IO_DONE, as a fast fsync tests the latter without
+	 * the ordered_tree_lock. Pairs with the test_and_clear_bit() in
+	 * btrfs_sync_file().
 	 */
+	smp_mb__before_atomic();
 	set_bit(BTRFS_ORDERED_IO_DONE, &ordered->flags);
 	cond_wake_up(&ordered->wait);
 	refcount_inc(&ordered->refs);

-- 
2.55.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 5/6] btrfs: don't report success for a skipped log sync
  2026-11-20 20:51 [PATCH v3 0/6] btrfs: data integrity fixes Josef Bacik
                   ` (3 preceding siblings ...)
  2026-11-20 20:51 ` [PATCH v3 4/6] btrfs: wait for ordered extent IO in fast fsync Josef Bacik
@ 2026-11-20 20:51 ` Josef Bacik
  2026-11-20 20:51 ` [PATCH v3 6/6] btrfs: don't trim the target of a device replace Josef Bacik
  5 siblings, 0 replies; 7+ messages in thread
From: Josef Bacik @ 2026-11-20 20:51 UTC (permalink / raw)
  To: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	Qu Wenruo, Boris Burkov, Filipe Manana
  Cc: linux-btrfs, linux-kernel, stable, Josef Bacik

When a commit of the log root tree gives up, because the transaction
needs a full commit or writing the log failed, btrfs_sync_log() still
advances log_transid_committed and hands the result to the contexts on
the list. Unlike a successful commit it does not advance log_transid, so
the log transaction it just retired is still the one new syncs join.

A sync that gets to the log root tree after that adds its context to
the emptied list, sees log_transid_committed >= its log_transid and
returns its log_ret, which is still 0:

  fsync A (subvol 1)           rename       fsync B (subvol 2)

                                            btrfs_need_log_full_commit()
                                              -> false
                               btrfs_set_log_full_commit()
  btrfs_need_log_full_commit()
    -> true
  btrfs_remove_all_log_ctxs()
  log_transid_committed = N
  log_transid stays N
                                            log_transid = N, log_ret = 0
                                            log_transid_committed >= N
                                            return 0

btrfs_sync_file() then ends the transaction without committing it, and a
crash before A's transaction commit loses what B was told is durable.
This was reproduced with delays added to btrfs_sync_log().

Fix by advancing log_transid when a commit of the log root tree gives
up, so a later sync joins the next log transaction and either commits it
or gives up itself. The check for an already committed log transaction
can then no longer be true, so warn on debug builds if it is, and force
a full commit.

Fixes: d1433debe7f4 ("Btrfs: just wait or commit our own log sub-transaction")
CC: stable@vger.kernel.org
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/tree-log.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index e50f924bca59..e15b26f9b920 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -3439,11 +3439,20 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
 		goto out;
 	}
 
-	if (log_root_tree->log_transid_committed >= root_log_ctx.log_transid) {
+	/*
+	 * A commit of the log root tree moves log_transid on even when it
+	 * gives up, so the log transaction we just joined can't be done.
+	 */
+	if (unlikely(log_root_tree->log_transid_committed >=
+		     root_log_ctx.log_transid)) {
+		DEBUG_WARN("log_transid_committed=%d log_transid=%d",
+			   log_root_tree->log_transid_committed,
+			   root_log_ctx.log_transid);
 		blk_finish_plug(&plug);
 		list_del_init(&root_log_ctx.list);
+		btrfs_wait_tree_log_extents(log, mark);
 		mutex_unlock(&log_root_tree->log_mutex);
-		ret = root_log_ctx.log_ret;
+		ret = BTRFS_LOG_FORCE_COMMIT;
 		trace_btrfs_sync_log_exit(trans, root, ctx, ret);
 		goto out;
 	}
@@ -3577,6 +3586,13 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
 	mutex_lock(&log_root_tree->log_mutex);
 	btrfs_remove_all_log_ctxs(log_root_tree, index2, ret);
 
+	/*
+	 * If the commit gave up, log_transid still names the log transaction
+	 * we are retiring. Move it on, so that nobody joins a log transaction
+	 * whose result has already been handed out.
+	 */
+	if (log_root_tree->log_transid == root_log_ctx.log_transid)
+		log_root_tree->log_transid++;
 	log_root_tree->log_transid_committed++;
 	log_root_tree->log_commit[index2] = false;
 	mutex_unlock(&log_root_tree->log_mutex);

-- 
2.55.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 6/6] btrfs: don't trim the target of a device replace
  2026-11-20 20:51 [PATCH v3 0/6] btrfs: data integrity fixes Josef Bacik
                   ` (4 preceding siblings ...)
  2026-11-20 20:51 ` [PATCH v3 5/6] btrfs: don't report success for a skipped log sync Josef Bacik
@ 2026-11-20 20:51 ` Josef Bacik
  5 siblings, 0 replies; 7+ messages in thread
From: Josef Bacik @ 2026-11-20 20:51 UTC (permalink / raw)
  To: David Sterba, Chris Mason, Naohiro Aota, Johannes Thumshirn,
	Qu Wenruo, Boris Burkov, Filipe Manana
  Cc: linux-btrfs, linux-kernel, stable, Josef Bacik

FITRIM also discards the space of every device that is not allocated to a
chunk. btrfs_trim_free_extents() goes over fs_devices->devices and, for
each device that is writeable, discards the ranges that have neither
CHUNK_ALLOCATED nor CHUNK_TRIMMED set in device->alloc_state.

The target device of a running replace operation is on that list and it is
writeable. But its alloc_state is empty. The allocated ranges of the
source device are only copied to it by btrfs_set_target_alloc_state(),
when the replace finishes. Until then the whole device looks unallocated,
so an fstrim discards everything that was copied to it so far, and what is
being written to it in parallel to the source device.

The replace finishes without any error and the source device is dropped.
With a single device filesystem, 700M of data and fstrim called in a loop
while the replace runs:

  BTRFS info: dev_replace from /dev/vdb (devid 1) to /dev/vdc finished
  BTRFS error: bad tree block start, mirror 1 want 30638080 have 0
  BTRFS error: bad tree block start, mirror 2 want 30638080 have 0
  BTRFS info: scrub: not finished on devid 1 with status: -5

Skip the target device, and don't count it in btrfs_ioctl_fitrim()
either. The flag is cleared with the device list mutex held, after the
alloc_state was set up, and the trimming code holds that mutex while it
works on a device.

Fixes: 929be17a9b49 ("btrfs: Switch btrfs_trim_free_extents to find_first_clear_extent_bit")
CC: stable@vger.kernel.org
Reviewed-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/extent-tree.c | 8 ++++++++
 fs/btrfs/ioctl.c       | 3 +++
 2 files changed, 11 insertions(+)

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 9b0a5c6b96f3..22155507f905 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -6641,6 +6641,14 @@ static int btrfs_trim_free_extents_throttle(struct btrfs_device *device,
 	if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) || !device->bdev)
 		return 0;
 
+	/*
+	 * The target of a running device replace has nothing in its alloc_state
+	 * until the replace finishes, all of it would look unallocated and we
+	 * would discard what was already copied to it.
+	 */
+	if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
+		return 0;
+
 	/* Discard not supported = nothing to do. */
 	if (!bdev_max_discard_sectors(device->bdev))
 		return 0;
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 651a780dd7b1..3d1ed1a5911c 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -507,6 +507,9 @@ static noinline int btrfs_ioctl_fitrim(struct btrfs_fs_info *fs_info,
 				dev_list) {
 		if (!device->bdev || !bdev_max_discard_sectors(device->bdev))
 			continue;
+		/* Trimming skips the target of a device replace. */
+		if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
+			continue;
 		num_devices++;
 		minlen = min_t(u64, bdev_discard_granularity(device->bdev),
 				    minlen);

-- 
2.55.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-20 20:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 20:51 [PATCH v3 0/6] btrfs: data integrity fixes Josef Bacik
2026-11-20 20:51 ` [PATCH v3 1/6] btrfs: don't drop the error of the last split bio Josef Bacik
2026-11-20 20:51 ` [PATCH v3 2/6] btrfs: clear dirty blocks after a delalloc failure Josef Bacik
2026-11-20 20:51 ` [PATCH v3 3/6] btrfs: make fsync wait for all incomplete COW writes Josef Bacik
2026-11-20 20:51 ` [PATCH v3 4/6] btrfs: wait for ordered extent IO in fast fsync Josef Bacik
2026-11-20 20:51 ` [PATCH v3 5/6] btrfs: don't report success for a skipped log sync Josef Bacik
2026-11-20 20:51 ` [PATCH v3 6/6] btrfs: don't trim the target of a device replace Josef Bacik

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®