Skip to content

Commit

Permalink
fs: add path_mounted()
Browse files Browse the repository at this point in the history
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) <sforshee@kernel.org>
Message-Id: <20230202-fs-move-mount-replace-v4-1-98f3d80d7eaa@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
  • Loading branch information
Christian Brauner committed May 19, 2023
1 parent f1fcbaa commit 78aa08a
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions fs/namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,19 @@ bool may_mount(void)
return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
}

/**
* path_mounted - check whether path is mounted
* @path: path to check
*
* Determine whether @path refers to the root of a mount.
*
* Return: true if @path is the root of a mount, false if not.
*/
static inline bool path_mounted(const struct path *path)
{
return path->mnt->mnt_root == path->dentry;
}

static void warn_mandlock(void)
{
pr_warn_once("=======================================================\n"
Expand All @@ -1782,7 +1795,7 @@ static int can_umount(const struct path *path, int flags)

if (!may_mount())
return -EPERM;
if (path->dentry != path->mnt->mnt_root)
if (!path_mounted(path))
return -EINVAL;
if (!check_mnt(mnt))
return -EINVAL;
Expand Down Expand Up @@ -2367,7 +2380,7 @@ static int do_change_type(struct path *path, int ms_flags)
int type;
int err = 0;

if (path->dentry != path->mnt->mnt_root)
if (!path_mounted(path))
return -EINVAL;

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

if (path->dentry != mnt->mnt.mnt_root)
if (!path_mounted(path))
return -EINVAL;

if (!can_change_locked_flags(mnt, mnt_flags))
Expand Down Expand Up @@ -2682,7 +2695,7 @@ static int do_remount(struct path *path, int ms_flags, int sb_flags,
if (!check_mnt(mnt))
return -EINVAL;

if (path->dentry != path->mnt->mnt_root)
if (!path_mounted(path))
return -EINVAL;

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

err = -EINVAL;
/* To and From paths should be mount roots */
if (from_path->dentry != from_path->mnt->mnt_root)
if (!path_mounted(from_path))
goto out;
if (to_path->dentry != to_path->mnt->mnt_root)
if (!path_mounted(to_path))
goto out;

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

if (old_path->dentry != old_path->mnt->mnt_root)
if (!path_mounted(old_path))
goto out;

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

/* Refuse the same filesystem on the same mount point */
if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
path->mnt->mnt_root == path->dentry)
if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb && path_mounted(path))
return -EBUSY;

if (d_is_symlink(newmnt->mnt.mnt_root))
Expand Down Expand Up @@ -3917,11 +3929,11 @@ SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
if (new_mnt == root_mnt || old_mnt == root_mnt)
goto out4; /* loop, on the same file system */
error = -EINVAL;
if (root.mnt->mnt_root != root.dentry)
if (!path_mounted(&root))
goto out4; /* not a mountpoint */
if (!mnt_has_parent(root_mnt))
goto out4; /* not attached */
if (new.mnt->mnt_root != new.dentry)
if (!path_mounted(&new))
goto out4; /* not a mountpoint */
if (!mnt_has_parent(new_mnt))
goto out4; /* not attached */
Expand Down Expand Up @@ -4124,7 +4136,7 @@ static int do_mount_setattr(struct path *path, struct mount_kattr *kattr)
struct mount *mnt = real_mount(path->mnt);
int err = 0;

if (path->dentry != mnt->mnt.mnt_root)
if (!path_mounted(path))
return -EINVAL;

if (kattr->mnt_userns) {
Expand Down

0 comments on commit 78aa08a

Please sign in to comment.