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 08/11] nfsd: simplify use of the current stateid.
Date: Wed, 19 Nov 2025 14:28:54 +1100 [thread overview]
Message-ID: <20251119033204.360415-9-neilb@ownmail.net> (raw)
In-Reply-To: <20251119033204.360415-1-neilb@ownmail.net>
From: NeilBrown <neil@brown.name>
In NFSv4.1 any use of a stateid in an OP should check if it is
the special stateid that
has "other" set to zero and "seqid" set to one
If it is, then the stateid saved from a previous operation should be
used unless that stateid as "special" stateid, in which case BAD_STATEID
must be returned.
Most ops that include a stateid pass it (indirectly) to
nfsd4_lookup_stateid(). The one exception is FREE_STATEID which uses
find_stateid_locked() directly. (TEST_STATEID is not considered. It is
given a list of stateids, among which any special stateid is explicitly
forbidden).
So rather than providing dedicated op_get_currentstateid functions for
each relevant op, we can include the required logic directly in
nfsd4_lookup_stateid() and nfsd4_free_stateid(). That is what this
patch does.
We also include an explicit return of nfserr_bad_stateid when the
special stateid is used, but no valid stateid is available. This does
not change behaviour as the error would have been returned eventually.
This patch also removes the "minorversion" test in put_stateid() and
instead checks that sessions are in use when the stateid is used. This
seems more natural. Note that nfsd4_free_stateid() doesn't need this
explicit test as the function cannot be called for v4.0.
Signed-off-by: NeilBrown <neil@brown.name>
---
fs/nfsd/current_stateid.h | 21 ----------
fs/nfsd/nfs4proc.c | 11 -----
fs/nfsd/nfs4state.c | 85 ++++++---------------------------------
fs/nfsd/xdr4.h | 2 -
4 files changed, 13 insertions(+), 106 deletions(-)
diff --git a/fs/nfsd/current_stateid.h b/fs/nfsd/current_stateid.h
index 24d769043207..eaa4b8e40dc8 100644
--- a/fs/nfsd/current_stateid.h
+++ b/fs/nfsd/current_stateid.h
@@ -16,25 +16,4 @@ extern void nfsd4_set_lockstateid(struct nfsd4_compound_state *,
union nfsd4_op_u *);
extern void nfsd4_set_closestateid(struct nfsd4_compound_state *,
union nfsd4_op_u *);
-
-/*
- * functions to consume current state id
- */
-extern void nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *,
- union nfsd4_op_u *);
-extern void nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *,
- union nfsd4_op_u *);
-extern void nfsd4_get_freestateid(struct nfsd4_compound_state *,
- union nfsd4_op_u *);
-extern void nfsd4_get_setattrstateid(struct nfsd4_compound_state *,
- union nfsd4_op_u *);
-extern void nfsd4_get_closestateid(struct nfsd4_compound_state *,
- union nfsd4_op_u *);
-extern void nfsd4_get_lockustateid(struct nfsd4_compound_state *,
- union nfsd4_op_u *);
-extern void nfsd4_get_readstateid(struct nfsd4_compound_state *,
- union nfsd4_op_u *);
-extern void nfsd4_get_writestateid(struct nfsd4_compound_state *,
- union nfsd4_op_u *);
-
#endif /* _NFSD4_CURRENT_STATE_H */
diff --git a/fs/nfsd/nfs4proc.c b/fs/nfsd/nfs4proc.c
index ae4094ff12dc..ca1ed0a1bcab 100644
--- a/fs/nfsd/nfs4proc.c
+++ b/fs/nfsd/nfs4proc.c
@@ -2933,8 +2933,6 @@ nfsd4_proc_compound(struct svc_rqst *rqstp)
if (op->status)
goto encode_op;
- if (op->opdesc->op_get_currentstateid)
- op->opdesc->op_get_currentstateid(cstate, &op->u);
op->status = op->opdesc->op_func(rqstp, cstate, &op->u);
trace_nfsd_compound_op_err(rqstp, op->opnum, op->status);
@@ -3381,7 +3379,6 @@ static const struct nfsd4_operation nfsd4_ops[] = {
.op_flags = OP_MODIFIES_SOMETHING,
.op_name = "OP_CLOSE",
.op_rsize_bop = nfsd4_status_stateid_rsize,
- .op_get_currentstateid = nfsd4_get_closestateid,
.op_set_currentstateid = nfsd4_set_closestateid,
},
[OP_COMMIT] = {
@@ -3401,7 +3398,6 @@ static const struct nfsd4_operation nfsd4_ops[] = {
.op_flags = OP_MODIFIES_SOMETHING,
.op_name = "OP_DELEGRETURN",
.op_rsize_bop = nfsd4_only_status_rsize,
- .op_get_currentstateid = nfsd4_get_delegreturnstateid,
},
[OP_GETATTR] = {
.op_func = nfsd4_getattr,
@@ -3442,7 +3438,6 @@ static const struct nfsd4_operation nfsd4_ops[] = {
.op_flags = OP_MODIFIES_SOMETHING,
.op_name = "OP_LOCKU",
.op_rsize_bop = nfsd4_status_stateid_rsize,
- .op_get_currentstateid = nfsd4_get_lockustateid,
},
[OP_LOOKUP] = {
.op_func = nfsd4_lookup,
@@ -3479,7 +3474,6 @@ static const struct nfsd4_operation nfsd4_ops[] = {
.op_flags = OP_MODIFIES_SOMETHING,
.op_name = "OP_OPEN_DOWNGRADE",
.op_rsize_bop = nfsd4_status_stateid_rsize,
- .op_get_currentstateid = nfsd4_get_opendowngradestateid,
.op_set_currentstateid = nfsd4_set_opendowngradestateid,
},
[OP_PUTFH] = {
@@ -3508,7 +3502,6 @@ static const struct nfsd4_operation nfsd4_ops[] = {
.op_release = nfsd4_read_release,
.op_name = "OP_READ",
.op_rsize_bop = nfsd4_read_rsize,
- .op_get_currentstateid = nfsd4_get_readstateid,
},
[OP_READDIR] = {
.op_func = nfsd4_readdir,
@@ -3567,7 +3560,6 @@ static const struct nfsd4_operation nfsd4_ops[] = {
.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME
| OP_NONTRIVIAL_ERROR_ENCODE,
.op_rsize_bop = nfsd4_setattr_rsize,
- .op_get_currentstateid = nfsd4_get_setattrstateid,
},
[OP_SETCLIENTID] = {
.op_func = nfsd4_setclientid,
@@ -3594,7 +3586,6 @@ static const struct nfsd4_operation nfsd4_ops[] = {
.op_flags = OP_MODIFIES_SOMETHING | OP_CACHEME,
.op_name = "OP_WRITE",
.op_rsize_bop = nfsd4_write_rsize,
- .op_get_currentstateid = nfsd4_get_writestateid,
},
[OP_RELEASE_LOCKOWNER] = {
.op_func = nfsd4_release_lockowner,
@@ -3676,7 +3667,6 @@ static const struct nfsd4_operation nfsd4_ops[] = {
.op_func = nfsd4_free_stateid,
.op_flags = ALLOWED_WITHOUT_LOCAL_FH | OP_MODIFIES_SOMETHING,
.op_name = "OP_FREE_STATEID",
- .op_get_currentstateid = nfsd4_get_freestateid,
.op_rsize_bop = nfsd4_only_status_rsize,
},
[OP_GET_DIR_DELEGATION] = {
@@ -3744,7 +3734,6 @@ static const struct nfsd4_operation nfsd4_ops[] = {
.op_release = nfsd4_read_release,
.op_name = "OP_READ_PLUS",
.op_rsize_bop = nfsd4_read_plus_rsize,
- .op_get_currentstateid = nfsd4_get_readstateid,
},
[OP_SEEK] = {
.op_func = nfsd4_seek,
diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index 7ffe0b8e44de..ce7b958fa076 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -7192,6 +7192,11 @@ nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
struct nfs4_stid *stid;
bool return_revoked = false;
+ if (nfsd4_has_session(cstate) && is_current_stateid(stateid)) {
+ if (!cstate->current_fh.fh_have_stateid)
+ return nfserr_bad_stateid;
+ memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
+ }
/*
* only return revoked delegations if explicitly asked.
* otherwise we report revoked or bad_stateid status.
@@ -7508,6 +7513,12 @@ nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
struct nfs4_client *cl = cstate->clp;
__be32 ret = nfserr_bad_stateid;
+ if (is_current_stateid(stateid)) {
+ if (!cstate->current_fh.fh_have_stateid)
+ return nfserr_bad_stateid;
+ memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
+ }
+
spin_lock(&cl->cl_lock);
s = find_stateid_locked(cl, stateid);
if (!s || s->sc_status & SC_STATUS_CLOSED)
@@ -9103,21 +9114,11 @@ nfs4_state_shutdown(void)
shrinker_free(nfsd_slot_shrinker);
}
-static void
-get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
-{
- if (cstate->current_fh.fh_have_stateid &&
- is_current_stateid(stateid))
- memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
-}
-
static void
put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
{
- if (cstate->minorversion) {
- memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
- cstate->current_fh.fh_have_stateid = true;
- }
+ memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
+ cstate->current_fh.fh_have_stateid = true;
}
/*
@@ -9151,66 +9152,6 @@ nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
put_stateid(cstate, &u->lock.lk_resp_stateid);
}
-/*
- * functions to consume current state id
- */
-
-void
-nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
- union nfsd4_op_u *u)
-{
- get_stateid(cstate, &u->open_downgrade.od_stateid);
-}
-
-void
-nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
- union nfsd4_op_u *u)
-{
- get_stateid(cstate, &u->delegreturn.dr_stateid);
-}
-
-void
-nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
- union nfsd4_op_u *u)
-{
- get_stateid(cstate, &u->free_stateid.fr_stateid);
-}
-
-void
-nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
- union nfsd4_op_u *u)
-{
- get_stateid(cstate, &u->setattr.sa_stateid);
-}
-
-void
-nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
- union nfsd4_op_u *u)
-{
- get_stateid(cstate, &u->close.cl_stateid);
-}
-
-void
-nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
- union nfsd4_op_u *u)
-{
- get_stateid(cstate, &u->locku.lu_stateid);
-}
-
-void
-nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
- union nfsd4_op_u *u)
-{
- get_stateid(cstate, &u->read.rd_stateid);
-}
-
-void
-nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
- union nfsd4_op_u *u)
-{
- get_stateid(cstate, &u->write.wr_stateid);
-}
-
/**
* nfsd4_vet_deleg_time - vet and set the timespec for a delegated timestamp update
* @req: timestamp from the client
diff --git a/fs/nfsd/xdr4.h b/fs/nfsd/xdr4.h
index b6ab32c7b243..55dab88b011c 100644
--- a/fs/nfsd/xdr4.h
+++ b/fs/nfsd/xdr4.h
@@ -1034,8 +1034,6 @@ struct nfsd4_operation {
/* Try to get response size before operation */
u32 (*op_rsize_bop)(const struct svc_rqst *rqstp,
const struct nfsd4_op *op);
- void (*op_get_currentstateid)(struct nfsd4_compound_state *,
- union nfsd4_op_u *);
void (*op_set_currentstateid)(struct nfsd4_compound_state *,
union nfsd4_op_u *);
};
--
2.50.0.107.gf914562f5916.dirty
next prev 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 ` [PATCH v5 05/11] nfsd: drop explicit tests for special stateids which would be invalid NeilBrown
2025-11-19 19:11 ` 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 ` NeilBrown [this message]
2025-11-19 3:28 ` [PATCH v5 09/11] nfsd: simplify saving of the current stateid 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-9-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