Linux CIFS filesystem development
 help / color / mirror / Atom feed
* [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write
@ 2026-09-21 23:07 Paulo Alcantara
  2026-09-21 23:07 ` [PATCH v2 2/5] smb: client: clear post-EOF pagecache when extending a file via truncate Paulo Alcantara
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: Paulo Alcantara @ 2026-09-21 23:07 UTC (permalink / raw)
  To: linux-cifs, netfs
  Cc: David Howells, Christian Brauner, Matthew Wilcox, Namjae Jeon,
	Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM, stable

Fix netfs to erase the contents of a hole created after the EOF by an
ordinary write if dirty data has been previously left there by writes
through an mmapped region.  Neither the buffered nor the unbuffered/DIO
write path clears that stale pagecache.

Zero the tail of the folio straddling the EOF before an extending
write.  That is the only folio that can hold data written past the EOF
through an mmap, as pages wholly beyond the EOF can't be faulted in.
The folio is zeroed rather than dropped so a concurrent extending write
can't lose data.

truncate_pagecache() can't be used here: it must be called with the
i_rwsem held exclusively, but these write paths only hold it shared,
and it would block unconditionally, breaking IOCB_NOWAIT.

Export the helper so filesystems can clear the same stale data from
their own truncate paths.

Closes: https://sashiko.dev/#/patchset/20260921154957.903891-1-pc%40manguebit.org
Fixes: 938e13a73b24 ("netfs: Implement buffered write API")
Fixes: 153a9961b551 ("netfs: Implement unbuffered/DIO write support")
Reviewed-by: David Howells <dhowells@redhat.com>
Signed-off-by: Paulo Alcantara <pc@manguebit.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Tom Talpey <tom@talpey.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Cc: stable@vger.kernel.org
---
 Documentation/filesystems/netfs_library.rst | 18 ++++++++
 fs/netfs/buffered_write.c                   |  9 ++++
 fs/netfs/direct_write.c                     |  9 ++++
 fs/netfs/misc.c                             | 51 +++++++++++++++++++++
 include/linux/netfs.h                       |  2 +
 5 files changed, 89 insertions(+)

diff --git a/Documentation/filesystems/netfs_library.rst b/Documentation/filesystems/netfs_library.rst
index ddd799df6ce3..58b0adacc2f3 100644
--- a/Documentation/filesystems/netfs_library.rst
+++ b/Documentation/filesystems/netfs_library.rst
@@ -451,6 +451,24 @@ one.
 The inode should be marked ``NETFS_ICTX_SINGLE_NO_UPLOAD`` if this API is to be
 used.  The writeback function requires the buffer to be of ITER_FOLIOQ type.
 
+Clearing Stale Post-EOF Pagecache
+---------------------------------
+
+When a file is extended, data left in the pagecache past the old EOF by a write
+through an mmap must not be exposed as file content.  Netfslib clears this on
+its own write paths, and exports a helper so a filesystem can do the same from,
+for instance, its truncate or fallocate paths::
+
+	int netfs_clear_stale_post_eof(struct inode *inode, unsigned long long from,
+				       unsigned long long to, bool nowait);
+
+This zeroes any such data within the ``[from, to)`` hole to be made, where @from
+is the current EOF.  Only the folio straddling @from can hold data written past
+the EOF through an mmap, as pages wholly beyond the EOF can't be faulted in, so
+the zeroing is limited to that folio and clamped to the top of the hole.  The
+folio is zeroed rather than dropped so that a concurrent extending write can't
+lose data.  If @nowait is set, it returns ``-EAGAIN`` rather than blocking.
+
 High-Level VM API
 ==================
 
diff --git a/fs/netfs/buffered_write.c b/fs/netfs/buffered_write.c
index 2cdb68e6b16f..cc0561f77bd0 100644
--- a/fs/netfs/buffered_write.c
+++ b/fs/netfs/buffered_write.c
@@ -469,6 +469,7 @@ ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *fr
 					 struct netfs_group *netfs_group)
 {
 	struct file *file = iocb->ki_filp;
+	struct inode *inode = file_inode(file);
 	ssize_t ret;
 
 	trace_netfs_write_iter(iocb, from);
@@ -481,6 +482,14 @@ ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *fr
 	if (ret)
 		return ret;
 
+	if (iocb->ki_pos > i_size_read(inode)) {
+		ret = netfs_clear_stale_post_eof(inode, i_size_read(inode),
+						 iocb->ki_pos,
+						 iocb->ki_flags & IOCB_NOWAIT);
+		if (ret)
+			return ret;
+	}
+
 	return netfs_perform_write(iocb, from, netfs_group);
 }
 EXPORT_SYMBOL(netfs_buffered_write_iter_locked);
diff --git a/fs/netfs/direct_write.c b/fs/netfs/direct_write.c
index 2361277416c7..36c36bc6c9d2 100644
--- a/fs/netfs/direct_write.c
+++ b/fs/netfs/direct_write.c
@@ -359,6 +359,15 @@ ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	ret = file_update_time(file);
 	if (ret < 0)
 		goto out;
+
+	if (iocb->ki_pos > i_size_read(inode)) {
+		ret = netfs_clear_stale_post_eof(inode, i_size_read(inode),
+						 iocb->ki_pos,
+						 iocb->ki_flags & IOCB_NOWAIT);
+		if (ret < 0)
+			goto out;
+	}
+
 	if (iocb->ki_flags & IOCB_NOWAIT) {
 		/* We could block if there are any pages in the range. */
 		ret = -EAGAIN;
diff --git a/fs/netfs/misc.c b/fs/netfs/misc.c
index f5c1c463f4ff..b9ca24b3dfda 100644
--- a/fs/netfs/misc.c
+++ b/fs/netfs/misc.c
@@ -6,6 +6,7 @@
  */
 
 #include <linux/swap.h>
+#include <linux/rmap.h>
 #include "internal.h"
 
 /**
@@ -582,3 +583,53 @@ void netfs_wait_for_put_ra_refs(struct netfs_io_request *rreq)
 	trace_netfs_rreq(rreq, netfs_rreq_trace_waited_put_ra_refs);
 	finish_wait(&rreq->waitq, &myself);
 }
+
+/**
+ * netfs_clear_stale_post_eof - Clear stale pagecache in a to-be-created hole
+ * @inode: The inode to act upon.
+ * @from: The base of the hole to be made (the current EOF).
+ * @to: The top of the hole to be made.
+ * @nowait: True to return -EAGAIN rather than block.
+ *
+ * Before extending a file, zero any data left in the pagecache within the
+ * [@from, @to) hole by a write through an mmap so that it isn't exposed as file
+ * content once the file is extended.  Only the uptodate folio straddling @from
+ * can hold such data as pages wholly beyond the EOF can't be faulted in, so the
+ * zeroing is limited to that folio.  The folio is zeroed rather than dropped so
+ * that a concurrent extending write can't lose data.
+ *
+ * Return: 0 on success, or -EAGAIN if @nowait is set and the folio would need
+ * to be waited on.
+ */
+int netfs_clear_stale_post_eof(struct inode *inode, unsigned long long from,
+			       unsigned long long to, bool nowait)
+{
+	struct address_space *mapping = inode->i_mapping;
+	fgf_t fgp = FGP_LOCK;
+	struct folio *folio;
+
+	if (from >= to)
+		return 0;
+
+	if (nowait)
+		fgp |= FGP_NOWAIT;
+
+	folio = __filemap_get_folio(mapping, from >> PAGE_SHIFT, fgp, 0);
+	if (IS_ERR(folio))
+		return PTR_ERR(folio) == -EAGAIN ? -EAGAIN : 0;
+
+	if (folio_mkclean(folio))
+		folio_mark_dirty(folio);
+
+	if (folio_test_uptodate(folio)) {
+		size_t end = umin(to - folio_pos(folio), folio_size(folio));
+		size_t offset = offset_in_folio(folio, from);
+
+		folio_zero_segment(folio, offset, end);
+	}
+
+	folio_unlock(folio);
+	folio_put(folio);
+	return 0;
+}
+EXPORT_SYMBOL(netfs_clear_stale_post_eof);
diff --git a/include/linux/netfs.h b/include/linux/netfs.h
index b4dd32863dd4..2981290b9a77 100644
--- a/include/linux/netfs.h
+++ b/include/linux/netfs.h
@@ -397,6 +397,8 @@ ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from);
 ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *iter,
 					   struct netfs_group *netfs_group);
 ssize_t netfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from);
+int netfs_clear_stale_post_eof(struct inode *inode, unsigned long long from,
+			       unsigned long long to, bool nowait);
 
 /* Single, monolithic object read/write API. */
 void netfs_single_mark_inode_dirty(struct inode *inode);
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 2/5] smb: client: clear post-EOF pagecache when extending a file via truncate
  2026-09-21 23:07 [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write Paulo Alcantara
@ 2026-09-21 23:07 ` Paulo Alcantara
  2026-09-21 23:07 ` [PATCH v2 3/5] smb: client: discard post-EOF pagecache when extending a file via zero range Paulo Alcantara
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Paulo Alcantara @ 2026-09-21 23:07 UTC (permalink / raw)
  To: linux-cifs, netfs
  Cc: David Howells, Christian Brauner, Namjae Jeon, Ronnie Sahlberg,
	Shyam Prasad N, Tom Talpey, Bharath SM, stable

cifs_setsize() relied on pagecache_isize_extended() to zero the tail of
the folio straddling the old EOF, but that helper is a no-op on CIFS
(i_blkbits is 14), so data dirtied past EOF through an mmap survived and
became visible once the file was extended.

When extending, zero the tail of the folio straddling the old EOF with
netfs_clear_stale_post_eof() instead, mirroring what
pagecache_isize_extended() does for filesystems with a sub-page block
size.  truncate_pagecache() is then called as in truncate_setsize(): it
is essentially a no-op on extend and drops the pagecache beyond the new
EOF on shrink.

Closes: https://sashiko.dev/#/patchset/20260921154957.903891-1-pc%40manguebit.org
Fixes: c510edb9734a ("cifs: call pagecache_isize_extended() in cifs_setsize() when extending")
Reviewed-by: David Howells <dhowells@redhat.com>
Signed-off-by: Paulo Alcantara <pc@manguebit.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Tom Talpey <tom@talpey.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Cc: stable@vger.kernel.org
---
 fs/smb/client/inode.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/fs/smb/client/inode.c b/fs/smb/client/inode.c
index 1fe0ef0a95db..9abb40f871c6 100644
--- a/fs/smb/client/inode.c
+++ b/fs/smb/client/inode.c
@@ -3071,8 +3071,16 @@ void cifs_setsize(struct inode *inode, loff_t offset)
 		inode->i_blocks = blocks;
 	spin_unlock(&inode->i_lock);
 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
+	/*
+	 * When extending, zero the tail of the folio straddling the old EOF
+	 * so that data dirtied past EOF through an mmap isn't exposed once the
+	 * file is extended.  This mirrors pagecache_isize_extended(), which
+	 * can't be used here as it is a no-op when the block size is >=
+	 * PAGE_SIZE, as on CIFS.  truncate_pagecache() then drops the
+	 * pagecache beyond the new EOF, as in truncate_setsize().
+	 */
 	if (offset > old_size)
-		pagecache_isize_extended(inode, old_size, offset);
+		netfs_clear_stale_post_eof(inode, old_size, offset, false);
 	truncate_pagecache(inode, offset);
 	netfs_wait_for_outstanding_io(inode);
 }
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 3/5] smb: client: discard post-EOF pagecache when extending a file via zero range
  2026-09-21 23:07 [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write Paulo Alcantara
  2026-09-21 23:07 ` [PATCH v2 2/5] smb: client: clear post-EOF pagecache when extending a file via truncate Paulo Alcantara
@ 2026-09-21 23:07 ` Paulo Alcantara
  2026-09-21 23:07 ` [PATCH v2 4/5] smb: client: discard post-EOF pagecache when extending a file via copy range Paulo Alcantara
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Paulo Alcantara @ 2026-09-21 23:07 UTC (permalink / raw)
  To: linux-cifs, netfs
  Cc: David Howells, Christian Brauner, Namjae Jeon, Ronnie Sahlberg,
	Shyam Prasad N, Tom Talpey, Bharath SM, stable

smb3_zero_range() removed the pagecache only for the range being zeroed,
so when it extends the file the folio straddling the old EOF - which may
hold data dirtied past EOF through an mmap - was never dropped and became
visible as file content once the file was extended.

Lower the discard boundary to the smaller of the zero-range offset and the
old EOF.

Fixes: 72c419d9b073 ("cifs: fix smb3_zero_range so it can expand the file-size when required")
Reviewed-by: David Howells <dhowells@redhat.com>
Signed-off-by: Paulo Alcantara <pc@manguebit.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Tom Talpey <tom@talpey.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Cc: stable@vger.kernel.org
---
 fs/smb/client/smb2ops.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/fs/smb/client/smb2ops.c b/fs/smb/client/smb2ops.c
index 3464470d3297..7282ed916470 100644
--- a/fs/smb/client/smb2ops.c
+++ b/fs/smb/client/smb2ops.c
@@ -3558,8 +3558,11 @@ static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
 	/*
 	 * We zero the range through ioctl, so we need remove the page caches
 	 * first, otherwise the data may be inconsistent with the server.
+	 *
+	 * Start at the old EOF when extending so the folio straddling it, which
+	 * may hold data written past EOF through an mmap, is dropped too.
 	 */
-	truncate_pagecache_range(inode, offset, offset + len - 1);
+	truncate_pagecache_range(inode, min(offset, i_size), offset + len - 1);
 	netfs_wait_for_outstanding_io(inode);
 
 	/* if file not oplocked can't be sure whether asking to extend size */
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 4/5] smb: client: discard post-EOF pagecache when extending a file via copy range
  2026-09-21 23:07 [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write Paulo Alcantara
  2026-09-21 23:07 ` [PATCH v2 2/5] smb: client: clear post-EOF pagecache when extending a file via truncate Paulo Alcantara
  2026-09-21 23:07 ` [PATCH v2 3/5] smb: client: discard post-EOF pagecache when extending a file via zero range Paulo Alcantara
@ 2026-09-21 23:07 ` Paulo Alcantara
  2026-09-21 23:07 ` [PATCH v2 5/5] smb: client: discard post-EOF pagecache when extending a file via clone range Paulo Alcantara
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Paulo Alcantara @ 2026-09-21 23:07 UTC (permalink / raw)
  To: linux-cifs, netfs
  Cc: David Howells, Christian Brauner, Namjae Jeon, Ronnie Sahlberg,
	Shyam Prasad N, Tom Talpey, Bharath SM, stable

cifs_file_copychunk_range() invalidated the pagecache only for the
destination region, so when a copy extends the file the folio straddling
the old EOF - which may hold data dirtied past EOF through an mmap - was
never dropped and became visible as file content once the file was
extended.

Start the invalidation at the smaller of the destination offset and the
old EOF.  filemap_invalidate_inode() writes back before invalidating
while i_size is still old, so nothing past the old EOF is flushed to the
server.

Fixes: 8101d6e112e2 ("cifs: Fix copy offload to flush destination region")
Reviewed-by: David Howells <dhowells@redhat.com>
Signed-off-by: Paulo Alcantara <pc@manguebit.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Tom Talpey <tom@talpey.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Cc: stable@vger.kernel.org
---
 fs/smb/client/cifsfs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/fs/smb/client/cifsfs.c b/fs/smb/client/cifsfs.c
index 7ecd70efdfea..f1fb4b7b7b7c 100644
--- a/fs/smb/client/cifsfs.c
+++ b/fs/smb/client/cifsfs.c
@@ -1581,8 +1581,13 @@ ssize_t cifs_file_copychunk_range(unsigned int xid,
 	/* Flush and invalidate all the folios in the destination region.  If
 	 * the copy was successful, then some of the flush is extra overhead,
 	 * but we need to allow for the copy failing in some way (eg. ENOSPC).
+	 *
+	 * Start at the old EOF when extending so the folio straddling it, which
+	 * may hold data written past EOF through an mmap, is dropped too.
 	 */
-	rc = filemap_invalidate_inode(target_inode, true, destoff, destoff + len - 1);
+	rc = filemap_invalidate_inode(target_inode, true,
+				      min(destoff, i_size_read(target_inode)),
+				      destoff + len - 1);
 	if (rc)
 		goto unlock;
 
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 5/5] smb: client: discard post-EOF pagecache when extending a file via clone range
  2026-09-21 23:07 [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write Paulo Alcantara
                   ` (2 preceding siblings ...)
  2026-09-21 23:07 ` [PATCH v2 4/5] smb: client: discard post-EOF pagecache when extending a file via copy range Paulo Alcantara
@ 2026-09-21 23:07 ` Paulo Alcantara
  2026-09-22  0:08 ` [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write Paulo Alcantara
  2026-09-22  1:08 ` Matthew Wilcox
  5 siblings, 0 replies; 11+ messages in thread
From: Paulo Alcantara @ 2026-09-21 23:07 UTC (permalink / raw)
  To: linux-cifs, netfs
  Cc: David Howells, Christian Brauner, Namjae Jeon, Ronnie Sahlberg,
	Shyam Prasad N, Tom Talpey, Bharath SM, stable

cifs_remap_file_range() discarded the pagecache only for the folios
overlapping the destination region, so when a clone extends the file the
folio straddling the old EOF - which may hold data dirtied past EOF
through an mmap - was never dropped and became visible as file content
once the file was extended.

Start the discard at the smaller of fstart and the old EOF.  i_size is
still old here, so nothing past the old EOF is flushed to the server.

Fixes: c54fc3a4f375 ("cifs: Fix flushing, invalidation and file size with FICLONE")
Reviewed-by: David Howells <dhowells@redhat.com>
Signed-off-by: Paulo Alcantara <pc@manguebit.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Namjae Jeon <linkinjeon@kernel.org>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Cc: Shyam Prasad N <sprasad@microsoft.com>
Cc: Tom Talpey <tom@talpey.com>
Cc: Bharath SM <bharathsm@microsoft.com>
Cc: stable@vger.kernel.org
---
 fs/smb/client/cifsfs.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/fs/smb/client/cifsfs.c b/fs/smb/client/cifsfs.c
index f1fb4b7b7b7c..6410249f529a 100644
--- a/fs/smb/client/cifsfs.c
+++ b/fs/smb/client/cifsfs.c
@@ -1467,9 +1467,14 @@ static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
 	i_size = target_inode->i_size;
 	spin_unlock(&target_inode->i_lock);
 
-	/* Discard all the folios that overlap the destination region. */
+	/*
+	 * Discard all the folios that overlap the destination region.  Start at
+	 * the old EOF when extending so the folio straddling it, which may hold
+	 * data written past EOF through an mmap, is dropped too.
+	 */
 	cifs_dbg(FYI, "about to discard pages %llx-%llx\n", fstart, fend);
-	truncate_inode_pages_range(&target_inode->i_data, fstart, fend);
+	truncate_inode_pages_range(&target_inode->i_data,
+				   min(fstart, i_size), fend);
 
 	fscache_invalidate(cifs_inode_cookie(target_inode), NULL, i_size, 0);
 
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write
  2026-09-21 23:07 [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write Paulo Alcantara
                   ` (3 preceding siblings ...)
  2026-09-21 23:07 ` [PATCH v2 5/5] smb: client: discard post-EOF pagecache when extending a file via clone range Paulo Alcantara
@ 2026-09-22  0:08 ` Paulo Alcantara
  2026-09-22  1:08 ` Matthew Wilcox
  5 siblings, 0 replies; 11+ messages in thread
From: Paulo Alcantara @ 2026-09-22  0:08 UTC (permalink / raw)
  To: linux-cifs, netfs
  Cc: David Howells, Christian Brauner, Matthew Wilcox, Namjae Jeon,
	Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM, stable

Paulo Alcantara <pc@manguebit.org> writes:

> Fix netfs to erase the contents of a hole created after the EOF by an
> ordinary write if dirty data has been previously left there by writes
> through an mmapped region.  Neither the buffered nor the unbuffered/DIO
> write path clears that stale pagecache.
>
> Zero the tail of the folio straddling the EOF before an extending
> write.  That is the only folio that can hold data written past the EOF
> through an mmap, as pages wholly beyond the EOF can't be faulted in.
> The folio is zeroed rather than dropped so a concurrent extending write
> can't lose data.
>
> truncate_pagecache() can't be used here: it must be called with the
> i_rwsem held exclusively, but these write paths only hold it shared,
> and it would block unconditionally, breaking IOCB_NOWAIT.
>
> Export the helper so filesystems can clear the same stale data from
> their own truncate paths.
>
> Closes: https://sashiko.dev/#/patchset/20260921154957.903891-1-pc%40manguebit.org
> Fixes: 938e13a73b24 ("netfs: Implement buffered write API")
> Fixes: 153a9961b551 ("netfs: Implement unbuffered/DIO write support")
> Reviewed-by: David Howells <dhowells@redhat.com>
> Signed-off-by: Paulo Alcantara <pc@manguebit.org>
> Cc: Christian Brauner <brauner@kernel.org>
> Cc: Matthew Wilcox <willy@infradead.org>
> Cc: Namjae Jeon <linkinjeon@kernel.org>
> Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>
> Cc: Shyam Prasad N <sprasad@microsoft.com>
> Cc: Tom Talpey <tom@talpey.com>
> Cc: Bharath SM <bharathsm@microsoft.com>
> Cc: stable@vger.kernel.org
> ---
>  Documentation/filesystems/netfs_library.rst | 18 ++++++++
>  fs/netfs/buffered_write.c                   |  9 ++++
>  fs/netfs/direct_write.c                     |  9 ++++
>  fs/netfs/misc.c                             | 51 +++++++++++++++++++++
>  include/linux/netfs.h                       |  2 +
>  5 files changed, 89 insertions(+)

Will address more Sashiko comments in next version.  Please ignore this
one.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write
  2026-09-21 23:07 [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write Paulo Alcantara
                   ` (4 preceding siblings ...)
  2026-09-22  0:08 ` [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write Paulo Alcantara
@ 2026-09-22  1:08 ` Matthew Wilcox
  2026-09-23 19:20   ` David Howells
  2026-09-23 23:46   ` Paulo Alcantara
  5 siblings, 2 replies; 11+ messages in thread
From: Matthew Wilcox @ 2026-09-22  1:08 UTC (permalink / raw)
  To: Paulo Alcantara
  Cc: linux-cifs, netfs, David Howells, Christian Brauner, Namjae Jeon,
	Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM, stable

On Mon, Sep 21, 2026 at 08:07:51PM -0300, Paulo Alcantara wrote:
> +	int netfs_clear_stale_post_eof(struct inode *inode, unsigned long long from,
> +				       unsigned long long to, bool nowait);

Should this be uoff_t?  Or even loff_t?

> +int netfs_clear_stale_post_eof(struct inode *inode, unsigned long long from,
> +			       unsigned long long to, bool nowait)
> +{
> +	struct address_space *mapping = inode->i_mapping;
> +	fgf_t fgp = FGP_LOCK;
> +	struct folio *folio;
> +
> +	if (from >= to)
> +		return 0;
> +
> +	if (nowait)
> +		fgp |= FGP_NOWAIT;
> +
> +	folio = __filemap_get_folio(mapping, from >> PAGE_SHIFT, fgp, 0);
> +	if (IS_ERR(folio))
> +		return PTR_ERR(folio) == -EAGAIN ? -EAGAIN : 0;
> +
> +	if (folio_mkclean(folio))
> +		folio_mark_dirty(folio);
> +
> +	if (folio_test_uptodate(folio)) {
> +		size_t end = umin(to - folio_pos(folio), folio_size(folio));
> +		size_t offset = offset_in_folio(folio, from);
> +
> +		folio_zero_segment(folio, offset, end);
> +	}
> +
> +	folio_unlock(folio);
> +	folio_put(folio);
> +	return 0;
> +}
> +EXPORT_SYMBOL(netfs_clear_stale_post_eof);

This looks suspiciously similar to pagecache_isize_extended() but does
not mention it.  So any updates to that function will probably imss this
one.  That's not great.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write
  2026-09-22  1:08 ` Matthew Wilcox
@ 2026-09-23 19:20   ` David Howells
  2026-09-23 23:46   ` Paulo Alcantara
  1 sibling, 0 replies; 11+ messages in thread
From: David Howells @ 2026-09-23 19:20 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: dhowells, Paulo Alcantara, linux-cifs, netfs, Christian Brauner,
	Namjae Jeon, Ronnie Sahlberg, Shyam Prasad N, Tom Talpey,
	Bharath SM, stable

Matthew Wilcox <willy@infradead.org> wrote:

> > +	int netfs_clear_stale_post_eof(struct inode *inode, unsigned long long from,
> > +				       unsigned long long to, bool nowait);
> 
> Should this be uoff_t?  Or even loff_t?

uoff_t please.  There's no reason to have a signed value there.  Also, I have
a patch queued with Christian for next to convert netfs generally to use
uoff_t where it can.  It's a lot shorter than unsigned long long.

David


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write
  2026-09-22  1:08 ` Matthew Wilcox
  2026-09-23 19:20   ` David Howells
@ 2026-09-23 23:46   ` Paulo Alcantara
  2026-09-24  4:16     ` Matthew Wilcox
  1 sibling, 1 reply; 11+ messages in thread
From: Paulo Alcantara @ 2026-09-23 23:46 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: linux-cifs, netfs, David Howells, Christian Brauner, Namjae Jeon,
	Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM, stable

Matthew Wilcox <willy@infradead.org> writes:

> On Mon, Sep 21, 2026 at 08:07:51PM -0300, Paulo Alcantara wrote:
>> +	int netfs_clear_stale_post_eof(struct inode *inode, unsigned long long from,
>> +				       unsigned long long to, bool nowait);
>
> Should this be uoff_t?  Or even loff_t?

uoff_t.

>
>> +int netfs_clear_stale_post_eof(struct inode *inode, unsigned long long from,
>> +			       unsigned long long to, bool nowait)
>> +{
>> +	struct address_space *mapping = inode->i_mapping;
>> +	fgf_t fgp = FGP_LOCK;
>> +	struct folio *folio;
>> +
>> +	if (from >= to)
>> +		return 0;
>> +
>> +	if (nowait)
>> +		fgp |= FGP_NOWAIT;
>> +
>> +	folio = __filemap_get_folio(mapping, from >> PAGE_SHIFT, fgp, 0);
>> +	if (IS_ERR(folio))
>> +		return PTR_ERR(folio) == -EAGAIN ? -EAGAIN : 0;
>> +
>> +	if (folio_mkclean(folio))
>> +		folio_mark_dirty(folio);
>> +
>> +	if (folio_test_uptodate(folio)) {
>> +		size_t end = umin(to - folio_pos(folio), folio_size(folio));
>> +		size_t offset = offset_in_folio(folio, from);
>> +
>> +		folio_zero_segment(folio, offset, end);
>> +	}
>> +
>> +	folio_unlock(folio);
>> +	folio_put(folio);
>> +	return 0;
>> +}
>> +EXPORT_SYMBOL(netfs_clear_stale_post_eof);
>
> This looks suspiciously similar to pagecache_isize_extended() but does
> not mention it.  So any updates to that function will probably imss this
> one.  That's not great.

True.  Sorry about that.

There are some reasons that I ended up with our own helper:

- The block size checks make no sense for CIFS, as reads and writes will
  be byte aligned.  For instance, CIFS and AFS set block size to >=
  PAGE_SIZE, therefore calling pagecache_isize_extended() is a no-op for
  them.

- netfslib needs to honour NOWAIT, which pagecache_isize_extended()
  can't do due to filemap_lock_folio() and folio_mkclean().  Besides, we
  would also need to return -EAGAIN in case the folio is mapped or under
  writeback.

- netfs_clear_stale_post_eof() will be called before i_size is updated,
  so we re-check i_size after acquiring the folio lock.  It runs with
  i_rwsem held shared in both the buffered and unbuffered/DIO paths, so
  extending writers might concurrently change i_size while we run.
  pagecache_isize_extended() can't also be used as it requires to be
  called after i_size is updated, therefore hitting WARN_ON(to >
  inode->i_size).

NFS also has its own helper (nfs_truncate_last_folio), but couldn't
re-use it due to above reasons.

Please let me know if you have any ideas.

This is what I currently have for new helper (trying to address some
issues found out by sashiko as well):

int netfs_clear_stale_post_eof(struct inode *inode,
			       uoff_t from, uoff_t to, bool nowait)
{
	struct address_space *mapping = inode->i_mapping;
	fgf_t fgp = FGP_LOCK;
	struct folio *folio;
	int ret;

	if (from >= to)
		return 0;

	if (nowait)
		fgp |= FGP_NOWAIT;

	folio = __filemap_get_folio(mapping, from >> PAGE_SHIFT, fgp, 0);
	if (IS_ERR(folio))
		return PTR_ERR(folio) == -EAGAIN ? -EAGAIN : 0;

	ret = 0;
	if (nowait && (folio_mapped(folio) || folio_test_writeback(folio))) {
		ret = -EAGAIN;
		goto out;
	}

	folio_wait_writeback(folio);

	if (folio_mkclean(folio))
		folio_mark_dirty(folio);

	if (folio_test_uptodate(folio)) {
		uoff_t fpos = folio_pos(folio);

		from = umax(from, i_size_read(inode));
		if (from < to && from < fpos + folio_size(folio)) {
			size_t end = umin(to - fpos, folio_size(folio));
			size_t offset = from - fpos;

			folio_zero_segment(folio, offset, end);
		}
	}
out:
	folio_unlock(folio);
	folio_put(folio);
	return ret;
}

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write
  2026-09-23 23:46   ` Paulo Alcantara
@ 2026-09-24  4:16     ` Matthew Wilcox
  2026-09-24 13:43       ` Paulo Alcantara
  0 siblings, 1 reply; 11+ messages in thread
From: Matthew Wilcox @ 2026-09-24  4:16 UTC (permalink / raw)
  To: Paulo Alcantara
  Cc: linux-cifs, netfs, David Howells, Christian Brauner, Namjae Jeon,
	Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM, stable

On Wed, Sep 23, 2026 at 08:46:35PM -0300, Paulo Alcantara wrote:
> - netfslib needs to honour NOWAIT, which pagecache_isize_extended()
>   can't do due to filemap_lock_folio() and folio_mkclean().  Besides, we
>   would also need to return -EAGAIN in case the folio is mapped or under
>   writeback.

btw, if IOCB_NOWAIT is important to you, you need to be calling
kiocb_modified() instead of file_remove_privs() and file_update_time()
in netfs_buffered_write_iter_locked().

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write
  2026-09-24  4:16     ` Matthew Wilcox
@ 2026-09-24 13:43       ` Paulo Alcantara
  0 siblings, 0 replies; 11+ messages in thread
From: Paulo Alcantara @ 2026-09-24 13:43 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: linux-cifs, netfs, David Howells, Christian Brauner, Namjae Jeon,
	Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM, stable

Matthew Wilcox <willy@infradead.org> writes:

> On Wed, Sep 23, 2026 at 08:46:35PM -0300, Paulo Alcantara wrote:
>> - netfslib needs to honour NOWAIT, which pagecache_isize_extended()
>>   can't do due to filemap_lock_folio() and folio_mkclean().  Besides, we
>>   would also need to return -EAGAIN in case the folio is mapped or under
>>   writeback.
>
> btw, if IOCB_NOWAIT is important to you, you need to be calling
> kiocb_modified() instead of file_remove_privs() and file_update_time()
> in netfs_buffered_write_iter_locked().

Yes, you're right.  My life was so much easier when I initially started
writing the fixes without having to care about IOCB_NOWAIT as I'd
noticed that netfs wasn't actually handling it correctly in some places,
then I thought: let's not make it any worse, handle it correctly in the
new helper at least and then send other patches later to fix the other
parts.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-09-24 13:43 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-21 23:07 [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write Paulo Alcantara
2026-09-21 23:07 ` [PATCH v2 2/5] smb: client: clear post-EOF pagecache when extending a file via truncate Paulo Alcantara
2026-09-21 23:07 ` [PATCH v2 3/5] smb: client: discard post-EOF pagecache when extending a file via zero range Paulo Alcantara
2026-09-21 23:07 ` [PATCH v2 4/5] smb: client: discard post-EOF pagecache when extending a file via copy range Paulo Alcantara
2026-09-21 23:07 ` [PATCH v2 5/5] smb: client: discard post-EOF pagecache when extending a file via clone range Paulo Alcantara
2026-09-22  0:08 ` [PATCH v2 1/5] netfs: clear post-EOF pagecache when extending a file via write Paulo Alcantara
2026-09-22  1:08 ` Matthew Wilcox
2026-09-23 19:20   ` David Howells
2026-09-23 23:46   ` Paulo Alcantara
2026-09-24  4:16     ` Matthew Wilcox
2026-09-24 13:43       ` Paulo Alcantara

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox