new helper: topmost_overmount()

Returns the final (topmost) mount in the chain of overmounts
starting at given mount.  Same locking rules as for any mount
tree traversal - either the spinlock side of mount_lock, or
rcu + sample the seqcount side of mount_lock before the call
and recheck afterwards.

Reviewed-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
This commit is contained in:
Al Viro
2025-08-20 03:23:12 -04:00
parent ed8ba4aad7
commit 25423edc78
2 changed files with 10 additions and 6 deletions

View File

@@ -235,4 +235,11 @@ static inline void mnt_notify_add(struct mount *m)
}
#endif
static inline struct mount *topmost_overmount(struct mount *m)
{
while (m->overmount)
m = m->overmount;
return m;
}
struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry);

View File

@@ -2696,10 +2696,9 @@ static int attach_recursive_mnt(struct mount *source_mnt,
child->mnt_mountpoint);
commit_tree(child);
if (q) {
struct mount *r = topmost_overmount(child);
struct mountpoint *mp = root.mp;
struct mount *r = child;
while (unlikely(r->overmount))
r = r->overmount;
if (unlikely(shorter) && child != source_mnt)
mp = shorter;
mnt_change_mountpoint(r, mp, q);
@@ -6168,9 +6167,7 @@ bool current_chrooted(void)
guard(mount_locked_reader)();
root = current->nsproxy->mnt_ns->root;
while (unlikely(root->overmount))
root = root->overmount;
root = topmost_overmount(current->nsproxy->mnt_ns->root);
return fs_root.mnt != &root->mnt || !path_mounted(&fs_root);
}