Re: [PATCH v3 4/6] btrfs: wait for ordered extent IO in fast fsync

From: Filipe Manana

Date: Mon Nov 23 2026 - 06:21:24 EST


On Fri, Nov 20, 2026 at 8:54 PM Josef Bacik <josef@xxxxxxxxxxxxxx> wrote:
>
> 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@xxxxxxxxxxxxxxx # 6.10+
> Signed-off-by: Josef Bacik <josef@xxxxxxxxxxxxxx>
> ---
> 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;

Nit: declare ret in the scope below where it's used.

Reviewed-by: Filipe Manana <fdmanana@xxxxxxxx>

Thanks.

> +
> + 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
>
>