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