| 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 | u64 seq; /* Sequence number to prevent loops */ |
| 21 | union { |
| 22 | wait_queue_head_t poll; |
| 23 | struct rcu_head mnt_ns_rcu; |
| 24 | }; |
| 25 | u64 seq_origin; /* Sequence number of origin mount namespace */ |
| 26 | u64 event; |
| 27 | #ifdef CONFIG_FSNOTIFY |
| 28 | __u32 n_fsnotify_mask; |
| 29 | struct fsnotify_mark_connector __rcu *n_fsnotify_marks; |
| 30 | #endif |
| 31 | unsigned int nr_mounts; /* # of mounts in the namespace */ |
| 32 | unsigned int pending_mounts; |
| 33 | struct rb_node mnt_ns_tree_node; /* node in the mnt_ns_tree */ |
| 34 | struct list_head mnt_ns_list; /* entry in the sequential list of mounts namespace */ |
| 35 | refcount_t passive; /* number references not pinning @mounts */ |
| 36 | } __randomize_layout; |
| 37 | |
| 38 | struct mnt_pcp { |
| 39 | int mnt_count; |
| 40 | int mnt_writers; |
| 41 | }; |
| 42 | |
| 43 | struct mountpoint { |
| 44 | struct hlist_node m_hash; |
| 45 | struct dentry *m_dentry; |
| 46 | struct hlist_head m_list; |
| 47 | int m_count; |
| 48 | }; |
| 49 | |
| 50 | struct mount { |
| 51 | struct hlist_node mnt_hash; |
| 52 | struct mount *mnt_parent; |
| 53 | struct dentry *mnt_mountpoint; |
| 54 | struct vfsmount mnt; |
| 55 | union { |
| 56 | struct rb_node mnt_node; /* node in the ns->mounts rbtree */ |
| 57 | struct rcu_head mnt_rcu; |
| 58 | struct llist_node mnt_llist; |
| 59 | }; |
| 60 | #ifdef CONFIG_SMP |
| 61 | struct mnt_pcp __percpu *mnt_pcp; |
| 62 | #else |
| 63 | int mnt_count; |
| 64 | int mnt_writers; |
| 65 | #endif |
| 66 | struct list_head mnt_mounts; /* list of children, anchored here */ |
| 67 | struct list_head mnt_child; /* and going through their mnt_child */ |
| 68 | struct list_head mnt_instance; /* mount instance on sb->s_mounts */ |
| 69 | const char *mnt_devname; /* Name of device e.g. /dev/dsk/hda1 */ |
| 70 | struct list_head mnt_list; |
| 71 | struct list_head mnt_expire; /* link in fs-specific expiry list */ |
| 72 | struct list_head mnt_share; /* circular list of shared mounts */ |
| 73 | struct list_head mnt_slave_list;/* list of slave mounts */ |
| 74 | struct list_head mnt_slave; /* slave list entry */ |
| 75 | struct mount *mnt_master; /* slave is on master->mnt_slave_list */ |
| 76 | struct mnt_namespace *mnt_ns; /* containing namespace */ |
| 77 | struct mountpoint *mnt_mp; /* where is it mounted */ |
| 78 | union { |
| 79 | struct hlist_node mnt_mp_list; /* list mounts with the same mountpoint */ |
| 80 | struct hlist_node mnt_umount; |
| 81 | }; |
| 82 | struct list_head mnt_umounting; /* list entry for umount propagation */ |
| 83 | #ifdef CONFIG_FSNOTIFY |
| 84 | struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks; |
| 85 | __u32 mnt_fsnotify_mask; |
| 86 | struct list_head to_notify; /* need to queue notification */ |
| 87 | struct mnt_namespace *prev_ns; /* previous namespace (NULL if none) */ |
| 88 | #endif |
| 89 | int mnt_id; /* mount identifier, reused */ |
| 90 | u64 mnt_id_unique; /* mount ID unique until reboot */ |
| 91 | int mnt_group_id; /* peer group identifier */ |
| 92 | int mnt_expiry_mark; /* true if marked for expiry */ |
| 93 | struct hlist_head mnt_pins; |
| 94 | struct hlist_head mnt_stuck_children; |
| 95 | } __randomize_layout; |
| 96 | |
| 97 | #define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */ |
| 98 | |
| 99 | static inline struct mount *real_mount(struct vfsmount *mnt) |
| 100 | { |
| 101 | return container_of(mnt, struct mount, mnt); |
| 102 | } |
| 103 | |
| 104 | static inline int mnt_has_parent(struct mount *mnt) |
| 105 | { |
| 106 | return mnt != mnt->mnt_parent; |
| 107 | } |
| 108 | |
| 109 | static inline int is_mounted(struct vfsmount *mnt) |
| 110 | { |
| 111 | /* neither detached nor internal? */ |
| 112 | return !IS_ERR_OR_NULL(ptr: real_mount(mnt)->mnt_ns); |
| 113 | } |
| 114 | |
| 115 | extern struct mount *__lookup_mnt(struct vfsmount *, struct dentry *); |
| 116 | |
| 117 | extern int __legitimize_mnt(struct vfsmount *, unsigned); |
| 118 | |
| 119 | static inline bool __path_is_mountpoint(const struct path *path) |
| 120 | { |
| 121 | struct mount *m = __lookup_mnt(path->mnt, path->dentry); |
| 122 | return m && likely(!(m->mnt.mnt_flags & MNT_SYNC_UMOUNT)); |
| 123 | } |
| 124 | |
| 125 | extern void __detach_mounts(struct dentry *dentry); |
| 126 | |
| 127 | static inline void detach_mounts(struct dentry *dentry) |
| 128 | { |
| 129 | if (!d_mountpoint(dentry)) |
| 130 | return; |
| 131 | __detach_mounts(dentry); |
| 132 | } |
| 133 | |
| 134 | static inline void get_mnt_ns(struct mnt_namespace *ns) |
| 135 | { |
| 136 | refcount_inc(r: &ns->ns.count); |
| 137 | } |
| 138 | |
| 139 | extern seqlock_t mount_lock; |
| 140 | |
| 141 | struct proc_mounts { |
| 142 | struct mnt_namespace *ns; |
| 143 | struct path root; |
| 144 | int (*show)(struct seq_file *, struct vfsmount *); |
| 145 | }; |
| 146 | |
| 147 | extern const struct seq_operations mounts_op; |
| 148 | |
| 149 | extern bool __is_local_mountpoint(struct dentry *dentry); |
| 150 | static inline bool is_local_mountpoint(struct dentry *dentry) |
| 151 | { |
| 152 | if (!d_mountpoint(dentry)) |
| 153 | return false; |
| 154 | |
| 155 | return __is_local_mountpoint(dentry); |
| 156 | } |
| 157 | |
| 158 | static inline bool is_anon_ns(struct mnt_namespace *ns) |
| 159 | { |
| 160 | return ns->seq == 0; |
| 161 | } |
| 162 | |
| 163 | static inline bool mnt_ns_attached(const struct mount *mnt) |
| 164 | { |
| 165 | return !RB_EMPTY_NODE(&mnt->mnt_node); |
| 166 | } |
| 167 | |
| 168 | static inline bool mnt_ns_empty(const struct mnt_namespace *ns) |
| 169 | { |
| 170 | return RB_EMPTY_ROOT(&ns->mounts); |
| 171 | } |
| 172 | |
| 173 | static inline void move_from_ns(struct mount *mnt, struct list_head *dt_list) |
| 174 | { |
| 175 | struct mnt_namespace *ns = mnt->mnt_ns; |
| 176 | WARN_ON(!mnt_ns_attached(mnt)); |
| 177 | if (ns->mnt_last_node == &mnt->mnt_node) |
| 178 | ns->mnt_last_node = rb_prev(&mnt->mnt_node); |
| 179 | if (ns->mnt_first_node == &mnt->mnt_node) |
| 180 | ns->mnt_first_node = rb_next(&mnt->mnt_node); |
| 181 | rb_erase(&mnt->mnt_node, &ns->mounts); |
| 182 | RB_CLEAR_NODE(&mnt->mnt_node); |
| 183 | list_add_tail(new: &mnt->mnt_list, head: dt_list); |
| 184 | } |
| 185 | |
| 186 | bool has_locked_children(struct mount *mnt, struct dentry *dentry); |
| 187 | struct mnt_namespace *get_sequential_mnt_ns(struct mnt_namespace *mnt_ns, |
| 188 | bool previous); |
| 189 | |
| 190 | static inline struct mnt_namespace *to_mnt_ns(struct ns_common *ns) |
| 191 | { |
| 192 | return container_of(ns, struct mnt_namespace, ns); |
| 193 | } |
| 194 | |
| 195 | #ifdef CONFIG_FSNOTIFY |
| 196 | static inline void mnt_notify_add(struct mount *m) |
| 197 | { |
| 198 | /* Optimize the case where there are no watches */ |
| 199 | if ((m->mnt_ns && m->mnt_ns->n_fsnotify_marks) || |
| 200 | (m->prev_ns && m->prev_ns->n_fsnotify_marks)) |
| 201 | list_add_tail(new: &m->to_notify, head: ¬ify_list); |
| 202 | else |
| 203 | m->prev_ns = m->mnt_ns; |
| 204 | } |
| 205 | #else |
| 206 | static inline void mnt_notify_add(struct mount *m) |
| 207 | { |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | struct mnt_namespace *mnt_ns_from_dentry(struct dentry *dentry); |
| 212 | |