]> www.infradead.org Git - users/hch/misc.git/commitdiff
new helper: topmost_overmount()
authorAl Viro <viro@zeniv.linux.org.uk>
Wed, 20 Aug 2025 07:23:12 +0000 (03:23 -0400)
committerAl Viro <viro@zeniv.linux.org.uk>
Tue, 16 Sep 2025 01:26:05 +0000 (21:26 -0400)
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>
fs/mount.h
fs/namespace.c

index ed8c83ba836a7418d849c0cb3f3812aa01791e8b..04d0eadc4c1060639c80dd2751856779514b7b80 100644 (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);
index b48bfb46b351d1ae91241a3b7cc1c950cf233329..b6983adaa73b7aa4c544331f0a736351b894cfca 100644 (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);
 }