From 1ecc5b7c95ae585042ccbf3c587897dd99e2ddb5 Mon Sep 17 00:00:00 2001 From: orkunsari Date: Wed, 4 Nov 2026 14:56:15 +0300 Subject: [PATCH] nfsd: unlock the locked parent after crossing a mount in CREATE Since commit f58a48389c14 ("nfsd: correctly handle CREATE of mounted-on files"), nfsd_proc_create() and nfsd3_create_file() call nfsd_cross_mnt() on the dentry returned by start_creating(). When the name refers to a file that has something mounted on it, nfsd_cross_mnt() replaces that dentry with the root of the mounted filesystem and drops its reference to the original one. The replacement is later passed to end_creating(), which unlocks the d_parent of the dentry it is given. That is the parent of the mount source, not the directory start_creating() locked, so the directory being created in is left locked and another directory's i_rwsem is released without being held. Do the crossing on a separate reference that is only used for fh_compose(), so that the dentry returned by start_creating() is the one passed to end_creating(). Only positive regular files are crossed, so the later checks on the child dentry give the same result. This was found by AI-assisted code review (Claude Code). The fix has been build-tested with W=1 only; no reproducer was run. Fixes: f58a48389c14 ("nfsd: correctly handle CREATE of mounted-on files") Signed-off-by: orkunsari Link: https://patch.msgid.link/20261104-nfsd-cross-mnt-v1-1-5ec5fca0a553@gmail.com Signed-off-by: Chuck Lever --- fs/nfsd/nfs3proc.c | 14 +++++++++++--- fs/nfsd/nfsproc.c | 18 ++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/fs/nfsd/nfs3proc.c b/fs/nfsd/nfs3proc.c index 60cd01b6a37d..0711e93c9963 100644 --- a/fs/nfsd/nfs3proc.c +++ b/fs/nfsd/nfs3proc.c @@ -334,7 +334,7 @@ nfsd3_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct svc_fh *resfhp, struct nfsd3_createargs *argp) { struct iattr *iap = &argp->attrs; - struct dentry *parent, *child; + struct dentry *parent, *child, *mnt_child; struct nfsd_attrs attrs = { .na_iattr = iap, }; @@ -378,21 +378,29 @@ nfsd3_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp, } exp = exp_get(fhp->fh_export); + /* + * Cross on a separate reference: child must remain the dentry + * start_creating() returned so that end_creating() unlocks the + * right directory. + */ + mnt_child = dget(child); if (argp->createmode == NFS3_CREATE_UNCHECKED) { /* * If name is already in dcache we need to check for mountpoints */ if (d_is_reg(child) && unlikely(nfsd_mountpoint(child, exp))) { - status = nfsd_cross_mnt(rqstp, &child, &exp); + status = nfsd_cross_mnt(rqstp, &mnt_child, &exp); if (status != nfs_ok) { + dput(mnt_child); exp_put(exp); goto out; } } } - status = fh_compose(resfhp, exp, child, fhp); + status = fh_compose(resfhp, exp, mnt_child, fhp); + dput(mnt_child); exp_put(exp); if (status != nfs_ok) goto out; diff --git a/fs/nfsd/nfsproc.c b/fs/nfsd/nfsproc.c index 1f74008bc06a..2cecae4e0cdf 100644 --- a/fs/nfsd/nfsproc.c +++ b/fs/nfsd/nfsproc.c @@ -682,7 +682,7 @@ nfsd_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp, .na_iattr = attr, }; struct svc_export *exp; - struct dentry *dchild; + struct dentry *dchild, *dmnt; struct inode *inode; int type, mode; __be32 status; @@ -705,20 +705,26 @@ nfsd_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp, status = nfserrno(PTR_ERR(dchild)); goto out_drop_write; } - /* - * If name exists we need to check for mountpoints - */ exp = exp_get(fhp->fh_export); + /* + * If name exists we need to check for mountpoints. Cross on a + * separate reference: end_creating() unlocks the parent of the + * dentry it is given, and only dchild has the locked directory + * as its parent. + */ + dmnt = dget(dchild); if (d_is_reg(dchild) && unlikely(nfsd_mountpoint(dchild, exp))) { - status = nfsd_cross_mnt(rqstp, &dchild, &exp); + status = nfsd_cross_mnt(rqstp, &dmnt, &exp); if (status != nfs_ok) { + dput(dmnt); exp_put(exp); goto out; } } - status = fh_compose(resfhp, exp, dchild, fhp); + status = fh_compose(resfhp, exp, dmnt, fhp); + dput(dmnt); exp_put(exp); if (!status && d_really_is_negative(dchild)) status = nfserr_noent;