1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/mount.h>
3 #include <linux/seq_file.h>
4 #include <linux/poll.h>
5 #include <linux/ns_common.h>
6 #include <linux/fs_pin.h>
8 extern struct list_head notify_list;
10 struct mnt_namespace {
14 struct rb_root mounts; /* Protected by namespace_sem */
15 struct rb_node *mnt_last_node; /* last (rightmost) mount in the rbtree */
16 struct rb_node *mnt_first_node; /* first (leftmost) mount in the rbtree */
18 struct user_namespace *user_ns;
19 struct ucounts *ucounts;
20 u64 seq; /* Sequence number to prevent loops */
22 wait_queue_head_t poll;
23 struct rcu_head mnt_ns_rcu;
25 u64 seq_origin; /* Sequence number of origin mount namespace */
27 #ifdef CONFIG_FSNOTIFY
28 __u32 n_fsnotify_mask;
29 struct fsnotify_mark_connector __rcu *n_fsnotify_marks;
31 unsigned int nr_mounts; /* # of mounts in the namespace */
32 unsigned int pending_mounts;
33 struct rb_node mnt_ns_tree_node; /* node in the mnt_ns_tree */
34 struct list_head mnt_ns_list; /* entry in the sequential list of mounts namespace */
35 refcount_t passive; /* number references not pinning @mounts */
44 struct hlist_node m_hash;
45 struct dentry *m_dentry;
46 struct hlist_head m_list;
51 struct hlist_node mnt_hash;
52 struct mount *mnt_parent;
53 struct dentry *mnt_mountpoint;
56 struct rb_node mnt_node; /* node in the ns->mounts rbtree */
57 struct rcu_head mnt_rcu;
58 struct llist_node mnt_llist;
61 struct mnt_pcp __percpu *mnt_pcp;
66 struct list_head mnt_mounts; /* list of children, anchored here */
67 struct list_head mnt_child; /* and going through their mnt_child */
68 struct list_head mnt_instance; /* mount instance on sb->s_mounts */
69 const char *mnt_devname; /* Name of device e.g. /dev/dsk/hda1 */
70 struct list_head mnt_list;
71 struct list_head mnt_expire; /* link in fs-specific expiry list */
72 struct list_head mnt_share; /* circular list of shared mounts */
73 struct list_head mnt_slave_list;/* list of slave mounts */
74 struct list_head mnt_slave; /* slave list entry */
75 struct mount *mnt_master; /* slave is on master->mnt_slave_list */
76 struct mnt_namespace *mnt_ns; /* containing namespace */
77 struct mountpoint *mnt_mp; /* where is it mounted */
79 struct hlist_node mnt_mp_list; /* list mounts with the same mountpoint */
80 struct hlist_node mnt_umount;
82 struct list_head mnt_umounting; /* list entry for umount propagation */
83 #ifdef CONFIG_FSNOTIFY
84 struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks;
85 __u32 mnt_fsnotify_mask;
86 struct list_head to_notify; /* need to queue notification */
87 struct mnt_namespace *prev_ns; /* previous namespace (NULL if none) */
89 int mnt_id; /* mount identifier, reused */
90 u64 mnt_id_unique; /* mount ID unique until reboot */
91 int mnt_group_id; /* peer group identifier */
92 int mnt_expiry_mark; /* true if marked for expiry */
93 struct hlist_head mnt_pins;
94 struct hlist_head mnt_stuck_children;
97 #define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */
99 static inline struct mount *real_mount(struct vfsmount *mnt)
101 return container_of(mnt, struct mount, mnt);
104 static inline int mnt_has_parent(struct mount *mnt)
106 return mnt != mnt->mnt_parent;
109 static inline int is_mounted(struct vfsmount *mnt)
111 /* neither detached nor internal? */
112 return !IS_ERR_OR_NULL(real_mount(mnt)->mnt_ns);
115 extern struct mount *__lookup_mnt(struct vfsmount *, struct dentry *);
117 extern int __legitimize_mnt(struct vfsmount *, unsigned);
119 static inline bool __path_is_mountpoint(const struct path *path)
121 struct mount *m = __lookup_mnt(path->mnt, path->dentry);
122 return m && likely(!(m->mnt.mnt_flags & MNT_SYNC_UMOUNT));
125 extern void __detach_mounts(struct dentry *dentry);
127 static inline void detach_mounts(struct dentry *dentry)
129 if (!d_mountpoint(dentry))
131 __detach_mounts(dentry);
134 static inline void get_mnt_ns(struct mnt_namespace *ns)
136 refcount_inc(&ns->ns.count);
139 extern seqlock_t mount_lock;
142 struct mnt_namespace *ns;
144 int (*show)(struct seq_file *, struct vfsmount *);
147 extern const struct seq_operations mounts_op;
149 extern bool __is_local_mountpoint(struct dentry *dentry);
150 static inline bool is_local_mountpoint(struct dentry *dentry)
152 if (!d_mountpoint(dentry))
155 return __is_local_mountpoint(dentry);
158 static inline bool is_anon_ns(struct mnt_namespace *ns)
163 static inline bool mnt_ns_attached(const struct mount *mnt)
165 return !RB_EMPTY_NODE(&mnt->mnt_node);
168 static inline bool mnt_ns_empty(const struct mnt_namespace *ns)
170 return RB_EMPTY_ROOT(&ns->mounts);
173 static inline void move_from_ns(struct mount *mnt, struct list_head *dt_list)
175 struct mnt_namespace *ns = mnt->mnt_ns;
176 WARN_ON(!mnt_ns_attached(mnt));
177 if (ns->mnt_last_node == &mnt->mnt_node)
178 ns->mnt_last_node = rb_prev(&mnt->mnt_node);
179 if (ns->mnt_first_node == &mnt->mnt_node)
180 ns->mnt_first_node = rb_next(&mnt->mnt_node);
181 rb_erase(&mnt->mnt_node, &ns->mounts);
182 RB_CLEAR_NODE(&mnt->mnt_node);
183 list_add_tail(&mnt->mnt_list, dt_list);
186 bool has_locked_children(struct mount *mnt, struct dentry *dentry);
187 struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mnt_ns,
190 static inline struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
192 return container_of(ns, struct mnt_namespace, ns);
195 #ifdef CONFIG_FSNOTIFY
196 static inline void mnt_notify_add(struct mount *m)
198 /* Optimize the case where there are no watches */
199 if ((m->mnt_ns && m->mnt_ns->n_fsnotify_marks) ||
200 (m->prev_ns && m->prev_ns->n_fsnotify_marks))
201 list_add_tail(&m->to_notify, ¬ify_list);
203 m->prev_ns = m->mnt_ns;
206 static inline void mnt_notify_add(struct mount *m)
211 struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry);