xref: /linux/fs/mount.h (revision 794cbac9c053155754d04231b9365f91ea4ce7d2)
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 struct 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 	u64			seq;	/* Sequence number to prevent loops */
21 	union {
22 		wait_queue_head_t	poll;
23 		struct rcu_head		mnt_ns_rcu;
24 	};
25 	u64			seq_origin; /* Sequence number of origin mount namespace */
26 	u64 event;
27 #ifdef CONFIG_FSNOTIFY
28 	__u32			n_fsnotify_mask;
29 	struct fsnotify_mark_connector __rcu *n_fsnotify_marks;
30 #endif
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 */
36 } __randomize_layout;
37 
38 struct mnt_pcp {
39 	int mnt_count;
40 	int mnt_writers;
41 };
42 
43 struct mountpoint {
44 	struct hlist_node m_hash;
45 	struct dentry *m_dentry;
46 	struct hlist_head m_list;
47 };
48 
49 struct mount {
50 	struct hlist_node mnt_hash;
51 	struct mount *mnt_parent;
52 	struct dentry *mnt_mountpoint;
53 	struct vfsmount mnt;
54 	union {
55 		struct rb_node mnt_node; /* node in the ns->mounts rbtree */
56 		struct rcu_head mnt_rcu;
57 		struct llist_node mnt_llist;
58 	};
59 #ifdef CONFIG_SMP
60 	struct mnt_pcp __percpu *mnt_pcp;
61 #else
62 	int mnt_count;
63 	int mnt_writers;
64 #endif
65 	struct list_head mnt_mounts;	/* list of children, anchored here */
66 	struct list_head mnt_child;	/* and going through their mnt_child */
67 	struct list_head mnt_instance;	/* mount instance on sb->s_mounts */
68 	const char *mnt_devname;	/* Name of device e.g. /dev/dsk/hda1 */
69 	struct list_head mnt_list;
70 	struct list_head mnt_expire;	/* link in fs-specific expiry list */
71 	struct list_head mnt_share;	/* circular list of shared mounts */
72 	struct hlist_head mnt_slave_list;/* list of slave mounts */
73 	struct hlist_node mnt_slave;	/* slave list entry */
74 	struct mount *mnt_master;	/* slave is on master->mnt_slave_list */
75 	struct mnt_namespace *mnt_ns;	/* containing namespace */
76 	struct mountpoint *mnt_mp;	/* where is it mounted */
77 	union {
78 		struct hlist_node mnt_mp_list;	/* list mounts with the same mountpoint */
79 		struct hlist_node mnt_umount;
80 	};
81 #ifdef CONFIG_FSNOTIFY
82 	struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks;
83 	__u32 mnt_fsnotify_mask;
84 	struct list_head to_notify;	/* need to queue notification */
85 	struct mnt_namespace *prev_ns;	/* previous namespace (NULL if none) */
86 #endif
87 	int mnt_t_flags;		/* namespace_sem-protected flags */
88 	int mnt_id;			/* mount identifier, reused */
89 	u64 mnt_id_unique;		/* mount ID unique until reboot */
90 	int mnt_group_id;		/* peer group identifier */
91 	int mnt_expiry_mark;		/* true if marked for expiry */
92 	struct hlist_head mnt_pins;
93 	struct hlist_head mnt_stuck_children;
94 	struct mount *overmount;	/* mounted on ->mnt_root */
95 } __randomize_layout;
96 
97 enum {
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 
113 #define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */
114 
real_mount(struct vfsmount * mnt)115 static inline struct mount *real_mount(struct vfsmount *mnt)
116 {
117 	return container_of(mnt, struct mount, mnt);
118 }
119 
mnt_has_parent(const struct mount * mnt)120 static inline int mnt_has_parent(const struct mount *mnt)
121 {
122 	return mnt != mnt->mnt_parent;
123 }
124 
is_mounted(struct vfsmount * mnt)125 static inline int is_mounted(struct vfsmount *mnt)
126 {
127 	/* neither detached nor internal? */
128 	return !IS_ERR_OR_NULL(real_mount(mnt)->mnt_ns);
129 }
130 
131 extern struct mount *__lookup_mnt(struct vfsmount *, struct dentry *);
132 
133 extern int __legitimize_mnt(struct vfsmount *, unsigned);
134 
__path_is_mountpoint(const struct path * path)135 static 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 
141 extern void __detach_mounts(struct dentry *dentry);
142 
detach_mounts(struct dentry * dentry)143 static inline void detach_mounts(struct dentry *dentry)
144 {
145 	if (!d_mountpoint(dentry))
146 		return;
147 	__detach_mounts(dentry);
148 }
149 
get_mnt_ns(struct mnt_namespace * ns)150 static inline void get_mnt_ns(struct mnt_namespace *ns)
151 {
152 	refcount_inc(&ns->ns.count);
153 }
154 
155 extern seqlock_t mount_lock;
156 
157 struct proc_mounts {
158 	struct mnt_namespace *ns;
159 	struct path root;
160 	int (*show)(struct seq_file *, struct vfsmount *);
161 };
162 
163 extern const struct seq_operations mounts_op;
164 
165 extern bool __is_local_mountpoint(const struct dentry *dentry);
is_local_mountpoint(const struct dentry * dentry)166 static inline bool is_local_mountpoint(const struct dentry *dentry)
167 {
168 	if (!d_mountpoint(dentry))
169 		return false;
170 
171 	return __is_local_mountpoint(dentry);
172 }
173 
is_anon_ns(struct mnt_namespace * ns)174 static inline bool is_anon_ns(struct mnt_namespace *ns)
175 {
176 	return ns->seq == 0;
177 }
178 
anon_ns_root(const struct mount * m)179 static 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 
mnt_ns_attached(const struct mount * mnt)186 static inline bool mnt_ns_attached(const struct mount *mnt)
187 {
188 	return !RB_EMPTY_NODE(&mnt->mnt_node);
189 }
190 
mnt_ns_empty(const struct mnt_namespace * ns)191 static inline bool mnt_ns_empty(const struct mnt_namespace *ns)
192 {
193 	return RB_EMPTY_ROOT(&ns->mounts);
194 }
195 
move_from_ns(struct mount * mnt)196 static inline void move_from_ns(struct mount *mnt)
197 {
198 	struct mnt_namespace *ns = mnt->mnt_ns;
199 	WARN_ON(!mnt_ns_attached(mnt));
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);
205 	RB_CLEAR_NODE(&mnt->mnt_node);
206 }
207 
208 bool has_locked_children(struct mount *mnt, struct dentry *dentry);
209 struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mnt_ns,
210 					    bool previous);
211 
to_mnt_ns(struct ns_common * ns)212 static inline struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
213 {
214 	return container_of(ns, struct mnt_namespace, ns);
215 }
216 
217 #ifdef CONFIG_FSNOTIFY
mnt_notify_add(struct mount * m)218 static 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
mnt_notify_add(struct mount * m)228 static inline void mnt_notify_add(struct mount *m)
229 {
230 }
231 #endif
232 
233 struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry);
234