xref: /linux/fs/mount.h (revision 85c8700cb6e67cac228bfb313fa8e33d1750410f)
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 event;
26 #ifdef CONFIG_FSNOTIFY
27 	__u32			n_fsnotify_mask;
28 	struct fsnotify_mark_connector __rcu *n_fsnotify_marks;
29 #endif
30 	unsigned int		nr_mounts; /* # of mounts in the namespace */
31 	unsigned int		pending_mounts;
32 	struct rb_node		mnt_ns_tree_node; /* node in the mnt_ns_tree */
33 	struct list_head	mnt_ns_list; /* entry in the sequential list of mounts namespace */
34 	refcount_t		passive; /* number references not pinning @mounts */
35 } __randomize_layout;
36 
37 struct mnt_pcp {
38 	int mnt_count;
39 	int mnt_writers;
40 };
41 
42 struct mountpoint {
43 	struct hlist_node m_hash;
44 	struct dentry *m_dentry;
45 	struct hlist_head m_list;
46 	int m_count;
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 list_head mnt_slave_list;/* list of slave mounts */
73 	struct list_head 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 	struct list_head mnt_umounting; /* list entry for umount propagation */
82 #ifdef CONFIG_FSNOTIFY
83 	struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks;
84 	__u32 mnt_fsnotify_mask;
85 	struct list_head to_notify;	/* need to queue notification */
86 	struct mnt_namespace *prev_ns;	/* previous namespace (NULL if none) */
87 #endif
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 } __randomize_layout;
95 
96 #define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */
97 
98 static inline struct mount *real_mount(struct vfsmount *mnt)
99 {
100 	return container_of(mnt, struct mount, mnt);
101 }
102 
103 static inline int mnt_has_parent(struct mount *mnt)
104 {
105 	return mnt != mnt->mnt_parent;
106 }
107 
108 static inline int is_mounted(struct vfsmount *mnt)
109 {
110 	/* neither detached nor internal? */
111 	return !IS_ERR_OR_NULL(real_mount(mnt)->mnt_ns);
112 }
113 
114 extern struct mount *__lookup_mnt(struct vfsmount *, struct dentry *);
115 
116 extern int __legitimize_mnt(struct vfsmount *, unsigned);
117 
118 static inline bool __path_is_mountpoint(const struct path *path)
119 {
120 	struct mount *m = __lookup_mnt(path->mnt, path->dentry);
121 	return m && likely(!(m->mnt.mnt_flags & MNT_SYNC_UMOUNT));
122 }
123 
124 extern void __detach_mounts(struct dentry *dentry);
125 
126 static inline void detach_mounts(struct dentry *dentry)
127 {
128 	if (!d_mountpoint(dentry))
129 		return;
130 	__detach_mounts(dentry);
131 }
132 
133 static inline void get_mnt_ns(struct mnt_namespace *ns)
134 {
135 	refcount_inc(&ns->ns.count);
136 }
137 
138 extern seqlock_t mount_lock;
139 
140 struct proc_mounts {
141 	struct mnt_namespace *ns;
142 	struct path root;
143 	int (*show)(struct seq_file *, struct vfsmount *);
144 };
145 
146 extern const struct seq_operations mounts_op;
147 
148 extern bool __is_local_mountpoint(struct dentry *dentry);
149 static inline bool is_local_mountpoint(struct dentry *dentry)
150 {
151 	if (!d_mountpoint(dentry))
152 		return false;
153 
154 	return __is_local_mountpoint(dentry);
155 }
156 
157 static inline bool is_anon_ns(struct mnt_namespace *ns)
158 {
159 	return ns->seq == 0;
160 }
161 
162 static inline bool mnt_ns_attached(const struct mount *mnt)
163 {
164 	return !RB_EMPTY_NODE(&mnt->mnt_node);
165 }
166 
167 static inline void move_from_ns(struct mount *mnt, struct list_head *dt_list)
168 {
169 	struct mnt_namespace *ns = mnt->mnt_ns;
170 	WARN_ON(!mnt_ns_attached(mnt));
171 	if (ns->mnt_last_node == &mnt->mnt_node)
172 		ns->mnt_last_node = rb_prev(&mnt->mnt_node);
173 	if (ns->mnt_first_node == &mnt->mnt_node)
174 		ns->mnt_first_node = rb_next(&mnt->mnt_node);
175 	rb_erase(&mnt->mnt_node, &ns->mounts);
176 	RB_CLEAR_NODE(&mnt->mnt_node);
177 	list_add_tail(&mnt->mnt_list, dt_list);
178 }
179 
180 bool has_locked_children(struct mount *mnt, struct dentry *dentry);
181 struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mnt_ns,
182 					    bool previous);
183 
184 static inline struct mnt_namespace *to_mnt_ns(struct ns_common *ns)
185 {
186 	return container_of(ns, struct mnt_namespace, ns);
187 }
188 
189 #ifdef CONFIG_FSNOTIFY
190 static inline void mnt_notify_add(struct mount *m)
191 {
192 	/* Optimize the case where there are no watches */
193 	if ((m->mnt_ns && m->mnt_ns->n_fsnotify_marks) ||
194 	    (m->prev_ns && m->prev_ns->n_fsnotify_marks))
195 		list_add_tail(&m->to_notify, &notify_list);
196 	else
197 		m->prev_ns = m->mnt_ns;
198 }
199 #else
200 static inline void mnt_notify_add(struct mount *m)
201 {
202 }
203 #endif
204 
205 struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry);
206