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