From: Qu Wenruo <quwenruo.btrfs@gmx.com>
To: Josef Bacik <josef@toxicpanda.com>,
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
Subject: Re: [PATCH v3 3/6] btrfs: make fsync wait for all incomplete COW writes
Date: Sun, 22 Nov 2026 07:30:05 +1030 [thread overview]
Message-ID: <3fe960f6-7488-4352-ab9e-1c4884ff68b4@gmx.com> (raw)
In-Reply-To: <20261120-b4-btrfs-data-integrity-v3-3-4de4b91b826e@toxicpanda.com>
在 2026/11/21 07:21, 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>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Thanks,
Qu
> ---
> 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);
> }
>
next prev parent reply other threads:[~2026-11-21 21:00 UTC|newest]
Thread overview: 9+ 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-21 21:00 ` Qu Wenruo [this message]
2026-11-20 20:51 ` [PATCH v3 4/6] btrfs: wait for ordered extent IO in fast fsync Josef Bacik
2026-11-21 21:09 ` Qu Wenruo
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=3fe960f6-7488-4352-ab9e-1c4884ff68b4@gmx.com \
--to=quwenruo.btrfs@gmx.com \
--cc=boris@bur.io \
--cc=dsterba@suse.com \
--cc=fdmanana@suse.com \
--cc=johannes.thumshirn@wdc.com \
--cc=josef@toxicpanda.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®