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 list_head notify_list;
9
10 typedef __u32 __bitwise mntns_flags_t;
11
12 #define MNTNS_PROPAGATING ((__force mntns_flags_t)(1 << 0))
13
14 struct mnt_namespace {
15 struct ns_common ns;
16 struct mount * root;
17 struct {
18 struct rb_root mounts; /* Protected by namespace_sem */
19 struct rb_node *mnt_last_node; /* last (rightmost) mount in the rbtree */
20 struct rb_node *mnt_first_node; /* first (leftmost) mount in the rbtree */
21 };
22 struct user_namespace *user_ns;
23 struct ucounts *ucounts;
24 u64 seq; /* Sequence number to prevent loops */
25 union {
26 wait_queue_head_t poll;
27 struct rcu_head mnt_ns_rcu;
28 };
29 u64 seq_origin; /* Sequence number of origin mount namespace */
30 u64 event;
31 #ifdef CONFIG_FSNOTIFY
32 __u32 n_fsnotify_mask;
33 struct fsnotify_mark_connector __rcu *n_fsnotify_marks;
34 #endif
35 unsigned int nr_mounts; /* # of mounts in the namespace */
36 unsigned int pending_mounts;
37 struct rb_node mnt_ns_tree_node; /* node in the mnt_ns_tree */
38 struct list_head mnt_ns_list; /* entry in the sequential list of mounts namespace */
39 refcount_t passive; /* number references not pinning @mounts */
40 mntns_flags_t mntns_flags;
41 } __randomize_layout;
42
43 struct mnt_pcp {
44 int mnt_count;
45 int mnt_writers;
46 };
47
48 struct mountpoint {
49 struct hlist_node m_hash;
50 struct dentry *m_dentry;
51 struct hlist_head m_list;
52 int m_count;
53 };
54
55 struct mount {
56 struct hlist_node mnt_hash;
57 struct mount *mnt_parent;
58 struct dentry *mnt_mountpoint;
59 struct vfsmount mnt;
60 union {
61 struct rb_node mnt_node; /* node in the ns->mounts rbtree */
62 struct rcu_head mnt_rcu;
63 struct llist_node mnt_llist;
64 };
65 #ifdef CONFIG_SMP
66 struct mnt_pcp __percpu *mnt_pcp;
67 #else
68 int mnt_count;
69 int mnt_writers;
70 #endif
71 struct list_head mnt_mounts; /* list of children, anchored here */
72 struct list_head mnt_child; /* and going through their mnt_child */
73 struct list_head mnt_instance; /* mount instance on sb->s_mounts */
74 const char *mnt_devname; /* Name of device e.g. /dev/dsk/hda1 */
75 struct list_head mnt_list;
76 struct list_head mnt_expire; /* link in fs-specific expiry list */
77 struct list_head mnt_share; /* circular list of shared mounts */
78 struct list_head mnt_slave_list;/* list of slave mounts */
79 struct list_head mnt_slave; /* slave list entry */
80 struct mount *mnt_master; /* slave is on master->mnt_slave_list */
81 struct mnt_namespace *mnt_ns; /* containing namespace */
82 struct mountpoint *mnt_mp; /* where is it mounted */
83 union {
84 struct hlist_node mnt_mp_list; /* list mounts with the same mountpoint */
85 struct hlist_node mnt_umount;
86 };
87 struct list_head mnt_umounting; /* list entry for umount propagation */
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_id; /* mount identifier, reused */
95 u64 mnt_id_unique; /* mount ID unique until reboot */
96 int mnt_group_id; /* peer group identifier */
97 int mnt_expiry_mark; /* true if marked for expiry */
98 struct hlist_head mnt_pins;
99 struct hlist_head mnt_stuck_children;
100 } __randomize_layout;
101
102 #define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */
103
real_mount(struct vfsmount * mnt)104 static inline struct mount *real_mount(struct vfsmount *mnt)
105 {
106 return container_of(mnt, struct mount, mnt);
107 }
108
mnt_has_parent(struct mount * mnt)109 static inline int mnt_has_parent(struct mount *mnt)
110 {
111 return mnt != mnt->mnt_parent;
112 }
113
is_mounted(struct vfsmount * mnt)114 static inline int is_mounted(struct vfsmount *mnt)
115 {
116 /* neither detached nor internal? */
117 return !IS_ERR_OR_NULL(real_mount(mnt)->mnt_ns);
118 }
119
120 extern struct mount *__lookup_mnt(struct vfsmount *, struct dentry *);
121
122 extern int __legitimize_mnt(struct vfsmount *, unsigned);
123
__path_is_mountpoint(const struct path * path)124 static inline bool __path_is_mountpoint(const struct path *path)
125 {
126 struct mount *m = __lookup_mnt(path->mnt, path->dentry);
127 return m && likely(!(m->mnt.mnt_flags & MNT_SYNC_UMOUNT));
128 }
129
130 extern void __detach_mounts(struct dentry *dentry);
131
detach_mounts(struct dentry * dentry)132 static inline void detach_mounts(struct dentry *dentry)
133 {
134 if (!d_mountpoint(dentry))
135 return;
136 __detach_mounts(dentry);
137 }
138
get_mnt_ns(struct mnt_namespace * ns)139 static inline void get_mnt_ns(struct mnt_namespace *ns)
140 {
141 refcount_inc(&ns->ns.count);
142 }
143
144 extern seqlock_t mount_lock;
145
146 struct proc_mounts {
147 struct mnt_namespace *ns;
148 struct path root;
149 int (*show)(struct seq_file *, struct vfsmount *);
150 };
151
152 extern const struct seq_operations mounts_op;
153
154 extern bool __is_local_mountpoint(struct dentry *dentry);
is_local_mountpoint(struct dentry * dentry)155 static inline bool is_local_mountpoint(struct dentry *dentry)
156 {
157 if (!d_mountpoint(dentry))
158 return false;
159
160 return __is_local_mountpoint(dentry);
161 }
162
is_anon_ns(struct mnt_namespace * ns)163 static inline bool is_anon_ns(struct mnt_namespace *ns)
164 {
165 return ns->seq == 0;
166 }
167
mnt_ns_attached(const struct mount * mnt)168 static inline bool mnt_ns_attached(const struct mount *mnt)
169 {
170 return !RB_EMPTY_NODE(&mnt->mnt_node);
171 }
172
mnt_ns_empty(const struct mnt_namespace * ns)173 static inline bool mnt_ns_empty(const struct mnt_namespace *ns)
174 {
175 return RB_EMPTY_ROOT(&ns->mounts);
176 }
177
move_from_ns(struct mount * mnt,struct list_head * dt_list)178 static inline void move_from_ns(struct mount *mnt, struct list_head *dt_list)
179 {
180 struct mnt_namespace *ns = mnt->mnt_ns;
181 WARN_ON(!mnt_ns_attached(mnt));
182 if (ns->mnt_last_node == &mnt->mnt_node)
183 ns->mnt_last_node = rb_prev(&mnt->mnt_node);
184 if (ns->mnt_first_node == &mnt->mnt_node)
185 ns->mnt_first_node = rb_next(&mnt->mnt_node);
186 rb_erase(&mnt->mnt_node, &ns->mounts);
187 RB_CLEAR_NODE(&mnt->mnt_node);
188 list_add_tail(&mnt->mnt_list, dt_list);
189 }
190
191 bool has_locked_children(struct mount *mnt, struct dentry *dentry);
192 struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mnt_ns,
193 bool previous);
194
to_mnt_ns(struct ns_common * ns)195 static inline struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
196 {
197 return container_of(ns, struct mnt_namespace, ns);
198 }
199
200 #ifdef CONFIG_FSNOTIFY
mnt_notify_add(struct mount * m)201 static inline void mnt_notify_add(struct mount *m)
202 {
203 /* Optimize the case where there are no watches */
204 if ((m->mnt_ns && m->mnt_ns->n_fsnotify_marks) ||
205 (m->prev_ns && m->prev_ns->n_fsnotify_marks))
206 list_add_tail(&m->to_notify, ¬ify_list);
207 else
208 m->prev_ns = m->mnt_ns;
209 }
210 #else
mnt_notify_add(struct mount * m)211 static inline void mnt_notify_add(struct mount *m)
212 {
213 }
214 #endif
215
216 struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry);
217