Skip to content

Commit 80b5dce

Browse files
Eric W. BiedermanAl Viro
authored andcommitted
vfs: Add a function to lazily unmount all mounts from any dentry.
The new function detach_mounts comes in two pieces. The first piece is a static inline test of d_mounpoint that returns immediately without taking any locks if d_mounpoint is not set. In the common case when mountpoints are absent this allows the vfs to continue running with it's same cacheline foot print. The second piece of detach_mounts __detach_mounts actually does the work and it assumes that a mountpoint is present so it is slow and takes namespace_sem for write, and then locks the mount hash (aka mount_lock) after a struct mountpoint has been found. With those two locks held each entry on the list of mounts on a mountpoint is selected and lazily unmounted until all of the mount have been lazily unmounted. v7: Wrote a proper change description and removed the changelog documenting deleted wrong turns. Signed-off-by: Eric W. Biederman <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent e2dfa93 commit 80b5dce

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

fs/mount.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ extern struct mount *__lookup_mnt_last(struct vfsmount *, struct dentry *);
8787

8888
extern bool legitimize_mnt(struct vfsmount *, unsigned);
8989

90+
extern void __detach_mounts(struct dentry *dentry);
91+
92+
static inline void detach_mounts(struct dentry *dentry)
93+
{
94+
if (!d_mountpoint(dentry))
95+
return;
96+
__detach_mounts(dentry);
97+
}
98+
9099
static inline void get_mnt_ns(struct mnt_namespace *ns)
91100
{
92101
atomic_inc(&ns->count);

fs/namespace.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,37 @@ static int do_umount(struct mount *mnt, int flags)
14681468
return retval;
14691469
}
14701470

1471+
/*
1472+
* __detach_mounts - lazily unmount all mounts on the specified dentry
1473+
*
1474+
* During unlink, rmdir, and d_drop it is possible to loose the path
1475+
* to an existing mountpoint, and wind up leaking the mount.
1476+
* detach_mounts allows lazily unmounting those mounts instead of
1477+
* leaking them.
1478+
*
1479+
* The caller may hold dentry->d_inode->i_mutex.
1480+
*/
1481+
void __detach_mounts(struct dentry *dentry)
1482+
{
1483+
struct mountpoint *mp;
1484+
struct mount *mnt;
1485+
1486+
namespace_lock();
1487+
mp = lookup_mountpoint(dentry);
1488+
if (!mp)
1489+
goto out_unlock;
1490+
1491+
lock_mount_hash();
1492+
while (!hlist_empty(&mp->m_list)) {
1493+
mnt = hlist_entry(mp->m_list.first, struct mount, mnt_mp_list);
1494+
umount_tree(mnt, 2);
1495+
}
1496+
unlock_mount_hash();
1497+
put_mountpoint(mp);
1498+
out_unlock:
1499+
namespace_unlock();
1500+
}
1501+
14711502
/*
14721503
* Is the caller allowed to modify his namespace?
14731504
*/

0 commit comments

Comments
 (0)