Skip to contentSkip to conversation

pNFS: layout recalls and writes that have to be resent

PATCH New Benjamin Coddington 1 participant 13 messages Dec 4

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

v5 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 8 adds a second count on the layout segment, pls_hold, which keeps the segment allocated without keeping it on the layout. Patches 9 and 10 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 12 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.

A held segment keeps its mirrors on the flexfiles layout header's list after the segment has been returned, so patch 7 has the lookup for a deleted deviceid walk the segments that are on the layout.

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 8 to 10 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.

Patch 2 returns the error from an O_DIRECT read whose requests were all dropped, and ends the read's count at the first request 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 9 a task can wait for a layout with the folio locked, so patch 11 has nfs_wb_folio() wait and resend a folio that the layout driver refuses with -EAGAIN. That replaces 1/5. The rest of that series is posted on top of v4 of this one, and I'll rebase it on this version.

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 11 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 changes since v4 are for cases these tests don't reach, and were made from the review alone.

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 v4, from the sashiko review:
https://lore.kernel.org/linux-nfs/[email protected]/
- New patch 7: the flexfiles lookup for a deleted deviceid walks the layout's segments.
- Patch 9 (was 8): a LAYOUTERROR takes its segment reference only if the segment still has one. A descriptor's segment may have left the layout by then, and v4 took the reference from zero.
- Patch 9: a write that is rescheduled from the submitter drops its open context reference through nfsiod, as writeback must.
- Patch 9: the reference for I/O is taken without the inode's i_lock.
- Patch 10 (was 9): pnfs_generic_commit_pagelist() keeps the layout header while it goes through the commit lists. In v4 a commit bucket's hold could be the last reference on the header that the lists are part of.
- Patch 11 (was 10): nfs_launder_folio() doesn't resend from an exiting task or a kernel thread, where no signal ends the wait, and a fatal signal is checked after every wait.
- Patch 4: a failure to create the DS RPC client returns the layout unless the write goes to the MDS. v4 retried at once on the same layout.
- Patch 2: the read's count ends at the first request dropped. A pNFS read that is resent can drop requests from the middle.
- Patch 3: an O_DIRECT write that fails with a hard error ends its count at the request that was refused, whose copy another mirror may have sent.
- Patches 3 and 4: the stable tag is v6.6+, where patch 1 applies.
- Changelogs of patches 10 and 11 corrected (LOCALIO commit, nfs_update_folio()).

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 8 to 10: the pageio descriptor and the commit buckets don't keep a layout in use.
- Patch 12 (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 11. 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: a fatal error preparing the DS no longer returns the layout under FF_FLAGS_NO_IO_THRU_MDS.
- Patch 1: unchanged.

Benjamin Coddington (12):
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/flexfiles: look for a deviceid in the layout's segments
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                        |  74 +++++---
 fs/nfs/file.c                          |   6 +-
 fs/nfs/filelayout/filelayout.c         |   2 +-
 fs/nfs/flexfilelayout/flexfilelayout.c | 117 ++++++++-----
 fs/nfs/inode.c                         |   1 +
 fs/nfs/internal.h                      |   3 +-
 fs/nfs/nfs42proc.c                     |   6 +-
 fs/nfs/pagelist.c                      |  46 ++++-
 fs/nfs/pnfs.c                          | 233 +++++++++++++++++++++----
 fs/nfs/pnfs.h                          |  12 ++
 fs/nfs/pnfs_nfs.c                      |  74 +++++---
 fs/nfs/read.c                          |   3 +-
 fs/nfs/write.c                         |  49 ++++--
 include/linux/nfs_fs.h                 |   1 +
 include/linux/nfs_xdr.h                |   3 +-
 16 files changed, 499 insertions(+), 139 deletions(-)

base-commit: 3e19991734326332c736e24c9b47b2cffb8f9e42

Signature · 1 line
2.53.0
#1 lore raw

PATCH v5 1/12 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 v5 2/12 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, and end the read's count at the first request dropped. Most often the requests dropped are the last ones of the read, but a pNFS read that is resent after an error can drop requests from the middle, and the completion of a later part would count over them. A read that got data before the first dropped request 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 | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/nfs/direct.c +8 -0
index c771bb1b0730..77df7e3b8224 100644
@@ -333,6 +333,14 @@ 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)
 {
+	struct nfs_page *req;
+
+	spin_lock(&dreq->lock);
+	list_for_each_entry(req, head, wb_list)
+		nfs_direct_truncate_request(dreq, req);
+	if (!list_empty(head) && !dreq->error)
+		dreq->error = error;
+	spin_unlock(&dreq->lock);
 	nfs_release_request_list(head);
 }
 
Signature · 1 line
2.53.0
#3 lore raw

PATCH v5 3/12 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.

A mirror can now have sent its copy of a request that the current mirror goes on to refuse. If an O_DIRECT write fails there with a hard error, end the write's count at that request: its bytes were never counted as requested.

Fixes a7d42ddb3099 ("nfs: add mirroring support to pgio layer")
---
 fs/nfs/direct.c   |  4 ++++
 fs/nfs/pagelist.c | 39 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 42 insertions(+), 1 deletion(-)
fs/nfs/direct.c +4 -0
index 77df7e3b8224..274ed8aabb78 100644
@@ -937,6 +937,10 @@ static ssize_t nfs_direct_write_schedule_iovec(struct nfs_direct_req *dreq,
 			/* Exit on hard errors */
 			if (desc.pg_error < 0 && desc.pg_error != -EAGAIN) {
 				result = desc.pg_error;
+				/* A mirror may have sent its copy of req */
+				spin_lock(&dreq->lock);
+				nfs_direct_truncate_request(dreq, req);
+				spin_unlock(&dreq->lock);
 				nfs_unlock_and_release_request(req);
 				nfs_release_request_list(&nfs_page_list);
 				break;
fs/nfs/pagelist.c +38 -1
index 0bb18acaf872..0a7b140c0e1f 100644
@@ -1202,6 +1202,43 @@ 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.  A mirror that asks for a retry is recoalesced
+ * while the others still hold their requests.
+ */
+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 +1300,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 v5 4/12 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. 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 failure to create the DS RPC client comes after the DS is prepared, so pg_init has nothing to wait for and the write would be refused again at once. Return the layout for that failure whenever the write doesn't go to the MDS, including when another DS is available, so that the retry waits for a new layout. 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 | 56 ++++++++++++++++++++------
 1 file changed, 43 insertions(+), 13 deletions(-)
fs/nfs/flexfilelayout/flexfilelayout.c +43 -13
index a03ef583c391..7af1d0a94e64 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,
@@ -2421,6 +2439,7 @@ ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
 	u32 idx = hdr->pgio_mirror_idx;
 	u32 dss_id;
 	bool ds_fatal_error = false;
+	bool ds_clnt_error = false;
 
 	mirror = FF_LAYOUT_COMP(lseg, idx);
 	dss_id = nfs4_ff_layout_calc_dss_id(
@@ -2437,8 +2456,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_clnt_error = true;
 		goto out_failed;
+	}
 
 	ds_cred = ff_layout_get_ds_cred(mirror, &lseg->pls_range, hdr->cred,
 					mirror_ds, dss_id);
@@ -2491,21 +2512,30 @@ ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
 
 out_failed:
 	nfs4_ff_layout_put_deviceid(mirror_ds);
-	if (ff_layout_avoid_mds_available_ds(lseg) && !ds_fatal_error)
+	/*
+	 * The DS is already prepared when its RPC client can't be created,
+	 * so a retry on this layout would fail the same way at once.
+	 */
+	if (ff_layout_avoid_mds_available_ds(lseg) && !ds_fatal_error &&
+	    !ds_clnt_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 v5 5/12 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 274ed8aabb78..2dd78fc12e2e 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;
@@ -748,7 +748,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;
@@ -769,7 +770,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 v5 6/12 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 2dd78fc12e2e..66b87965e77b 100644
@@ -635,6 +635,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);
 }
@@ -753,6 +758,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);
@@ -771,7 +777,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 v5 7/12 pNFS/flexfiles: look for a deviceid in the layout's segments

In reply to Benjamin Coddington

From: Benjamin Coddington <[email protected]>

ff_layout_references_deviceid() walks the layout header's mirror list. A mirror stays on that list until the last segment that uses it is freed. The next patch lets a segment stay allocated after it has left the layout and been returned, and the recovery for a deleted deviceid would then find a reference in a layout the server no longer counts it in. If the layout stateid is still good, that re-establishes the client ID.

Walk the segments on the layout and those waiting for their LAYOUTRETURN.

---
 fs/nfs/flexfilelayout/flexfilelayout.c | 28 ++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
fs/nfs/flexfilelayout/flexfilelayout.c +22 -6
index 7af1d0a94e64..f663dc026347 100644
@@ -2707,19 +2707,35 @@ static void ff_layout_cancel_io(struct pnfs_layout_segment *lseg,
 	}
 }
 
-/* Called under @lo's inode i_lock. */
-static bool ff_layout_references_deviceid(struct pnfs_layout_hdr *lo,
-					  const struct nfs4_deviceid *id)
+static bool ff_lseg_references_deviceid(struct pnfs_layout_segment *lseg,
+					const struct nfs4_deviceid *id)
 {
-	struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo);
 	struct nfs4_ff_layout_mirror *mirror;
-	u32 dss_id;
+	u32 idx, dss_id;
 
-	list_for_each_entry(mirror, &flo->mirrors, mirrors)
+	for (idx = 0; idx < FF_LAYOUT_MIRROR_COUNT(lseg); idx++) {
+		mirror = FF_LAYOUT_COMP(lseg, idx);
 		for (dss_id = 0; dss_id < mirror->dss_count; dss_id++)
 			if (memcmp(&mirror->dss[dss_id].devid, id,
 				   sizeof(*id)) == 0)
 				return true;
+	}
+	return false;
+}
+
+/* Called under @lo's inode i_lock. */
+static bool ff_layout_references_deviceid(struct pnfs_layout_hdr *lo,
+					  const struct nfs4_deviceid *id)
+{
+	struct pnfs_layout_segment *lseg;
+
+	list_for_each_entry(lseg, &lo->plh_segs, pls_list)
+		if (ff_lseg_references_deviceid(lseg, id))
+			return true;
+	/* The server has these until their LAYOUTRETURN is done */
+	list_for_each_entry(lseg, &lo->plh_return_segs, pls_list)
+		if (ff_lseg_references_deviceid(lseg, id))
+			return true;
 	return false;
 }
 
Signature · 1 line
2.53.0
#8 lore raw

PATCH v5 8/12 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
#9 lore raw

PATCH v5 9/12 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. That happens in the submitter, which may be writeback, so the open context reference is dropped with put_nfs_open_context_async().

The segment may have left the layout by the time pg_init reports a data server error on it. nfs42_alloc_layouterror_data() takes its reference only if the segment still has one, and no LAYOUTERROR is sent otherwise.

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/inode.c                         |   1 +
 fs/nfs/nfs42proc.c                     |   6 +-
 fs/nfs/pnfs.c                          | 146 ++++++++++++++++++++-----
 fs/nfs/pnfs.h                          |   5 +
 6 files changed, 138 insertions(+), 55 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 f663dc026347..a003599b4317 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/inode.c +1 -0
index 1d79ee0ce3e3..cd48e3578145 100644
@@ -1331,6 +1331,7 @@ void put_nfs_open_context_async(struct nfs_open_context *ctx)
 	INIT_WORK(&ctx->free_work, nfs_free_open_context_work);
 	queue_work(nfsiod_workqueue, &ctx->free_work);
 }
+EXPORT_SYMBOL_GPL(put_nfs_open_context_async);
 
 static void put_nfs_open_context_sync(struct nfs_open_context *ctx)
 {
fs/nfs/nfs42proc.c +4 -2
index c0c4d99d354b..e7dc017c8a7d 100644
@@ -1105,9 +1105,11 @@ nfs42_alloc_layouterror_data(struct pnfs_layout_segment *lseg, gfp_t gfp_flags)
 	if (data) {
 		data->args.inode = data->inode = nfs_igrab_and_active(inode);
 		if (data->inode) {
-			data->lseg = pnfs_get_lseg(lseg);
-			if (data->lseg)
+			/* A held segment may have left its layout */
+			if (refcount_inc_not_zero(&lseg->pls_refcount)) {
+				data->lseg = lseg;
 				return data;
+			}
 			nfs_iput_and_deactive(data->inode);
 		}
 		kfree(data);
fs/nfs/pnfs.c +119 -27

220 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
#10 lore raw

PATCH v5 10/12 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. A LOCALIO commit doesn't go through rpc_call_prepare and used to be done; it writes again now, and so does the files layout driver, which used to send the COMMIT.

The bucket's hold can be the last reference on the layout header, and the commit lists of a buffered write are part of the header. pnfs_generic_commit_pagelist() keeps the header while it goes through them.

---
 fs/nfs/pnfs.c     |  2 +-
 fs/nfs/pnfs.h     |  1 +
 fs/nfs/pnfs_nfs.c | 74 +++++++++++++++++++++++++++++++++--------------
 3 files changed, 55 insertions(+), 22 deletions(-)
fs/nfs/pnfs.c +1 -1
index ff0053ce722b..694e08e4a447 100644
@@ -679,7 +679,7 @@ EXPORT_SYMBOL_GPL(pnfs_lseg_ref_to_hold);
  * reference is dropped, so a recall either finds this reference or keeps
  * it from being used.
  */
-static struct pnfs_layout_segment *
+struct pnfs_layout_segment *
 pnfs_lseg_get_io(struct pnfs_layout_segment *lseg)
 {
 	if (!lseg || !refcount_inc_not_zero(&lseg->pls_refcount))
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 +53 -21

147 lines not on this page · raw message

Signature · 1 line
2.53.0
#11 lore raw

PATCH v5 11/12 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_update_folio() through nfs_try_to_update_request(), 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. nfs_launder_folio() doesn't resend from an exiting task or a kernel thread, where a file can be released and no signal ends the wait.

Fixes 1d62e659c0bf ("NFSv4/flexfiles: honor FF_FLAGS_NO_IO_THRU_MDS in pg_get_mirror_count_write")
---
 fs/nfs/file.c          |  6 +++++-
 fs/nfs/write.c         | 44 ++++++++++++++++++++++++++++++++----------
 include/linux/nfs_fs.h |  1 +
 3 files changed, 40 insertions(+), 11 deletions(-)
fs/nfs/file.c +5 -1
index b377c16c92fe..e90073b955cb 100644
@@ -571,7 +571,11 @@ static int nfs_launder_folio(struct folio *folio)
 		inode->i_ino, folio_pos(folio));
 
 	folio_wait_private_2(folio); /* [DEPRECATED] */
-	ret = nfs_wb_folio(inode, folio);
+	/* No signal ends a wait to resend in an exiting task or a kthread */
+	if (nfs_current_task_exiting() || (current->flags & PF_KTHREAD))
+		ret = nfs_wb_folio_noresend(inode, folio);
+	else
+		ret = nfs_wb_folio(inode, folio);
 	trace_nfs_launder_folio_done(inode, folio_pos(folio),
 			folio_size(folio), ret);
 	return ret;
fs/nfs/write.c +34 -10

74 lines not on this page · raw message

include/linux/nfs_fs.h +1 -0
index 29f59796ed57..da8931814516 100644
@@ -627,6 +627,7 @@ extern int  nfs_update_folio(struct file *file, struct folio *folio,
 extern int nfs_sync_inode(struct inode *inode);
 extern int nfs_wb_all(struct inode *inode);
 extern int nfs_wb_folio(struct inode *inode, struct folio *folio);
+int nfs_wb_folio_noresend(struct inode *inode, struct folio *folio);
 extern int nfs_wb_folio_reclaim(struct inode *inode, struct folio *folio);
 int nfs_wb_folio_cancel(struct inode *inode, struct folio *folio);
 extern int  nfs_commit_inode(struct inode *, int);
Signature · 1 line
2.53.0
#12 lore raw

PATCH v5 12/12 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 694e08e4a447..d537138c1492 100644
@@ -2887,6 +2887,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