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>
7
8 extern struct file_system_type nullfs_fs_type;
9 extern struct list_head notify_list;
10
11 struct mnt_namespace {
12 struct ns_common ns;
13 struct mount * root;
14 struct {
15 struct rb_root mounts; /* Protected by namespace_sem */
16 struct rb_node *mnt_last_node; /* last (rightmost) mount in the rbtree */
17 struct rb_node *mnt_first_node; /* first (leftmost) mount in the rbtree */
18 };
19 struct user_namespace *user_ns;
20 struct ucounts *ucounts;
21 wait_queue_head_t poll;
22 u64 seq_origin; /* Sequence number of origin mount namespace */
23 u64 event;
24 #ifdef CONFIG_FSNOTIFY
25 __u32 n_fsnotify_mask;
26 struct fsnotify_mark_connector __rcu *n_fsnotify_marks;
27 #endif
28 unsigned int nr_mounts; /* # of mounts in the namespace */
29 unsigned int pending_mounts;
30 refcount_t passive; /* number references not pinning @mounts */
31 bool is_anon;
32 } __randomize_layout;
33
34 struct mnt_pcp {
35 int mnt_count;
36 int mnt_writers;
37 };
38
39 struct mountpoint {
40 struct hlist_node m_hash;
41 struct dentry *m_dentry;
42 struct hlist_head m_list;
43 };
44
45 struct mount {
46 struct hlist_node mnt_hash;
47 struct mount *mnt_parent;
48 struct dentry *mnt_mountpoint;
49 struct vfsmount mnt;
50 union {
51 struct rb_node mnt_node; /* node in the ns->mounts rbtree */
52 struct rcu_head mnt_rcu;
53 struct llist_node mnt_llist;
54 };
55 #ifdef CONFIG_SMP
56 struct mnt_pcp __percpu *mnt_pcp;
57 #else
58 int mnt_count;
59 int mnt_writers;
60 #endif
61 struct list_head mnt_mounts; /* list of children, anchored here */
62 struct list_head mnt_child; /* and going through their mnt_child */
63 struct mount *mnt_next_for_sb; /* the next two fields are hlist_node, */
64 struct mount * __aligned(1) *mnt_pprev_for_sb;
65 /* except that LSB of pprev is stolen */
66 #define WRITE_HOLD 1 /* ... for use by mnt_hold_writers() */
67 const char *mnt_devname; /* Name of device e.g. /dev/dsk/hda1 */
68 struct list_head mnt_list;
69 struct list_head mnt_expire; /* link in fs-specific expiry list */
70 struct list_head mnt_share; /* circular list of shared mounts */
71 struct hlist_head mnt_slave_list;/* list of slave mounts */
72 struct hlist_node mnt_slave; /* slave list entry */
73 struct mount *mnt_master; /* slave is on master->mnt_slave_list */
74 /*
75 * Containing namespace (active or deactivating, non-refcounted).
76 * Normally protected by namespace_sem.
77 * Can also be accessed locklessly under RCU. RCU readers can't rely on
78 * the namespace still being active, but implicitly hold a passive
79 * reference (because an RCU delay happens between a namespace being
80 * deactivated and the corresponding passive refcount drop).
81 */
82 struct mnt_namespace *mnt_ns;
83 struct mountpoint *mnt_mp; /* where is it mounted */
84 union {
85 struct hlist_node mnt_mp_list; /* list mounts with the same mountpoint */
86 struct hlist_node mnt_umount;
87 };
88 #ifdef CONFIG_FSNOTIFY
89 struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks;
90 __u32 mnt_fsnotify_mask;
91 struct list_head to_notify; /* need to queue notification */
92 struct mnt_namespace *prev_ns; /* previous namespace (NULL if none) */
93 #endif
94 int mnt_t_flags; /* namespace_sem-protected flags */
95 int mnt_id; /* mount identifier, reused */
96 u64 mnt_id_unique; /* mount ID unique until reboot */
97 int mnt_group_id; /* peer group identifier */
98 int mnt_expiry_mark; /* true if marked for expiry */
99 struct hlist_head mnt_pins;
100 struct hlist_head mnt_stuck_children;
101 struct mount *overmount; /* mounted on ->mnt_root */
102 } __randomize_layout;
103
104 enum {
105 T_SHARED = 1, /* mount is shared */
106 T_UNBINDABLE = 2, /* mount is unbindable */
107 T_MARKED = 4, /* internal mark for propagate_... */
108 T_UMOUNT_CANDIDATE = 8, /* for propagate_umount */
109
110 /*
111 * T_SHARED_MASK is the set of flags that should be cleared when a
112 * mount becomes shared. Currently, this is only the flag that says a
113 * mount cannot be bind mounted, since this is how we create a mount
114 * that shares events with another mount. If you add a new T_*
115 * flag, consider how it interacts with shared mounts.
116 */
117 T_SHARED_MASK = T_UNBINDABLE,
118 };
119
120 #define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */
121
real_mount(struct vfsmount * mnt)122 static inline struct mount *real_mount(struct vfsmount *mnt)
123 {
124 return container_of(mnt, struct mount, mnt);
125 }
126
mnt_has_parent(const struct mount * mnt)127 static inline int mnt_has_parent(const struct mount *mnt)
128 {
129 return mnt != mnt->mnt_parent;
130 }
131
is_mounted(struct vfsmount * mnt)132 static inline int is_mounted(struct vfsmount *mnt)
133 {
134 /* neither detached nor internal? */
135 return !IS_ERR_OR_NULL(real_mount(mnt)->mnt_ns);
136 }
137
138 extern struct mount *__lookup_mnt(struct vfsmount *, struct dentry *);
139
140 extern int __legitimize_mnt(struct vfsmount *, unsigned);
141
__path_is_mountpoint(const struct path * path)142 static inline bool __path_is_mountpoint(const struct path *path)
143 {
144 struct mount *m = __lookup_mnt(path->mnt, path->dentry);
145 return m && likely(!(m->mnt.mnt_flags & MNT_SYNC_UMOUNT));
146 }
147
148 extern void __detach_mounts(struct dentry *dentry);
149
detach_mounts(struct dentry * dentry)150 static inline void detach_mounts(struct dentry *dentry)
151 {
152 if (!d_mountpoint(dentry))
153 return;
154 __detach_mounts(dentry);
155 }
156
get_mnt_ns(struct mnt_namespace * ns)157 static inline void get_mnt_ns(struct mnt_namespace *ns)
158 {
159 ns_ref_inc(ns);
160 }
161
162 extern seqlock_t mount_lock;
163
164 DEFINE_LOCK_GUARD_0(mount_writer, write_seqlock(&mount_lock),
165 write_sequnlock(&mount_lock))
166 DEFINE_LOCK_GUARD_0(mount_locked_reader, read_seqlock_excl(&mount_lock),
167 read_sequnlock_excl(&mount_lock))
168
169 struct proc_mounts {
170 struct mnt_namespace *ns;
171 struct path root;
172 int (*show)(struct seq_file *, struct vfsmount *);
173 };
174
175 extern const struct seq_operations mounts_op;
176
177 extern bool __is_local_mountpoint(const struct dentry *dentry);
is_local_mountpoint(const struct dentry * dentry)178 static inline bool is_local_mountpoint(const struct dentry *dentry)
179 {
180 if (!d_mountpoint(dentry))
181 return false;
182
183 return __is_local_mountpoint(dentry);
184 }
185
is_anon_ns(struct mnt_namespace * ns)186 static inline bool is_anon_ns(struct mnt_namespace *ns)
187 {
188 return ns->is_anon;
189 }
190
anon_ns_root(const struct mount * m)191 static inline bool anon_ns_root(const struct mount *m)
192 {
193 struct mnt_namespace *ns = READ_ONCE(m->mnt_ns);
194
195 return !IS_ERR_OR_NULL(ns) && is_anon_ns(ns) && m == ns->root;
196 }
197
mnt_ns_attached(const struct mount * mnt)198 static inline bool mnt_ns_attached(const struct mount *mnt)
199 {
200 return !RB_EMPTY_NODE(&mnt->mnt_node);
201 }
202
mnt_ns_empty(const struct mnt_namespace * ns)203 static inline bool mnt_ns_empty(const struct mnt_namespace *ns)
204 {
205 return RB_EMPTY_ROOT(&ns->mounts);
206 }
207
move_from_ns(struct mount * mnt)208 static inline void move_from_ns(struct mount *mnt)
209 {
210 struct mnt_namespace *ns = mnt->mnt_ns;
211 WARN_ON(!mnt_ns_attached(mnt));
212 if (ns->mnt_last_node == &mnt->mnt_node)
213 ns->mnt_last_node = rb_prev(&mnt->mnt_node);
214 if (ns->mnt_first_node == &mnt->mnt_node)
215 ns->mnt_first_node = rb_next(&mnt->mnt_node);
216 rb_erase(&mnt->mnt_node, &ns->mounts);
217 RB_CLEAR_NODE(&mnt->mnt_node);
218 }
219
220 bool has_locked_children(struct mount *mnt, struct dentry *dentry);
221 struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mnt_ns,
222 bool previous);
223
to_mnt_ns(struct ns_common * ns)224 static inline struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
225 {
226 return container_of(ns, struct mnt_namespace, ns);
227 }
228
229 #ifdef CONFIG_FSNOTIFY
mnt_notify_add(struct mount * m)230 static inline void mnt_notify_add(struct mount *m)
231 {
232 /* Optimize the case where there are no watches */
233 if ((m->mnt_ns && m->mnt_ns->n_fsnotify_marks) ||
234 (m->prev_ns && m->prev_ns->n_fsnotify_marks))
235 list_add_tail(&m->to_notify, ¬ify_list);
236 else
237 m->prev_ns = m->mnt_ns;
238 }
239 #else
mnt_notify_add(struct mount * m)240 static inline void mnt_notify_add(struct mount *m)
241 {
242 }
243 #endif
244
topmost_overmount(struct mount * m)245 static inline struct mount *topmost_overmount(struct mount *m)
246 {
247 while (m->overmount)
248 m = m->overmount;
249 return m;
250 }
251
252 static inline bool __test_write_hold(struct mount * __aligned(1) *val)
253 {
254 return (unsigned long)val & WRITE_HOLD;
255 }
256
test_write_hold(const struct mount * m)257 static inline bool test_write_hold(const struct mount *m)
258 {
259 return __test_write_hold(m->mnt_pprev_for_sb);
260 }
261
set_write_hold(struct mount * m)262 static inline void set_write_hold(struct mount *m)
263 {
264 m->mnt_pprev_for_sb = (void *)((unsigned long)m->mnt_pprev_for_sb
265 | WRITE_HOLD);
266 }
267
clear_write_hold(struct mount * m)268 static inline void clear_write_hold(struct mount *m)
269 {
270 m->mnt_pprev_for_sb = (void *)((unsigned long)m->mnt_pprev_for_sb
271 & ~WRITE_HOLD);
272 }
273
274 struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry);
275