All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: Chuck Lever <cel@kernel.org>, NeilBrown <neil@brown.name>,
	 Olga Kornievskaia <okorniev@redhat.com>,
	Dai Ngo <Dai.Ngo@oracle.com>,  Tom Talpey <tom@talpey.com>,
	Christian Brauner <brauner@kernel.org>
Cc: "Aurélien Couderc" <aurelien.couderc2002@gmail.com>,
	linux-nfs@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Jeff Layton" <jlayton@kernel.org>
Subject: [PATCH v2 06/11] nfsd: pin the client while a GETATTR holds a conflicting delegation
Date: Mon, 23 Nov 2026 04:59:09 -0800	[thread overview]
Message-ID: <20261123-delegts-fixes-v2-6-960ce0fb2fed@kernel.org> (raw)
In-Reply-To: <20261123-delegts-fixes-v2-0-960ce0fb2fed@kernel.org>

nfsd4_deleg_getattr_conflict() takes a reference on a write delegation
it finds through the inode's lease, sends CB_GETATTR, waits for the
reply, and drops the reference only after the wait.
nfsd4_encode_fattr4() can also hold the delegation until the GETATTR
reply is encoded.

A delegation reference doesn't keep its client alive. __destroy_client()
doesn't wait for stateid references, and the final nfs4_put_stid() uses
the client (cl_lock, the stateid idr and ->sc_free). If the client is
destroyed while the GETATTR waits, the GETATTR's put can touch a freed
nfs4_client. For example, a client that does not reply to the CB_GETATTR
can have its lease expire during the wait.

Under flc_lock, while the lease is still on the inode, take a client
reference along with the delegation reference. Client teardown removes
the lease before free_client() drops the client's initial reference, so
the client is still live while the lease is on the inode.
nfsd4_put_leased_deleg() drops both references.

Fixes: c5967721e106 ("NFSD: handle GETATTR conflict with write delegation")
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/nfsd/nfs4state.c | 41 ++++++++++++++++++++++++++++++++++++++---
 fs/nfsd/nfs4xdr.c   |  2 +-
 fs/nfsd/state.h     |  1 +
 3 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
index 4b28553efc8d..4e7bbaf65050 100644
--- a/fs/nfsd/nfs4state.c
+++ b/fs/nfsd/nfs4state.c
@@ -2912,6 +2912,38 @@ void nfsd4_put_client(struct nfs4_client *clp)
 	kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
 }
 
+/*
+ * Reference a delegation found through its lease, for use after flc_lock
+ * is dropped. A delegation reference alone doesn't keep the client.
+ * __destroy_client() doesn't wait for stateid references, yet
+ * nfs4_put_stid() uses the client, so pin the client as well. Caller
+ * holds flc_lock with @dp's lease on the inode. Client teardown removes
+ * the lease before free_client() drops the client's initial reference,
+ * so the initial reference is still held here.
+ */
+static bool nfsd4_get_leased_deleg(struct nfs4_delegation *dp)
+{
+	if (!refcount_inc_not_zero(&dp->dl_stid.sc_count))
+		return false;
+	nfsd4_get_client(dp->dl_stid.sc_client);
+	return true;
+}
+
+/**
+ * nfsd4_put_leased_deleg - release a delegation found through its lease
+ * @dp: delegation to release
+ *
+ * Drops the delegation and client references that the lease walk took.
+ * May free the client, so the caller must be able to sleep.
+ */
+void nfsd4_put_leased_deleg(struct nfs4_delegation *dp)
+{
+	struct nfs4_client *clp = dp->dl_stid.sc_client;
+
+	nfs4_put_stid(&dp->dl_stid);
+	nfsd4_put_client(clp);
+}
+
 static void
 free_client(struct nfs4_client *clp)
 {
@@ -10880,7 +10912,7 @@ static int cb_getattr_update_times(struct dentry *dentry, struct nfs4_delegation
  *
  * Returns 0 if there is no conflict; otherwise an nfs_stat
  * code is returned. If @pdp is set to a non-NULL value, then the
- * caller must put the reference.
+ * caller must release it with nfsd4_put_leased_deleg().
  */
 __be32
 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,
@@ -10925,7 +10957,10 @@ nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,
 		return 0;
 	}
 
-	refcount_inc(&dp->dl_stid.sc_count);
+	if (!nfsd4_get_leased_deleg(dp)) {
+		spin_unlock(&ctx->flc_lock);
+		return nfserr_jukebox;
+	}
 	ncf = &dp->dl_cb_fattr;
 	nfs4_cb_getattr(&dp->dl_cb_fattr);
 	spin_unlock(&ctx->flc_lock);
@@ -10966,7 +11001,7 @@ nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,
 	}
 	status = nfs_ok;
 out_status:
-	nfs4_put_stid(&dp->dl_stid);
+	nfsd4_put_leased_deleg(dp);
 	return status;
 }
 
diff --git a/fs/nfsd/nfs4xdr.c b/fs/nfsd/nfs4xdr.c
index 950c67362566..e6131095024f 100644
--- a/fs/nfsd/nfs4xdr.c
+++ b/fs/nfsd/nfs4xdr.c
@@ -4106,7 +4106,7 @@ nfsd4_encode_fattr4(struct svc_rqst *rqstp, struct xdr_stream *xdr,
 		if (!timespec64_is_epoch(&ncf->ncf_cb_atime))
 			args.stat.atime = ncf->ncf_cb_atime;
 
-		nfs4_put_stid(&dp->dl_stid);
+		nfsd4_put_leased_deleg(dp);
 	} else {
 		args.change_attr = nfsd4_change_attribute(&args.stat);
 	}
diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h
index e9816000c943..f80abcc278f6 100644
--- a/fs/nfsd/state.h
+++ b/fs/nfsd/state.h
@@ -977,6 +977,7 @@ extern void nfsd4_shutdown_callback(struct nfs4_client *);
 extern void nfsd4_shutdown_copy(struct nfs4_client *clp);
 void nfsd4_get_client(struct nfs4_client *clp);
 void nfsd4_put_client(struct nfs4_client *clp);
+void nfsd4_put_leased_deleg(struct nfs4_delegation *dp);
 void nfsd4_async_copy_reaper(struct nfsd_net *nn);
 bool nfsd4_has_active_async_copies(struct nfs4_client *clp);
 void nfsd_update_cmtime_attr(struct file *f, unsigned int flags);

-- 
2.56.1


  parent reply	other threads:[~2026-11-23 12:59 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 12:59 [PATCH v2 00/11] nfsd: various delegated timestamp fixes Jeff Layton
2026-11-23 12:59 ` [PATCH v2 01/11] nfsd: don't hold inode_lock while waiting for a delegation in nfsd_setattr() Jeff Layton
2026-11-23 12:59 ` [PATCH v2 02/11] nfsd: take freeze protection before updating delegation timestamps Jeff Layton
2026-11-23 17:46   ` Chuck Lever
2026-11-23 12:59 ` [PATCH v2 03/11] nfsd: count a delegation in cl_delegs_in_recall only once Jeff Layton
2026-11-23 12:59 ` [PATCH v2 04/11] nfsd: don't grant write delegations to NFSv4.0 clients on CLAIM_PREVIOUS Jeff Layton
2026-11-24  7:39   ` Aurélien Couderc
2026-11-23 12:59 ` [PATCH v2 05/11] nfsd: set ctime when a SETATTR with delegated times changes other attributes Jeff Layton
2026-11-23 12:59 ` Jeff Layton [this message]
2026-11-23 12:59 ` [PATCH v2 07/11] nfsd: recall when a CB_GETATTR reply omits the size Jeff Layton
2026-11-23 12:59 ` [PATCH v2 08/11] nfsd: apply delegated times in a separate times-only setattr Jeff Layton
2026-11-23 12:59 ` [PATCH v2 09/11] nfsd: grant a plain read delegation when timestamps are wanted on a read open Jeff Layton
2026-11-23 12:59 ` [PATCH v2 10/11] nfsd: check fi_had_conflict under deleg_lock before hashing a delegation Jeff Layton
2026-11-23 12:59 ` [PATCH v2 11/11] nfsd: take the write delegation grant stat before hashing Jeff Layton

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=20261123-delegts-fixes-v2-6-960ce0fb2fed@kernel.org \
    --to=jlayton@kernel.org \
    --cc=Dai.Ngo@oracle.com \
    --cc=aurelien.couderc2002@gmail.com \
    --cc=brauner@kernel.org \
    --cc=cel@kernel.org \
    --cc=linux-kernel@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.