mount.h 7.5 KB

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