Skip to content

Commit 78aa08a

Browse files
committed
fs: add path_mounted()
Add a small helper to check whether a path refers to the root of the mount instead of open-coding this everywhere. Reviewed-by: Seth Forshee (DigitalOcean) <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent f1fcbaa commit 78aa08a

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

fs/namespace.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,19 @@ bool may_mount(void)
17671767
return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
17681768
}
17691769

1770+
/**
1771+
* path_mounted - check whether path is mounted
1772+
* @path: path to check
1773+
*
1774+
* Determine whether @path refers to the root of a mount.
1775+
*
1776+
* Return: true if @path is the root of a mount, false if not.
1777+
*/
1778+
static inline bool path_mounted(const struct path *path)
1779+
{
1780+
return path->mnt->mnt_root == path->dentry;
1781+
}
1782+
17701783
static void warn_mandlock(void)
17711784
{
17721785
pr_warn_once("=======================================================\n"
@@ -1782,7 +1795,7 @@ static int can_umount(const struct path *path, int flags)
17821795

17831796
if (!may_mount())
17841797
return -EPERM;
1785-
if (path->dentry != path->mnt->mnt_root)
1798+
if (!path_mounted(path))
17861799
return -EINVAL;
17871800
if (!check_mnt(mnt))
17881801
return -EINVAL;
@@ -2367,7 +2380,7 @@ static int do_change_type(struct path *path, int ms_flags)
23672380
int type;
23682381
int err = 0;
23692382

2370-
if (path->dentry != path->mnt->mnt_root)
2383+
if (!path_mounted(path))
23712384
return -EINVAL;
23722385

23732386
type = flags_to_propagation_type(ms_flags);
@@ -2643,7 +2656,7 @@ static int do_reconfigure_mnt(struct path *path, unsigned int mnt_flags)
26432656
if (!check_mnt(mnt))
26442657
return -EINVAL;
26452658

2646-
if (path->dentry != mnt->mnt.mnt_root)
2659+
if (!path_mounted(path))
26472660
return -EINVAL;
26482661

26492662
if (!can_change_locked_flags(mnt, mnt_flags))
@@ -2682,7 +2695,7 @@ static int do_remount(struct path *path, int ms_flags, int sb_flags,
26822695
if (!check_mnt(mnt))
26832696
return -EINVAL;
26842697

2685-
if (path->dentry != path->mnt->mnt_root)
2698+
if (!path_mounted(path))
26862699
return -EINVAL;
26872700

26882701
if (!can_change_locked_flags(mnt, mnt_flags))
@@ -2772,9 +2785,9 @@ static int do_set_group(struct path *from_path, struct path *to_path)
27722785

27732786
err = -EINVAL;
27742787
/* To and From paths should be mount roots */
2775-
if (from_path->dentry != from_path->mnt->mnt_root)
2788+
if (!path_mounted(from_path))
27762789
goto out;
2777-
if (to_path->dentry != to_path->mnt->mnt_root)
2790+
if (!path_mounted(to_path))
27782791
goto out;
27792792

27802793
/* Setting sharing groups is only allowed across same superblock */
@@ -2855,7 +2868,7 @@ static int do_move_mount(struct path *old_path, struct path *new_path)
28552868
if (old->mnt.mnt_flags & MNT_LOCKED)
28562869
goto out;
28572870

2858-
if (old_path->dentry != old_path->mnt->mnt_root)
2871+
if (!path_mounted(old_path))
28592872
goto out;
28602873

28612874
if (d_is_dir(new_path->dentry) !=
@@ -2937,8 +2950,7 @@ static int do_add_mount(struct mount *newmnt, struct mountpoint *mp,
29372950
}
29382951

29392952
/* Refuse the same filesystem on the same mount point */
2940-
if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
2941-
path->mnt->mnt_root == path->dentry)
2953+
if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && path_mounted(path))
29422954
return -EBUSY;
29432955

29442956
if (d_is_symlink(newmnt->mnt.mnt_root))
@@ -3917,11 +3929,11 @@ SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
39173929
if (new_mnt == root_mnt || old_mnt == root_mnt)
39183930
goto out4; /* loop, on the same file system */
39193931
error = -EINVAL;
3920-
if (root.mnt->mnt_root != root.dentry)
3932+
if (!path_mounted(&root))
39213933
goto out4; /* not a mountpoint */
39223934
if (!mnt_has_parent(root_mnt))
39233935
goto out4; /* not attached */
3924-
if (new.mnt->mnt_root != new.dentry)
3936+
if (!path_mounted(&new))
39253937
goto out4; /* not a mountpoint */
39263938
if (!mnt_has_parent(new_mnt))
39273939
goto out4; /* not attached */
@@ -4124,7 +4136,7 @@ static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
41244136
struct mount *mnt = real_mount(path->mnt);
41254137
int err = 0;
41264138

4127-
if (path->dentry != mnt->mnt.mnt_root)
4139+
if (!path_mounted(path))
41284140
return -EINVAL;
41294141

41304142
if (kattr->mnt_userns) {

0 commit comments

Comments
 (0)