xref: /linux/fs/mount.h (revision b615879dbfea6cf1236acbc3f2fb25ae84e07071)
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 	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 } __randomize_layout;
31 
32 struct mnt_pcp {
33 	int mnt_count;
34 	int mnt_writers;
35 };
36 
37 struct mountpoint {
38 	struct hlist_node m_hash;
39 	struct dentry *m_dentry;
40 	struct hlist_head m_list;
41 };
42 
43 struct mount {
44 	struct hlist_node mnt_hash;
45 	struct mount *mnt_parent;
46 	struct dentry *mnt_mountpoint;
47 	struct vfsmount mnt;
48 	union {
49 		struct rb_node mnt_node; /* node in the ns->mounts rbtree */
50 		struct rcu_head mnt_rcu;
51 		struct llist_node mnt_llist;
52 	};
53 #ifdef CONFIG_SMP
54 	struct mnt_pcp __percpu *mnt_pcp;
55 #else
56 	int mnt_count;
57 	int mnt_writers;
58 #endif
59 	struct list_head mnt_mounts;	/* list of children, anchored here */
60 	struct list_head mnt_child;	/* and going through their mnt_child */
61 	struct list_head mnt_instance;	/* mount instance on sb->s_mounts */
62 	const char *mnt_devname;	/* Name of device e.g. /dev/dsk/hda1 */
63 	struct list_head mnt_list;
64 	struct list_head mnt_expire;	/* link in fs-specific expiry list */
65 	struct list_head mnt_share;	/* circular list of shared mounts */
66 	struct hlist_head mnt_slave_list;/* list of slave mounts */
67 	struct hlist_node mnt_slave;	/* slave list entry */
68 	struct mount *mnt_master;	/* slave is on master->mnt_slave_list */
69 	struct mnt_namespace *mnt_ns;	/* containing namespace */
70 	struct mountpoint *mnt_mp;	/* where is it mounted */
71 	union {
72 		struct hlist_node mnt_mp_list;	/* list mounts with the same mountpoint */
73 		struct hlist_node mnt_umount;
74 	};
75 #ifdef CONFIG_FSNOTIFY
76 	struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks;
77 	__u32 mnt_fsnotify_mask;
78 	struct list_head to_notify;	/* need to queue notification */
79 	struct mnt_namespace *prev_ns;	/* previous namespace (NULL if none) */
80 #endif
81 	int mnt_t_flags;		/* namespace_sem-protected flags */
82 	int mnt_id;			/* mount identifier, reused */
83 	u64 mnt_id_unique;		/* mount ID unique until reboot */
84 	int mnt_group_id;		/* peer group identifier */
85 	int mnt_expiry_mark;		/* true if marked for expiry */
86 	struct hlist_head mnt_pins;
87 	struct hlist_head mnt_stuck_children;
88 	struct mount *overmount;	/* mounted on ->mnt_root */
89 } __randomize_layout;
90 
91 enum {
92 	T_SHARED		= 1, /* mount is shared */
93 	T_UNBINDABLE		= 2, /* mount is unbindable */
94 	T_MARKED		= 4, /* internal mark for propagate_... */
95 	T_UMOUNT_CANDIDATE	= 8, /* for propagate_umount */
96 
97 	/*
98 	 * T_SHARED_MASK is the set of flags that should be cleared when a
99 	 * mount becomes shared.  Currently, this is only the flag that says a
100 	 * mount cannot be bind mounted, since this is how we create a mount
101 	 * that shares events with another mount.  If you add a new T_*
102 	 * flag, consider how it interacts with shared mounts.
103 	 */
104 	T_SHARED_MASK	= T_UNBINDABLE,
105 };
106 
107 #define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */
108 
109 static inline struct mount *real_mount(struct vfsmount *mnt)
110 {
111 	return container_of(mnt, struct mount, mnt);
112 }
113 
114 static inline int mnt_has_parent(const struct mount *mnt)
115 {
116 	return mnt != mnt->mnt_parent;
117 }
118 
119 static inline int is_mounted(struct vfsmount *mnt)
120 {
121 	/* neither detached nor internal? */
122 	return !IS_ERR_OR_NULL(real_mount(mnt)->mnt_ns);
123 }
124 
125 extern struct mount *__lookup_mnt(struct vfsmount *, struct dentry *);
126 
127 extern int __legitimize_mnt(struct vfsmount *, unsigned);
128 
129 static inline bool __path_is_mountpoint(const struct path *path)
130 {
131 	struct mount *m = __lookup_mnt(path->mnt, path->dentry);
132 	return m && likely(!(m->mnt.mnt_flags & MNT_SYNC_UMOUNT));
133 }
134 
135 extern void __detach_mounts(struct dentry *dentry);
136 
137 static inline void detach_mounts(struct dentry *dentry)
138 {
139 	if (!d_mountpoint(dentry))
140 		return;
141 	__detach_mounts(dentry);
142 }
143 
144 static inline void get_mnt_ns(struct mnt_namespace *ns)
145 {
146 	ns_ref_inc(ns);
147 }
148 
149 extern seqlock_t mount_lock;
150 
151 struct proc_mounts {
152 	struct mnt_namespace *ns;
153 	struct path root;
154 	int (*show)(struct seq_file *, struct vfsmount *);
155 };
156 
157 extern const struct seq_operations mounts_op;
158 
159 extern bool __is_local_mountpoint(const struct dentry *dentry);
160 static inline bool is_local_mountpoint(const struct dentry *dentry)
161 {
162 	if (!d_mountpoint(dentry))
163 		return false;
164 
165 	return __is_local_mountpoint(dentry);
166 }
167 
168 static inline bool is_anon_ns(struct mnt_namespace *ns)
169 {
170 	return ns->ns.ns_id == 0;
171 }
172 
173 static inline bool anon_ns_root(const struct mount *m)
174 {
175 	struct mnt_namespace *ns = READ_ONCE(m->mnt_ns);
176 
177 	return !IS_ERR_OR_NULL(ns) && is_anon_ns(ns) && m == ns->root;
178 }
179 
180 static inline bool mnt_ns_attached(const struct mount *mnt)
181 {
182 	return !RB_EMPTY_NODE(&mnt->mnt_node);
183 }
184 
185 static inline bool mnt_ns_empty(const struct mnt_namespace *ns)
186 {
187 	return RB_EMPTY_ROOT(&ns->mounts);
188 }
189 
190 static inline void move_from_ns(struct mount *mnt)
191 {
192 	struct mnt_namespace *ns = mnt->mnt_ns;
193 	WARN_ON(!mnt_ns_attached(mnt));
194 	if (ns->mnt_last_node == &mnt->mnt_node)
195 		ns->mnt_last_node = rb_prev(&mnt->mnt_node);
196 	if (ns->mnt_first_node == &mnt->mnt_node)
197 		ns->mnt_first_node = rb_next(&mnt->mnt_node);
198 	rb_erase(&mnt->mnt_node, &ns->mounts);
199 	RB_CLEAR_NODE(&mnt->mnt_node);
200 }
201 
202 bool has_locked_children(struct mount *mnt, struct dentry *dentry);
203 struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mnt_ns,
204 					    bool previous);
205 
206 static inline struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
207 {
208 	return container_of(ns, struct mnt_namespace, ns);
209 }
210 
211 #ifdef CONFIG_FSNOTIFY
212 static inline void mnt_notify_add(struct mount *m)
213 {
214 	/* Optimize the case where there are no watches */
215 	if ((m->mnt_ns && m->mnt_ns->n_fsnotify_marks) ||
216 	    (m->prev_ns && m->prev_ns->n_fsnotify_marks))
217 		list_add_tail(&m->to_notify, &notify_list);
218 	else
219 		m->prev_ns = m->mnt_ns;
220 }
221 #else
222 static inline void mnt_notify_add(struct mount *m)
223 {
224 }
225 #endif
226 
227 struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry);
228