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