NFSD: NFSv4.0 stateowner replay cache fixes
Subsystems (2)
Chuck Lever
maintainercover NFSD: NFSv4.0 stateowner replay cache fixes
NFSD caches an NFSv4.0 open-owner's or lock-owner's most recent reply in the so_replay field. When OPEN, OPEN_CONFIRM, OPEN_DOWNGRADE, CLOSE, LOCK, or LOCKU fails with a status that advances the seqid, NFSD currently advances so_seqid but leaves the previous reply in so_replay, and sends the cached reply in response to a request that reuses the failed operation's seqid.
pynfs found the defect by resending an OPEN unchanged after NFS4ERR_DELAY, a resend RFC 7530 Section 9.1.7 forbids. A client that follows Section 9.1.7 reaches the same code when it retransmits after a reconnect. The duplicate reply cache does not catch these
requests: nfsd4_decode_compound() marks a COMPOUND RC_NOCACHE unless one of its operations has OP_CACHEME, and no seqid-mutating operation has it. NFSv4.1 and later are not affected, since a COMPOUND in a session has no replay owner.
The OPEN replay path has a second defect: when fh_verify() of rp_openfh fails, NFSD advances so_seqid as for a new OPEN. Once failed operations are saved, that path would also overwrite the cached OPEN reply with the fh_verify() error, so its fix comes first (patch 1).
After this series, a seqid-mutating operation that succeeds and is then truncated to NFS4ERR_RESOURCE still advances so_seqid without a save, so a request that reuses its seqid receives the previous operation's reply. NFSD also still compares only the seqid, though RFC 7530 Section 9.1.9 requires NFS4ERR_BAD_SEQID when the operation differs too.
I've tested this series with pynfs NFSv4.0, augmenting it with new st_replay.py tests that resend a failed OPEN, OPEN_CONFIRM, OPEN_DOWNGRADE, CLOSE, and LOCKU with the same seqid and a new xid, expecting the cached error and then success at seqid N+1.
The new tests will be posted to pynfs separately.
---
Chuck Lever (2):
NFSD: Release the replay owner when an OPEN replay fails
NFSD: Save the stateowner reply when a seqid-mutating operation fails
fs/nfsd/nfs4proc.c | 7 +++++-- fs/nfsd/nfs4xdr.c | 15 +++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-)
---
base-commit: ee791ecdff6410f241e180338694033889b37607
change-id: 20261005-open-replay-fixes-8913e40d9d91
Best regards,
Signature · 1 line
Chuck Lever <[email protected]>
Chuck Lever
maintainerPATCH 1/2 NFSD: Release the replay owner when an OPEN replay fails
nfsd4_open() handles a replayed OPEN by copying rp_openfh into the current filehandle and calling fh_verify(). If fh_verify() fails, its error replaces nfserr_replay_me, but the open-owner remains the replay owner, so nfsd4_bump_seqid() advances so_seqid as for a new OPEN. The client's next request carries the sequence ID NFSD expected before that advance, so NFSD replies to it from so_replay.
Call nfsd4_cstate_clear_replay() when fh_verify() fails. After the change, nfsd4_bump_seqid() leaves so_seqid alone, and the client still receives the fh_verify() error. nfsd4_encode_operation() also leaves so_replay alone, so the cached reply to the original OPEN survives once NFSD caches the replies of failed operations too.
RFC 7530 Section 9.1.7 requires the stored response for a duplicate request, but without the filehandle the GETFH after the stored NFS4_OK fails with NFS4ERR_NOFILEHANDLE, and the NFS client, FreeBSD, and illumos each discard the stateid without a CLOSE. All three advance their seqid once after NFS4ERR_STALE on the OPEN.
--- fs/nfsd/nfs4proc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/nfsd/nfs4proc.c +1 -2
@@ -707,8 +707,7 @@ nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, &rp->rp_openfh); status =fh_verify (rqstp, &cstate->current_fh,0 ,NFSD_MAY_NOP );if (status) -dprintk ("nfsd4_open: replay failed" -" restoring previous filehandle\n" ); +nfsd4_cstate_clear_replay (cstate);else status = nfserr_replay_me; }
Signature · 1 line
2.55.0
Chuck Lever
maintainerPATCH 2/2 NFSD: Save the stateowner reply when a seqid-mutating operation fails
In reply to Chuck LeverAn NFSv4.0 stateowner caches its last reply in so_replay. When a seqid-mutating operation fails with a status that advances the seqid, nfsd4_bump_seqid() advances so_seqid, but commit b7571e4cd39a ("nfsd4:
skip encoder in trivial error cases") sends the failure past the save in nfsd4_encode_operation(). so_replay keeps the previous reply, which NFSD then sends to a request that reuses the failed seqid. When pynfs resent an OPEN that drew NFS4ERR_DELAY, NFSD replied with NFS4_OK and an OPEN_CONFIRM body.
Move the save below the status label so that every operation with a replay owner stores its final status and body.
Save the status that goes on the wire. The save currently runs before nfsd4_map_status(), so a replay of a failed symlink OPEN would carry nfserr_symlink_not_dir in place of NFS4ERR_SYMLINK.
Skip the filehandle restore in nfsd4_open() when the cached reply is an error. rp_openfh names a file only if the OPEN got far enough to record one, and fh_verify() on a stale handle would replace the cached error with NFS4ERR_STALE.
After the change, a client that reuses a seqid after an error that advances the seqid receives the cached error until it stops resending. The Linux NFS client, FreeBSD, and illumos do not reuse a seqid after NFS4ERR_DELAY. illumos does reuse the seqid after NFS4ERR_OLD_STATEID, which NFSD treats as seqid-advancing. Whether illumos then repeats the request was not determined.
--- fs/nfsd/nfs4proc.c | 4 ++++ fs/nfsd/nfs4xdr.c | 15 +++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-)
fs/nfsd/nfs4proc.c +4 -0
@@ -702,6 +702,10 @@ nfsd4_open(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, status =nfsd4_process_open1 (cstate, open, nn);if (status == nfserr_replay_me) {struct nfs4_replay *rp = &open->op_openowner->oo_owner.so_replay; + +/* A cached error does not guarantee rp_openfh was set. */ +if (rp->rp_status) + goto out;fh_put (&cstate->current_fh);fh_copy_shallow (&cstate->current_fh.fh_handle, &rp->rp_openfh);
fs/nfsd/nfs4xdr.c +9 -6
@@ -6771,7 +6771,13 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)*/ warn_on_nonidempotent_op (op);xdr_truncate_encode (xdr, op_status_offset +XDR_UNIT ); - }else if (so) { +/* Keep a truncated reply out of so_replay. */ + so =NULL ; + } +status: + op->status =nfsd4_map_status (op->status, + resp->cstate.minorversion); +if (so) {int len = xdr->buf->len - (op_status_offset +XDR_UNIT ); so->so_replay.rp_status = op->status;
@@ -6782,8 +6788,8 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)if (buf) { so->so_replay.rp_buf = buf; }else { -/* rp_buflen already zeroed; skip caching */ - goto status; +/* A body this long would overrun rp_ibuf. */ + len =0 ; } }else if (so->so_replay.rp_buf != so->so_replay.rp_ibuf) {nfs4_replay_free_cache (&so->so_replay);
@@ -6793,9 +6799,6 @@ nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op) op_status_offset +XDR_UNIT , so->so_replay.rp_buf, len); } -status: - op->status =nfsd4_map_status (op->status, - resp->cstate.minorversion);write_bytes_to_xdr_buf (xdr->buf, op_status_offset, &op->status,XDR_UNIT ); release:
Signature · 1 line
2.55.0