Skip to content

Commit bf630c4

Browse files
Miklos Szeredibrauner
authored andcommitted
vfs: add notifications for mount attach and detach
Add notifications for attaching and detaching mounts to fs/namespace.c Signed-off-by: Miklos Szeredi <mszeredi@redhat.com> Link: https://lore.kernel.org/r/20250129165803.72138-4-mszeredi@redhat.com Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 0f46d81 commit bf630c4

3 files changed

Lines changed: 101 additions & 2 deletions

File tree

fs/mount.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <linux/ns_common.h>
66
#include <linux/fs_pin.h>
77

8+
extern struct list_head notify_list;
9+
810
struct mnt_namespace {
911
struct ns_common ns;
1012
struct mount * root;
@@ -80,6 +82,8 @@ struct mount {
8082
#ifdef CONFIG_FSNOTIFY
8183
struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks;
8284
__u32 mnt_fsnotify_mask;
85+
struct list_head to_notify; /* need to queue notification */
86+
struct mnt_namespace *prev_ns; /* previous namespace (NULL if none) */
8387
#endif
8488
int mnt_id; /* mount identifier, reused */
8589
u64 mnt_id_unique; /* mount ID unique until reboot */
@@ -182,4 +186,20 @@ static inline struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
182186
return container_of(ns, struct mnt_namespace, ns);
183187
}
184188

189+
#ifdef CONFIG_FSNOTIFY
190+
static inline void mnt_notify_add(struct mount *m)
191+
{
192+
/* Optimize the case where there are no watches */
193+
if ((m->mnt_ns && m->mnt_ns->n_fsnotify_marks) ||
194+
(m->prev_ns && m->prev_ns->n_fsnotify_marks))
195+
list_add_tail(&m->to_notify, &notify_list);
196+
else
197+
m->prev_ns = m->mnt_ns;
198+
}
199+
#else
200+
static inline void mnt_notify_add(struct mount *m)
201+
{
202+
}
203+
#endif
204+
185205
struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry);

fs/namespace.c

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ static HLIST_HEAD(unmounted); /* protected by namespace_sem */
8181
static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
8282
static DEFINE_SEQLOCK(mnt_ns_tree_lock);
8383

84+
#ifdef CONFIG_FSNOTIFY
85+
LIST_HEAD(notify_list); /* protected by namespace_sem */
86+
#endif
8487
static struct rb_root mnt_ns_tree = RB_ROOT; /* protected by mnt_ns_tree_lock */
8588
static LIST_HEAD(mnt_ns_list); /* protected by mnt_ns_tree_lock */
8689

@@ -163,6 +166,7 @@ static void mnt_ns_release(struct mnt_namespace *ns)
163166
{
164167
/* keep alive for {list,stat}mount() */
165168
if (refcount_dec_and_test(&ns->passive)) {
169+
fsnotify_mntns_delete(ns);
166170
put_user_ns(ns->user_ns);
167171
kfree(ns);
168172
}
@@ -1176,6 +1180,8 @@ static void mnt_add_to_ns(struct mnt_namespace *ns, struct mount *mnt)
11761180
ns->mnt_first_node = &mnt->mnt_node;
11771181
rb_link_node(&mnt->mnt_node, parent, link);
11781182
rb_insert_color(&mnt->mnt_node, &ns->mounts);
1183+
1184+
mnt_notify_add(mnt);
11791185
}
11801186

11811187
/*
@@ -1723,6 +1729,50 @@ int may_umount(struct vfsmount *mnt)
17231729

17241730
EXPORT_SYMBOL(may_umount);
17251731

1732+
#ifdef CONFIG_FSNOTIFY
1733+
static void mnt_notify(struct mount *p)
1734+
{
1735+
if (!p->prev_ns && p->mnt_ns) {
1736+
fsnotify_mnt_attach(p->mnt_ns, &p->mnt);
1737+
} else if (p->prev_ns && !p->mnt_ns) {
1738+
fsnotify_mnt_detach(p->prev_ns, &p->mnt);
1739+
} else if (p->prev_ns == p->mnt_ns) {
1740+
fsnotify_mnt_move(p->mnt_ns, &p->mnt);
1741+
} else {
1742+
fsnotify_mnt_detach(p->prev_ns, &p->mnt);
1743+
fsnotify_mnt_attach(p->mnt_ns, &p->mnt);
1744+
}
1745+
p->prev_ns = p->mnt_ns;
1746+
}
1747+
1748+
static void notify_mnt_list(void)
1749+
{
1750+
struct mount *m, *tmp;
1751+
/*
1752+
* Notify about mounts that were added/reparented/detached/remain
1753+
* connected after unmount.
1754+
*/
1755+
list_for_each_entry_safe(m, tmp, &notify_list, to_notify) {
1756+
mnt_notify(m);
1757+
list_del_init(&m->to_notify);
1758+
}
1759+
}
1760+
1761+
static bool need_notify_mnt_list(void)
1762+
{
1763+
return !list_empty(&notify_list);
1764+
}
1765+
#else
1766+
static void notify_mnt_list(void)
1767+
{
1768+
}
1769+
1770+
static bool need_notify_mnt_list(void)
1771+
{
1772+
return false;
1773+
}
1774+
#endif
1775+
17261776
static void namespace_unlock(void)
17271777
{
17281778
struct hlist_head head;
@@ -1733,7 +1783,18 @@ static void namespace_unlock(void)
17331783
hlist_move_list(&unmounted, &head);
17341784
list_splice_init(&ex_mountpoints, &list);
17351785

1736-
up_write(&namespace_sem);
1786+
if (need_notify_mnt_list()) {
1787+
/*
1788+
* No point blocking out concurrent readers while notifications
1789+
* are sent. This will also allow statmount()/listmount() to run
1790+
* concurrently.
1791+
*/
1792+
downgrade_write(&namespace_sem);
1793+
notify_mnt_list();
1794+
up_read(&namespace_sem);
1795+
} else {
1796+
up_write(&namespace_sem);
1797+
}
17371798

17381799
shrink_dentry_list(&list);
17391800

@@ -1846,6 +1907,19 @@ static void umount_tree(struct mount *mnt, enum umount_tree_flags how)
18461907
change_mnt_propagation(p, MS_PRIVATE);
18471908
if (disconnect)
18481909
hlist_add_head(&p->mnt_umount, &unmounted);
1910+
1911+
/*
1912+
* At this point p->mnt_ns is NULL, notification will be queued
1913+
* only if
1914+
*
1915+
* - p->prev_ns is non-NULL *and*
1916+
* - p->prev_ns->n_fsnotify_marks is non-NULL
1917+
*
1918+
* This will preclude queuing the mount if this is a cleanup
1919+
* after a failed copy_tree() or destruction of an anonymous
1920+
* namespace, etc.
1921+
*/
1922+
mnt_notify_add(p);
18491923
}
18501924
}
18511925

@@ -2555,6 +2629,7 @@ static int attach_recursive_mnt(struct mount *source_mnt,
25552629
dest_mp = smp;
25562630
unhash_mnt(source_mnt);
25572631
attach_mnt(source_mnt, top_mnt, dest_mp, beneath);
2632+
mnt_notify_add(source_mnt);
25582633
touch_mnt_namespace(source_mnt->mnt_ns);
25592634
} else {
25602635
if (source_mnt->mnt_ns) {
@@ -4476,6 +4551,8 @@ SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
44764551
list_del_init(&new_mnt->mnt_expire);
44774552
put_mountpoint(root_mp);
44784553
unlock_mount_hash();
4554+
mnt_notify_add(root_mnt);
4555+
mnt_notify_add(new_mnt);
44794556
chroot_fs_refs(&root, &new);
44804557
error = 0;
44814558
out4:

fs/pnode.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,10 @@ static void restore_mounts(struct list_head *to_restore)
549549
mp = parent->mnt_mp;
550550
parent = parent->mnt_parent;
551551
}
552-
if (parent != mnt->mnt_parent)
552+
if (parent != mnt->mnt_parent) {
553553
mnt_change_mountpoint(parent, mp, mnt);
554+
mnt_notify_add(mnt);
555+
}
554556
}
555557
}
556558

0 commit comments

Comments
 (0)