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 file_system_type nullfs_fs_type; 9 extern struct list_head notify_list; 10 11 struct mnt_namespace { 12 struct ns_common ns; 13 struct mount * root; 14 struct { 15 struct rb_root mounts; /* Protected by namespace_sem */ 16 struct rb_node *mnt_last_node; /* last (rightmost) mount in the rbtree */ 17 struct rb_node *mnt_first_node; /* first (leftmost) mount in the rbtree */ 18 }; 19 struct user_namespace *user_ns; 20 struct ucounts *ucounts; 21 wait_queue_head_t poll; 22 u64 seq_origin; /* Sequence number of origin mount namespace */ 23 u64 event; 24 #ifdef CONFIG_FSNOTIFY 25 __u32 n_fsnotify_mask; 26 struct fsnotify_mark_connector __rcu *n_fsnotify_marks; 27 #endif 28 struct hlist_head mnt_visible_mounts; /* SB_I_USERNS_VISIBLE mounts */ 29 unsigned int nr_mounts; /* # of mounts in the namespace */ 30 unsigned int pending_mounts; 31 refcount_t passive; /* number references not pinning @mounts */ 32 bool is_anon; 33 } __randomize_layout; 34 35 struct mnt_pcp { 36 int mnt_count; 37 int mnt_writers; 38 }; 39 40 struct mountpoint { 41 struct hlist_node m_hash; 42 struct dentry *m_dentry; 43 struct hlist_head m_list; 44 }; 45 46 struct mount { 47 struct hlist_node mnt_hash; 48 struct mount *mnt_parent; 49 struct dentry *mnt_mountpoint; 50 struct vfsmount mnt; 51 union { 52 struct rb_node mnt_node; /* node in the ns->mounts rbtree */ 53 struct rcu_head mnt_rcu; 54 struct llist_node mnt_llist; 55 }; 56 #ifdef CONFIG_SMP 57 struct mnt_pcp __percpu *mnt_pcp; 58 #else 59 int mnt_count; 60 int mnt_writers; 61 #endif 62 struct list_head mnt_mounts; /* list of children, anchored here */ 63 struct list_head mnt_child; /* and going through their mnt_child */ 64 struct mount *mnt_next_for_sb; /* the next two fields are hlist_node, */ 65 struct mount * __aligned(1) *mnt_pprev_for_sb; 66 /* except that LSB of pprev is stolen */ 67 #define WRITE_HOLD 1 /* ... for use by mnt_hold_writers() */ 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 hlist_head mnt_slave_list;/* list of slave mounts */ 73 struct hlist_node mnt_slave; /* slave list entry */ 74 struct mount *mnt_master; /* slave is on master->mnt_slave_list */ 75 /* 76 * Containing namespace (active or deactivating, non-refcounted). 77 * Normally protected by namespace_sem. 78 * Can also be accessed locklessly under RCU. RCU readers can't rely on 79 * the namespace still being active, but implicitly hold a passive 80 * reference (because an RCU delay happens between a namespace being 81 * deactivated and the corresponding passive refcount drop). 82 */ 83 struct mnt_namespace *mnt_ns; 84 struct mountpoint *mnt_mp; /* where is it mounted */ 85 union { 86 struct hlist_node mnt_mp_list; /* list mounts with the same mountpoint */ 87 struct hlist_node mnt_umount; 88 }; 89 #ifdef CONFIG_FSNOTIFY 90 struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks; 91 __u32 mnt_fsnotify_mask; 92 struct list_head to_notify; /* need to queue notification */ 93 struct mnt_namespace *prev_ns; /* previous namespace (NULL if none) */ 94 #endif 95 int mnt_t_flags; /* namespace_sem-protected flags */ 96 int mnt_id; /* mount identifier, reused */ 97 u64 mnt_id_unique; /* mount ID unique until reboot */ 98 int mnt_group_id; /* peer group identifier */ 99 int mnt_expiry_mark; /* true if marked for expiry */ 100 struct hlist_head mnt_pins; 101 struct hlist_head mnt_stuck_children; 102 struct hlist_node mnt_ns_visible; /* link in ns->mnt_visible_mounts */ 103 struct mount *overmount; /* mounted on ->mnt_root */ 104 } __randomize_layout; 105 106 enum { 107 T_SHARED = 1, /* mount is shared */ 108 T_UNBINDABLE = 2, /* mount is unbindable */ 109 T_MARKED = 4, /* internal mark for propagate_... */ 110 T_UMOUNT_CANDIDATE = 8, /* for propagate_umount */ 111 112 /* 113 * T_SHARED_MASK is the set of flags that should be cleared when a 114 * mount becomes shared. Currently, this is only the flag that says a 115 * mount cannot be bind mounted, since this is how we create a mount 116 * that shares events with another mount. If you add a new T_* 117 * flag, consider how it interacts with shared mounts. 118 */ 119 T_SHARED_MASK = T_UNBINDABLE, 120 }; 121 122 #define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */ 123 124 static inline struct mount *real_mount(struct vfsmount *mnt) 125 { 126 return container_of(mnt, struct mount, mnt); 127 } 128 129 static inline int mnt_has_parent(const struct mount *mnt) 130 { 131 return mnt != mnt->mnt_parent; 132 } 133 134 static inline int is_mounted(struct vfsmount *mnt) 135 { 136 /* neither detached nor internal? */ 137 return !IS_ERR_OR_NULL(real_mount(mnt)->mnt_ns); 138 } 139 140 extern struct mount *__lookup_mnt(struct vfsmount *, struct dentry *); 141 142 extern int __legitimize_mnt(struct vfsmount *, unsigned); 143 144 static inline bool __path_is_mountpoint(const struct path *path) 145 { 146 struct mount *m = __lookup_mnt(path->mnt, path->dentry); 147 return m && likely(!(m->mnt.mnt_flags & MNT_SYNC_UMOUNT)); 148 } 149 150 extern void __detach_mounts(struct dentry *dentry); 151 152 static inline void detach_mounts(struct dentry *dentry) 153 { 154 if (!d_mountpoint(dentry)) 155 return; 156 __detach_mounts(dentry); 157 } 158 159 static inline void get_mnt_ns(struct mnt_namespace *ns) 160 { 161 ns_ref_inc(ns); 162 } 163 164 extern seqlock_t mount_lock; 165 166 DEFINE_LOCK_GUARD_0(mount_writer, write_seqlock(&mount_lock), 167 write_sequnlock(&mount_lock)) 168 DEFINE_LOCK_GUARD_0(mount_locked_reader, read_seqlock_excl(&mount_lock), 169 read_sequnlock_excl(&mount_lock)) 170 171 struct proc_mounts { 172 struct mnt_namespace *ns; 173 struct path root; 174 int (*show)(struct seq_file *, struct vfsmount *); 175 }; 176 177 extern const struct seq_operations mounts_op; 178 179 extern bool __is_local_mountpoint(const struct dentry *dentry); 180 static inline bool is_local_mountpoint(const struct dentry *dentry) 181 { 182 if (!d_mountpoint(dentry)) 183 return false; 184 185 return __is_local_mountpoint(dentry); 186 } 187 188 static inline bool is_anon_ns(struct mnt_namespace *ns) 189 { 190 return ns->is_anon; 191 } 192 193 static inline bool anon_ns_root(const struct mount *m) 194 { 195 struct mnt_namespace *ns = READ_ONCE(m->mnt_ns); 196 197 return !IS_ERR_OR_NULL(ns) && is_anon_ns(ns) && m == ns->root; 198 } 199 200 static inline bool mnt_ns_attached(const struct mount *mnt) 201 { 202 return !RB_EMPTY_NODE(&mnt->mnt_node); 203 } 204 205 static inline bool mnt_ns_empty(const struct mnt_namespace *ns) 206 { 207 return RB_EMPTY_ROOT(&ns->mounts); 208 } 209 210 static inline void move_from_ns(struct mount *mnt) 211 { 212 struct mnt_namespace *ns = mnt->mnt_ns; 213 WARN_ON(!mnt_ns_attached(mnt)); 214 if (ns->mnt_last_node == &mnt->mnt_node) 215 ns->mnt_last_node = rb_prev(&mnt->mnt_node); 216 if (ns->mnt_first_node == &mnt->mnt_node) 217 ns->mnt_first_node = rb_next(&mnt->mnt_node); 218 rb_erase(&mnt->mnt_node, &ns->mounts); 219 RB_CLEAR_NODE(&mnt->mnt_node); 220 if (!hlist_unhashed(&mnt->mnt_ns_visible)) 221 hlist_del_init(&mnt->mnt_ns_visible); 222 } 223 224 bool has_locked_children(struct mount *mnt, struct dentry *dentry); 225 struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mnt_ns, 226 bool previous); 227 228 static inline struct mnt_namespace *to_mnt_ns(struct ns_common *ns) 229 { 230 return container_of(ns, struct mnt_namespace, ns); 231 } 232 233 #ifdef CONFIG_FSNOTIFY 234 static inline void mnt_notify_add(struct mount *m) 235 { 236 /* Optimize the case where there are no watches */ 237 if ((m->mnt_ns && m->mnt_ns->n_fsnotify_marks) || 238 (m->prev_ns && m->prev_ns->n_fsnotify_marks)) 239 list_add_tail(&m->to_notify, ¬ify_list); 240 else 241 m->prev_ns = m->mnt_ns; 242 } 243 #else 244 static inline void mnt_notify_add(struct mount *m) 245 { 246 } 247 #endif 248 249 static inline struct mount *topmost_overmount(struct mount *m) 250 { 251 while (m->overmount) 252 m = m->overmount; 253 return m; 254 } 255 256 static inline bool __test_write_hold(struct mount * __aligned(1) *val) 257 { 258 return (unsigned long)val & WRITE_HOLD; 259 } 260 261 static inline bool test_write_hold(const struct mount *m) 262 { 263 return __test_write_hold(m->mnt_pprev_for_sb); 264 } 265 266 static inline void set_write_hold(struct mount *m) 267 { 268 m->mnt_pprev_for_sb = (void *)((unsigned long)m->mnt_pprev_for_sb 269 | WRITE_HOLD); 270 } 271 272 static inline void clear_write_hold(struct mount *m) 273 { 274 m->mnt_pprev_for_sb = (void *)((unsigned long)m->mnt_pprev_for_sb 275 & ~WRITE_HOLD); 276 } 277 278 struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry); 279