Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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) <sforshee@kernel.org>
Message-Id: <20230202-fs-move-mount-replace-v4-3-98f3d80d7eaa@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>

+28 -21
+28 -21
fs/namespace.c
··· 2318 2318 static struct mountpoint *lock_mount(struct path *path) 2319 2319 { 2320 2320 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))) { 2334 2328 inode_unlock(dentry->d_inode); 2335 - return mp; 2329 + return ERR_PTR(-ENOENT); 2336 2330 } 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); 2338 2343 } 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; 2345 2352 } 2346 2353 2347 2354 static void unlock_mount(struct mountpoint *where)