Linux NFS development
 help / color / mirror / Atom feed
From: NeilBrown <neilb@ownmail.net>
To: Chuck Lever <chuck.lever@oracle.com>, Jeff Layton <jlayton@kernel.org>
Cc: Olga Kornievskaia <okorniev@redhat.com>,
	Dai Ngo <Dai.Ngo@oracle.com>, Tom Talpey <tom@talpey.com>,
	linux-nfs@vger.kernel.org
Subject: [PATCH v5 05/11] nfsd: drop explicit tests for special stateids which would be invalid.
Date: Wed, 19 Nov 2025 14:28:51 +1100	[thread overview]
Message-ID: <20251119033204.360415-6-neilb@ownmail.net> (raw)
In-Reply-To: <20251119033204.360415-1-neilb@ownmail.net>

From: NeilBrown <neil@brown.name>

In two places nfsd has code to test for special stateids and to report
nfserr_bad_stateid if they are found.
One is for handling TEST_STATEID ops which always forbid these stateids,
and one is for all other places that a stateid is used, and the code is
*after* any checks for special stateids which might be permitted.

These tests add no value.  In each case there is a subsequent lookup for
the stateid which will return the same error code if the stateid is not
found, and special stateids never will be found.

Special stateids have a si.opaque.so_id which is either 0 or UINT_MAX.
Stateids stored in the idr only have so_id ranging from 1 or INT_MAX.
So there is no possibility of a special stateid being found.

Having the explicit test optimises the unexpected case where a special
stateid is incorrectly given, and adds unnecessary comparisons to the
common case of a non-special stateid being given.

In nfsd4_lookup_stateid(), simply removing the test would mean that
a special stateid could result in the incorrect nfserr_stale_stateid
error, as the validity of so_clid is checked before so_id.  So we
add extra checks to only return nfserr_stale_stateid if the stateid
looks like it might have been locally generated - so_id not
all zeroes or all ones.

Signed-off-by: NeilBrown <neil@brown.name>
---
 fs/nfsd/nfs4state.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index 35004568d43e..ea931e606f40 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -74,6 +74,23 @@ static const stateid_t close_stateid = {
 	.si_generation = 0xffffffffU,
 };
 
+/*
+ * In NFSv4.0 there is a case were we should return NFS4ERR_STALE_STATEID
+ * if the stateid looks like one we might have created previously, and
+ * NFS4ERR_BAD_STATEID if it looks like it was never valid.
+ * There is not a lot of redundancy in the stateid that can be used to make
+ * this distinction, but it would be useful to differentiate special
+ * stateids from locally generated stateid.
+ * Special stateids have si.opaque.so_id being either all zeros or all 1s,
+ * so 0 or (u32)-1. Locally generated stateids have si.opaque.so_id as
+ * a number from 1 to INT_MAX (as generated by idr_alloc_cyclic()).
+ * We can test for the later range with some simple arithmetic.
+ */
+static inline bool stateid_well_formed(stateid_t *stid)
+{
+	return (stid->si_opaque.so_id - 1) < INT_MAX;
+}
+
 static u64 current_sessionid = 1;
 
 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
@@ -7129,9 +7146,6 @@ static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
 	struct nfs4_stid *s;
 	__be32 status = nfserr_bad_stateid;
 
-	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
-		CLOSE_STATEID(stateid))
-		return status;
 	spin_lock(&cl->cl_lock);
 	s = find_stateid_locked(cl, stateid);
 	if (!s)
@@ -7186,14 +7200,15 @@ nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
 
 	statusmask |= SC_STATUS_ADMIN_REVOKED | SC_STATUS_FREEABLE;
 
-	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
-		CLOSE_STATEID(stateid))
-		return nfserr_bad_stateid;
 	status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
 	if (status == nfserr_stale_clientid) {
-		if (cstate->session)
-			return nfserr_bad_stateid;
-		return nfserr_stale_stateid;
+		if (!cstate->session && stateid_well_formed(stateid))
+			/*
+			 * Might be from earlier instance - v4.0 likes
+			 * to know
+			 */
+			return nfserr_stale_stateid;
+		return nfserr_bad_stateid;
 	}
 	if (status)
 		return status;
-- 
2.50.0.107.gf914562f5916.dirty


  parent reply	other threads:[~2025-11-19  3:32 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-19  3:28 [PATCH v5 00/11] nfsd: assorted cleanups involving v4 special stateids NeilBrown
2025-11-19  3:28 ` [PATCH v5 01/11] nfsd: rename ALLOWED_WITHOUT_FH to ALLOWED_WITHOUT_LOCAL_FH and revise use NeilBrown
2025-11-19 16:02   ` Chuck Lever
2025-11-19 21:13     ` NeilBrown
2025-11-19 19:12   ` Jeff Layton
2025-11-19  3:28 ` [PATCH v5 02/11] nfsd: discard NFSD4_FH_FOREIGN NeilBrown
2025-11-19 16:27   ` Chuck Lever
2025-11-19 21:25     ` NeilBrown
2025-11-19 19:13   ` Jeff Layton
2025-11-19  3:28 ` [PATCH v5 03/11] nfsd: simplify foreign-filehandle handling to better match RFC-7862 NeilBrown
2025-11-19 16:55   ` Chuck Lever
2025-11-19 21:38     ` NeilBrown
2025-11-20 21:58       ` Chuck Lever
2025-11-22  0:46         ` NeilBrown
2025-11-19 19:23   ` Jeff Layton
2025-11-19  3:28 ` [PATCH v5 04/11] nfsd: report correct error for attempt to use foreign filehandle NeilBrown
2025-11-19 19:26   ` Jeff Layton
2025-11-19  3:28 ` NeilBrown [this message]
2025-11-19 19:11   ` [PATCH v5 05/11] nfsd: drop explicit tests for special stateids which would be invalid Chuck Lever
2025-11-19 19:32   ` Jeff Layton
2025-11-19  3:28 ` [PATCH v5 06/11] nfsd: revise names of special stateid, and predicate functions NeilBrown
2025-11-19 19:27   ` Chuck Lever
2025-11-19 21:47     ` NeilBrown
2025-11-19  3:28 ` [PATCH v5 07/11] nfsd: simplify clearing of current-state-id NeilBrown
2025-11-19 20:23   ` Chuck Lever
2025-11-19 21:55     ` NeilBrown
2025-11-19  3:28 ` [PATCH v5 08/11] nfsd: simplify use of the current stateid NeilBrown
2025-11-19  3:28 ` [PATCH v5 09/11] nfsd: simplify saving " NeilBrown
2025-11-19  3:28 ` [PATCH v5 10/11] nfsd: discard current_stateid.h NeilBrown
2025-11-19  3:28 ` [PATCH v5 11/11] nfsd: conditionally clear seqid when current_stateid is used NeilBrown
2025-11-19 20:32 ` [PATCH v5 00/11] nfsd: assorted cleanups involving v4 special stateids Chuck Lever

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251119033204.360415-6-neilb@ownmail.net \
    --to=neilb@ownmail.net \
    --cc=Dai.Ngo@oracle.com \
    --cc=chuck.lever@oracle.com \
    --cc=jlayton@kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=neil@brown.name \
    --cc=okorniev@redhat.com \
    --cc=tom@talpey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox