Skip to contentSkip to conversation

pNFS: layout recalls and writes that have to be resent

PATCH New Benjamin Coddington 1 participant 12 messages Dec 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover pNFS: layout recalls and writes that have to be resent

From: Benjamin Coddington <[email protected]>

v3 answered a CB_LAYOUTRECALL with NFS4ERR_DELAY while I/O on the layout drains. Trond pointed out on 5/6 that a layout segment reference does not mean I/O is in flight: a pageio descriptor keeps its segment while it waits for a folio lock or faults in an O_DIRECT buffer, and a commit bucket keeps one until the COMMIT is sent. Those holders keep a recall from completing, and what they wait for can be waiting for the layout the recall holds back. Two cases deadlock, with or without v3:

- An O_DIRECT write whose buffer is an mmap of the same file. The writer faults the buffer in with its descriptor and its commit buckets holding segments, and the fault's read waits for a layout.

- Writeback that has queued one folio and waits for the lock on the next, while the task holding that lock is in nfs_wb_folio() or nfs_read_folio() waiting for a layout.

This version stops those holders from keeping the layout in use. Patch 7 adds a second count on the layout segment, pls_hold, which keeps the segment allocated without keeping it on the layout. Patches 8 and 9 move the pageio descriptor and the commit buckets to it. They take a reference only when they put I/O or a COMMIT on the wire, and if the segment has been invalidated by then the requests are sent again with a new one. Patch 11 is v3's file recall patch without the flag that blocked LAYOUTGET: it answers NFS4ERR_DELAY only while a segment in the recalled range is in use.

That resend path is the one the mirrored write fallback series fixes, and the recall tests don't pass without it, so its remaining three patches are here as patches 1, 3 and 4, and that series ends:
https://lore.kernel.org/linux-nfs/[email protected]/
With patches 7 to 9 and without patch 1, an O_DIRECT write under recalls returns success with mirrors left holding old data. Without patch 4, KASAN reports a use-after-free of the mirror array. Patches 3 and 4 have changes from the sashiko review of that posting, listed below.

Patch 2 returns the error from an O_DIRECT read whose requests were all dropped. Patches 5 and 6 pace the O_DIRECT write resend, which otherwise runs without a pause until a layout is available. Patch 5 is 2/5 of the FF_FLAGS_NO_IO_THRU_MDS series unchanged:
https://lore.kernel.org/linux-nfs/[email protected]/

Splitting the references also solves the nfs_wb_folio() problem I asked about on 1/5 of the FF_FLAGS_NO_IO_THRU_MDS series. With patch 8 a task can wait for a layout with the folio locked, so patch 10 has nfs_wb_folio() wait and resend a folio that the layout driver refuses with -EAGAIN. That replaces 1/5, and I'll repost the rest of that series on top of this one.

v3's 6/6 (write through the MDS while a recalled layout drains) is dropped.

Tested on a KASAN kernel against a reffs MDS that hands out six-way mirrored flexfiles layouts with FF_FLAGS_NO_IO_THRU_MDS, with knfsd as the NFSv3 data server so that writes are UNSTABLE and need a COMMIT:

- Buffered and O_DIRECT readers and writers on three files while FSID, ALL and FILE recalls, with clora_changed set and clear, run in a loop. The files and each of the six mirrors match the data written. On the base, a buffered write(2) in this test fails with EAGAIN in some runs. With patch 10 it doesn't.

- The O_DIRECT write from an mmap of the same file, with an FSID recall sent while the writer is between chunks. On the base the writer stays in ff_layout_pg_retry_wait() until it is killed. With the series the write completes and the recall gets NFS4ERR_NOMATCHING_LAYOUT.

- Writeback stopped after queueing its first folio, with a test-only delay, while a FILE recall is outstanding and a second task reads or writes the next dirty folio. On the base both tasks hang. With the series both finish and the data is correct.

- Buffered data written UNSTABLE and not yet committed when an FSID or FILE recall arrives. The data is written again with the new layout and reads back correctly.

Not tested: layouts with a single mirror, the files layout driver, and the block layout drivers.

The series is based on Trond's testing branch at 3e1999173432, which has 1/6 to 4/6 of v3 and 2/4 of the mirrored write fallback series.

Changes since v3:
https://lore.kernel.org/linux-nfs/[email protected]/
- 1/6 to 4/6 are in Trond's testing branch and are not reposted.
- New patches 7 to 9: the pageio descriptor and the commit buckets don't keep a layout in use.
- Patch 11 (was 5/6): doesn't set NFS_LAYOUT_BULK_RECALL, so LAYOUTGET is not blocked after the recall completes and a partial recall only waits for segments in its range. I/O is cancelled on every recall with clora_changed set.
- 6/6 dropped.
- New patches 2, 6 and 10. Patch 5 is from the FF_FLAGS_NO_IO_THRU_MDS series.

Changes to patches 1, 3 and 4 since v2 of the mirrored write fallback series:
- Patch 3: when the current mirror asks for a retry, the other mirrors are not sent until it has been coalesced again. The changelog no longer says the mirrors stay in step when their wsize differs.
- Patch 4: failing to create the DS RPC client is treated like a fatal error preparing the DS, and a fatal error no longer returns the layout under FF_FLAGS_NO_IO_THRU_MDS.
- Patch 1: unchanged.

Benjamin Coddington (11):
NFS: resend O_DIRECT write requests that error cleanup gets with
-EAGAIN
NFS: report the error when an O_DIRECT read's requests are never sent
NFS: send every mirror when one of them can take no more pNFS/flexfiles: don't switch a busy mirrored write descriptor to the
MDS
NFS: make the O_DIRECT write work a delayed_work
NFS: wait before resending an O_DIRECT write that was refused again
pNFS: add a layout segment hold that doesn't keep the layout in use
pNFS: don't keep a layout in use from an idle pageio descriptor
pNFS: don't keep a layout in use from a commit bucket
NFS: wait and resend a folio refused with -EAGAIN in nfs_wb_folio()
pNFS: answer a file recall with NFS4ERR_DELAY while I/O drains

 fs/nfs/callback_proc.c                 |   8 +
 fs/nfs/direct.c                        |  68 +++++---
 fs/nfs/filelayout/filelayout.c         |   2 +-
 fs/nfs/flexfilelayout/flexfilelayout.c |  81 +++++----
 fs/nfs/internal.h                      |   3 +-
 fs/nfs/pagelist.c                      |  45 ++++-
 fs/nfs/pnfs.c                          | 226 +++++++++++++++++++++----
 fs/nfs/pnfs.h                          |  12 ++
 fs/nfs/pnfs_nfs.c                      |  61 ++++---
 fs/nfs/read.c                          |   3 +-
 fs/nfs/write.c                         |  42 +++--
 include/linux/nfs_xdr.h                |   3 +-
 12 files changed, 425 insertions(+), 129 deletions(-)

base-commit: 3e19991734326332c736e24c9b47b2cffb8f9e42

Signature · 1 line
2.53.0
#1 lore raw

PATCH v4 1/11 NFS: resend O_DIRECT write requests that error cleanup gets with -EAGAIN

From: Benjamin Coddington <[email protected]>

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")
---
 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(-)
fs/nfs/direct.c +30 -18
index a3c6e8f4ea06..c771bb1b0730 100644
@@ -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,
fs/nfs/pagelist.c +4 -3
index c76c32a33be5..0bb18acaf872 100644
@@ -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;
 	}
fs/nfs/read.c +2 -1
index 1fdca07761f6..ad9e948f4cb1 100644
@@ -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;
 
fs/nfs/write.c +3 -2
index af8735d0d67f..7967487e026c 100644
@@ -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 = {
include/linux/nfs_xdr.h +2 -1
index c0e29b4dfa62..fce0e773bd7b 100644
@@ -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);
Signature · 1 line
2.53.0
#2 lore raw

PATCH v4 2/11 NFS: report the error when an O_DIRECT read's requests are never sent

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

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")
---
 fs/nfs/direct.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/nfs/direct.c +6 -0
index c771bb1b0730..4630dddeafa1 100644
@@ -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);
 }
 
Signature · 1 line
2.53.0
#3 lore raw

PATCH v4 3/11 NFS: send every mirror when one of them can take no more

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

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")
---
 fs/nfs/pagelist.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)
fs/nfs/pagelist.c +37 -1
index 0bb18acaf872..504b880f15a0 100644
@@ -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 */
Signature · 1 line
2.53.0
#4 lore raw

PATCH v4 4/11 pNFS/flexfiles: don't switch a busy mirrored write descriptor to the MDS

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

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")
---
 fs/nfs/flexfilelayout/flexfilelayout.c | 48 +++++++++++++++++++-------
 1 file changed, 36 insertions(+), 12 deletions(-)
fs/nfs/flexfilelayout/flexfilelayout.c +36 -12
index a03ef583c391..309f50b29ccd 100644
@@ -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 *
Signature · 1 line
2.53.0
#5 lore raw

PATCH v4 5/11 NFS: make the O_DIRECT write work a delayed_work

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

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.

---
 fs/nfs/direct.c   | 8 +++++---
 fs/nfs/internal.h | 2 +-
 2 files changed, 6 insertions(+), 4 deletions(-)
fs/nfs/direct.c +5 -3
index 4630dddeafa1..b355d2bbd919 100644
@@ -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)
fs/nfs/internal.h +1 -1
index f3ca9be5e08a..f34da925f3d5 100644
@@ -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 */
Signature · 1 line
2.53.0
#6 lore raw

PATCH v4 6/11 NFS: wait before resending an O_DIRECT write that was refused again

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

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.

---
 fs/nfs/direct.c   | 8 +++++++-
 fs/nfs/internal.h | 1 +
 2 files changed, 8 insertions(+), 1 deletion(-)
fs/nfs/direct.c +7 -1
index b355d2bbd919..efce806c2a95 100644
@@ -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)
fs/nfs/internal.h +1 -0
index f34da925f3d5..5a5c1b960546 100644
@@ -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 */
Signature · 1 line
2.53.0
#7 lore raw

PATCH v4 7/11 pNFS: add a layout segment hold that doesn't keep the layout in use

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

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.

---
 fs/nfs/pnfs.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---
 fs/nfs/pnfs.h |  3 +++
 2 files changed, 52 insertions(+), 3 deletions(-)
fs/nfs/pnfs.c +49 -3
index d2ff78acbed9..58ba779a8711 100644
@@ -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,
fs/nfs/pnfs.h +3 -0
index bbe56b682dc0..2b8e77f0437b 100644
@@ -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 *);
Signature · 1 line
2.53.0
#8 lore raw

PATCH v4 8/11 pNFS: don't keep a layout in use from an idle pageio descriptor

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

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.

---
 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(-)
fs/nfs/filelayout/filelayout.c +1 -1
index 53808e42546a..4b466c57d75d 100644
@@ -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
fs/nfs/flexfilelayout/flexfilelayout.c +8 -25
index 309f50b29ccd..0c5004ec1c3b 100644
@@ -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);
fs/nfs/pnfs.c +112 -27

213 lines not on this page · raw message

fs/nfs/pnfs.h +5 -0
index 2b8e77f0437b..e05f37bdb916 100644
@@ -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 *);
Signature · 1 line
2.53.0
#9 lore raw

PATCH v4 9/11 pNFS: don't keep a layout in use from a commit bucket

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

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.

---
 fs/nfs/pnfs.c     |  2 +-
 fs/nfs/pnfs.h     |  1 +
 fs/nfs/pnfs_nfs.c | 61 +++++++++++++++++++++++++++++++----------------
 3 files changed, 42 insertions(+), 22 deletions(-)
fs/nfs/pnfs.c +1 -1
index 3c01acca147a..4a0ae9ccd859 100644
@@ -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;
fs/nfs/pnfs.h +1 -0
index e05f37bdb916..6b320c379467 100644
@@ -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);
fs/nfs/pnfs_nfs.c +40 -21

119 lines not on this page · raw message

Signature · 1 line
2.53.0
#10 lore raw

PATCH v4 10/11 NFS: wait and resend a folio refused with -EAGAIN in nfs_wb_folio()

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

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")
---
 fs/nfs/write.c | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)
fs/nfs/write.c +27 -10
index 7967487e026c..dd41e3bd88aa 100644
@@ -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;
 	}
Signature · 1 line
2.53.0
#11 lore raw

PATCH v4 11/11 pNFS: answer a file recall with NFS4ERR_DELAY while I/O drains

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

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")
---
 fs/nfs/callback_proc.c |  8 ++++++++
 fs/nfs/pnfs.c          | 35 +++++++++++++++++++++++++++++++++++
 fs/nfs/pnfs.h          |  3 +++
 3 files changed, 46 insertions(+)
fs/nfs/callback_proc.c +8 -0
index f9362f7409d1..fb70b2af715c 100644
@@ -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;
fs/nfs/pnfs.c +35 -0
index 4a0ae9ccd859..78cd101b7485 100644
@@ -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,
fs/nfs/pnfs.h +3 -0
index 6b320c379467..94bf24eaa0dc 100644
@@ -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);
Signature · 1 line
2.53.0