Skip to contentSkip to conversation

btrfs: invalidate pages instead of truncate after reflinking

PATCH Superseded [email protected] 4 participants 8 messages Jan 8 – Jan 9 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: invalidate pages instead of truncate after reflinking

From: Filipe Manana <[email protected]>

Qu reported that generic/164 often fails because the read operations get zeroes when it expects to either get all bytes with a value of 0x61 or 0x62. The issue stems from truncating the pages from the page cache instead of invalidating, as truncating can zero page contents.

So instead of truncating, invalidate the page cache range with a call to filemap_invalidate_inode(), which besides not doing any zeroing also ensures that while it's invalidating folios, no new folios are added. This helps ensure that buffered reads that happen while a reflink operation is in progress always get either the whole old data (the one before the reflink) or the whole new data, which is what generic/164 expects.

---
 fs/btrfs/reflink.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
fs/btrfs/reflink.c +12 -9
index e746980567da..f7ddd3765249 100644
@@ -705,7 +705,6 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
 	struct inode *src = file_inode(file_src);
 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
 	int ret;
-	int wb_ret;
 	u64 len = olen;
 	u64 bs = fs_info->sectorsize;
 	u64 end;
@@ -750,25 +749,29 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
 	btrfs_lock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
 	ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
 	btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
+	if (ret < 0)
+		return ret;
 
 	/*
 	 * We may have copied an inline extent into a page of the destination
 	 * range, so wait for writeback to complete before truncating pages
 	 * from the page cache. This is a rare case.
 	 */
-	wb_ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
-	ret = ret ? ret : wb_ret;
+	ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
+	if (ret < 0)
+		return ret;
+
 	/*
-	 * Truncate page cache pages so that future reads will see the cloned
-	 * data immediately and not the previous data.
+	 * Invalidate page cache so that future reads will see the cloned data
+	 * immediately and not the previous data.
 	 */
-	truncate_inode_pages_range(&inode->i_data,
-				round_down(destoff, PAGE_SIZE),
-				round_up(destoff + len, PAGE_SIZE) - 1);
+	ret = filemap_invalidate_inode(inode, false, destoff, end);
+	if (ret < 0)
+		return ret;
 
 	btrfs_btree_balance_dirty(fs_info);
 
-	return ret;
+	return 0;
 }
 
 static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
Signature · 1 line
2.47.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · On Thu, Jan 08, 2026 at 05:10:04PM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

Qu reported that generic/164 often fails because the read operations get zeroes when it expects to either get all bytes with a value of 0x61 or 0x62. The issue stems from truncating the pages from the page cache instead of invalidating, as truncating can zero page contents.

Can you make it more clear if this is a subpage/large folios issue or a generic issue? Or maybe just explain the "can zero page contents" in a little more detail? I agree that it is the wrong "contract" so your change makes sense either way, this is just for future knowledge/archaeology.

The documentation of truncate_inode_pages_range only mentions zeroing partial pages when "lstart or lend + 1 is not page aligned" but our call is

	truncate_inode_pages_range(&inode->i_data,
				round_down(destoff, PAGE_SIZE),
				round_up(destoff + len, PAGE_SIZE) - 1);

which appears to align it?

Sorry if I am missing something obvious :)

Quoted 10 lines

So instead of truncating, invalidate the page cache range with a call to filemap_invalidate_inode(), which besides not doing any zeroing also ensures that while it's invalidating folios, no new folios are added. This helps ensure that buffered reads that happen while a reflink operation is in progress always get either the whole old data (the one before the reflink) or the whole new data, which is what generic/164 expects.

Quoted 18 lines
---
 fs/btrfs/reflink.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
fs/btrfs/reflink.c +0 -1
index e746980567da..f7ddd3765249 100644
@@ -705,7 +705,6 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
 	struct inode *src = file_inode(file_src);
 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
 	int ret;
-	int wb_ret;
 	u64 len = olen;
 	u64 bs = fs_info->sectorsize;
 	u64 end;

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · line 757

Quoted 9 lines
fs/btrfs/reflink.c +2 -0
@@ -750,25 +749,29 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
 	btrfs_lock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
 	ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
 	btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
+	if (ret < 0)
+		return ret;
 
 	/*
 	 * We may have copied an inline extent into a page of the destination
 	 * range, so wait for writeback to complete before truncating pages

s/truncating/invalidating/

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · line 763

Quoted 8 lines
fs/btrfs/reflink.c +4 -2
 	 * from the page cache. This is a rare case.
 	 */
-	wb_ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
-	ret = ret ? ret : wb_ret;
+	ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
+	if (ret < 0)
+		return ret;
+

Even if it's true, not worth doing in a fix like this, but a question:
I think buffered reads will check for outstanding ordered_extents and wait for them. If we are invalidating the cache next, then how can we read the file without seeing this ordered_extent? Is this wait_ordered_range still necessary?

Again, not a blocker for this patch.

Quoted 23 lines
fs/btrfs/reflink.c +6 -6
 	/*
-	 * Truncate page cache pages so that future reads will see the cloned
-	 * data immediately and not the previous data.
+	 * Invalidate page cache so that future reads will see the cloned data
+	 * immediately and not the previous data.
 	 */
-	truncate_inode_pages_range(&inode->i_data,
-				round_down(destoff, PAGE_SIZE),
-				round_up(destoff + len, PAGE_SIZE) - 1);
+	ret = filemap_invalidate_inode(inode, false, destoff, end);
+	if (ret < 0)
+		return ret;
 
 	btrfs_btree_balance_dirty(fs_info);
 
-	return ret;
+	return 0;
 }
 
 static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
Signature · 1 line
2.47.2
#2 lore raw
Quoted 8 lines · Boris Burkov
Quoted 6 lines · On Thu, Jan 08, 2026 at 05:10:04PM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

Qu reported that generic/164 often fails because the read operations get zeroes when it expects to either get all bytes with a value of 0x61 or 0x62. The issue stems from truncating the pages from the page cache instead of invalidating, as truncating can zero page contents.

Quoted 5 lines

Can you make it more clear if this is a subpage/large folios issue or a generic issue? Or maybe just explain the "can zero page contents" in a little more detail? I agree that it is the wrong "contract" so your change makes sense either way, this is just for future knowledge/archaeology.

I answered part of my own question. It reproduced in 10 tries on my x86 system :)

Quoted 96 lines

The documentation of truncate_inode_pages_range only mentions zeroing partial pages when "lstart or lend + 1 is not page aligned" but our call is

	truncate_inode_pages_range(&inode->i_data,
				round_down(destoff, PAGE_SIZE),
				round_up(destoff + len, PAGE_SIZE) - 1);

which appears to align it?

Sorry if I am missing something obvious :)

Quoted 10 lines

So instead of truncating, invalidate the page cache range with a call to filemap_invalidate_inode(), which besides not doing any zeroing also ensures that while it's invalidating folios, no new folios are added. This helps ensure that buffered reads that happen while a reflink operation is in progress always get either the whole old data (the one before the reflink) or the whole new data, which is what generic/164 expects.

Quoted 18 lines
---
 fs/btrfs/reflink.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
fs/btrfs/reflink.c +0 -1
index e746980567da..f7ddd3765249 100644
@@ -705,7 +705,6 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
 	struct inode *src = file_inode(file_src);
 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
 	int ret;
-	int wb_ret;
 	u64 len = olen;
 	u64 bs = fs_info->sectorsize;
 	u64 end;

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · line 757

Quoted 9 lines
fs/btrfs/reflink.c +2 -0
@@ -750,25 +749,29 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
 	btrfs_lock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
 	ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
 	btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
+	if (ret < 0)
+		return ret;
 
 	/*
 	 * We may have copied an inline extent into a page of the destination
 	 * range, so wait for writeback to complete before truncating pages

s/truncating/invalidating/

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · line 763

Quoted 8 lines
fs/btrfs/reflink.c +4 -2
 	 * from the page cache. This is a rare case.
 	 */
-	wb_ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
-	ret = ret ? ret : wb_ret;
+	ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
+	if (ret < 0)
+		return ret;
+

Even if it's true, not worth doing in a fix like this, but a question:
I think buffered reads will check for outstanding ordered_extents and wait for them. If we are invalidating the cache next, then how can we read the file without seeing this ordered_extent? Is this wait_ordered_range still necessary?

Again, not a blocker for this patch.

Quoted 23 lines
fs/btrfs/reflink.c +6 -6
 	/*
-	 * Truncate page cache pages so that future reads will see the cloned
-	 * data immediately and not the previous data.
+	 * Invalidate page cache so that future reads will see the cloned data
+	 * immediately and not the previous data.
 	 */
-	truncate_inode_pages_range(&inode->i_data,
-				round_down(destoff, PAGE_SIZE),
-				round_up(destoff + len, PAGE_SIZE) - 1);
+	ret = filemap_invalidate_inode(inode, false, destoff, end);
+	if (ret < 0)
+		return ret;
 
 	btrfs_btree_balance_dirty(fs_info);
 
-	return ret;
+	return 0;
 }
 
 static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
Signature · 1 line
2.47.2
#3 lore raw
Quoted 11 lines · Boris Burkov
Quoted 10 lines · Boris Burkov
Quoted 6 lines · On Thu, Jan 08, 2026 at 05:10:04PM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

Qu reported that generic/164 often fails because the read operations get zeroes when it expects to either get all bytes with a value of 0x61 or 0x62. The issue stems from truncating the pages from the page cache instead of invalidating, as truncating can zero page contents.

Can you make it more clear if this is a subpage/large folios issue or a generic issue?

It's a generic one, and it's large folios involved.

We can have the following case:

0 4K 8K 12K 16K
| | | | |
|<---- Extent A ----->|<----- Extent B ------>|

The page size is still 4K, but the folio we got is 16K.

Then if we remap the range for [8K, 16K), then truncate_inode_pages_range() will got the large folio 0 sized 16K, then call truncate_inode_partial_folio().

Which later calls folio_zero_range() for the [8K, 16K) range first, then try to split the folio into smaller ones to properly drop them from the cache.

But if split failed (e.g. racing with other operations holding the filemap lock), the partially zeroed large folio will be kept, resulting the range [8K, 16K) being zeroed meanwhile the folio is still a 16K sized large one.

Quoted 18 lines

Or maybe just explain the "can zero page contents" in a

Quoted 3 lines

little more detail? I agree that it is the wrong "contract" so your change makes sense either way, this is just for future knowledge/archaeology.

I answered part of my own question. It reproduced in 10 tries on my x86 system :)

Quoted 10 lines

The documentation of truncate_inode_pages_range only mentions zeroing partial pages when "lstart or lend + 1 is not page aligned" but our call is

	truncate_inode_pages_range(&inode->i_data,
				round_down(destoff, PAGE_SIZE),
				round_up(destoff + len, PAGE_SIZE) - 1);

which appears to align it?

I think it's also a good time to ask for a comment update on that function. Mentioning the above failed-to-split case will be helpful for any future users.

Thanks,
Qu

Quoted 87 lines
Quoted 86 lines

Sorry if I am missing something obvious :)

Quoted 10 lines

So instead of truncating, invalidate the page cache range with a call to filemap_invalidate_inode(), which besides not doing any zeroing also ensures that while it's invalidating folios, no new folios are added. This helps ensure that buffered reads that happen while a reflink operation is in progress always get either the whole old data (the one before the reflink) or the whole new data, which is what generic/164 expects.

Quoted 18 lines
---
  fs/btrfs/reflink.c | 21 ++++++++++++---------
  1 file changed, 12 insertions(+), 9 deletions(-)
fs/btrfs/reflink.c +0 -1
index e746980567da..f7ddd3765249 100644
@@ -705,7 +705,6 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
  	struct inode *src = file_inode(file_src);
  	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  	int ret;
-	int wb_ret;
  	u64 len = olen;
  	u64 bs = fs_info->sectorsize;
  	u64 end;

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · line 757

Quoted 9 lines
fs/btrfs/reflink.c +2 -0
@@ -750,25 +749,29 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
  	btrfs_lock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
  	ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
  	btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
+	if (ret < 0)
+		return ret;
 
  	/*
  	 * We may have copied an inline extent into a page of the destination
  	 * range, so wait for writeback to complete before truncating pages

s/truncating/invalidating/

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · line 763

Quoted 8 lines
fs/btrfs/reflink.c +4 -2
  	 * from the page cache. This is a rare case.
  	 */
-	wb_ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
-	ret = ret ? ret : wb_ret;
+	ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
+	if (ret < 0)
+		return ret;
+

Even if it's true, not worth doing in a fix like this, but a question:
I think buffered reads will check for outstanding ordered_extents and wait for them. If we are invalidating the cache next, then how can we read the file without seeing this ordered_extent? Is this wait_ordered_range still necessary?

Again, not a blocker for this patch.

Quoted 23 lines
fs/btrfs/reflink.c +6 -6
  	/*
-	 * Truncate page cache pages so that future reads will see the cloned
-	 * data immediately and not the previous data.
+	 * Invalidate page cache so that future reads will see the cloned data
+	 * immediately and not the previous data.
  	 */
-	truncate_inode_pages_range(&inode->i_data,
-				round_down(destoff, PAGE_SIZE),
-				round_up(destoff + len, PAGE_SIZE) - 1);
+	ret = filemap_invalidate_inode(inode, false, destoff, end);
+	if (ret < 0)
+		return ret;
 
  	btrfs_btree_balance_dirty(fs_info);
 
-	return ret;
+	return 0;
  }
 
  static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
Signature · 1 line
2.47.2
#4 lore raw
Quoted 24 lines · Qu Wenruo
Quoted 11 lines · Boris Burkov
Quoted 10 lines · Boris Burkov
Quoted 6 lines · On Thu, Jan 08, 2026 at 05:10:04PM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

Qu reported that generic/164 often fails because the read operations get zeroes when it expects to either get all bytes with a value of 0x61 or 0x62. The issue stems from truncating the pages from the page cache instead of invalidating, as truncating can zero page contents.

Can you make it more clear if this is a subpage/large folios issue or a generic issue?

It's a generic one, and it's large folios involved.

We can have the following case:

    0          4K         8K         12K          16K

         |          |          |          |            |
         |<---- Extent A ----->|<----- Extent B ------>|
Quoted 7 lines

The page size is still 4K, but the folio we got is 16K.

Then if we remap the range for [8K, 16K), then truncate_inode_pages_range() will got the large folio 0 sized 16K, then call truncate_inode_partial_folio().

Which later calls folio_zero_range() for the [8K, 16K) range first, then try to split the folio into smaller ones to properly drop them from the cache.

But if split failed (e.g. racing with other operations holding the filemap lock), the partially zeroed large folio will be kept, resulting the range [8K, 16K) being zeroed meanwhile the folio is still a 16K sized large one.

With that said, the best solution would be introducing some fs aware range dropping, so that even if the folio split failed, we don't just get some range zeroed by folio_zero_range() only, but also got the uptodate bitmap cleared for that range.

So that when the next time we need to read the full 16K folio, we will only need to read the last dropped [8K, 16K) range, without the need to re-read the range [0, 8K).

But that will be something way more complex, and the current fix is pretty safe (will drop every folio covering the range).

Thanks,
Qu

Quoted 115 lines
Quoted 20 lines

Or maybe just explain the "can zero page contents" in a

Quoted 3 lines

little more detail? I agree that it is the wrong "contract" so your change makes sense either way, this is just for future knowledge/archaeology.

I answered part of my own question. It reproduced in 10 tries on my x86 system :)

Quoted 10 lines

The documentation of truncate_inode_pages_range only mentions zeroing partial pages when "lstart or lend + 1 is not page aligned" but our call is

    truncate_inode_pages_range(&inode->i_data, round_down(destoff, PAGE_SIZE), round_up(destoff + len, PAGE_SIZE) - 1);

which appears to align it?

I think it's also a good time to ask for a comment update on that function. Mentioning the above failed-to-split case will be helpful for any future users.

Thanks,
Qu

Quoted 87 lines
Quoted 86 lines

Sorry if I am missing something obvious :)

Quoted 10 lines

So instead of truncating, invalidate the page cache range with a call to filemap_invalidate_inode(), which besides not doing any zeroing also ensures that while it's invalidating folios, no new folios are added. This helps ensure that buffered reads that happen while a reflink operation is in progress always get either the whole old data (the one before the reflink) or the whole new data, which is what generic/164 expects.

Quoted 19 lines

---
  fs/btrfs/reflink.c | 21 ++++++++++++---------

  1 file changed, 12 insertions(+), 9 deletions(-)
fs/btrfs/reflink.c +0 -1
index e746980567da..f7ddd3765249 100644
@@ -705,7 +705,6 @@ static noinline int btrfs_clone_files(struct 
 file *file, struct file *file_src,
       struct inode *src = file_inode(file_src);
       struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
       int ret;
-    int wb_ret;
       u64 len = olen;
       u64 bs = fs_info->sectorsize;

      u64 end;

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct · line 757

Quoted 9 lines
fs/btrfs/reflink.c +2 -0
@@ -750,25 +749,29 @@ static noinline int btrfs_clone_files(struct 
 file *file, struct file *file_src,
       btrfs_lock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
       ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
       btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
+    if (ret < 0)
+        return ret;
       /*
        * We may have copied an inline extent into a page of the destination
        * range, so wait for writeback to complete before truncating pages

s/truncating/invalidating/

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · line 763

Quoted 8 lines
fs/btrfs/reflink.c +4 -2
        * from the page cache. This is a rare case.
        */
-    wb_ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
-    ret = ret ? ret : wb_ret;
+    ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
+    if (ret < 0)
+        return ret;
+

Even if it's true, not worth doing in a fix like this, but a question:
I think buffered reads will check for outstanding ordered_extents and wait for them. If we are invalidating the cache next, then how can we read the file without seeing this ordered_extent? Is this wait_ordered_range still necessary?

Again, not a blocker for this patch.

Quoted 22 lines
fs/btrfs/reflink.c +0 -1
       /*
-     * Truncate page cache pages so that future reads will see the 

cloned
-     * data immediately and not the previous data.
+     * Invalidate page cache so that future reads will see the cloned data
+     * immediately and not the previous data.
       */
-    truncate_inode_pages_range(&inode->i_data,
-                round_down(destoff, PAGE_SIZE),
-                round_up(destoff + len, PAGE_SIZE) - 1);
+    ret = filemap_invalidate_inode(inode, false, destoff, end); +    if (ret < 0)
+        return ret;
      btrfs_btree_balance_dirty(fs_info);
-    return ret;
+    return 0;
  }
  static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,

Signature · 1 line
2.47.2
#5 lore raw
In reply to Boris Burkov
Quoted 79 lines · Boris Burkov
Quoted 6 lines · On Thu, Jan 08, 2026 at 05:10:04PM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

Qu reported that generic/164 often fails because the read operations get zeroes when it expects to either get all bytes with a value of 0x61 or 0x62. The issue stems from truncating the pages from the page cache instead of invalidating, as truncating can zero page contents.

Can you make it more clear if this is a subpage/large folios issue or a generic issue? Or maybe just explain the "can zero page contents" in a little more detail? I agree that it is the wrong "contract" so your change makes sense either way, this is just for future knowledge/archaeology.

The documentation of truncate_inode_pages_range only mentions zeroing partial pages when "lstart or lend + 1 is not page aligned" but our call is

        truncate_inode_pages_range(&inode->i_data,
                                round_down(destoff, PAGE_SIZE),
                                round_up(destoff + len, PAGE_SIZE) - 1);

which appears to align it?

Sorry if I am missing something obvious :)

Quoted 10 lines

So instead of truncating, invalidate the page cache range with a call to filemap_invalidate_inode(), which besides not doing any zeroing also ensures that while it's invalidating folios, no new folios are added. This helps ensure that buffered reads that happen while a reflink operation is in progress always get either the whole old data (the one before the reflink) or the whole new data, which is what generic/164 expects.

Quoted 18 lines
---
 fs/btrfs/reflink.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
fs/btrfs/reflink.c +0 -1
index e746980567da..f7ddd3765249 100644
@@ -705,7 +705,6 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
      struct inode *src = file_inode(file_src);
      struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
      int ret;
-     int wb_ret;
      u64 len = olen;
      u64 bs = fs_info->sectorsize;
      u64 end;

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · line 757

Quoted 9 lines
fs/btrfs/reflink.c +2 -0
@@ -750,25 +749,29 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
      btrfs_lock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
      ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
      btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
+     if (ret < 0)
+             return ret;
 
      /*
       * We may have copied an inline extent into a page of the destination
       * range, so wait for writeback to complete before truncating pages

s/truncating/invalidating/

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · line 763

Quoted 8 lines
fs/btrfs/reflink.c +4 -2
       * from the page cache. This is a rare case.
       */
-     wb_ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
-     ret = ret ? ret : wb_ret;
+     ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
+     if (ret < 0)
+             return ret;
+
Quoted 5 lines

Even if it's true, not worth doing in a fix like this, but a question:
I think buffered reads will check for outstanding ordered_extents and wait for them. If we are invalidating the cache next, then how can we read the file without seeing this ordered_extent? Is this wait_ordered_range still necessary?

The call to btrfs_wait_ordered_range() has nothing to do with the read path waiting or not for ordered extents.

The call is there to flush any dirty page due to copying an inline extent. Because any dirty page would cause the truncation to fail and run the risk of not truncating any pages after it (we can have regular extents following an inline extent for a few cases like compression and fallocate).

With invalidate it's not necessary as the rest of the range is not skipped. It can be removed now. Will do that separately.

Quoted 26 lines

Again, not a blocker for this patch.

Quoted 23 lines
fs/btrfs/reflink.c +6 -7
      /*
-      * Truncate page cache pages so that future reads will see the cloned
-      * data immediately and not the previous data.
+      * Invalidate page cache so that future reads will see the cloned data
+      * immediately and not the previous data.
       */
-     truncate_inode_pages_range(&inode->i_data,
-                             round_down(destoff, PAGE_SIZE),
-                             round_up(destoff + len, PAGE_SIZE) - 1);
+     ret = filemap_invalidate_inode(inode, false, destoff, end);
+     if (ret < 0)
+             return ret;
 
      btrfs_btree_balance_dirty(fs_info);
 
-     return ret;
+     return 0;
 }
 
 static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
--

2.47.2

#6 lore raw
Quoted 90 lines · Filipe Manana
Quoted 79 lines · Boris Burkov
Quoted 6 lines · On Thu, Jan 08, 2026 at 05:10:04PM +0000, [email protected] wrote:

From: Filipe Manana <[email protected]>

Qu reported that generic/164 often fails because the read operations get zeroes when it expects to either get all bytes with a value of 0x61 or 0x62. The issue stems from truncating the pages from the page cache instead of invalidating, as truncating can zero page contents.

Can you make it more clear if this is a subpage/large folios issue or a generic issue? Or maybe just explain the "can zero page contents" in a little more detail? I agree that it is the wrong "contract" so your change makes sense either way, this is just for future knowledge/archaeology.

The documentation of truncate_inode_pages_range only mentions zeroing partial pages when "lstart or lend + 1 is not page aligned" but our call is

        truncate_inode_pages_range(&inode->i_data,
                                round_down(destoff, PAGE_SIZE),
                                round_up(destoff + len, PAGE_SIZE) - 1);

which appears to align it?

Sorry if I am missing something obvious :)

Quoted 10 lines

So instead of truncating, invalidate the page cache range with a call to filemap_invalidate_inode(), which besides not doing any zeroing also ensures that while it's invalidating folios, no new folios are added. This helps ensure that buffered reads that happen while a reflink operation is in progress always get either the whole old data (the one before the reflink) or the whole new data, which is what generic/164 expects.

Quoted 18 lines
---
 fs/btrfs/reflink.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
fs/btrfs/reflink.c +0 -1
index e746980567da..f7ddd3765249 100644
@@ -705,7 +705,6 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
      struct inode *src = file_inode(file_src);
      struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
      int ret;
-     int wb_ret;
      u64 len = olen;
      u64 bs = fs_info->sectorsize;
      u64 end;

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · line 757

Quoted 9 lines
fs/btrfs/reflink.c +2 -0
@@ -750,25 +749,29 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
      btrfs_lock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
      ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
      btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
+     if (ret < 0)
+             return ret;
 
      /*
       * We may have copied an inline extent into a page of the destination
       * range, so wait for writeback to complete before truncating pages

s/truncating/invalidating/

fs/btrfs/reflink.c static noinline int btrfs_clone_files(struct file *file, struct file *file_src, · line 763

Quoted 8 lines
fs/btrfs/reflink.c +4 -2
       * from the page cache. This is a rare case.
       */
-     wb_ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
-     ret = ret ? ret : wb_ret;
+     ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
+     if (ret < 0)
+             return ret;
+
Quoted 5 lines

Even if it's true, not worth doing in a fix like this, but a question:
I think buffered reads will check for outstanding ordered_extents and wait for them. If we are invalidating the cache next, then how can we read the file without seeing this ordered_extent? Is this wait_ordered_range still necessary?

The call to btrfs_wait_ordered_range() has nothing to do with the read path waiting or not for ordered extents.

Quoted 8 lines

The call is there to flush any dirty page due to copying an inline extent. Because any dirty page would cause the truncation to fail and run the risk of not truncating any pages after it (we can have regular extents following an inline extent for a few cases like compression and fallocate).

With invalidate it's not necessary as the rest of the range is not skipped. It can be removed now. Will do that separately.

Nevermind, it's still needed.

Invalidate calls release folio (btrfs_release_folio()) which will fail if for example a folio is dirty, making the invalidation return -EBUSY (but still tries to invalidate all other folios in the range).

I've updated the comment here:

https://lore.kernel.org/linux-btrfs/[email protected]/

Quoted 27 lines
Quoted 27 lines

Again, not a blocker for this patch.

Quoted 23 lines
fs/btrfs/reflink.c +6 -7
      /*
-      * Truncate page cache pages so that future reads will see the cloned
-      * data immediately and not the previous data.
+      * Invalidate page cache so that future reads will see the cloned data
+      * immediately and not the previous data.
       */
-     truncate_inode_pages_range(&inode->i_data,
-                             round_down(destoff, PAGE_SIZE),
-                             round_up(destoff + len, PAGE_SIZE) - 1);
+     ret = filemap_invalidate_inode(inode, false, destoff, end);
+     if (ret < 0)
+             return ret;
 
      btrfs_btree_balance_dirty(fs_info);
 
-     return ret;
+     return 0;
 }
 
 static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,
--

2.47.2

Reviewed-by on v1 1/1
#7 lore raw
In reply to [email protected]
Quoted 15 lines · 在 2026/1/9 03:40, [email protected] 写道:

From: Filipe Manana <[email protected]>

Qu reported that generic/164 often fails because the read operations get zeroes when it expects to either get all bytes with a value of 0x61 or 0x62. The issue stems from truncating the pages from the page cache instead of invalidating, as truncating can zero page contents.

So instead of truncating, invalidate the page cache range with a call to filemap_invalidate_inode(), which besides not doing any zeroing also ensures that while it's invalidating folios, no new folios are added. This helps ensure that buffered reads that happen while a reflink operation is in progress always get either the whole old data (the one before the reflink) or the whole new data, which is what generic/164 expects.

Quoted 2 lines

Thanks,
Qu

Quoted 54 lines
---
  fs/btrfs/reflink.c | 21 ++++++++++++---------
  1 file changed, 12 insertions(+), 9 deletions(-)
fs/btrfs/reflink.c +12 -9
index e746980567da..f7ddd3765249 100644
@@ -705,7 +705,6 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
  	struct inode *src = file_inode(file_src);
  	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
  	int ret;
-	int wb_ret;
  	u64 len = olen;
  	u64 bs = fs_info->sectorsize;
  	u64 end;
@@ -750,25 +749,29 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
  	btrfs_lock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
  	ret = btrfs_clone(src, inode, off, olen, len, destoff, 0);
  	btrfs_unlock_extent(&BTRFS_I(inode)->io_tree, destoff, end, &cached_state);
+	if (ret < 0)
+		return ret;
 
  	/*
  	 * We may have copied an inline extent into a page of the destination
  	 * range, so wait for writeback to complete before truncating pages
  	 * from the page cache. This is a rare case.
  	 */
-	wb_ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
-	ret = ret ? ret : wb_ret;
+	ret = btrfs_wait_ordered_range(BTRFS_I(inode), destoff, len);
+	if (ret < 0)
+		return ret;
+
  	/*
-	 * Truncate page cache pages so that future reads will see the cloned
-	 * data immediately and not the previous data.
+	 * Invalidate page cache so that future reads will see the cloned data
+	 * immediately and not the previous data.
  	 */
-	truncate_inode_pages_range(&inode->i_data,
-				round_down(destoff, PAGE_SIZE),
-				round_up(destoff + len, PAGE_SIZE) - 1);
+	ret = filemap_invalidate_inode(inode, false, destoff, end);
+	if (ret < 0)
+		return ret;
 
  	btrfs_btree_balance_dirty(fs_info);
 
-	return ret;
+	return 0;
  }
 
  static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,