[PATCH 2/2] NFS: Tie the netfs reference to the pageio header
From: Josef Bacik
Date: Fri Dec 04 2026 - 17:11:15 EST
With fscache, a netfs subrequest is finished when the last reference to
its nfs_netfs_io_data is dropped. A pageio header takes a reference in
nfs_initiate_read(), when its RPC is set up, and drops it at the end of
nfs_read_completion(). The two do not always go together:
- A header with NFS_IOHDR_REDO leaves nfs_read_completion() at once.
If a data server has failed the READ, and the files or flexfiles
driver has sent the requests again by way of other headers, the
reference of the first header is never dropped. The subrequest never
ends and the folios stay locked: the reader hangs, and so does
everybody else who wants these pages.
- The block and SCSI layout drivers do the I/O without an RPC: no
reference is taken, but one is dropped. The subrequest can finish
while the I/O is in flight, and whichever of nfs_netfs_issue_read()
and the completion comes second uses a freed nfs_netfs_io_data.
Take the reference where the header gets the pointer, in
nfs_pgheader_init(), and drop it in every completion. A header that is
redone adds nothing to the result: its count, EOF flag and error belong
to the attempt that was given up.
If sending the requests again fails, whoever sends them passes the error
to netfs: nfs_pageio_resend(), or nfs_netfs_issue_read() if they went
back to its own descriptor. The header cannot carry it: when the
failure comes in nfs_pageio_complete(), only pg_error holds it and
nfs_pageio_resend() returns 0. Without the error, the EOF flag of
another header would make netfs clear the tail and mark folios up to
date that were never read.
A redone read now comes to an end, and its replies can be short, so this
needs "NFS: Report all of a read done in parts to netfs" with it.
Fixes: 000dbe0bec05 ("NFS: Convert buffered read paths to use netfs when fscache is enabled")
Signed-off-by: Josef Bacik <josef@xxxxxxxx>
---
fs/nfs/fscache.c | 21 +++++++++++----------
fs/nfs/fscache.h | 14 ++++++++++++--
fs/nfs/pagelist.c | 2 ++
fs/nfs/read.c | 4 +---
4 files changed, 26 insertions(+), 15 deletions(-)
diff --git a/fs/nfs/fscache.c b/fs/nfs/fscache.c
index 8108e53addb8f..a2ca3e00616e7 100644
--- a/fs/nfs/fscache.c
+++ b/fs/nfs/fscache.c
@@ -331,19 +331,11 @@ static void nfs_netfs_issue_read(struct netfs_io_subrequest *sreq)
}
out:
nfs_pageio_complete_read(&pgio);
+ if (pgio.pg_error < 0)
+ netfs->error = pgio.pg_error;
nfs_netfs_put(netfs);
}
-void nfs_netfs_initiate_read(struct nfs_pgio_header *hdr)
-{
- struct nfs_netfs_io_data *netfs = hdr->netfs;
-
- if (!netfs)
- return;
-
- nfs_netfs_get(netfs);
-}
-
int nfs_netfs_folio_unlock(struct folio *folio)
{
struct inode *inode = folio->mapping->host;
@@ -365,6 +357,14 @@ void nfs_netfs_read_completion(struct nfs_pgio_header *hdr)
if (!netfs)
return;
+ /*
+ * A redone header gave up its requests, so its count, EOF flag and
+ * error say nothing about them. If sending them again fails, netfs
+ * hears of it from nfs_pageio_resend() or nfs_netfs_issue_read().
+ */
+ if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
+ goto out;
+
sreq = netfs->sreq;
if (test_bit(NFS_IOHDR_EOF, &hdr->flags) &&
sreq->rreq->origin != NETFS_UNBUFFERED_READ &&
@@ -376,6 +376,7 @@ void nfs_netfs_read_completion(struct nfs_pgio_header *hdr)
else
atomic64_add(hdr->good_bytes, &netfs->transferred);
+out:
nfs_netfs_put(netfs);
hdr->netfs = NULL;
}
diff --git a/fs/nfs/fscache.h b/fs/nfs/fscache.h
index 9d86868f49984..a50a3cecb578b 100644
--- a/fs/nfs/fscache.h
+++ b/fs/nfs/fscache.h
@@ -82,7 +82,6 @@ static inline void nfs_netfs_inode_init(struct nfs_inode *nfsi)
{
netfs_inode_init(&nfsi->netfs, &nfs_netfs_ops, false);
}
-extern void nfs_netfs_initiate_read(struct nfs_pgio_header *hdr);
extern void nfs_netfs_read_completion(struct nfs_pgio_header *hdr);
extern int nfs_netfs_folio_unlock(struct folio *folio);
@@ -149,6 +148,9 @@ static inline void nfs_netfs_set_pgio_header(struct nfs_pgio_header *hdr,
struct nfs_pageio_descriptor *desc)
{
hdr->netfs = desc->pg_netfs;
+ /* Dropped by nfs_netfs_read_completion() */
+ if (hdr->netfs)
+ nfs_netfs_get(hdr->netfs);
}
static inline void nfs_netfs_set_pageio_descriptor(struct nfs_pageio_descriptor *desc,
struct nfs_pgio_header *hdr)
@@ -159,9 +161,16 @@ static inline void nfs_netfs_reset_pageio_descriptor(struct nfs_pageio_descripto
{
desc->pg_netfs = NULL;
}
+
+static inline void nfs_netfs_set_error(struct nfs_pgio_header *hdr, int error)
+{
+ struct nfs_netfs_io_data *netfs = hdr->netfs;
+
+ if (netfs && error < 0)
+ netfs->error = error;
+}
#else /* CONFIG_NFS_FSCACHE */
static inline void nfs_netfs_inode_init(struct nfs_inode *nfsi) {}
-static inline void nfs_netfs_initiate_read(struct nfs_pgio_header *hdr) {}
static inline void nfs_netfs_read_completion(struct nfs_pgio_header *hdr) {}
static inline int nfs_netfs_folio_unlock(struct folio *folio)
{
@@ -198,5 +207,6 @@ static inline void nfs_netfs_set_pgio_header(struct nfs_pgio_header *hdr,
static inline void nfs_netfs_set_pageio_descriptor(struct nfs_pageio_descriptor *desc,
struct nfs_pgio_header *hdr) {}
static inline void nfs_netfs_reset_pageio_descriptor(struct nfs_pageio_descriptor *desc) {}
+static inline void nfs_netfs_set_error(struct nfs_pgio_header *hdr, int error) {}
#endif /* CONFIG_NFS_FSCACHE */
#endif /* _NFS_FSCACHE_H */
diff --git a/fs/nfs/pagelist.c b/fs/nfs/pagelist.c
index 71f0ce2bc4eab..f7e7eef0fdff6 100644
--- a/fs/nfs/pagelist.c
+++ b/fs/nfs/pagelist.c
@@ -1438,8 +1438,10 @@ int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
break;
}
nfs_pageio_complete(desc);
+ nfs_netfs_set_error(hdr, desc->pg_error);
if (!list_empty(&pages)) {
int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
+ nfs_netfs_set_error(hdr, err);
hdr->completion_ops->error_cleanup(&pages, err);
nfs_set_pgio_error(hdr, err, hdr->io_start);
return err;
diff --git a/fs/nfs/read.c b/fs/nfs/read.c
index e7497b029d6ce..a79941e9a463c 100644
--- a/fs/nfs/read.c
+++ b/fs/nfs/read.c
@@ -203,9 +203,8 @@ static void nfs_read_completion(struct nfs_pgio_header *hdr)
nfs_list_remove_request(req);
nfs_readpage_release(req, error);
}
- nfs_netfs_read_completion(hdr);
-
out:
+ nfs_netfs_read_completion(hdr);
hdr->release(hdr);
}
@@ -215,7 +214,6 @@ static void nfs_initiate_read(struct nfs_pgio_header *hdr,
struct rpc_task_setup *task_setup_data, int how)
{
rpc_ops->read_setup(hdr, msg);
- nfs_netfs_initiate_read(hdr);
trace_nfs_initiate_read(hdr);
}