Skip to contentSkip to conversation

Initial NFS client support for RWF_DONTCACHE

PATCH Superseded Trond Myklebust 1 participant 3 messages Aug 12, 2025

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Trond Myklebust

author maintainer
#0 lore raw

v2 cover Initial NFS client support for RWF_DONTCACHE

From: Trond Myklebust <[email protected]>

The following patch set attempts to add support for the RWF_DONTCACHE flag in preadv2() and pwritev2() on NFS filesystems.

The main issue is allowing support on 2 stage writes (i.e. unstable WRITE followed by a COMMIT) since those don't follow the current assumption that the 'dropbehind' flag can be fulfilled as soon as the writeback lock is dropped.

v2:
- Make use of the new iocb parameter for nfs_write_begin()

Trond Myklebust (2):
filemap: Add a helper for filesystems implementing dropbehind
NFS: Enable the RWF_DONTCACHE flag for the NFS client

 fs/nfs/file.c            |  7 +++----
 fs/nfs/nfs4file.c        |  2 ++
 fs/nfs/write.c           | 12 +++++++++++-
 include/linux/nfs_page.h |  1 +
 include/linux/pagemap.h  |  1 +
 mm/filemap.c             | 16 ++++++++++++++++
 6 files changed, 34 insertions(+), 5 deletions(-)
Signature · 1 line
2.50.1
author maintainer
#1 lore raw

PATCH v2 1/2 filemap: Add a helper for filesystems implementing dropbehind

From: Trond Myklebust <[email protected]>

Add a helper to allow filesystems to attempt to free the 'dropbehind' folio.

---
 include/linux/pagemap.h |  1 +
 mm/filemap.c            | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)
include/linux/pagemap.h +1 -0
index 12a12dae727d..201b7c6f6441 100644
@@ -1221,6 +1221,7 @@ void folio_wait_writeback(struct folio *folio);
 int folio_wait_writeback_killable(struct folio *folio);
 void end_page_writeback(struct page *page);
 void folio_end_writeback(struct folio *folio);
+void folio_end_dropbehind(struct folio *folio);
 void folio_wait_stable(struct folio *folio);
 void __folio_mark_dirty(struct folio *folio, struct address_space *, int warn);
 void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb);
mm/filemap.c +16 -0
index 751838ef05e5..9878ab702e54 100644
@@ -1603,6 +1603,22 @@ static void filemap_end_dropbehind(struct folio *folio)
 		folio_unmap_invalidate(mapping, folio, 0);
 }
 
+/*
+ * Helper for filesystems that want to implement dropbehind, and that
+ * need to keep the folio around after folio_end_writeback, e.g. due to
+ * the need to first commit NFS stable writes.
+ */
+void folio_end_dropbehind(struct folio *folio)
+{
+	if (folio_trylock(folio)) {
+		if (folio->mapping && !folio_test_dirty(folio) &&
+		    !folio_test_writeback(folio))
+			folio_unmap_invalidate(folio->mapping, folio, 0);
+		folio_unlock(folio);
+	}
+}
+EXPORT_SYMBOL(folio_end_dropbehind);
+
 /*
  * If folio was marked as dropbehind, then pages should be dropped when writeback
  * completes. Do that now. If we fail, it's likely because of a big folio -
Signature · 1 line
2.50.1
author maintainer
#2 lore raw

PATCH v2 2/2 NFS: Enable the RWF_DONTCACHE flag for the NFS client

In reply to Trond Myklebust

From: Trond Myklebust <[email protected]>

While the NFS readahead code has no problems using the generic code to manage the dropbehind behaviour enabled by RWF_DONTCACHE, the write code needs to deal with the fact that NFS writeback uses a 2 step process (UNSTABLE write followed by COMMIT).
This commit replaces the use of the folio dropbehind flag with a local NFS request flag that triggers the dropbehind behaviour once the data has been written to stable storage.

---
 fs/nfs/file.c            |  7 +++----
 fs/nfs/nfs4file.c        |  2 ++
 fs/nfs/write.c           | 12 +++++++++++-
 include/linux/nfs_page.h |  1 +
 4 files changed, 17 insertions(+), 5 deletions(-)
fs/nfs/file.c +3 -4
index 86e36c630f09..fffb536bfcdd 100644
@@ -348,7 +348,6 @@ static int nfs_write_begin(const struct kiocb *iocb,
 			   loff_t pos, unsigned len, struct folio **foliop,
 			   void **fsdata)
 {
-	fgf_t fgp = FGP_WRITEBEGIN;
 	struct folio *folio;
 	struct file *file = iocb->ki_filp;
 	int once_thru = 0;
@@ -357,10 +356,8 @@ static int nfs_write_begin(const struct kiocb *iocb,
 	dfprintk(PAGECACHE, "NFS: write_begin(%pD2(%lu), %u@%lld)\n",
 		file, mapping->host->i_ino, len, (long long) pos);
 
-	fgp |= fgf_set_order(len);
 start:
-	folio = __filemap_get_folio(mapping, pos >> PAGE_SHIFT, fgp,
-				    mapping_gfp_mask(mapping));
+	folio = write_begin_get_folio(iocb, mapping, pos >> PAGE_SHIFT, len);
 	if (IS_ERR(folio))
 		return PTR_ERR(folio);
 	*foliop = folio;
@@ -915,5 +912,7 @@ const struct file_operations nfs_file_operations = {
 	.splice_write	= iter_file_splice_write,
 	.check_flags	= nfs_check_flags,
 	.setlease	= simple_nosetlease,
+
+	.fop_flags	= FOP_DONTCACHE,
 };
 EXPORT_SYMBOL_GPL(nfs_file_operations);
fs/nfs/nfs4file.c +2 -0
index 1d6b5f4230c9..70f6887ded0e 100644
@@ -454,4 +454,6 @@ const struct file_operations nfs4_file_operations = {
 #else
 	.llseek		= nfs_file_llseek,
 #endif
+
+	.fop_flags	= FOP_DONTCACHE,
 };
fs/nfs/write.c +11 -1
index fa5c41d0989a..e6b1f69058cf 100644
@@ -359,8 +359,12 @@ static void nfs_folio_end_writeback(struct folio *folio)
 static void nfs_page_end_writeback(struct nfs_page *req)
 {
 	if (nfs_page_group_sync_on_bit(req, PG_WB_END)) {
+		struct folio *folio = nfs_page_to_folio(req);
+
+		if (folio_test_clear_dropbehind(folio))
+			set_bit(PG_DROPBEHIND, &req->wb_flags);
 		nfs_unlock_request(req);
-		nfs_folio_end_writeback(nfs_page_to_folio(req));
+		nfs_folio_end_writeback(folio);
 	} else
 		nfs_unlock_request(req);
 }
@@ -797,6 +801,9 @@ static void nfs_inode_remove_request(struct nfs_page *req)
 			clear_bit(PG_MAPPED, &req->wb_head->wb_flags);
 		}
 		spin_unlock(&mapping->i_private_lock);
+
+		if (test_bit(PG_DROPBEHIND, &req->wb_flags))
+			folio_end_dropbehind(folio);
 	}
 
 	if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) {
@@ -2077,6 +2084,7 @@ int nfs_wb_folio(struct inode *inode, struct folio *folio)
 		.range_start = range_start,
 		.range_end = range_start + len - 1,
 	};
+	bool dropbehind = folio_test_clear_dropbehind(folio);
 	int ret;
 
 	trace_nfs_writeback_folio(inode, range_start, len);
@@ -2097,6 +2105,8 @@ int nfs_wb_folio(struct inode *inode, struct folio *folio)
 			goto out_error;
 	}
 out_error:
+	if (dropbehind)
+		folio_set_dropbehind(folio);
 	trace_nfs_writeback_folio_done(inode, range_start, len, ret);
 	return ret;
 }
include/linux/nfs_page.h +1 -0
index 169b4ae30ff4..1a017b5b476f 100644
@@ -37,6 +37,7 @@ enum {
 	PG_REMOVE,		/* page group sync bit in write path */
 	PG_CONTENDED1,		/* Is someone waiting for a lock? */
 	PG_CONTENDED2,		/* Is someone waiting for a lock? */
+	PG_DROPBEHIND,		/* Implement RWF_DONTCACHE */
 };
 
 struct nfs_inode;
Signature · 1 line
2.50.1