[PATCH v4 8/8] afs: lookup_atsys to drop and reclaim lock.

From: NeilBrown

Date: Sun Nov 29 2026 - 16:34:40 EST


From: NeilBrown <neil@xxxxxxxxxx>

If afs is asked to lookup a name ending with @sys, it needs to look up
a different name for which it might need to allocate a dentry with
d_alloc_parallel().
This is done while the parent lock is held which will be a problem in
a future patch where the ordering of the parent lock and
d_alloc_parallel() locking is reversed.

There is no actual need to hold the lock during this nested lookup. The
original dentry will never be used - it is left unhashed and a dentry
with the alternate name is returned.

With this patch we unlock the directory and unlock (d_lookup_done) the
dnetry early and reclaim the directory lock later. Then we use
lookup_noperm_unlocked() for the nested lookup.

Signed-off-by: NeilBrown <neil@xxxxxxxxxx>
---
fs/afs/dir.c | 23 ++++++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/fs/afs/dir.c b/fs/afs/dir.c
index b40e02143224..b4f2bcdc35b3 100644
--- a/fs/afs/dir.c
+++ b/fs/afs/dir.c
@@ -913,7 +913,8 @@ static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry)
/*
* Look up an entry in a directory with @sys substitution.
*/
-static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
+static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
+ unsigned int flags)
{
struct afs_sysnames *subs;
struct afs_net *net = afs_i2net(dir);
@@ -938,6 +939,18 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
refcount_inc(&subs->usage);
read_unlock(&net->sysnames_lock);

+ /*
+ * We need the directory to be unlocked so we can perform other lookups.
+ * We don't need the lock any more, we just need the name which we
+ * have taken a copy of.
+ * We won't be instantiating this dentry, so end lookup now. This
+ * makes it safe to take the parent lock in lookup_noperm_unlocked().
+ */
+ d_lookup_done(dentry);
+ if (flags & LOOKUP_SHARED)
+ inode_unlock_shared(dir);
+ else
+ inode_unlock(dir);
for (i = 0; i < subs->nr; i++) {
name = subs->subs[i];
len = dentry->d_name.len - 4 + strlen(name);
@@ -947,7 +960,7 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
}

strcpy(p, name);
- ret = lookup_noperm(&QSTR(buf), dentry->d_parent);
+ ret = lookup_noperm_unlocked(&QSTR(buf), dentry->d_parent);
if (IS_ERR(ret) || d_is_positive(ret))
goto out_s;
dput(ret);
@@ -958,6 +971,10 @@ static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry)
*/
ret = NULL;
out_s:
+ if (flags & LOOKUP_SHARED)
+ inode_lock_shared(dir);
+ else
+ inode_lock_nested(dir, I_MUTEX_PARENT);
afs_put_sysnames(subs);
kfree(buf);
out_p:
@@ -1003,7 +1020,7 @@ struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
dentry->d_name.name[dentry->d_name.len - 1] == 's')
- return afs_lookup_atsys(dir, dentry);
+ return afs_lookup_atsys(dir, dentry, flags);

afs_stat_v(dvnode, n_lookup);
inode = afs_do_lookup(dir, dentry);
--
2.50.0.107.gf914562f5916.dirty