[PATCH v2] ceph: drop stale dentry when ceph_unlink() gets -ENOENT

From: zhangdaolong

Date: Tue Nov 10 2026 - 01:15:45 EST


Dentries under .snap do not have MDS leases. If a snapshot is removed
in the window between d_revalidate() and the synchronous RMSNAP request
in ceph_unlink(), the MDS replies with -ENOENT and no dentry trace.

Because ceph_fill_trace() only invalidates the parent directory when
result == 0 and neither ceph_unlink() nor the VFS layer unhashes the
dentry on error, the stale positive snapshot dentry remains hashed in
the dcache. Subsequent stat on .snap/<snap> still finds the deleted
snapshot and mkdir .snap/<snap> fails with -EEXIST.

For regular files and directories, a concurrent unlink by another client
revokes the local dentry lease and parent Fs caps, so dropping the
dentry and clearing parent completeness on -ENOENT serves as hardening
that matches what ceph_async_unlink_cb() already does on failure.

Fix the stale .snap dentry issue and harden ceph_unlink() by calling
ceph_dir_clear_complete(dir) and d_drop(dentry) when
ceph_mdsc_do_request() returns -ENOENT.

Suggested-by: Xiubo Li <xiubli@xxxxxxxxxx>
Tested-by: Xiubo Li <xiubo.li@xxxxxxxxx>
Reviewed-by: Xiubo Li <xiubo.li@xxxxxxxxx>
Signed-off-by: zhangdaolong <dlzhangak@xxxxxxxxxxxxxx>
---
v2:
- Narrow down the commit message to explain the .snap race window and
describe the regular file/dir part as hardening (Xiubo Li).
- Add Tested-by and Reviewed-by tags from Xiubo Li.

fs/ceph/dir.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c
index 6eb5b1cb039b..f4c0521f3bcc 100644
--- a/fs/ceph/dir.c
+++ b/fs/ceph/dir.c
@@ -1473,8 +1473,12 @@ static int ceph_unlink(struct inode *dir, struct dentry *dentry)
} else {
set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
err = ceph_mdsc_do_request(mdsc, dir, req);
- if (!err && !req->r_reply_info.head->is_dentry)
+ if (!err && !req->r_reply_info.head->is_dentry) {
d_delete(dentry);
+ } else if (err == -ENOENT) {
+ ceph_dir_clear_complete(dir);
+ d_drop(dentry);
+ }
}

ceph_mdsc_put_request(req);
--
2.48.1