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