Linux 6.17-rc6
[linux-2.6-block.git] / fs / mount.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
b2dba1af 2#include <linux/mount.h>
0226f492
AV
3#include <linux/seq_file.h>
4#include <linux/poll.h>
435d5f4b 5#include <linux/ns_common.h>
87b95ce0 6#include <linux/fs_pin.h>
0226f492 7
bf630c40
MS
8extern struct list_head notify_list;
9
0226f492 10struct mnt_namespace {
435d5f4b 11 struct ns_common ns;
be08d6d2 12 struct mount * root;
2ce23285
CB
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 };
771b1371 18 struct user_namespace *user_ns;
537f7ccb 19 struct ucounts *ucounts;
8823c079 20 u64 seq; /* Sequence number to prevent loops */
5dcbd85d
CB
21 union {
22 wait_queue_head_t poll;
23 struct rcu_head mnt_ns_rcu;
24 };
3b0cdba4 25 u64 seq_origin; /* Sequence number of origin mount namespace */
c7999c36 26 u64 event;
b944249b
MS
27#ifdef CONFIG_FSNOTIFY
28 __u32 n_fsnotify_mask;
29 struct fsnotify_mark_connector __rcu *n_fsnotify_marks;
30#endif
2eea9ce4 31 unsigned int nr_mounts; /* # of mounts in the namespace */
d2921684 32 unsigned int pending_mounts;
1901c924 33 struct rb_node mnt_ns_tree_node; /* node in the mnt_ns_tree */
4368898b 34 struct list_head mnt_ns_list; /* entry in the sequential list of mounts namespace */
1901c924 35 refcount_t passive; /* number references not pinning @mounts */
3859a271 36} __randomize_layout;
b2dba1af 37
68e8a9fe
AV
38struct mnt_pcp {
39 int mnt_count;
40 int mnt_writers;
41};
42
84d17192 43struct mountpoint {
0818bf27 44 struct hlist_node m_hash;
84d17192 45 struct dentry *m_dentry;
0a5eb7c8 46 struct hlist_head m_list;
84d17192
AV
47};
48
7d6fec45 49struct mount {
38129a13 50 struct hlist_node mnt_hash;
0714a533 51 struct mount *mnt_parent;
a73324da 52 struct dentry *mnt_mountpoint;
7d6fec45 53 struct vfsmount mnt;
9ea459e1 54 union {
344bac8f 55 struct rb_node mnt_node; /* node in the ns->mounts rbtree */
9ea459e1
AV
56 struct rcu_head mnt_rcu;
57 struct llist_node mnt_llist;
58 };
68e8a9fe
AV
59#ifdef CONFIG_SMP
60 struct mnt_pcp __percpu *mnt_pcp;
68e8a9fe
AV
61#else
62 int mnt_count;
63 int mnt_writers;
64#endif
6b41d536
AV
65 struct list_head mnt_mounts; /* list of children, anchored here */
66 struct list_head mnt_child; /* and going through their mnt_child */
39f7c4db 67 struct list_head mnt_instance; /* mount instance on sb->s_mounts */
52ba1621 68 const char *mnt_devname; /* Name of device e.g. /dev/dsk/hda1 */
344bac8f 69 struct list_head mnt_list;
6776db3d
AV
70 struct list_head mnt_expire; /* link in fs-specific expiry list */
71 struct list_head mnt_share; /* circular list of shared mounts */
8c5a853f
AV
72 struct hlist_head mnt_slave_list;/* list of slave mounts */
73 struct hlist_node mnt_slave; /* slave list entry */
32301920 74 struct mount *mnt_master; /* slave is on master->mnt_slave_list */
143c8c91 75 struct mnt_namespace *mnt_ns; /* containing namespace */
84d17192 76 struct mountpoint *mnt_mp; /* where is it mounted */
56cbb429
AV
77 union {
78 struct hlist_node mnt_mp_list; /* list mounts with the same mountpoint */
79 struct hlist_node mnt_umount;
80 };
c63181e6 81#ifdef CONFIG_FSNOTIFY
08991e83 82 struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks;
c63181e6 83 __u32 mnt_fsnotify_mask;
bf630c40
MS
84 struct list_head to_notify; /* need to queue notification */
85 struct mnt_namespace *prev_ns; /* previous namespace (NULL if none) */
c63181e6 86#endif
406fea79 87 int mnt_t_flags; /* namespace_sem-protected flags */
98d2b430
MS
88 int mnt_id; /* mount identifier, reused */
89 u64 mnt_id_unique; /* mount ID unique until reboot */
15169fe7 90 int mnt_group_id; /* peer group identifier */
863d684f 91 int mnt_expiry_mark; /* true if marked for expiry */
215752fc 92 struct hlist_head mnt_pins;
56cbb429 93 struct hlist_head mnt_stuck_children;
ffdc52fb 94 struct mount *overmount; /* mounted on ->mnt_root */
3859a271 95} __randomize_layout;
7d6fec45 96
406fea79
AV
97enum {
98 T_SHARED = 1, /* mount is shared */
99 T_UNBINDABLE = 2, /* mount is unbindable */
100 T_MARKED = 4, /* internal mark for propagate_... */
101 T_UMOUNT_CANDIDATE = 8, /* for propagate_umount */
102
103 /*
104 * T_SHARED_MASK is the set of flags that should be cleared when a
105 * mount becomes shared. Currently, this is only the flag that says a
106 * mount cannot be bind mounted, since this is how we create a mount
107 * that shares events with another mount. If you add a new T_*
108 * flag, consider how it interacts with shared mounts.
109 */
110 T_SHARED_MASK = T_UNBINDABLE,
111};
112
f7a99c5b
AV
113#define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */
114
7d6fec45
AV
115static inline struct mount *real_mount(struct vfsmount *mnt)
116{
117 return container_of(mnt, struct mount, mnt);
118}
119
0e84653e 120static inline int mnt_has_parent(const struct mount *mnt)
b2dba1af 121{
0714a533 122 return mnt != mnt->mnt_parent;
b2dba1af 123}
c7105365 124
f7a99c5b
AV
125static inline int is_mounted(struct vfsmount *mnt)
126{
127 /* neither detached nor internal? */
260a459d 128 return !IS_ERR_OR_NULL(real_mount(mnt)->mnt_ns);
f7a99c5b
AV
129}
130
474279dc 131extern struct mount *__lookup_mnt(struct vfsmount *, struct dentry *);
0226f492 132
294d71ff 133extern int __legitimize_mnt(struct vfsmount *, unsigned);
48a066e7 134
c6609c0a
IK
135static inline bool __path_is_mountpoint(const struct path *path)
136{
137 struct mount *m = __lookup_mnt(path->mnt, path->dentry);
138 return m && likely(!(m->mnt.mnt_flags & MNT_SYNC_UMOUNT));
139}
140
80b5dce8
EB
141extern void __detach_mounts(struct dentry *dentry);
142
143static inline void detach_mounts(struct dentry *dentry)
144{
145 if (!d_mountpoint(dentry))
146 return;
147 __detach_mounts(dentry);
148}
149
0226f492
AV
150static inline void get_mnt_ns(struct mnt_namespace *ns)
151{
1a7b8969 152 refcount_inc(&ns->ns.count);
0226f492
AV
153}
154
48a066e7 155extern seqlock_t mount_lock;
719ea2fb 156
0226f492 157struct proc_mounts {
0226f492
AV
158 struct mnt_namespace *ns;
159 struct path root;
160 int (*show)(struct seq_file *, struct vfsmount *);
161};
162
163extern const struct seq_operations mounts_op;
7af1364f 164
e031251c
AV
165extern bool __is_local_mountpoint(const struct dentry *dentry);
166static inline bool is_local_mountpoint(const struct dentry *dentry)
7af1364f
EB
167{
168 if (!d_mountpoint(dentry))
169 return false;
170
171 return __is_local_mountpoint(dentry);
172}
74e83122
AV
173
174static inline bool is_anon_ns(struct mnt_namespace *ns)
175{
176 return ns->seq == 0;
177}
9f6c61f9 178
05da054d
AV
179static inline bool anon_ns_root(const struct mount *m)
180{
181 struct mnt_namespace *ns = READ_ONCE(m->mnt_ns);
182
183 return !IS_ERR_OR_NULL(ns) && is_anon_ns(ns) && m == ns->root;
184}
185
344bac8f
CB
186static inline bool mnt_ns_attached(const struct mount *mnt)
187{
188 return !RB_EMPTY_NODE(&mnt->mnt_node);
189}
190
2f576220
CB
191static inline bool mnt_ns_empty(const struct mnt_namespace *ns)
192{
193 return RB_EMPTY_ROOT(&ns->mounts);
194}
195
66320685 196static inline void move_from_ns(struct mount *mnt)
2eea9ce4 197{
2ce23285 198 struct mnt_namespace *ns = mnt->mnt_ns;
344bac8f 199 WARN_ON(!mnt_ns_attached(mnt));
2ce23285
CB
200 if (ns->mnt_last_node == &mnt->mnt_node)
201 ns->mnt_last_node = rb_prev(&mnt->mnt_node);
202 if (ns->mnt_first_node == &mnt->mnt_node)
203 ns->mnt_first_node = rb_next(&mnt->mnt_node);
204 rb_erase(&mnt->mnt_node, &ns->mounts);
344bac8f 205 RB_CLEAR_NODE(&mnt->mnt_node);
2eea9ce4
MS
206}
207
620c266f 208bool has_locked_children(struct mount *mnt, struct dentry *dentry);
4368898b
CB
209struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mnt_ns,
210 bool previous);
211
a1d220d9
CB
212static inline struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
213{
214 return container_of(ns, struct mnt_namespace, ns);
215}
0f46d81f 216
bf630c40
MS
217#ifdef CONFIG_FSNOTIFY
218static inline void mnt_notify_add(struct mount *m)
219{
220 /* Optimize the case where there are no watches */
221 if ((m->mnt_ns && m->mnt_ns->n_fsnotify_marks) ||
222 (m->prev_ns && m->prev_ns->n_fsnotify_marks))
223 list_add_tail(&m->to_notify, &notify_list);
224 else
225 m->prev_ns = m->mnt_ns;
226}
227#else
228static inline void mnt_notify_add(struct mount *m)
229{
230}
231#endif
232
0f46d81f 233struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry);