From: Josef Bacik <josef@toxicpanda.com>
To: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
Naohiro Aota <naohiro.aota@wdc.com>,
Johannes Thumshirn <johannes.thumshirn@wdc.com>,
Qu Wenruo <wqu@suse.com>, Boris Burkov <boris@bur.io>,
Filipe Manana <fdmanana@suse.com>
Cc: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org, Josef Bacik <josef@toxicpanda.com>
Subject: [PATCH v3 4/6] btrfs: wait for ordered extent IO in fast fsync
Date: Fri, 20 Nov 2026 20:51:32 +0000 [thread overview]
Message-ID: <20261120-b4-btrfs-data-integrity-v3-4-4de4b91b826e@toxicpanda.com> (raw)
In-Reply-To: <20261120-b4-btrfs-data-integrity-v3-0-4de4b91b826e@toxicpanda.com>
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
next prev parent reply other threads:[~2026-11-20 20:51 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Josef Bacik [this message]
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
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=20261120-b4-btrfs-data-integrity-v3-4-4de4b91b826e@toxicpanda.com \
--to=josef@toxicpanda.com \
--cc=boris@bur.io \
--cc=dsterba@suse.com \
--cc=fdmanana@suse.com \
--cc=johannes.thumshirn@wdc.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mason@kernel.org \
--cc=naohiro.aota@wdc.com \
--cc=stable@vger.kernel.org \
--cc=wqu@suse.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®