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