Skip to content

Commit 9cb79ed

Browse files
author
Al Viro
committed
new predicate: mount_is_ancestor()
mount_is_ancestor(p1, p2) returns true iff there is a possibly empty ancestry chain from p1 to p2. Convert the open-coded checks. Unlike those open-coded variants it does not depend upon p1 not being root... Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent 592238c commit 9cb79ed

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

fs/namespace.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3483,6 +3483,17 @@ static inline bool path_overmounted(const struct path *path)
34833483
return unlikely(!no_child);
34843484
}
34853485

3486+
/*
3487+
* Check if there is a possibly empty chain of descent from p1 to p2.
3488+
* Locks: namespace_sem (shared) or mount_lock (read_seqlock_excl).
3489+
*/
3490+
static bool mount_is_ancestor(const struct mount *p1, const struct mount *p2)
3491+
{
3492+
while (p2 != p1 && mnt_has_parent(p2))
3493+
p2 = p2->mnt_parent;
3494+
return p2 == p1;
3495+
}
3496+
34863497
/**
34873498
* can_move_mount_beneath - check that we can mount beneath the top mount
34883499
* @from: mount to mount beneath
@@ -3534,9 +3545,8 @@ static int can_move_mount_beneath(const struct path *from,
35343545
if (parent_mnt_to == current->nsproxy->mnt_ns->root)
35353546
return -EINVAL;
35363547

3537-
for (struct mount *p = mnt_from; mnt_has_parent(p); p = p->mnt_parent)
3538-
if (p == mnt_to)
3539-
return -EINVAL;
3548+
if (mount_is_ancestor(mnt_to, mnt_from))
3549+
return -EINVAL;
35403550

35413551
/*
35423552
* If the parent mount propagates to the child mount this would
@@ -3705,9 +3715,8 @@ static int do_move_mount(struct path *old_path,
37053715
err = -ELOOP;
37063716
if (!check_for_nsfs_mounts(old))
37073717
goto out;
3708-
for (; mnt_has_parent(p); p = p->mnt_parent)
3709-
if (p == old)
3710-
goto out;
3718+
if (mount_is_ancestor(old, p))
3719+
goto out;
37113720

37123721
err = attach_recursive_mnt(old, real_mount(new_path->mnt), mp, flags);
37133722
if (err)

0 commit comments

Comments
 (0)