1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2b2dba1afSAl Viro #include <linux/mount.h> 30226f492SAl Viro #include <linux/seq_file.h> 40226f492SAl Viro #include <linux/poll.h> 5435d5f4bSAl Viro #include <linux/ns_common.h> 687b95ce0SAl Viro #include <linux/fs_pin.h> 70226f492SAl Viro 80226f492SAl Viro struct mnt_namespace { 90226f492SAl Viro atomic_t count; 10435d5f4bSAl Viro struct ns_common ns; 11be08d6d2SAl Viro struct mount * root; 120226f492SAl Viro struct list_head list; 13771b1371SEric W. Biederman struct user_namespace *user_ns; 14537f7ccbSEric W. Biederman struct ucounts *ucounts; 158823c079SEric W. Biederman u64 seq; /* Sequence number to prevent loops */ 160226f492SAl Viro wait_queue_head_t poll; 17c7999c36SAl Viro u64 event; 18d2921684SEric W. Biederman unsigned int mounts; /* # of mounts in the namespace */ 19d2921684SEric W. Biederman unsigned int pending_mounts; 203859a271SKees Cook } __randomize_layout; 21b2dba1afSAl Viro 2268e8a9feSAl Viro struct mnt_pcp { 2368e8a9feSAl Viro int mnt_count; 2468e8a9feSAl Viro int mnt_writers; 2568e8a9feSAl Viro }; 2668e8a9feSAl Viro 2784d17192SAl Viro struct mountpoint { 280818bf27SAl Viro struct hlist_node m_hash; 2984d17192SAl Viro struct dentry *m_dentry; 300a5eb7c8SEric W. Biederman struct hlist_head m_list; 3184d17192SAl Viro int m_count; 3284d17192SAl Viro }; 3384d17192SAl Viro 347d6fec45SAl Viro struct mount { 3538129a13SAl Viro struct hlist_node mnt_hash; 360714a533SAl Viro struct mount *mnt_parent; 37a73324daSAl Viro struct dentry *mnt_mountpoint; 387d6fec45SAl Viro struct vfsmount mnt; 399ea459e1SAl Viro union { 4048a066e7SAl Viro struct rcu_head mnt_rcu; 419ea459e1SAl Viro struct llist_node mnt_llist; 429ea459e1SAl Viro }; 4368e8a9feSAl Viro #ifdef CONFIG_SMP 4468e8a9feSAl Viro struct mnt_pcp __percpu *mnt_pcp; 4568e8a9feSAl Viro #else 4668e8a9feSAl Viro int mnt_count; 4768e8a9feSAl Viro int mnt_writers; 4868e8a9feSAl Viro #endif 496b41d536SAl Viro struct list_head mnt_mounts; /* list of children, anchored here */ 506b41d536SAl Viro struct list_head mnt_child; /* and going through their mnt_child */ 5139f7c4dbSMiklos Szeredi struct list_head mnt_instance; /* mount instance on sb->s_mounts */ 5252ba1621SAl Viro const char *mnt_devname; /* Name of device e.g. /dev/dsk/hda1 */ 531a4eeaf2SAl Viro struct list_head mnt_list; 546776db3dSAl Viro struct list_head mnt_expire; /* link in fs-specific expiry list */ 556776db3dSAl Viro struct list_head mnt_share; /* circular list of shared mounts */ 566776db3dSAl Viro struct list_head mnt_slave_list;/* list of slave mounts */ 576776db3dSAl Viro struct list_head mnt_slave; /* slave list entry */ 5832301920SAl Viro struct mount *mnt_master; /* slave is on master->mnt_slave_list */ 59143c8c91SAl Viro struct mnt_namespace *mnt_ns; /* containing namespace */ 6084d17192SAl Viro struct mountpoint *mnt_mp; /* where is it mounted */ 6156cbb429SAl Viro union { 620a5eb7c8SEric W. Biederman struct hlist_node mnt_mp_list; /* list mounts with the same mountpoint */ 6356cbb429SAl Viro struct hlist_node mnt_umount; 6456cbb429SAl Viro }; 6599b19d16SEric W. Biederman struct list_head mnt_umounting; /* list entry for umount propagation */ 66c63181e6SAl Viro #ifdef CONFIG_FSNOTIFY 6708991e83SJan Kara struct fsnotify_mark_connector __rcu *mnt_fsnotify_marks; 68c63181e6SAl Viro __u32 mnt_fsnotify_mask; 69c63181e6SAl Viro #endif 7015169fe7SAl Viro int mnt_id; /* mount identifier */ 7115169fe7SAl Viro int mnt_group_id; /* peer group identifier */ 72863d684fSAl Viro int mnt_expiry_mark; /* true if marked for expiry */ 73215752fcSAl Viro struct hlist_head mnt_pins; 7456cbb429SAl Viro struct hlist_head mnt_stuck_children; 753859a271SKees Cook } __randomize_layout; 767d6fec45SAl Viro 77f7a99c5bSAl Viro #define MNT_NS_INTERNAL ERR_PTR(-EINVAL) /* distinct from any mnt_namespace */ 78f7a99c5bSAl Viro 797d6fec45SAl Viro static inline struct mount *real_mount(struct vfsmount *mnt) 807d6fec45SAl Viro { 817d6fec45SAl Viro return container_of(mnt, struct mount, mnt); 827d6fec45SAl Viro } 837d6fec45SAl Viro 84676da58dSAl Viro static inline int mnt_has_parent(struct mount *mnt) 85b2dba1afSAl Viro { 860714a533SAl Viro return mnt != mnt->mnt_parent; 87b2dba1afSAl Viro } 88c7105365SAl Viro 89f7a99c5bSAl Viro static inline int is_mounted(struct vfsmount *mnt) 90f7a99c5bSAl Viro { 91f7a99c5bSAl Viro /* neither detached nor internal? */ 92260a459dSEric W. Biederman return !IS_ERR_OR_NULL(real_mount(mnt)->mnt_ns); 93f7a99c5bSAl Viro } 94f7a99c5bSAl Viro 95474279dcSAl Viro extern struct mount *__lookup_mnt(struct vfsmount *, struct dentry *); 960226f492SAl Viro 97294d71ffSAl Viro extern int __legitimize_mnt(struct vfsmount *, unsigned); 9848a066e7SAl Viro extern bool legitimize_mnt(struct vfsmount *, unsigned); 9948a066e7SAl Viro 100c6609c0aSIan Kent static inline bool __path_is_mountpoint(const struct path *path) 101c6609c0aSIan Kent { 102c6609c0aSIan Kent struct mount *m = __lookup_mnt(path->mnt, path->dentry); 103c6609c0aSIan Kent return m && likely(!(m->mnt.mnt_flags & MNT_SYNC_UMOUNT)); 104c6609c0aSIan Kent } 105c6609c0aSIan Kent 10680b5dce8SEric W. Biederman extern void __detach_mounts(struct dentry *dentry); 10780b5dce8SEric W. Biederman 10880b5dce8SEric W. Biederman static inline void detach_mounts(struct dentry *dentry) 10980b5dce8SEric W. Biederman { 11080b5dce8SEric W. Biederman if (!d_mountpoint(dentry)) 11180b5dce8SEric W. Biederman return; 11280b5dce8SEric W. Biederman __detach_mounts(dentry); 11380b5dce8SEric W. Biederman } 11480b5dce8SEric W. Biederman 1150226f492SAl Viro static inline void get_mnt_ns(struct mnt_namespace *ns) 1160226f492SAl Viro { 1170226f492SAl Viro atomic_inc(&ns->count); 1180226f492SAl Viro } 1190226f492SAl Viro 12048a066e7SAl Viro extern seqlock_t mount_lock; 121719ea2fbSAl Viro 122719ea2fbSAl Viro static inline void lock_mount_hash(void) 123719ea2fbSAl Viro { 12448a066e7SAl Viro write_seqlock(&mount_lock); 125719ea2fbSAl Viro } 126719ea2fbSAl Viro 127719ea2fbSAl Viro static inline void unlock_mount_hash(void) 128719ea2fbSAl Viro { 12948a066e7SAl Viro write_sequnlock(&mount_lock); 130719ea2fbSAl Viro } 131719ea2fbSAl Viro 1320226f492SAl Viro struct proc_mounts { 1330226f492SAl Viro struct mnt_namespace *ns; 1340226f492SAl Viro struct path root; 1350226f492SAl Viro int (*show)(struct seq_file *, struct vfsmount *); 136c7999c36SAl Viro void *cached_mount; 137c7999c36SAl Viro u64 cached_event; 138c7999c36SAl Viro loff_t cached_index; 1390226f492SAl Viro }; 1400226f492SAl Viro 1410226f492SAl Viro extern const struct seq_operations mounts_op; 1427af1364fSEric W. Biederman 1437af1364fSEric W. Biederman extern bool __is_local_mountpoint(struct dentry *dentry); 1447af1364fSEric W. Biederman static inline bool is_local_mountpoint(struct dentry *dentry) 1457af1364fSEric W. Biederman { 1467af1364fSEric W. Biederman if (!d_mountpoint(dentry)) 1477af1364fSEric W. Biederman return false; 1487af1364fSEric W. Biederman 1497af1364fSEric W. Biederman return __is_local_mountpoint(dentry); 1507af1364fSEric W. Biederman } 15174e83122SAl Viro 15274e83122SAl Viro static inline bool is_anon_ns(struct mnt_namespace *ns) 15374e83122SAl Viro { 15474e83122SAl Viro return ns->seq == 0; 15574e83122SAl Viro } 156