Re: [PATCH v3 2/3] btrfs: sync the uptodate bitmap when attaching a data folio state

From: Qu Wenruo

Date: Thu Dec 03 2026 - 15:37:07 EST




在 2026/12/4 00:00, Brahmajit Das 写道:
From: Brahmajit Das <bdas@xxxxxxx>

A data folio can lose its private while it stays in the page cache, e.g.
btrfs_release_folio() from a partial invalidation, or from a large folio
split that then fails. Such a folio was only released, not invalidated,
so it can still be uptodate.

When something calls set_folio_extent_mapped() on it again, such as
prepare_one_folio(), btrfs_page_mkwrite() or btrfs_truncate_block(), a
new btrfs_folio_state is attached with empty bitmaps, while PG_uptodate
is still set on the folio. Those callers see an uptodate folio, skip
reading it, and only update the uptodate bits of their own range. The
other blocks are left marked not uptodate in the subpage bitmap.

That can trip the ASSERT() in can_skip_one_ordered_range(), which
expects the whole range up to the ordered extent end or folio end to be
uptodate if its first block is.

Fix it by setting the uptodate bitmap in btrfs_attach_folio_state() if
the data folio is uptodate. On the read path, private is attached
before the folio is uptodate, so nothing changes there.

Note: this was only build tested.

This patch was written with the help of an AI coding assistant (Claude),
following a review comment from Sashiko AI review.

Signed-off-by: Brahmajit Das <bdas@xxxxxxx>
---
fs/btrfs/subpage.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/fs/btrfs/subpage.c b/fs/btrfs/subpage.c
index ebf18efe1ea3..97d4824ae623 100644
--- a/fs/btrfs/subpage.c
+++ b/fs/btrfs/subpage.c
@@ -63,6 +63,19 @@ int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
if (IS_ERR(bfs))
return PTR_ERR(bfs);
+ /*
+ * A data folio can lose its private while staying uptodate in the
+ * page cache, e.g. btrfs_release_folio() on a folio that then fails
+ * to be removed or split. The new state starts with empty bitmaps,
+ * so sync the uptodate bitmap with the folio flag, or the blocks not
+ * touched by the caller would be considered not uptodate.
+ */
+ if (type == BTRFS_SUBPAGE_DATA && folio_test_uptodate(folio)) {
+ const unsigned int bpf = btrfs_blocks_per_folio(fs_info, folio);
+
+ bitmap_set(bfs->bitmaps, bpf * btrfs_bitmap_nr_uptodate, bpf);
+ }

There is really only one call site requiring this.

Please do it in the btrfs_data_dirty_folio() call site.

+
folio_attach_private(folio, bfs);
return 0;
}