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