Skip to contentSkip to conversation

nfsd: CB_SEQUENCE error handling fixes and cleanups

PATCH In review Jeff Layton 2 participants 9 messages Feb 9, 2025

Subsystems (2)
lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

v6 cover nfsd: CB_SEQUENCE error handling fixes and cleanups

This patch is mostly the same as the v5 series. Just small cleanups and dropping of special NFS4ERR_SEQ_MISORDERED handling.

---
Changes in v6:
- Reorder patch to move v4.0 handling to earlier in series
- Drop special handling of NFS4ERR_SEQ_MISORDERED. Always treat it like NFS4ERR_BADSLOT.
- rename requeue_callback() to nfsd4_queue_cb()
- Comment and changelog cleanups
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- don't ignore return of rpc_restart_call() and rpc_restart_call_prepare()
- Break up the nfsd4_cb_sequence_done() error handling changes into multiple patches
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Hold back on session refcounting changes for now and just send CB_SEQUENCE error handling rework.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- rename cb_session_changed to nfsd4_cb_session_changed
- rename restart_callback to requeue_callback, and rename need_restart:
label to requeue:
- don't increment seq_nr on -ESERVERFAULT
- comment cleanups
- drop client-side rpc patch (will send separately)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- make nfsd4_session be RCU-freed
- change code to keep reference to session over callback RPCs
- rework error handling in nfsd4_cb_sequence_done()
- move NFSv4.0 handling out of nfsd4_cb_sequence_done()
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Jeff Layton (7):
nfsd: prepare nfsd4_cb_sequence_done() for error handling rework
nfsd: lift NFSv4.0 handling out of nfsd4_cb_sequence_done()
nfsd: always release slot when requeueing callback
nfsd: only check RPC_SIGNALLED() when restarting rpc_task
nfsd: when CB_SEQUENCE gets ESERVERFAULT don't increment seq_nr
nfsd: handle NFS4ERR_BADSLOT on CB_SEQUENCE better
nfsd: eliminate special handling of NFS4ERR_SEQ_MISORDERED

 fs/nfsd/nfs4callback.c | 100 +++++++++++++++++++++++++++----------------------
 1 file changed, 56 insertions(+), 44 deletions(-)

---
base-commit: 50934b1a613cabba2b917879c3e722882b72f628
change-id: 20250123-nfsd-6-14-b0797e385dc0

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>
author maintainer
#1 lore raw

PATCH v6 1/7 nfsd: prepare nfsd4_cb_sequence_done() for error handling rework

There is only one case where we want to proceed with processing the rest of the CB_COMPOUND, and that's when the cb_seq_status is 0. Make the default return value be false, and only set it to true in that case.

Rename the "need_restart" label to "requeue", to better indicate that it's being requeued to the workqueue.

---
 fs/nfsd/nfs4callback.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
fs/nfsd/nfs4callback.c +9 -11
index cf6d29828f4e561418b812ea2c9402929dd52bd0..79abc981e6416a88d9a81497e03e12faa3ce6d0e 100644
@@ -1328,11 +1328,12 @@ static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
 	rpc_call_start(task);
 }
 
+/* Returns true if CB_COMPOUND processing should continue */
 static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback *cb)
 {
 	struct nfs4_client *clp = cb->cb_clp;
 	struct nfsd4_session *session = clp->cl_cb_session;
-	bool ret = true;
+	bool ret = false;
 
 	if (!clp->cl_minorversion) {
 		/*
@@ -1345,13 +1346,13 @@ static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback
 		 * handle that case here.
 		 */
 		if (RPC_SIGNALLED(task))
-			goto need_restart;
+			goto requeue;
 
 		return true;
 	}
 
 	if (cb->cb_held_slot < 0)
-		goto need_restart;
+		goto requeue;
 
 	/* This is the operation status code for CB_SEQUENCE */
 	trace_nfsd_cb_seq_status(task, cb);
@@ -1365,11 +1366,11 @@ static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback
 		 * (sequence ID, cached reply) MUST NOT change.
 		 */
 		++session->se_cb_seq_nr[cb->cb_held_slot];
+		ret = true;
 		break;
 	case -ESERVERFAULT:
 		++session->se_cb_seq_nr[cb->cb_held_slot];
 		nfsd4_mark_cb_fault(cb->cb_clp);
-		ret = false;
 		break;
 	case 1:
 		/*
@@ -1381,13 +1382,11 @@ static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback
 		fallthrough;
 	case -NFS4ERR_BADSESSION:
 		nfsd4_mark_cb_fault(cb->cb_clp);
-		ret = false;
-		goto need_restart;
+		goto requeue;
 	case -NFS4ERR_DELAY:
 		cb->cb_seq_status = 1;
 		if (!rpc_restart_call(task))
 			goto out;
-
 		rpc_delay(task, 2 * HZ);
 		return false;
 	case -NFS4ERR_BADSLOT:
@@ -1405,14 +1404,13 @@ static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback
 	nfsd41_cb_release_slot(cb);
 
 	if (RPC_SIGNALLED(task))
-		goto need_restart;
+		goto requeue;
 out:
 	return ret;
 retry_nowait:
-	if (rpc_restart_call_prepare(task))
-		ret = false;
+	rpc_restart_call_prepare(task);
 	goto out;
-need_restart:
+requeue:
 	if (!test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags)) {
 		trace_nfsd_cb_restart(clp, cb);
 		task->tk_status = 0;
Signature · 1 line
2.48.1
author maintainer
#2 lore raw

PATCH v6 2/7 nfsd: lift NFSv4.0 handling out of nfsd4_cb_sequence_done()

In reply to Jeff Layton

It's a bit strange to call nfsd4_cb_sequence_done() on a callback with no CB_SEQUENCE. Lift the handling of restarting a call into a new helper, and move the handling of NFSv4.0 into nfsd4_cb_done().

---
 fs/nfsd/nfs4callback.c | 51 ++++++++++++++++++++++++++------------------------
 1 file changed, 27 insertions(+), 24 deletions(-)
fs/nfsd/nfs4callback.c +27 -24
index 79abc981e6416a88d9a81497e03e12faa3ce6d0e..f983d2879bd3fe4c5aa2b0381f968fac753d79dc 100644
@@ -1328,28 +1328,22 @@ static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
 	rpc_call_start(task);
 }
 
-/* Returns true if CB_COMPOUND processing should continue */
-static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback *cb)
+static void nfsd4_requeue_cb(struct rpc_task *task, struct nfsd4_callback *cb)
 {
 	struct nfs4_client *clp = cb->cb_clp;
-	struct nfsd4_session *session = clp->cl_cb_session;
-	bool ret = false;
-
-	if (!clp->cl_minorversion) {
-		/*
-		 * If the backchannel connection was shut down while this
-		 * task was queued, we need to resubmit it after setting up
-		 * a new backchannel connection.
-		 *
-		 * Note that if we lost our callback connection permanently
-		 * the submission code will error out, so we don't need to
-		 * handle that case here.
-		 */
-		if (RPC_SIGNALLED(task))
-			goto requeue;
 
-		return true;
+	if (!test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags)) {
+		trace_nfsd_cb_restart(clp, cb);
+		task->tk_status = 0;
+		cb->cb_need_restart = true;
 	}
+}
+
+/* Returns true if CB_COMPOUND processing should continue */
+static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback *cb)
+{
+	struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
+	bool ret = false;
 
 	if (cb->cb_held_slot < 0)
 		goto requeue;
@@ -1411,11 +1405,7 @@ static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback
 	rpc_restart_call_prepare(task);
 	goto out;
 requeue:
-	if (!test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags)) {
-		trace_nfsd_cb_restart(clp, cb);
-		task->tk_status = 0;
-		cb->cb_need_restart = true;
-	}
+	nfsd4_requeue_cb(task, cb);
 	return false;
 }
 
@@ -1426,8 +1416,21 @@ static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
 
 	trace_nfsd_cb_rpc_done(clp);
 
-	if (!nfsd4_cb_sequence_done(task, cb))
+	if (!clp->cl_minorversion) {
+		/*
+		 * If the backchannel connection was shut down while this
+		 * task was queued, we need to resubmit it after setting up
+		 * a new backchannel connection.
+		 *
+		 * Note that if we lost our callback connection permanently
+		 * the submission code will error out, so we don't need to
+		 * handle that case here.
+		 */
+		if (RPC_SIGNALLED(task))
+			nfsd4_requeue_cb(task, cb);
+	} else if (!nfsd4_cb_sequence_done(task, cb)) {
 		return;
+	}
 
 	if (cb->cb_status) {
 		WARN_ONCE(task->tk_status,
Signature · 1 line
2.48.1
author maintainer
#3 lore raw

PATCH v6 3/7 nfsd: always release slot when requeueing callback

In reply to Jeff Layton

If the callback is going to be requeued to the workqueue, then release the slot. The callback client and session could change and the slot may no longer be valid after that point.

---
 fs/nfsd/nfs4callback.c | 1 +
 1 file changed, 1 insertion(+)
fs/nfsd/nfs4callback.c +1 -0
index f983d2879bd3fe4c5aa2b0381f968fac753d79dc..7596d0f7dcad44f462ca483979b9c9138ab5dbaf 100644
@@ -1405,6 +1405,7 @@ static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback
 	rpc_restart_call_prepare(task);
 	goto out;
 requeue:
+	nfsd41_cb_release_slot(cb);
 	nfsd4_requeue_cb(task, cb);
 	return false;
 }
Signature · 1 line
2.48.1
author maintainer
#4 lore raw

PATCH v6 4/7 nfsd: only check RPC_SIGNALLED() when restarting rpc_task

In reply to Jeff Layton

nfsd4_cb_sequence_done() currently checks RPC_SIGNALLED() when processing the compound and releasing the slot. If RPC_SIGNALLED() returns true, then that means that the client is going to be torn down.

Don't check RPC_SIGNALLED() after processing a successful reply. Check it only before restarting the rpc_task. If it returns true, then requeue the callback instead of restarting the task.

Also, handle rpc_restart_call() and rpc_restart_call_prepare() failures correctly, by requeueing the callback.

---
 fs/nfsd/nfs4callback.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
fs/nfsd/nfs4callback.c +10 -8
index 7596d0f7dcad44f462ca483979b9c9138ab5dbaf..8cfd6ffa6e18c4db5924d35f7b3344dbf350700e 100644
@@ -1379,8 +1379,8 @@ static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback
 		goto requeue;
 	case -NFS4ERR_DELAY:
 		cb->cb_seq_status = 1;
-		if (!rpc_restart_call(task))
-			goto out;
+		if (RPC_SIGNALLED(task) || !rpc_restart_call(task))
+			goto requeue;
 		rpc_delay(task, 2 * HZ);
 		return false;
 	case -NFS4ERR_BADSLOT:
@@ -1396,14 +1396,16 @@ static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback
 	}
 	trace_nfsd_cb_free_slot(task, cb);
 	nfsd41_cb_release_slot(cb);
-
-	if (RPC_SIGNALLED(task))
-		goto requeue;
-out:
 	return ret;
 retry_nowait:
-	rpc_restart_call_prepare(task);
-	goto out;
+	/*
+	 * RPC_SIGNALLED() means that the rpc_client is being torn down and
+	 * (possibly) recreated. Requeue the call in that case.
+	 */
+	if (!RPC_SIGNALLED(task)) {
+		if (rpc_restart_call_prepare(task))
+			return false;
+	}
 requeue:
 	nfsd41_cb_release_slot(cb);
 	nfsd4_requeue_cb(task, cb);
Signature · 1 line
2.48.1
author maintainer
#5 lore raw

PATCH v6 5/7 nfsd: when CB_SEQUENCE gets ESERVERFAULT don't increment seq_nr

In reply to Jeff Layton

ESERVERFAULT means that the server sent a successful and legitimate reply, but the session info didn't match what was expected. Don't increment the seq_nr in that case.

---
 fs/nfsd/nfs4callback.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/nfsd/nfs4callback.c +6 -1
index 8cfd6ffa6e18c4db5924d35f7b3344dbf350700e..18803ebe2bddd433308cabd6f99b64ec887069a7 100644
@@ -1363,7 +1363,12 @@ static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback
 		ret = true;
 		break;
 	case -ESERVERFAULT:
-		++session->se_cb_seq_nr[cb->cb_held_slot];
+		/*
+		 * Call succeeded, but the session, slot index, or slot
+		 * sequence number in the response do not match the same
+		 * in the server's call. The sequence information is thus
+		 * untrustworthy.
+		 */
 		nfsd4_mark_cb_fault(cb->cb_clp);
 		break;
 	case 1:
Signature · 1 line
2.48.1
author maintainer
#6 lore raw

PATCH v6 6/7 nfsd: handle NFS4ERR_BADSLOT on CB_SEQUENCE better

In reply to Jeff Layton

Currently it just restarts the call, without getting a new slot.

---
 fs/nfsd/nfs4callback.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/nfsd/nfs4callback.c +7 -0
index 18803ebe2bddd433308cabd6f99b64ec887069a7..8ba1a2831e8601ac3af9c5f147d3dcddcc1bec77 100644
@@ -1389,6 +1389,13 @@ static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback
 		rpc_delay(task, 2 * HZ);
 		return false;
 	case -NFS4ERR_BADSLOT:
+		/*
+		 * BADSLOT means that the client and server are out of sync
+		 * as to the backchannel parameters. Mark the backchannel faulty
+		 * and restart the RPC, but leak the slot so no one uses it.
+		 */
+		nfsd4_mark_cb_fault(cb->cb_clp);
+		cb->cb_held_slot = -1;
 		goto retry_nowait;
 	case -NFS4ERR_SEQ_MISORDERED:
 		if (session->se_cb_seq_nr[cb->cb_held_slot] != 1) {
Signature · 1 line
2.48.1
author maintainer
#7 lore raw

PATCH v6 7/7 nfsd: eliminate special handling of NFS4ERR_SEQ_MISORDERED

In reply to Jeff Layton

On a SEQ_MISORDERED error, the current code will reattempt the call, but set the slot sequence ID to 1. I can find no mention of this remedy in the spec, and it seems potentially dangerous. It's possible that the last call was sent with seqid 1, and doing this will cause a retransmission of the reply.

Drop this special handling, and always treat SEQ_MISORDERED like BADSLOT. Retry the call, but leak the slot so that it is no longer used.

---
 fs/nfsd/nfs4callback.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
fs/nfsd/nfs4callback.c +5 -9
index 8ba1a2831e8601ac3af9c5f147d3dcddcc1bec77..2c7ce787eea5a7200022511fe03e269cee43bf7c 100644
@@ -1388,21 +1388,17 @@ static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback
 			goto requeue;
 		rpc_delay(task, 2 * HZ);
 		return false;
+	case -NFS4ERR_SEQ_MISORDERED:
 	case -NFS4ERR_BADSLOT:
 		/*
-		 * BADSLOT means that the client and server are out of sync
-		 * as to the backchannel parameters. Mark the backchannel faulty
-		 * and restart the RPC, but leak the slot so no one uses it.
+		 * A SEQ_MISORDERED or BADSLOT error means that the client and
+		 * server are out of sync as to the backchannel parameters. Mark
+		 * the backchannel faulty and restart the RPC, but leak the slot
+		 * so that it's no longer used.
 		 */
 		nfsd4_mark_cb_fault(cb->cb_clp);
 		cb->cb_held_slot = -1;
 		goto retry_nowait;
-	case -NFS4ERR_SEQ_MISORDERED:
-		if (session->se_cb_seq_nr[cb->cb_held_slot] != 1) {
-			session->se_cb_seq_nr[cb->cb_held_slot] = 1;
-			goto retry_nowait;
-		}
-		break;
 	default:
 		nfsd4_mark_cb_fault(cb->cb_clp);
 	}
Signature · 1 line
2.48.1
#8 lore raw
In reply to Jeff Layton

From: Chuck Lever <[email protected]>

Quoted 4 lines · Jeff Layton

This patch is mostly the same as the v5 series. Just small cleanups and dropping of special NFS4ERR_SEQ_MISORDERED handling.

Nice work. The comments and change logs contain good rationale for the code modifications, and the new code should be easier to maintain.

Applied to nfsd-testing, thanks!

[1/7] nfsd: prepare nfsd4_cb_sequence_done() for error handling rework

      commit: 61d4e0b9d8e47ec93a90f57f8ec76a5b7ba970ea
[2/7] nfsd: lift NFSv4.0 handling out of nfsd4_cb_sequence_done()
      commit: 62dc8f549352ed1a38738112f9cf946b7e97e10a
[3/7] nfsd: always release slot when requeueing callback
      commit: 8d09824221012b221cfc1caa76215c6b4bba3e24
[4/7] nfsd: only check RPC_SIGNALLED() when restarting rpc_task
      commit: dde7c58af30ee0dc13cc47c7d57da9102124597d
[5/7] nfsd: when CB_SEQUENCE gets ESERVERFAULT don't increment seq_nr
      commit: 90b44566f9024e0edc8373ef72e39bc162286bc5
[6/7] nfsd: handle NFS4ERR_BADSLOT on CB_SEQUENCE better
      commit: 7f7c89cb74dd8d5484837256bc14a77de8d91075
[7/7] nfsd: eliminate special handling of NFS4ERR_SEQ_MISORDERED
      commit: 58e65798b82f31aeb6c85c8db13179a0a1bf2b2f

--
Chuck Lever