* [PATCH v4 01/11] NFS: resend O_DIRECT write requests that error cleanup gets with -EAGAIN
2026-12-03 13:56 [PATCH v4 00/11] pNFS: layout recalls and writes that have to be resent Benjamin Coddington
@ 2026-12-03 13:56 ` Benjamin Coddington
2026-12-03 13:56 ` [PATCH v4 02/11] NFS: report the error when an O_DIRECT read's requests are never sent Benjamin Coddington
` (9 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Benjamin Coddington @ 2026-12-03 13:56 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Mike Snitzer
Cc: Olga Kornievskaia, linux-nfs
From: Benjamin Coddington <bcodding@hammerspace.com>
A layout driver's pg_init can fail a descriptor with -EAGAIN while it
still holds queued requests; flexfiles does this when the mirror count
changes. nfs_pageio_error_cleanup() hands those requests to
error_cleanup, and for O_DIRECT writes nfs_write_sync_pgio_error() just
unlocks and releases them. nfs_direct_write_schedule_iovec() treats
-EAGAIN as soft and defers only the request it was adding, so the queued
requests, already counted in requested_bytes, are never written and no
error is reported.
Pass the descriptor's nfs_direct_req to error_cleanup. On -EAGAIN, put
the O_DIRECT write requests on the commit list and set
NFS_ODIRECT_RESCHED_WRITES so the write completion resends them. Move
that code out of nfs_direct_write_reschedule_io() into
nfs_direct_write_requeue() and use it in both places.
This also changes the other -EAGAIN cleanups of O_DIRECT write requests:
the requests queued in nfs_direct_write_reschedule()'s descriptor, a
mirror copy cleaned up by nfs_pageio_cleanup_request(), and the pages
left over when nfs_pageio_resend() fails with -EAGAIN are all requeued
rather than released.
Fixes: 954998b60caa ("NFS: Fix error handling for O_DIRECT write scheduling")
Cc: stable@vger.kernel.org # v6.6+
Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
fs/nfs/direct.c | 48 +++++++++++++++++++++++++----------------
fs/nfs/pagelist.c | 7 +++---
fs/nfs/read.c | 3 ++-
fs/nfs/write.c | 5 +++--
include/linux/nfs_xdr.h | 3 ++-
5 files changed, 41 insertions(+), 25 deletions(-)
diff --git a/fs/nfs/direct.c b/fs/nfs/direct.c
index a3c6e8f4ea06..c771bb1b0730 100644
--- a/fs/nfs/direct.c
+++ b/fs/nfs/direct.c
@@ -330,7 +330,8 @@ static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
hdr->release(hdr);
}
-static void nfs_read_sync_pgio_error(struct list_head *head, int error)
+static void nfs_read_sync_pgio_error(struct list_head *head, int error,
+ struct nfs_direct_req *dreq)
{
nfs_release_request_list(head);
}
@@ -818,39 +819,50 @@ static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
hdr->release(hdr);
}
-static void nfs_write_sync_pgio_error(struct list_head *head, int error)
+static void nfs_direct_write_requeue(struct nfs_direct_req *dreq,
+ struct list_head *head)
{
+ struct nfs_commit_info cinfo;
struct nfs_page *req;
+ nfs_init_cinfo_from_dreq(&cinfo, dreq);
+ spin_lock(&dreq->lock);
+ if (dreq->error == 0)
+ dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
+ spin_unlock(&dreq->lock);
while (!list_empty(head)) {
req = nfs_list_entry(head->next);
nfs_list_remove_request(req);
- nfs_unlock_and_release_request(req);
+ nfs_unlock_request(req);
+ nfs_mark_request_commit(req, NULL, &cinfo, 0);
}
}
-static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
+static void nfs_write_sync_pgio_error(struct list_head *head, int error,
+ struct nfs_direct_req *dreq)
{
- struct nfs_direct_req *dreq = hdr->dreq;
struct nfs_page *req;
- struct nfs_commit_info cinfo;
-
- trace_nfs_direct_write_reschedule_io(dreq);
- nfs_init_cinfo_from_dreq(&cinfo, dreq);
- spin_lock(&dreq->lock);
- if (dreq->error == 0)
- dreq->flags = NFS_ODIRECT_RESCHED_WRITES;
- set_bit(NFS_IOHDR_REDO, &hdr->flags);
- spin_unlock(&dreq->lock);
- while (!list_empty(&hdr->pages)) {
- req = nfs_list_entry(hdr->pages.next);
+ /* -EAGAIN: requests already counted by the caller must be resent */
+ if (error == -EAGAIN) {
+ nfs_direct_write_requeue(dreq, head);
+ return;
+ }
+ while (!list_empty(head)) {
+ req = nfs_list_entry(head->next);
nfs_list_remove_request(req);
- nfs_unlock_request(req);
- nfs_mark_request_commit(req, NULL, &cinfo, 0);
+ nfs_unlock_and_release_request(req);
}
}
+static void nfs_direct_write_reschedule_io(struct nfs_pgio_header *hdr)
+{
+ trace_nfs_direct_write_reschedule_io(hdr->dreq);
+
+ set_bit(NFS_IOHDR_REDO, &hdr->flags);
+ nfs_direct_write_requeue(hdr->dreq, &hdr->pages);
+}
+
static const struct nfs_pgio_completion_ops nfs_direct_write_completion_ops = {
.error_cleanup = nfs_write_sync_pgio_error,
.init_hdr = nfs_direct_pgio_init,
diff --git a/fs/nfs/pagelist.c b/fs/nfs/pagelist.c
index c76c32a33be5..0bb18acaf872 100644
--- a/fs/nfs/pagelist.c
+++ b/fs/nfs/pagelist.c
@@ -1209,7 +1209,8 @@ nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
LIST_HEAD(head);
nfs_list_move_request(req, &head);
- desc->pg_completion_ops->error_cleanup(&head, desc->pg_error);
+ desc->pg_completion_ops->error_cleanup(&head, desc->pg_error,
+ desc->pg_dreq);
}
/**
@@ -1340,7 +1341,7 @@ static void nfs_pageio_error_cleanup(struct nfs_pageio_descriptor *desc)
for (midx = 0; midx < desc->pg_mirror_count; midx++) {
mirror = nfs_pgio_get_mirror(desc, midx);
desc->pg_completion_ops->error_cleanup(&mirror->pg_list,
- desc->pg_error);
+ desc->pg_error, desc->pg_dreq);
}
}
@@ -1443,7 +1444,7 @@ int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
nfs_pageio_complete(desc);
if (!list_empty(&pages)) {
int err = desc->pg_error < 0 ? desc->pg_error : -EIO;
- hdr->completion_ops->error_cleanup(&pages, err);
+ hdr->completion_ops->error_cleanup(&pages, err, hdr->dreq);
nfs_set_pgio_error(hdr, err, hdr->io_start);
return err;
}
diff --git a/fs/nfs/read.c b/fs/nfs/read.c
index 1fdca07761f6..ad9e948f4cb1 100644
--- a/fs/nfs/read.c
+++ b/fs/nfs/read.c
@@ -220,7 +220,8 @@ static void nfs_initiate_read(struct nfs_pgio_header *hdr,
}
static void
-nfs_async_read_error(struct list_head *head, int error)
+nfs_async_read_error(struct list_head *head, int error,
+ struct nfs_direct_req *dreq)
{
struct nfs_page *req;
diff --git a/fs/nfs/write.c b/fs/nfs/write.c
index af8735d0d67f..7967487e026c 100644
--- a/fs/nfs/write.c
+++ b/fs/nfs/write.c
@@ -1378,7 +1378,8 @@ static void nfs_redirty_request(struct nfs_page *req)
nfs_release_request(req);
}
-static void nfs_async_write_error(struct list_head *head, int error)
+static void nfs_async_write_error(struct list_head *head, int error,
+ struct nfs_direct_req *dreq)
{
struct nfs_page *req;
@@ -1394,7 +1395,7 @@ static void nfs_async_write_error(struct list_head *head, int error)
static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
{
- nfs_async_write_error(&hdr->pages, 0);
+ nfs_async_write_error(&hdr->pages, 0, NULL);
}
static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
diff --git a/include/linux/nfs_xdr.h b/include/linux/nfs_xdr.h
index c0e29b4dfa62..fce0e773bd7b 100644
--- a/include/linux/nfs_xdr.h
+++ b/include/linux/nfs_xdr.h
@@ -1742,7 +1742,8 @@ struct nfs_commit_data {
};
struct nfs_pgio_completion_ops {
- void (*error_cleanup)(struct list_head *head, int);
+ void (*error_cleanup)(struct list_head *head, int error,
+ struct nfs_direct_req *dreq);
void (*init_hdr)(struct nfs_pgio_header *hdr);
void (*completion)(struct nfs_pgio_header *hdr);
void (*reschedule_io)(struct nfs_pgio_header *hdr);
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v4 02/11] NFS: report the error when an O_DIRECT read's requests are never sent
2026-12-03 13:56 [PATCH v4 00/11] pNFS: layout recalls and writes that have to be resent Benjamin Coddington
2026-12-03 13:56 ` [PATCH v4 01/11] NFS: resend O_DIRECT write requests that error cleanup gets with -EAGAIN Benjamin Coddington
@ 2026-12-03 13:56 ` Benjamin Coddington
2026-12-03 13:56 ` [PATCH v4 03/11] NFS: send every mirror when one of them can take no more Benjamin Coddington
` (8 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Benjamin Coddington @ 2026-12-03 13:56 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Mike Snitzer
Cc: Olga Kornievskaia, linux-nfs
From: Benjamin Coddington <bcodding@hammerspace.com>
When a pageio descriptor fails with requests still queued, error cleanup
releases them. For an O_DIRECT read nothing records that, so if none of
the read was sent, the read returns zero and the caller takes it for end
of file.
Record the descriptor's error in the nfs_direct_req. The requests that
are dropped are the last ones of the read, so a read that got some data
still returns it as a short read, and a read that got none returns the
error.
Fixes: c18b96a1b862 ("nfs: clean up rest of reqs when failing to add one")
Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
fs/nfs/direct.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/fs/nfs/direct.c b/fs/nfs/direct.c
index c771bb1b0730..4630dddeafa1 100644
--- a/fs/nfs/direct.c
+++ b/fs/nfs/direct.c
@@ -333,6 +333,12 @@ static void nfs_direct_read_completion(struct nfs_pgio_header *hdr)
static void nfs_read_sync_pgio_error(struct list_head *head, int error,
struct nfs_direct_req *dreq)
{
+ if (!list_empty(head)) {
+ spin_lock(&dreq->lock);
+ if (!dreq->error)
+ dreq->error = error;
+ spin_unlock(&dreq->lock);
+ }
nfs_release_request_list(head);
}
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v4 03/11] NFS: send every mirror when one of them can take no more
2026-12-03 13:56 [PATCH v4 00/11] pNFS: layout recalls and writes that have to be resent Benjamin Coddington
2026-12-03 13:56 ` [PATCH v4 01/11] NFS: resend O_DIRECT write requests that error cleanup gets with -EAGAIN Benjamin Coddington
2026-12-03 13:56 ` [PATCH v4 02/11] NFS: report the error when an O_DIRECT read's requests are never sent Benjamin Coddington
@ 2026-12-03 13:56 ` Benjamin Coddington
2026-12-03 13:56 ` [PATCH v4 04/11] pNFS/flexfiles: don't switch a busy mirrored write descriptor to the MDS Benjamin Coddington
` (7 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Benjamin Coddington @ 2026-12-03 13:56 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Mike Snitzer
Cc: Olga Kornievskaia, linux-nfs
From: Benjamin Coddington <bcodding@hammerspace.com>
When a request doesn't fit, __nfs_pageio_add_request() sends only the
current mirror. The other mirrors keep their requests until they fill
too or the descriptor completes, so a pg_init for the next request runs
while they still hold the previous ones. If that pg_init has to give up
the mirrors, as flexfiles does when the next range has no layout segment
and its write falls back to the MDS, the requests still queued on them
are stranded.
Send every mirror at that point. With the same wsize on each data
server the mirrors fill at the same request, so the copies go out
together and the next pg_init finds the descriptor empty. With different
sizes a mirror may be sent before it is full, or with its copy of the
request that didn't fit on another; a later pg_init can then still find
a request queued.
If the current mirror asks for a retry, leave the others queued: it is
recoalesced first, and the mirrors are sent together when the request
that didn't fit comes round again.
Only the current mirror is recoalesced on this path, so if sending
another mirror requeues its requests for a retry, fail the descriptor
with -EAGAIN; error cleanup then redirties or requeues the requests of
every mirror for the next pass.
Fixes: a7d42ddb3099 ("nfs: add mirroring support to pgio layer")
Cc: stable@vger.kernel.org # v4.0+
Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
fs/nfs/pagelist.c | 38 +++++++++++++++++++++++++++++++++++++-
1 file changed, 37 insertions(+), 1 deletion(-)
diff --git a/fs/nfs/pagelist.c b/fs/nfs/pagelist.c
index 0bb18acaf872..504b880f15a0 100644
--- a/fs/nfs/pagelist.c
+++ b/fs/nfs/pagelist.c
@@ -1202,6 +1202,42 @@ static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
}
}
+/*
+ * When one mirror can take no more, send every mirror: with the same wsize
+ * on each the copies stay in step, and a pg_init for the next request finds
+ * the descriptor empty.
+ */
+static void nfs_pageio_doio_mirrors(struct nfs_pageio_descriptor *desc)
+{
+ u32 cur = desc->pg_mirror_idx;
+ u32 midx, restore_idx;
+
+ nfs_pageio_doio(desc);
+ /* A single mirror can have a nonzero pg_mirror_idx (flexfiles reads) */
+ if (desc->pg_mirror_count == 1)
+ return;
+ /* It is recoalesced first; the mirrors go out together after that */
+ if (nfs_pgio_current_mirror(desc)->pg_recoalesce)
+ return;
+ for (midx = 0; midx < desc->pg_mirror_count; midx++) {
+ if (desc->pg_error < 0)
+ break;
+ if (midx == cur)
+ continue;
+ restore_idx = nfs_pgio_set_current_mirror(desc, midx);
+ nfs_pageio_doio(desc);
+ /*
+ * Only the current mirror is recoalesced from here, so fail a
+ * retry on another one; error cleanup then redirties or
+ * requeues the requests of every mirror.
+ */
+ if (desc->pg_error >= 0 &&
+ nfs_pgio_current_mirror(desc)->pg_recoalesce)
+ desc->pg_error = -EAGAIN;
+ nfs_pgio_set_current_mirror(desc, restore_idx);
+ }
+}
+
static void
nfs_pageio_cleanup_request(struct nfs_pageio_descriptor *desc,
struct nfs_page *req)
@@ -1263,7 +1299,7 @@ static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
/* Can't coalesce any more, so do I/O */
nfs_page_group_unlock(req);
desc->pg_moreio = 1;
- nfs_pageio_doio(desc);
+ nfs_pageio_doio_mirrors(desc);
if (desc->pg_error < 0 || mirror->pg_recoalesce)
return 0;
/* retry add_request for this subreq */
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v4 04/11] pNFS/flexfiles: don't switch a busy mirrored write descriptor to the MDS
2026-12-03 13:56 [PATCH v4 00/11] pNFS: layout recalls and writes that have to be resent Benjamin Coddington
` (2 preceding siblings ...)
2026-12-03 13:56 ` [PATCH v4 03/11] NFS: send every mirror when one of them can take no more Benjamin Coddington
@ 2026-12-03 13:56 ` Benjamin Coddington
2026-12-03 13:57 ` [PATCH v4 05/11] NFS: make the O_DIRECT write work a delayed_work Benjamin Coddington
` (6 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Benjamin Coddington @ 2026-12-03 13:56 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Mike Snitzer
Cc: Olga Kornievskaia, linux-nfs
From: Benjamin Coddington <bcodding@hammerspace.com>
nfs_pageio_reset_write_mds() replaces pg_ops before it completes the
descriptor, so the completion sees only mirror 0. Requests queued on the
other mirrors are never sent or cleaned up, the mirror array they are
linked on is freed, and their folios never leave writeback.
ff_layout_pg_init_write() falls back to the MDS from a later pg_init while
other mirrors still hold requests: when the layout segment is gone, or when
a mirror's DS cannot be prepared. After the previous patch the mirrors are
sent together, so this happens when one mirror's request is retried during
completion while the others still hold theirs, or when pg_init fails
partway through the mirror copies of a request. In that case return
-EAGAIN as for a mirror count change, so error cleanup redirties the
queued requests, or requeues them for an O_DIRECT write, with the
flexfiles ops still in place; the next pg_init starts from an empty
descriptor and can switch safely.
ff_layout_write_pagelist() returning PNFS_NOT_ATTEMPTED has the same
problem through pnfs_write_through_mds(). For a mirrored layout, handle
the failure the way FF_FLAGS_NO_IO_THRU_MDS already does: mark the layout
for return with the failing device and return PNFS_TRY_AGAIN. RFC 8435
section 7 asks the client to retry with a new layout before falling back to
the MDS, and section 8.2.3 leaves it to the server to decide, in that
layout, whether the errant mirror stays. The retry's pg_init then uses the
new layout, or goes to the MDS if the server has none to give. A fatal
error preparing the DS, such as -ENOMEM or a signal, is no evidence against
the device, so retry that one without returning the layout: pg_init
prepares the DS again and falls back to the MDS if that fails too. A
failure to create the DS RPC client is treated the same way; pg_init has
nothing to prepare then, and the write is tried again. With
FF_FLAGS_NO_IO_THRU_MDS such an error no longer returns the layout either;
pg_init waits and prepares the DS again. A single-mirror descriptor keeps
using PNFS_NOT_ATTEMPTED, now also when the RPC client can't be created:
pnfs_write_through_mds() switches it safely, since its only mirror is not
freed.
Fixes: d67ae825a59d ("pnfs/flexfiles: Add the FlexFile Layout Driver")
Cc: stable@vger.kernel.org # v4.0+
Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
fs/nfs/flexfilelayout/flexfilelayout.c | 48 +++++++++++++++++++-------
1 file changed, 36 insertions(+), 12 deletions(-)
diff --git a/fs/nfs/flexfilelayout/flexfilelayout.c b/fs/nfs/flexfilelayout/flexfilelayout.c
index a03ef583c391..309f50b29ccd 100644
--- a/fs/nfs/flexfilelayout/flexfilelayout.c
+++ b/fs/nfs/flexfilelayout/flexfilelayout.c
@@ -1150,6 +1150,22 @@ ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio,
nfs_pageio_reset_read_mds(pgio);
}
+/*
+ * Switching to the MDS discards the mirror array, so it must not happen while
+ * any mirror still holds requests.
+ */
+static bool ff_layout_pg_mirrors_busy(struct nfs_pageio_descriptor *pgio)
+{
+ u32 i;
+
+ if (pgio->pg_mirror_count == 1)
+ return false;
+ for (i = 0; i < pgio->pg_mirror_count; i++)
+ if (!list_empty(&pgio->pg_mirrors[i].pg_list))
+ return true;
+ return false;
+}
+
static void
ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
struct nfs_page *req)
@@ -1215,6 +1231,8 @@ ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
pgio->pg_error = -EAGAIN;
return;
out_mds:
+ if (ff_layout_pg_mirrors_busy(pgio))
+ goto out_eagain;
trace_pnfs_mds_fallback_pg_init_write(pgio->pg_inode,
0, NFS4_MAX_UINT64, IOMODE_RW,
NFS_I(pgio->pg_inode)->layout,
@@ -2437,8 +2455,10 @@ ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
ds_clnt = nfs4_ff_find_or_create_ds_client(mirror_ds, ds->ds_clp,
hdr->inode);
- if (IS_ERR(ds_clnt))
+ if (IS_ERR(ds_clnt)) {
+ ds_fatal_error = true;
goto out_failed;
+ }
ds_cred = ff_layout_get_ds_cred(mirror, &lseg->pls_range, hdr->cred,
mirror_ds, dss_id);
@@ -2493,19 +2513,23 @@ ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
nfs4_ff_layout_put_deviceid(mirror_ds);
if (ff_layout_avoid_mds_available_ds(lseg) && !ds_fatal_error)
return PNFS_TRY_AGAIN;
- if (ff_layout_no_fallback_to_mds(lseg)) {
- /*
- * FF_FLAGS_NO_IO_THRU_MDS: force fresh LAYOUTGET,
- * never fall through to MDS I/O.
- */
+ if (!ff_layout_no_fallback_to_mds(lseg) &&
+ FF_LAYOUT_MIRROR_COUNT(lseg) == 1) {
+ trace_pnfs_mds_fallback_write_pagelist(hdr->inode,
+ hdr->args.offset, hdr->args.count,
+ IOMODE_RW, NFS_I(hdr->inode)->layout, lseg);
+ return PNFS_NOT_ATTEMPTED;
+ }
+ /*
+ * FF_FLAGS_NO_IO_THRU_MDS forbids MDS I/O, and
+ * pnfs_write_through_mds() cannot switch a mirrored descriptor, so
+ * retry. A fatal error is no evidence against the DS: keep the
+ * layout then, and let pg_init of the retry prepare the DS again.
+ */
+ if (!ds_fatal_error)
pnfs_error_mark_layout_for_return(hdr->inode, lseg,
&mirror->dss[dss_id].devid);
- return PNFS_TRY_AGAIN;
- }
- trace_pnfs_mds_fallback_write_pagelist(hdr->inode,
- hdr->args.offset, hdr->args.count,
- IOMODE_RW, NFS_I(hdr->inode)->layout, lseg);
- return PNFS_NOT_ATTEMPTED;
+ return PNFS_TRY_AGAIN;
}
static struct nfs_fh *
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v4 05/11] NFS: make the O_DIRECT write work a delayed_work
2026-12-03 13:56 [PATCH v4 00/11] pNFS: layout recalls and writes that have to be resent Benjamin Coddington
` (3 preceding siblings ...)
2026-12-03 13:56 ` [PATCH v4 04/11] pNFS/flexfiles: don't switch a busy mirrored write descriptor to the MDS Benjamin Coddington
@ 2026-12-03 13:57 ` Benjamin Coddington
2026-12-03 13:57 ` [PATCH v4 06/11] NFS: wait before resending an O_DIRECT write that was refused again Benjamin Coddington
` (5 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Benjamin Coddington @ 2026-12-03 13:57 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Mike Snitzer
Cc: Olga Kornievskaia, linux-nfs
From: Benjamin Coddington <bcodding@hammerspace.com>
nfs_direct_write_complete() queues dreq->work on nfsiod_workqueue to
commit, reschedule or complete an O_DIRECT write. Make it a
delayed_work, queued with no delay, so that a reschedule can be paced
without sleeping in the worker.
Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
fs/nfs/direct.c | 8 +++++---
fs/nfs/internal.h | 2 +-
2 files changed, 6 insertions(+), 4 deletions(-)
diff --git a/fs/nfs/direct.c b/fs/nfs/direct.c
index 4630dddeafa1..b355d2bbd919 100644
--- a/fs/nfs/direct.c
+++ b/fs/nfs/direct.c
@@ -219,7 +219,7 @@ static inline struct nfs_direct_req *nfs_direct_req_alloc(void)
init_completion(&dreq->completion);
INIT_LIST_HEAD(&dreq->mds_cinfo.list);
pnfs_init_ds_commit_info(&dreq->ds_cinfo);
- INIT_WORK(&dreq->work, nfs_direct_write_schedule_work);
+ INIT_DELAYED_WORK(&dreq->work, nfs_direct_write_schedule_work);
spin_lock_init(&dreq->lock);
return dreq;
@@ -746,7 +746,8 @@ static void nfs_direct_write_clear_reqs(struct nfs_direct_req *dreq)
static void nfs_direct_write_schedule_work(struct work_struct *work)
{
- struct nfs_direct_req *dreq = container_of(work, struct nfs_direct_req, work);
+ struct nfs_direct_req *dreq = container_of(to_delayed_work(work),
+ struct nfs_direct_req, work);
int flags = dreq->flags;
dreq->flags = 0;
@@ -767,7 +768,8 @@ static void nfs_direct_write_schedule_work(struct work_struct *work)
static void nfs_direct_write_complete(struct nfs_direct_req *dreq)
{
trace_nfs_direct_write_complete(dreq);
- queue_work(nfsiod_workqueue, &dreq->work); /* Calls nfs_direct_write_schedule_work */
+ /* Calls nfs_direct_write_schedule_work */
+ queue_delayed_work(nfsiod_workqueue, &dreq->work, 0);
}
static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h
index f3ca9be5e08a..f34da925f3d5 100644
--- a/fs/nfs/internal.h
+++ b/fs/nfs/internal.h
@@ -1036,7 +1036,7 @@ struct nfs_direct_req {
/* commit state */
struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */
struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */
- struct work_struct work;
+ struct delayed_work work;
int flags;
/* for write */
#define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v4 06/11] NFS: wait before resending an O_DIRECT write that was refused again
2026-12-03 13:56 [PATCH v4 00/11] pNFS: layout recalls and writes that have to be resent Benjamin Coddington
` (4 preceding siblings ...)
2026-12-03 13:57 ` [PATCH v4 05/11] NFS: make the O_DIRECT write work a delayed_work Benjamin Coddington
@ 2026-12-03 13:57 ` Benjamin Coddington
2026-12-03 13:57 ` [PATCH v4 07/11] pNFS: add a layout segment hold that doesn't keep the layout in use Benjamin Coddington
` (4 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Benjamin Coddington @ 2026-12-03 13:57 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Mike Snitzer
Cc: Olga Kornievskaia, linux-nfs
From: Benjamin Coddington <bcodding@hammerspace.com>
nfs_direct_write_reschedule() resends the requests of an O_DIRECT write
from nfsiod. When the resend itself gets -EAGAIN, for example because
the layout driver has no layout segment to write with yet, the requests
go back on the commit list and the work is queued again at once. The
worker then resends in a loop until a layout is available.
Queue the work a second later when a resend ends with -EAGAIN. The first
resend of a write is still immediate.
Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
fs/nfs/direct.c | 8 +++++++-
fs/nfs/internal.h | 1 +
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/fs/nfs/direct.c b/fs/nfs/direct.c
index b355d2bbd919..efce806c2a95 100644
--- a/fs/nfs/direct.c
+++ b/fs/nfs/direct.c
@@ -633,6 +633,11 @@ static void nfs_direct_write_reschedule(struct nfs_direct_req *dreq)
}
}
+ spin_lock(&dreq->lock);
+ if (desc.pg_error == -EAGAIN &&
+ dreq->flags == NFS_ODIRECT_RESCHED_WRITES)
+ dreq->work_delay = HZ;
+ spin_unlock(&dreq->lock);
if (put_dreq(dreq))
nfs_direct_write_complete(dreq);
}
@@ -751,6 +756,7 @@ static void nfs_direct_write_schedule_work(struct work_struct *work)
int flags = dreq->flags;
dreq->flags = 0;
+ dreq->work_delay = 0;
switch (flags) {
case NFS_ODIRECT_DO_COMMIT:
nfs_direct_commit_schedule(dreq);
@@ -769,7 +775,7 @@ static void nfs_direct_write_complete(struct nfs_direct_req *dreq)
{
trace_nfs_direct_write_complete(dreq);
/* Calls nfs_direct_write_schedule_work */
- queue_delayed_work(nfsiod_workqueue, &dreq->work, 0);
+ queue_delayed_work(nfsiod_workqueue, &dreq->work, dreq->work_delay);
}
static void nfs_direct_write_completion(struct nfs_pgio_header *hdr)
diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h
index f34da925f3d5..5a5c1b960546 100644
--- a/fs/nfs/internal.h
+++ b/fs/nfs/internal.h
@@ -1037,6 +1037,7 @@ struct nfs_direct_req {
struct nfs_mds_commit_info mds_cinfo; /* Storage for cinfo */
struct pnfs_ds_commit_info ds_cinfo; /* Storage for cinfo */
struct delayed_work work;
+ unsigned long work_delay; /* jiffies */
int flags;
/* for write */
#define NFS_ODIRECT_DO_COMMIT (1) /* an unstable reply was received */
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v4 07/11] pNFS: add a layout segment hold that doesn't keep the layout in use
2026-12-03 13:56 [PATCH v4 00/11] pNFS: layout recalls and writes that have to be resent Benjamin Coddington
` (5 preceding siblings ...)
2026-12-03 13:57 ` [PATCH v4 06/11] NFS: wait before resending an O_DIRECT write that was refused again Benjamin Coddington
@ 2026-12-03 13:57 ` Benjamin Coddington
2026-12-03 13:57 ` [PATCH v4 08/11] pNFS: don't keep a layout in use from an idle pageio descriptor Benjamin Coddington
` (3 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Benjamin Coddington @ 2026-12-03 13:57 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Mike Snitzer
Cc: Olga Kornievskaia, linux-nfs
From: Benjamin Coddington <bcodding@hammerspace.com>
A reference on a layout segment keeps the segment on its layout, so a
recall or a return of the layout waits for every reference holder.
Add a second count, pls_hold, for holders that need the segment to stay
allocated but have no I/O in flight on it. The references share one hold,
and the layout driver frees the segment when the last hold is released.
A hold also takes a reference on the layout header, which the segment
points to.
Nothing takes a hold yet.
Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
fs/nfs/pnfs.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---
fs/nfs/pnfs.h | 3 +++
2 files changed, 52 insertions(+), 3 deletions(-)
diff --git a/fs/nfs/pnfs.c b/fs/nfs/pnfs.c
index d2ff78acbed9..58ba779a8711 100644
--- a/fs/nfs/pnfs.c
+++ b/fs/nfs/pnfs.c
@@ -595,19 +595,65 @@ pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
INIT_LIST_HEAD(&lseg->pls_lc_list);
INIT_LIST_HEAD(&lseg->pls_commits);
refcount_set(&lseg->pls_refcount, 1);
+ refcount_set(&lseg->pls_hold, 1);
set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
lseg->pls_layout = lo;
lseg->pls_range = *range;
lseg->pls_seq = be32_to_cpu(stateid->seqid);
}
+/*
+ * pls_refcount keeps a segment on its layout, where a recall or a return
+ * has to wait for it. pls_hold only keeps the memory: the pls_refcount
+ * references share one hold between them.
+ */
static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
{
- if (lseg != NULL) {
- struct inode *inode = lseg->pls_layout->plh_inode;
- NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg);
+ struct inode *inode;
+
+ if (!lseg || !refcount_dec_and_test(&lseg->pls_hold))
+ return;
+ inode = lseg->pls_layout->plh_inode;
+ NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg);
+}
+
+/**
+ * pnfs_lseg_hold - keep a layout segment allocated without keeping it in use
+ * @lseg: segment the caller has a reference to or holds
+ *
+ * A held segment and its layout header stay allocated, but the hold doesn't
+ * delay a recall or a return of the layout.
+ *
+ * Return: @lseg
+ */
+struct pnfs_layout_segment *pnfs_lseg_hold(struct pnfs_layout_segment *lseg)
+{
+ if (lseg) {
+ refcount_inc(&lseg->pls_hold);
+ pnfs_get_layout_hdr(lseg->pls_layout);
}
+ return lseg;
+}
+EXPORT_SYMBOL_GPL(pnfs_lseg_hold);
+
+/**
+ * pnfs_lseg_release - release a hold on a layout segment
+ * @lseg: held segment, or NULL
+ *
+ * Frees the segment if this was the last hold and it has no references.
+ * Takes the inode's i_lock, and can send a LAYOUTRETURN asynchronously.
+ */
+void pnfs_lseg_release(struct pnfs_layout_segment *lseg)
+{
+ struct pnfs_layout_hdr *lo;
+
+ if (!lseg)
+ return;
+ lo = lseg->pls_layout;
+ pnfs_free_lseg(lseg);
+ pnfs_put_layout_hdr(lo);
}
+EXPORT_SYMBOL_GPL(pnfs_lseg_release);
static void
pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
diff --git a/fs/nfs/pnfs.h b/fs/nfs/pnfs.h
index bbe56b682dc0..2b8e77f0437b 100644
--- a/fs/nfs/pnfs.h
+++ b/fs/nfs/pnfs.h
@@ -77,6 +77,7 @@ struct pnfs_layout_segment {
struct list_head pls_commits;
struct pnfs_layout_range pls_range;
refcount_t pls_refcount;
+ refcount_t pls_hold;
u32 pls_seq;
unsigned long pls_flags;
struct pnfs_layout_hdr *pls_layout;
@@ -286,6 +287,8 @@ extern int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp,
/* pnfs.c */
void pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo);
void pnfs_put_lseg(struct pnfs_layout_segment *lseg);
+struct pnfs_layout_segment *pnfs_lseg_hold(struct pnfs_layout_segment *lseg);
+void pnfs_lseg_release(struct pnfs_layout_segment *lseg);
void set_pnfs_layoutdriver(struct nfs_server *, const struct nfs_fh *, struct nfs_fsinfo *);
void unset_pnfs_layoutdriver(struct nfs_server *);
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v4 08/11] pNFS: don't keep a layout in use from an idle pageio descriptor
2026-12-03 13:56 [PATCH v4 00/11] pNFS: layout recalls and writes that have to be resent Benjamin Coddington
` (6 preceding siblings ...)
2026-12-03 13:57 ` [PATCH v4 07/11] pNFS: add a layout segment hold that doesn't keep the layout in use Benjamin Coddington
@ 2026-12-03 13:57 ` Benjamin Coddington
2026-12-03 13:57 ` [PATCH v4 09/11] pNFS: don't keep a layout in use from a commit bucket Benjamin Coddington
` (2 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Benjamin Coddington @ 2026-12-03 13:57 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Mike Snitzer
Cc: Olga Kornievskaia, linux-nfs
From: Benjamin Coddington <bcodding@hammerspace.com>
A pageio descriptor keeps a reference on its layout segment from pg_init
until pg_cleanup, or until the next pg_init finds the segment invalid. In
between it may have no I/O on the wire and be blocked: on a folio lock in
writeback, or in a page fault on the user buffer of an O_DIRECT write. A
recall of the layout can't complete until the descriptor lets go, and what
the descriptor waits for may itself be waiting for a new layout.
Make the descriptor hold its segment instead. When it sends I/O,
pnfs_generic_pg_readpages() and pnfs_generic_pg_writepages() take the
reference that the I/O keeps until it completes.
If the segment has been invalidated since pg_init, the requests are
coalesced again, so that pg_init gets another segment and they are sent in
the same pass. Mirrored and O_DIRECT writes are handed to reschedule_io
instead, as they are when the flexfiles driver refuses to send them on an
invalid segment: pg_init replaces the segment for every mirror, and an
O_DIRECT request that pg_init refuses isn't sent again.
pnfs_pgio_update_layout() replaces the open-coded calls to
pnfs_update_layout() that set pg_lseg. The files layout driver keeps its
own wrapper, which now returns a held segment.
Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
fs/nfs/filelayout/filelayout.c | 2 +-
fs/nfs/flexfilelayout/flexfilelayout.c | 33 ++----
fs/nfs/pnfs.c | 139 ++++++++++++++++++++-----
fs/nfs/pnfs.h | 5 +
4 files changed, 126 insertions(+), 53 deletions(-)
diff --git a/fs/nfs/filelayout/filelayout.c b/fs/nfs/filelayout/filelayout.c
index 53808e42546a..4b466c57d75d 100644
--- a/fs/nfs/filelayout/filelayout.c
+++ b/fs/nfs/filelayout/filelayout.c
@@ -863,7 +863,7 @@ fl_pnfs_update_layout(struct inode *ino,
lseg = NULL;
}
out:
- return lseg;
+ return pnfs_lseg_ref_to_hold(lseg);
}
static void
diff --git a/fs/nfs/flexfilelayout/flexfilelayout.c b/fs/nfs/flexfilelayout/flexfilelayout.c
index 309f50b29ccd..0c5004ec1c3b 100644
--- a/fs/nfs/flexfilelayout/flexfilelayout.c
+++ b/fs/nfs/flexfilelayout/flexfilelayout.c
@@ -1004,15 +1004,10 @@ ff_layout_pg_get_read(struct nfs_pageio_descriptor *pgio,
struct nfs_page *req,
bool strict_iomode)
{
- pnfs_put_lseg(pgio->pg_lseg);
- pgio->pg_lseg =
- pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
- req_offset(req), req->wb_bytes, IOMODE_READ,
- strict_iomode, nfs_io_gfp_mask());
- if (IS_ERR(pgio->pg_lseg)) {
- pgio->pg_error = PTR_ERR(pgio->pg_lseg);
- pgio->pg_lseg = NULL;
- }
+ pnfs_lseg_release(pgio->pg_lseg);
+ pgio->pg_lseg = NULL;
+ pnfs_pgio_update_layout(pgio, req, req->wb_bytes, IOMODE_READ,
+ strict_iomode);
}
static bool
@@ -1179,15 +1174,9 @@ ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
retry:
pnfs_generic_pg_check_layout(pgio, req);
if (!pgio->pg_lseg) {
- pgio->pg_lseg =
- pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
- req_offset(req), req->wb_bytes,
- IOMODE_RW, false, nfs_io_gfp_mask());
- if (IS_ERR(pgio->pg_lseg)) {
- pgio->pg_error = PTR_ERR(pgio->pg_lseg);
- pgio->pg_lseg = NULL;
+ if (pnfs_pgio_update_layout(pgio, req, req->wb_bytes,
+ IOMODE_RW, false))
return;
- }
}
/* If no lseg, fall back to write through mds */
if (pgio->pg_lseg == NULL)
@@ -1251,15 +1240,9 @@ ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
bool no_fallback;
if (!pgio->pg_lseg) {
- pgio->pg_lseg =
- pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
- req_offset(req), req->wb_bytes,
- IOMODE_RW, false, nfs_io_gfp_mask());
- if (IS_ERR(pgio->pg_lseg)) {
- pgio->pg_error = PTR_ERR(pgio->pg_lseg);
- pgio->pg_lseg = NULL;
+ if (pnfs_pgio_update_layout(pgio, req, req->wb_bytes,
+ IOMODE_RW, false))
goto out;
- }
}
if (pgio->pg_lseg)
return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg);
diff --git a/fs/nfs/pnfs.c b/fs/nfs/pnfs.c
index 58ba779a8711..3c01acca147a 100644
--- a/fs/nfs/pnfs.c
+++ b/fs/nfs/pnfs.c
@@ -655,6 +655,45 @@ void pnfs_lseg_release(struct pnfs_layout_segment *lseg)
}
EXPORT_SYMBOL_GPL(pnfs_lseg_release);
+/**
+ * pnfs_lseg_ref_to_hold - trade a reference on a layout segment for a hold
+ * @lseg: result of pnfs_update_layout()
+ *
+ * Return: @lseg, to be released with pnfs_lseg_release() unless it is NULL
+ * or an error.
+ */
+struct pnfs_layout_segment *
+pnfs_lseg_ref_to_hold(struct pnfs_layout_segment *lseg)
+{
+ if (!IS_ERR_OR_NULL(lseg)) {
+ pnfs_lseg_hold(lseg);
+ pnfs_put_lseg(lseg);
+ }
+ return lseg;
+}
+EXPORT_SYMBOL_GPL(pnfs_lseg_ref_to_hold);
+
+/*
+ * Take a reference on a held segment to do I/O with, unless the segment
+ * has been invalidated. mark_lseg_invalid() runs under the i_lock, so a
+ * recall either finds this reference or keeps it from being taken.
+ */
+static struct pnfs_layout_segment *
+pnfs_lseg_get_io(struct pnfs_layout_segment *lseg)
+{
+ struct pnfs_layout_segment *ret = NULL;
+ struct inode *inode;
+
+ if (!lseg)
+ return NULL;
+ inode = lseg->pls_layout->plh_inode;
+ spin_lock(&inode->i_lock);
+ if (pnfs_is_valid_lseg(lseg))
+ ret = pnfs_get_lseg(lseg);
+ spin_unlock(&inode->i_lock);
+ return ret;
+}
+
static void
pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
struct pnfs_layout_segment *lseg)
@@ -3320,11 +3359,33 @@ pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio,
(test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags) &&
pnfs_lseg_request_intersecting(pgio->pg_lseg, req)))
return;
- pnfs_put_lseg(pgio->pg_lseg);
+ pnfs_lseg_release(pgio->pg_lseg);
pgio->pg_lseg = NULL;
}
EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout);
+/*
+ * The descriptor holds the segment it gets. Returns zero, with pg_lseg NULL
+ * if there is no segment to use, or the error it set pg_error to.
+ */
+int pnfs_pgio_update_layout(struct nfs_pageio_descriptor *pgio,
+ struct nfs_page *req, u64 count,
+ enum pnfs_iomode iomode, bool strict_iomode)
+{
+ struct pnfs_layout_segment *lseg;
+
+ lseg = pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
+ req_offset(req), count, iomode,
+ strict_iomode, nfs_io_gfp_mask());
+ if (IS_ERR(lseg)) {
+ pgio->pg_error = PTR_ERR(lseg);
+ return pgio->pg_error;
+ }
+ pgio->pg_lseg = pnfs_lseg_ref_to_hold(lseg);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(pnfs_pgio_update_layout);
+
void
pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
{
@@ -3338,16 +3399,9 @@ pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *r
rd_size = nfs_dreq_bytes_left(pgio->pg_dreq,
req_offset(req));
- pgio->pg_lseg =
- pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
- req_offset(req), rd_size,
- IOMODE_READ, false,
- nfs_io_gfp_mask());
- if (IS_ERR(pgio->pg_lseg)) {
- pgio->pg_error = PTR_ERR(pgio->pg_lseg);
- pgio->pg_lseg = NULL;
+ if (pnfs_pgio_update_layout(pgio, req, rd_size, IOMODE_READ,
+ false))
return;
- }
}
/* If no lseg, fall back to read through mds */
if (pgio->pg_lseg == NULL)
@@ -3361,17 +3415,9 @@ pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
struct nfs_page *req, u64 wb_size)
{
pnfs_generic_pg_check_layout(pgio, req);
- if (pgio->pg_lseg == NULL) {
- pgio->pg_lseg =
- pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
- req_offset(req), wb_size, IOMODE_RW,
- false, nfs_io_gfp_mask());
- if (IS_ERR(pgio->pg_lseg)) {
- pgio->pg_error = PTR_ERR(pgio->pg_lseg);
- pgio->pg_lseg = NULL;
- return;
- }
- }
+ if (!pgio->pg_lseg &&
+ pnfs_pgio_update_layout(pgio, req, wb_size, IOMODE_RW, false))
+ return;
/* If no lseg, fall back to write through mds */
if (pgio->pg_lseg == NULL)
nfs_pageio_reset_write_mds(pgio);
@@ -3382,7 +3428,7 @@ void
pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
{
if (desc->pg_lseg) {
- pnfs_put_lseg(desc->pg_lseg);
+ pnfs_lseg_release(desc->pg_lseg);
desc->pg_lseg = NULL;
}
}
@@ -3535,6 +3581,26 @@ pnfs_do_write(struct nfs_pageio_descriptor *desc,
}
}
+/*
+ * The segment was invalidated after pg_init. Coalescing again sends the
+ * requests in this pass, but it runs pg_init, which replaces the segment for
+ * every mirror, and an O_DIRECT request that pg_init refuses isn't sent again.
+ */
+static bool pnfs_pgio_recoalesce_write(struct nfs_pageio_descriptor *desc)
+{
+ if (!desc->pg_lseg || desc->pg_mirror_count != 1 || desc->pg_dreq)
+ return false;
+ nfs_pgio_current_mirror(desc)->pg_recoalesce = 1;
+ return true;
+}
+
+static void pnfs_write_reschedule(struct nfs_pgio_header *hdr)
+{
+ if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
+ hdr->completion_ops->reschedule_io(hdr);
+ hdr->completion_ops->completion(hdr);
+}
+
static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
{
pnfs_put_ds_dev(hdr->ds_dev);
@@ -3545,22 +3611,30 @@ static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
int
pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
{
+ struct pnfs_layout_segment *lseg;
struct nfs_pgio_header *hdr;
int ret;
+ lseg = pnfs_lseg_get_io(desc->pg_lseg);
+ if (!lseg && pnfs_pgio_recoalesce_write(desc))
+ return 0;
hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
if (!hdr) {
+ pnfs_put_lseg(lseg);
desc->pg_error = -ENOMEM;
return desc->pg_error;
}
nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
- hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
+ hdr->lseg = lseg;
ret = nfs_generic_pgio(desc, hdr);
- if (!ret)
+ if (ret)
+ return ret;
+ if (hdr->lseg)
pnfs_do_write(desc, hdr, desc->pg_ioflags);
-
- return ret;
+ else
+ pnfs_write_reschedule(hdr);
+ return 0;
}
EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
@@ -3691,16 +3765,27 @@ static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
int
pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
{
+ struct pnfs_layout_segment *lseg;
struct nfs_pgio_header *hdr;
int ret;
+ if (WARN_ON_ONCE(!desc->pg_lseg)) {
+ desc->pg_error = -EIO;
+ return desc->pg_error;
+ }
+ lseg = pnfs_lseg_get_io(desc->pg_lseg);
+ if (!lseg) {
+ nfs_pgio_current_mirror(desc)->pg_recoalesce = 1;
+ return 0;
+ }
hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
if (!hdr) {
+ pnfs_put_lseg(lseg);
desc->pg_error = -ENOMEM;
return desc->pg_error;
}
nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
- hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
+ hdr->lseg = lseg;
ret = nfs_generic_pgio(desc, hdr);
if (!ret)
pnfs_do_read(desc, hdr);
diff --git a/fs/nfs/pnfs.h b/fs/nfs/pnfs.h
index 2b8e77f0437b..e05f37bdb916 100644
--- a/fs/nfs/pnfs.h
+++ b/fs/nfs/pnfs.h
@@ -289,6 +289,11 @@ void pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo);
void pnfs_put_lseg(struct pnfs_layout_segment *lseg);
struct pnfs_layout_segment *pnfs_lseg_hold(struct pnfs_layout_segment *lseg);
void pnfs_lseg_release(struct pnfs_layout_segment *lseg);
+struct pnfs_layout_segment *
+pnfs_lseg_ref_to_hold(struct pnfs_layout_segment *lseg);
+int pnfs_pgio_update_layout(struct nfs_pageio_descriptor *pgio,
+ struct nfs_page *req, u64 count,
+ enum pnfs_iomode iomode, bool strict_iomode);
void set_pnfs_layoutdriver(struct nfs_server *, const struct nfs_fh *, struct nfs_fsinfo *);
void unset_pnfs_layoutdriver(struct nfs_server *);
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v4 09/11] pNFS: don't keep a layout in use from a commit bucket
2026-12-03 13:56 [PATCH v4 00/11] pNFS: layout recalls and writes that have to be resent Benjamin Coddington
` (7 preceding siblings ...)
2026-12-03 13:57 ` [PATCH v4 08/11] pNFS: don't keep a layout in use from an idle pageio descriptor Benjamin Coddington
@ 2026-12-03 13:57 ` Benjamin Coddington
2026-12-03 13:57 ` [PATCH v4 10/11] NFS: wait and resend a folio refused with -EAGAIN in nfs_wb_folio() Benjamin Coddington
2026-12-03 13:57 ` [PATCH v4 11/11] pNFS: answer a file recall with NFS4ERR_DELAY while I/O drains Benjamin Coddington
10 siblings, 0 replies; 12+ messages in thread
From: Benjamin Coddington @ 2026-12-03 13:57 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Mike Snitzer
Cc: Olga Kornievskaia, linux-nfs
From: Benjamin Coddington <bcodding@hammerspace.com>
A commit bucket keeps a reference on its layout segment from the first
unstable write until a COMMIT is sent. For an O_DIRECT write that is
after every chunk has been scheduled, so a writer blocked in a page fault
on its buffer keeps a recalled layout in use, and the fault may be waiting
for a new layout.
Make the bucket hold the segment instead, and take the reference for the
COMMIT when the COMMIT is built. If the segment has been invalidated by
then, the requests are written again: a COMMIT can't be sent to the data
server once the layout is returned.
The flexfiles driver never puts a COMMIT on the wire for an invalid
segment. ff_layout_initiate_commit() builds one for a segment that is
marked for return, but ff_layout_commit_prepare_common() ends the task
and the data is written again. It is now written again without the
COMMIT being built. The files layout driver used to send the COMMIT, and
now writes again too.
Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
fs/nfs/pnfs.c | 2 +-
fs/nfs/pnfs.h | 1 +
fs/nfs/pnfs_nfs.c | 61 +++++++++++++++++++++++++++++++----------------
3 files changed, 42 insertions(+), 22 deletions(-)
diff --git a/fs/nfs/pnfs.c b/fs/nfs/pnfs.c
index 3c01acca147a..4a0ae9ccd859 100644
--- a/fs/nfs/pnfs.c
+++ b/fs/nfs/pnfs.c
@@ -678,7 +678,7 @@ EXPORT_SYMBOL_GPL(pnfs_lseg_ref_to_hold);
* has been invalidated. mark_lseg_invalid() runs under the i_lock, so a
* recall either finds this reference or keeps it from being taken.
*/
-static struct pnfs_layout_segment *
+struct pnfs_layout_segment *
pnfs_lseg_get_io(struct pnfs_layout_segment *lseg)
{
struct pnfs_layout_segment *ret = NULL;
diff --git a/fs/nfs/pnfs.h b/fs/nfs/pnfs.h
index e05f37bdb916..6b320c379467 100644
--- a/fs/nfs/pnfs.h
+++ b/fs/nfs/pnfs.h
@@ -291,6 +291,7 @@ struct pnfs_layout_segment *pnfs_lseg_hold(struct pnfs_layout_segment *lseg);
void pnfs_lseg_release(struct pnfs_layout_segment *lseg);
struct pnfs_layout_segment *
pnfs_lseg_ref_to_hold(struct pnfs_layout_segment *lseg);
+struct pnfs_layout_segment *pnfs_lseg_get_io(struct pnfs_layout_segment *lseg);
int pnfs_pgio_update_layout(struct nfs_pageio_descriptor *pgio,
struct nfs_page *req, u64 count,
enum pnfs_iomode iomode, bool strict_iomode);
diff --git a/fs/nfs/pnfs_nfs.c b/fs/nfs/pnfs_nfs.c
index 14d419b8bfc7..5487c8ee885e 100644
--- a/fs/nfs/pnfs_nfs.c
+++ b/fs/nfs/pnfs_nfs.c
@@ -76,7 +76,7 @@ pnfs_free_bucket_lseg(struct pnfs_commit_bucket *bucket)
}
/* The generic layer is about to remove the req from the commit list.
- * If this will make the bucket empty, it will need to put the lseg reference.
+ * If this will make the bucket empty, it will need to release the lseg.
* Note this must be called holding nfsi->commit_mutex
*/
void
@@ -94,7 +94,7 @@ pnfs_generic_clear_request_commit(struct nfs_page *req,
out:
nfs_request_remove_commit_list(req, cinfo);
if (bucket)
- pnfs_put_lseg(pnfs_free_bucket_lseg(bucket));
+ pnfs_lseg_release(pnfs_free_bucket_lseg(bucket));
}
EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit);
@@ -323,7 +323,7 @@ pnfs_bucket_recover_commit_reqs(struct list_head *dst,
ret += nwritten;
freeme = pnfs_free_bucket_lseg(b);
if (freeme) {
- pnfs_put_lseg(freeme);
+ pnfs_lseg_release(freeme);
goto restart;
}
}
@@ -356,20 +356,31 @@ void pnfs_generic_recover_commit_reqs(struct list_head *dst,
}
EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
+static void pnfs_bucket_splice_committing(struct list_head *head,
+ struct pnfs_commit_bucket *bucket,
+ struct nfs_commit_info *cinfo)
+{
+ struct list_head *pos;
+
+ list_for_each(pos, &bucket->committing)
+ cinfo->ds->ncommitting--;
+ list_splice_init(&bucket->committing, head);
+}
+
+/*
+ * Returns a reference on the bucket's segment for the COMMIT, or NULL if
+ * the segment has been invalidated and the requests have to be written again.
+ */
static struct pnfs_layout_segment *
pnfs_bucket_get_committing(struct list_head *head,
struct pnfs_commit_bucket *bucket,
struct nfs_commit_info *cinfo)
{
struct pnfs_layout_segment *lseg;
- struct list_head *pos;
- list_for_each(pos, &bucket->committing)
- cinfo->ds->ncommitting--;
- list_splice_init(&bucket->committing, head);
- lseg = pnfs_free_bucket_lseg(bucket);
- if (!lseg)
- lseg = pnfs_get_lseg(bucket->lseg);
+ pnfs_bucket_splice_committing(head, bucket, cinfo);
+ lseg = pnfs_lseg_get_io(bucket->lseg);
+ pnfs_lseg_release(pnfs_free_bucket_lseg(bucket));
return lseg;
}
@@ -391,17 +402,20 @@ static void pnfs_generic_retry_commit(struct pnfs_commit_bucket *buckets,
unsigned int idx)
{
struct pnfs_commit_bucket *bucket;
- struct pnfs_layout_segment *freeme;
+ struct pnfs_layout_segment *lseg;
LIST_HEAD(pages);
for (bucket = buckets; idx < nbuckets; bucket++, idx++) {
if (list_empty(&bucket->committing))
continue;
mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
- freeme = pnfs_bucket_get_committing(&pages, bucket, cinfo);
+ /* An invalid segment sends the requests to resched_write */
+ lseg = pnfs_lseg_hold(bucket->lseg);
+ pnfs_bucket_splice_committing(&pages, bucket, cinfo);
+ pnfs_lseg_release(pnfs_free_bucket_lseg(bucket));
mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
- nfs_retry_commit(&pages, freeme, cinfo, idx);
- pnfs_put_lseg(freeme);
+ nfs_retry_commit(&pages, lseg, cinfo, idx);
+ pnfs_lseg_release(lseg);
}
}
@@ -498,7 +512,12 @@ pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
RPC_TASK_CRED_NOREF, NULL);
} else {
nfs_init_commit(data, NULL, data->lseg, cinfo);
- initiate_commit(data, how);
+ if (data->lseg) {
+ initiate_commit(data, how);
+ continue;
+ }
+ pnfs_generic_prepare_to_resend_writes(data);
+ pnfs_generic_commit_release(data);
}
}
out:
@@ -1358,14 +1377,14 @@ pnfs_layout_mark_request_commit(struct nfs_page *req,
goto out_resched;
bucket = &array->buckets[ds_commit_idx];
list = &bucket->written;
- /* Non-empty buckets hold a reference on the lseg. That ref
- * is normally transferred to the COMMIT call and released
- * there. It could also be released if the last req is pulled
- * off due to a rewrite, in which case it will be done in
- * pnfs_common_clear_request_commit
+ /* Non-empty buckets hold the lseg without keeping the layout in
+ * use. The COMMIT call takes its own reference. The hold is
+ * released when the bucket empties, which could also be when the
+ * last req is pulled off due to a rewrite, in which case it will
+ * be done in pnfs_generic_clear_request_commit
*/
if (!bucket->lseg)
- bucket->lseg = pnfs_get_lseg(lseg);
+ bucket->lseg = pnfs_lseg_hold(lseg);
set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
cinfo->ds->nwritten++;
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v4 10/11] NFS: wait and resend a folio refused with -EAGAIN in nfs_wb_folio()
2026-12-03 13:56 [PATCH v4 00/11] pNFS: layout recalls and writes that have to be resent Benjamin Coddington
` (8 preceding siblings ...)
2026-12-03 13:57 ` [PATCH v4 09/11] pNFS: don't keep a layout in use from a commit bucket Benjamin Coddington
@ 2026-12-03 13:57 ` Benjamin Coddington
2026-12-03 13:57 ` [PATCH v4 11/11] pNFS: answer a file recall with NFS4ERR_DELAY while I/O drains Benjamin Coddington
10 siblings, 0 replies; 12+ messages in thread
From: Benjamin Coddington @ 2026-12-03 13:57 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Mike Snitzer
Cc: Olga Kornievskaia, linux-nfs
From: Benjamin Coddington <bcodding@hammerspace.com>
A pNFS layout driver can refuse a write with -EAGAIN so that it is
resent. Flexfiles does so from ff_layout_pg_init_write() when the
layout segment's mirror count no longer matches the descriptor's, and
from ff_layout_pg_get_mirror_count_write() when there is no layout
segment and the file's layouts carry FF_FLAGS_NO_IO_THRU_MDS.
nfs_do_writepage() redirties the folio and returns the -EAGAIN.
nfs_writepages() resends it, but nfs_wb_folio() returns it to its callers:
nfs_updatepage(), nfs_flush_incompatible(), nfs_read_folio(),
nfs_invalidate_folio() and nfs_launder_folio(). A read(2) or write(2) on a
blocking file then fails with EAGAIN, for example when the folio is dirty
while a layout is being recalled.
Resend the folio from nfs_wb_folio(), and wait a second before every
resend after the first. The caller holds the folio lock throughout.
That used to be unsafe: writeback kept its layout segment in use while
it waited for that folio, so a recall, and with it the layout this
resend needs, could wait on the folio. A pageio descriptor no longer
keeps its segment in use.
A fatal signal ends the wait, and a softerr mount still gets the
-EAGAIN, as it does from nfs_writepages(). nfs_migrate_folio() doesn't
resend: it ignores the result and returns -EBUSY while the folio still
has requests, and resending would hold up compaction.
Fixes: 1d62e659c0bf ("NFSv4/flexfiles: honor FF_FLAGS_NO_IO_THRU_MDS in pg_get_mirror_count_write")
Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
fs/nfs/write.c | 37 +++++++++++++++++++++++++++----------
1 file changed, 27 insertions(+), 10 deletions(-)
diff --git a/fs/nfs/write.c b/fs/nfs/write.c
index 7967487e026c..dd41e3bd88aa 100644
--- a/fs/nfs/write.c
+++ b/fs/nfs/write.c
@@ -2073,15 +2073,8 @@ int nfs_wb_folio_reclaim(struct inode *inode, struct folio *folio)
return 0;
}
-/**
- * nfs_wb_folio - Write back all requests on one page
- * @inode: pointer to page
- * @folio: pointer to folio
- *
- * Assumes that the folio has been locked by the caller, and will
- * not unlock it.
- */
-int nfs_wb_folio(struct inode *inode, struct folio *folio)
+static int __nfs_wb_folio(struct inode *inode, struct folio *folio,
+ bool resend)
{
loff_t range_start = folio_pos(folio);
size_t len = folio_size(folio);
@@ -2091,6 +2084,7 @@ int nfs_wb_folio(struct inode *inode, struct folio *folio)
.range_start = range_start,
.range_end = range_start + len - 1,
};
+ bool resent = false;
int ret;
trace_nfs_writeback_folio(inode, range_start, len);
@@ -2099,6 +2093,16 @@ int nfs_wb_folio(struct inode *inode, struct folio *folio)
folio_wait_writeback(folio);
if (folio_clear_dirty_for_io(folio)) {
ret = nfs_writepage_locked(folio, &wbc);
+ if (ret == -EAGAIN && resend &&
+ !(NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTERR)) {
+ if (resent && schedule_timeout_killable(HZ) &&
+ fatal_signal_pending(current)) {
+ ret = -ERESTARTSYS;
+ goto out_error;
+ }
+ resent = true;
+ continue;
+ }
if (ret < 0)
goto out_error;
continue;
@@ -2115,6 +2119,19 @@ int nfs_wb_folio(struct inode *inode, struct folio *folio)
return ret;
}
+/**
+ * nfs_wb_folio - Write back all requests on one page
+ * @inode: pointer to page
+ * @folio: pointer to folio
+ *
+ * Assumes that the folio has been locked by the caller, and will
+ * not unlock it.
+ */
+int nfs_wb_folio(struct inode *inode, struct folio *folio)
+{
+ return __nfs_wb_folio(inode, folio, true);
+}
+
#ifdef CONFIG_MIGRATION
int nfs_migrate_folio(struct address_space *mapping, struct folio *dst,
struct folio *src, enum migrate_mode mode)
@@ -2129,7 +2146,7 @@ int nfs_migrate_folio(struct address_space *mapping, struct folio *dst,
*/
if (folio_test_private(src)) {
if (mode == MIGRATE_SYNC)
- nfs_wb_folio(src->mapping->host, src);
+ __nfs_wb_folio(src->mapping->host, src, false);
if (folio_test_private(src))
return -EBUSY;
}
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread* [PATCH v4 11/11] pNFS: answer a file recall with NFS4ERR_DELAY while I/O drains
2026-12-03 13:56 [PATCH v4 00/11] pNFS: layout recalls and writes that have to be resent Benjamin Coddington
` (9 preceding siblings ...)
2026-12-03 13:57 ` [PATCH v4 10/11] NFS: wait and resend a folio refused with -EAGAIN in nfs_wb_folio() Benjamin Coddington
@ 2026-12-03 13:57 ` Benjamin Coddington
10 siblings, 0 replies; 12+ messages in thread
From: Benjamin Coddington @ 2026-12-03 13:57 UTC (permalink / raw)
To: Trond Myklebust, Anna Schumaker, Mike Snitzer
Cc: Olga Kornievskaia, linux-nfs
From: Benjamin Coddington <bcodding@hammerspace.com>
A layout stateid can be invalidated without a LAYOUTRETURN, for example by
pnfs_destroy_layout() after an NFS4ERR_BADHANDLE, while I/O already sent to
the data servers still holds its layout segments.
pnfs_check_callback_stateid() then answers a CB_LAYOUTRECALL for the file
with NFS4ERR_NOMATCHING_LAYOUT, so the server treats the layout as returned
while that I/O can still reach the storage device.
If the recall matches the invalid layout stateid and a segment within the
recalled range is still in use, answer NFS4ERR_DELAY, and cancel the I/O
on those segments if clora_changed is set.
Fixes: bd3d16a887b0 ("NFSv4.1: Fix a potential layoutget/layoutrecall deadlock")
Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com>
---
fs/nfs/callback_proc.c | 8 ++++++++
fs/nfs/pnfs.c | 35 +++++++++++++++++++++++++++++++++++
fs/nfs/pnfs.h | 3 +++
3 files changed, 46 insertions(+)
diff --git a/fs/nfs/callback_proc.c b/fs/nfs/callback_proc.c
index f9362f7409d1..fb70b2af715c 100644
--- a/fs/nfs/callback_proc.c
+++ b/fs/nfs/callback_proc.c
@@ -276,6 +276,14 @@ static u32 initiate_file_draining(struct nfs_client *clp,
goto out;
}
pnfs_get_layout_hdr(lo);
+ if (!pnfs_layout_is_valid(lo) &&
+ nfs4_stateid_match_other(&lo->plh_stateid, &args->cbl_stateid) &&
+ pnfs_invalid_layout_busy(lo, &args->cbl_range,
+ be32_to_cpu(args->cbl_stateid.seqid),
+ args->cbl_layoutchanged)) {
+ rv = NFS4ERR_DELAY;
+ goto unlock;
+ }
rv = pnfs_check_callback_stateid(lo, &args->cbl_stateid, cps);
if (rv != NFS_OK)
goto unlock;
diff --git a/fs/nfs/pnfs.c b/fs/nfs/pnfs.c
index 4a0ae9ccd859..78cd101b7485 100644
--- a/fs/nfs/pnfs.c
+++ b/fs/nfs/pnfs.c
@@ -2889,6 +2889,41 @@ pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
return -ENOENT;
}
+/**
+ * pnfs_invalid_layout_busy - check for I/O on segments of an invalid layout
+ * @lo: pointer to the invalid layout header
+ * @recall_range: range being recalled
+ * @seq: seqid of the recall
+ * @cancel_io: cancel I/O on the matching segments
+ *
+ * A recall that is answered NFS4ERR_DELAY is sent again, so with @cancel_io
+ * the layout driver is asked to cancel the I/O on every pass until the
+ * segments are freed.
+ *
+ * Context: the caller holds the inode's i_lock.
+ * Return: true if any segment matching the recall is still in use.
+ */
+bool pnfs_invalid_layout_busy(struct pnfs_layout_hdr *lo,
+ const struct pnfs_layout_range *recall_range,
+ u32 seq, bool cancel_io)
+{
+ struct nfs_server *server = NFS_SERVER(lo->plh_inode);
+ struct pnfs_layout_segment *lseg;
+ bool busy = false;
+
+ assert_spin_locked(&lo->plh_inode->i_lock);
+
+ list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
+ if (!pnfs_match_lseg_recall(lseg, recall_range, seq))
+ continue;
+ busy = true;
+ if (!cancel_io)
+ break;
+ pnfs_lseg_cancel_io(server, lseg, NULL);
+ }
+ return busy;
+}
+
static void
pnfs_mark_layout_for_return(struct inode *inode,
const struct pnfs_layout_range *range,
diff --git a/fs/nfs/pnfs.h b/fs/nfs/pnfs.h
index 6b320c379467..94bf24eaa0dc 100644
--- a/fs/nfs/pnfs.h
+++ b/fs/nfs/pnfs.h
@@ -335,6 +335,9 @@ int pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
const struct pnfs_layout_range *recall_range,
u32 seq, bool cancel_io,
const struct nfs4_deviceid *devid);
+bool pnfs_invalid_layout_busy(struct pnfs_layout_hdr *lo,
+ const struct pnfs_layout_range *recall_range,
+ u32 seq, bool cancel_io);
int pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
struct list_head *lseg_list);
void pnfs_set_layout_drain(struct pnfs_layout_hdr *lo);
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread