Skip to content

Commit 64f44b2

Browse files
committed
fs: use a for loop when locking a mount
Currently, lock_mount() uses a goto to retry the lookup until it succeeded in acquiring the namespace_lock() preventing the top mount from being overmounted. While that's perfectly fine we want to lookup the mountpoint on the parent of the top mount in later patches. So adapt the code to make this easier to implement. Also, the for loop is arguably a little cleaner and makes the code easier to follow. No functional changes intended. Reviewed-by: Seth Forshee (DigitalOcean) <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 104026c commit 64f44b2

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

fs/namespace.c

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,30 +2318,37 @@ static int attach_recursive_mnt(struct mount *source_mnt,
23182318
static struct mountpoint *lock_mount(struct path *path)
23192319
{
23202320
struct vfsmount *mnt;
2321-
struct dentry *dentry = path->dentry;
2322-
retry:
2323-
inode_lock(dentry->d_inode);
2324-
if (unlikely(cant_mount(dentry))) {
2325-
inode_unlock(dentry->d_inode);
2326-
return ERR_PTR(-ENOENT);
2327-
}
2328-
namespace_lock();
2329-
mnt = lookup_mnt(path);
2330-
if (likely(!mnt)) {
2331-
struct mountpoint *mp = get_mountpoint(dentry);
2332-
if (IS_ERR(mp)) {
2333-
namespace_unlock();
2321+
struct dentry *dentry;
2322+
struct mountpoint *mp;
2323+
2324+
for (;;) {
2325+
dentry = path->dentry;
2326+
inode_lock(dentry->d_inode);
2327+
if (unlikely(cant_mount(dentry))) {
23342328
inode_unlock(dentry->d_inode);
2335-
return mp;
2329+
return ERR_PTR(-ENOENT);
23362330
}
2337-
return mp;
2331+
2332+
namespace_lock();
2333+
2334+
mnt = lookup_mnt(path);
2335+
if (likely(!mnt))
2336+
break;
2337+
2338+
namespace_unlock();
2339+
inode_unlock(dentry->d_inode);
2340+
path_put(path);
2341+
path->mnt = mnt;
2342+
path->dentry = dget(mnt->mnt_root);
23382343
}
2339-
namespace_unlock();
2340-
inode_unlock(path->dentry->d_inode);
2341-
path_put(path);
2342-
path->mnt = mnt;
2343-
dentry = path->dentry = dget(mnt->mnt_root);
2344-
goto retry;
2344+
2345+
mp = get_mountpoint(dentry);
2346+
if (IS_ERR(mp)) {
2347+
namespace_unlock();
2348+
inode_unlock(dentry->d_inode);
2349+
}
2350+
2351+
return mp;
23452352
}
23462353

23472354
static void unlock_mount(struct mountpoint *where)

0 commit comments

Comments
 (0)