Skip to contentSkip to conversation

nfsd: recall deleg if a requested dir attr change can't be encoded

PATCH In review Jeff Layton 2 participants 2 messages Jun 20 – Jun 21

Subsystems (2)
lore
Share & tools
lore

Jeff Layton

author maintainer
#0 lore raw

PATCH v3 1/1 nfsd: recall deleg if a requested dir attr change can't be encoded

When the client requested NOTIFY4_CHANGE_DIR_ATTRS, nfsd4_cb_notify_prepare() tried to append the dir attribute change to the CB_NOTIFY, but silently dropped it if the attrmask space reservation failed or nfsd4_encode_dir_attr_change() failed to marshal the change into the buffer. It then returned true and transmitted a CB_NOTIFY lacking the requested notification, leaving the client's cached directory attributes stale with no indication.

RFC 8881 s10.9.4 requires the server to recall the delegation when the directory changes in a way that is not covered by the notification. Treat a failure to encode the dir attribute change like the per-event encode failures already handled in the loop above: set error and fall through to out_recall.

The granted notification mask and the requested dir attribute mask come from independent fields of the GET_DIR_DELEGATION request, so a client can be granted NOTIFY4_CHANGE_DIR_ATTRS while requesting no specific dir attributes. nfsd4_encode_dir_attr_change() now distinguishes a genuine encode failure (which forces the recall) from that benign case (which simply omits the event).

For that distinction to hold, nfsd4_setup_notify_entry4() must not mask genuine failures as an empty attribute set. It previously routed three different conditions through its noattrs label and returned true with a zero-length attribute set:

- a NULL or negative dentry (no attributes to report),
- a vfs_getattr() failure, and
- an nfsd4_encode_attr_vals() failure (XDR buffer exhaustion).

Only the first is benign; the latter two are genuine failures that were indistinguishable from the "no attributes requested" case, so a requested change could still be silently dropped (the per-event entries encoded by nfsd4_encode_notify_event() were affected the same way). Return false on a vfs_getattr() or nfsd4_encode_attr_vals() failure so the error propagates to the caller and the delegation is recalled, and reserve the noattrs path for the NULL or negative dentry case.

While we're in there, also remove an unneeded check for NOTIFY4_CHANGE_DIR_ATTRS from nfsd4_encode_dir_attr_change().

Fixes 757a16cd93d5 ("nfsd: add support to CB_NOTIFY for dir attribute changes")
Assisted-by Claude:claude-opus-4-8

---
This version should address Chuck's comments about the NULL pointer
handling.
---
Changes in v3:
- Recall delegations on vfs_getattr() failure instead of sending empty attr set
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Allow nfsd4_encode_dir_attr_change() to return ERR_PTR when there are errors
- Drop unneeded NOTIFY4_CHANGE_DIR_ATTRS check
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 fs/nfsd/nfs4state.c | 43 ++++++++++++++++++++++++--------------
 fs/nfsd/nfs4xdr.c   | 60 ++++++++++++++++++++++++++++++-----------------------
 2 files changed, 62 insertions(+), 41 deletions(-)
fs/nfsd/nfs4state.c +28 -15
index b830aed7ae39..4ae5d65c056a 100644
@@ -3598,23 +3598,36 @@ nfsd4_cb_notify_prepare(struct nfsd4_callback *cb)
 put_event:
 		nfsd_notify_event_put(nne);
 	}
-	if (!error) {
-		if (dp->dl_notify_mask & BIT(NOTIFY4_CHANGE_DIR_ATTRS)) {
-			u32 *maskp = (u32 *)xdr_reserve_space(&stream, sizeof(*maskp));
+	if (!error && (dp->dl_notify_mask & BIT(NOTIFY4_CHANGE_DIR_ATTRS))) {
+		u32 *maskp = (u32 *)xdr_reserve_space(&stream, sizeof(*maskp));
+		u8 *p;
 
-			if (maskp) {
-				u8 *p = nfsd4_encode_dir_attr_change(&stream, dp, nf);
-
-				if (p) {
-					*maskp = BIT(NOTIFY4_CHANGE_DIR_ATTRS);
-					ncn->ncn_nf[count].notify_mask.count = 1;
-					ncn->ncn_nf[count].notify_mask.element = maskp;
-					ncn->ncn_nf[count].notify_vals.data = p;
-					ncn->ncn_nf[count].notify_vals.len = (u8 *)stream.p - p;
-					++count;
-				}
-			}
+		if (maskp)
+			p = nfsd4_encode_dir_attr_change(&stream, dp, nf);
+		else
+			p = ERR_PTR(-ENOBUFS);
+
+		if (IS_ERR(p)) {
+			/*
+			 * The client asked to be told about dir attr changes
+			 * but the change could not be encoded. RFC 8881
+			 * s10.9.4 requires the server to recall the delegation
+			 * rather than drop a requested notification, so fall
+			 * through to recall. A NULL return instead means there
+			 * were no attributes to report, so omit the event in
+			 * that case.
+			 */
+			error = true;
+		} else if (p) {
+			*maskp = BIT(NOTIFY4_CHANGE_DIR_ATTRS);
+			ncn->ncn_nf[count].notify_mask.count = 1;
+			ncn->ncn_nf[count].notify_mask.element = maskp;
+			ncn->ncn_nf[count].notify_vals.data = p;
+			ncn->ncn_nf[count].notify_vals.len = (u8 *)stream.p - p;
+			++count;
 		}
+	}
+	if (!error) {
 		ncn->ncn_nf_cnt = count;
 		nfsd_file_put(nf);
 		return true;
fs/nfsd/nfs4xdr.c +34 -26

96 lines not on this page · raw message

---
base-commit: 6f90c7618528b5ca5887f8c6057f26dcc7a27a99
change-id: 20260617-dir-deleg-fixes-ecac84095b84

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>
maintainer
#1 lore raw
Quoted 9 lines · Jeff Layton

When the client requested NOTIFY4_CHANGE_DIR_ATTRS, nfsd4_cb_notify_prepare() tried to append the dir attribute change to the CB_NOTIFY, but silently dropped it if the attrmask space reservation failed or nfsd4_encode_dir_attr_change() failed to marshal the change into the buffer. It then returned true and transmitted a CB_NOTIFY lacking the requested notification, leaving the client's cached directory attributes stale with no indication.

[...]

Folded into "nfsd: add support to CB_NOTIFY for dir attribute changes", thanks!

[1/1] nfsd: recall deleg if a requested dir attr change can't be encoded
(no commit info)

--
Chuck Lever