Linux NFS development
 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 01/11] nfsd: don't hold inode_lock while waiting for a delegation in nfsd_setattr()
Date: Mon, 23 Nov 2026 04:59:04 -0800	[thread overview]
Message-ID: <20261123-delegts-fixes-v2-1-960ce0fb2fed@kernel.org> (raw)
In-Reply-To: <20261123-delegts-fixes-v2-0-960ce0fb2fed@kernel.org>

When notify_change() breaks a delegation, nfsd_setattr() waits up to
NFSD_DELEGRETURN_TIMEOUT for the holder to return the delegation and
then retries once. nfsd_setattr() waits with inode_lock still held, but
what the holder sends before DELEGRETURN can need that lock. WRITEs that
flush the holder's dirty data take i_rwsem exclusively on ext4 and xfs,
and a SETATTR with delegated timestamps in the DELEGRETURN compound goes
through nfsd_setattr() itself. The wait then always times out, and the
SETATTR fails with NFS4ERR_DELAY after stalling the DELEGRETURN that the
wait was for.

Drop inode_lock across the wait, as callers of break_deleg_wait() do.
The file can change while the lock is dropped, so start over after the
wait. Check again that the truncate is allowed, take fresh pre-op
attributes, check the guard again, and sanitize a fresh copy of the
attributes, since the setid bits that a chown must clear depend on the
current mode. Redoing these steps under the lock keeps them atomic with
the attempt that changes the inode. fh_fill_pre_attrs() does nothing
once pre-op attributes are saved, so restore fh_pre_saved to its value
on entry.

If the wait times out, still try once more rather than finishing with
the attributes from before the wait. A lease that is already being
broken isn't broken again, so the retry just gets -EAGAIN.

Don't start over once the size change is done, though, as when a
filesystem returns -EAGAIN for the other attributes after the truncate.
The retry would truncate again, over whatever was written while the lock
was dropped, and the retry's fresh pre-op attributes would hide the
first truncate. Return the error instead, as if the wait had failed.

Fixes: 34b91dda7124 ("NFSD: Make nfsd4_setattr() wait before returning NFS4ERR_DELAY")
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/nfsd/vfs.c | 48 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 17 deletions(-)

diff --git a/fs/nfsd/vfs.c b/fs/nfsd/vfs.c
index e033fbc225cf..4a5674e583cc 100644
--- a/fs/nfsd/vfs.c
+++ b/fs/nfsd/vfs.c
@@ -549,7 +549,9 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	int		host_err = 0;
 	bool		get_write_count;
 	bool		size_change = (iap->ia_valid & ATTR_SIZE);
-	int		retries;
+	bool		pre_saved = fhp->fh_pre_saved;
+	bool		retried = false;
+	struct iattr	attrs;
 
 	trace_nfsd_vfs_setattr(rqstp, fhp, iap, guardtime);
 
@@ -587,8 +589,6 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 	dentry = fhp->fh_dentry;
 	inode = d_inode(dentry);
 
-	nfsd_sanitize_attrs(inode, iap);
-
 	/*
 	 * The size case is special, it changes the file in addition to the
 	 * attributes, and file systems don't expect it to be mixed with
@@ -611,6 +611,7 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		}
 	}
 
+retry:
 	inode_lock(inode);
 	if (size_change) {
 		err = nfsd_may_truncate(rqstp, fhp, iap);
@@ -630,20 +631,33 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp,
 		}
 	}
 
-	for (retries = 1;;) {
-		struct iattr attrs;
-
-		/*
-		 * notify_change() can alter its iattr argument, making
-		 * @iap unsuitable for submission multiple times. Make a
-		 * copy for every loop iteration.
-		 */
-		attrs = *iap;
-		host_err = __nfsd_setattr(dentry, &attrs);
-		if (host_err != -EAGAIN || !retries--)
-			break;
-		if (!nfsd_wait_for_delegreturn(rqstp, inode))
-			break;
+	/*
+	 * notify_change() can alter its iattr argument, making @iap
+	 * unsuitable for submission multiple times. Pass a copy,
+	 * sanitized under inode_lock, because the setid bits that a
+	 * chown must clear depend on the current mode.
+	 */
+	attrs = *iap;
+	nfsd_sanitize_attrs(inode, &attrs);
+	host_err = __nfsd_setattr(dentry, &attrs);
+	/*
+	 * A delegation was broken. Drop inode_lock while waiting for the
+	 * holder to return the delegation, since what the holder sends
+	 * first, such as WRITEs that flush the holder's dirty data, can
+	 * need the lock. The file can change meanwhile, so start over,
+	 * taking fresh pre-op attributes and checking the guard again.
+	 * Starting over is safe only if this attempt changed nothing. A
+	 * retry would repeat a size change that is already done
+	 * (__nfsd_setattr() clears ATTR_SIZE) over whatever was written
+	 * after the truncate, so return the error in that case.
+	 */
+	if (host_err == -EAGAIN && !retried &&
+	    !(size_change && !(attrs.ia_valid & ATTR_SIZE))) {
+		inode_unlock(inode);
+		retried = true;
+		nfsd_wait_for_delegreturn(rqstp, inode);
+		fhp->fh_pre_saved = pre_saved;
+		goto retry;
 	}
 	if (attr->na_seclabel && attr->na_seclabel->len)
 		attr->na_labelerr = security_inode_setsecctx(dentry,

-- 
2.56.1


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

Thread overview: 12+ 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 ` Jeff Layton [this message]
2026-11-23 12:59 ` [PATCH v2 02/11] nfsd: take freeze protection before updating delegation timestamps Jeff Layton
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-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 ` [PATCH v2 06/11] nfsd: pin the client while a GETATTR holds a conflicting delegation Jeff Layton
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-1-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox