autofs_i.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright 1997-1998 Transmeta Corporation - All Rights Reserved
  4. * Copyright 2005-2006 Ian Kent <raven@themaw.net>
  5. */
  6. /* Internal header file for autofs */
  7. #include <linux/auto_fs.h>
  8. #include <linux/auto_dev-ioctl.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/time.h>
  12. #include <linux/string.h>
  13. #include <linux/wait.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/signal.h>
  16. #include <linux/mount.h>
  17. #include <linux/namei.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/mutex.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/list.h>
  22. #include <linux/completion.h>
  23. #include <linux/file.h>
  24. #include <linux/magic.h>
  25. /* This is the range of ioctl() numbers we claim as ours */
  26. #define AUTOFS_IOC_FIRST AUTOFS_IOC_READY
  27. #define AUTOFS_IOC_COUNT 32
  28. #define AUTOFS_DEV_IOCTL_IOC_FIRST (AUTOFS_DEV_IOCTL_VERSION)
  29. #define AUTOFS_DEV_IOCTL_IOC_COUNT \
  30. (AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD - AUTOFS_DEV_IOCTL_VERSION_CMD)
  31. #ifdef pr_fmt
  32. #undef pr_fmt
  33. #endif
  34. #define pr_fmt(fmt) KBUILD_MODNAME ":pid:%d:%s: " fmt, current->pid, __func__
  35. extern struct file_system_type autofs_fs_type;
  36. /*
  37. * Unified info structure. This is pointed to by both the dentry and
  38. * inode structures. Each file in the filesystem has an instance of this
  39. * structure. It holds a reference to the dentry, so dentries are never
  40. * flushed while the file exists. All name lookups are dealt with at the
  41. * dentry level, although the filesystem can interfere in the validation
  42. * process. Readdir is implemented by traversing the dentry lists.
  43. */
  44. struct autofs_info {
  45. struct dentry *dentry;
  46. struct inode *inode;
  47. int flags;
  48. struct completion expire_complete;
  49. struct list_head active;
  50. struct list_head expiring;
  51. struct autofs_sb_info *sbi;
  52. unsigned long last_used;
  53. int count;
  54. kuid_t uid;
  55. kgid_t gid;
  56. struct rcu_head rcu;
  57. };
  58. #define AUTOFS_INF_EXPIRING (1<<0) /* dentry in the process of expiring */
  59. #define AUTOFS_INF_WANT_EXPIRE (1<<1) /* the dentry is being considered
  60. * for expiry, so RCU_walk is
  61. * not permitted. If it progresses to
  62. * actual expiry attempt, the flag is
  63. * not cleared when EXPIRING is set -
  64. * in that case it gets cleared only
  65. * when it comes to clearing EXPIRING.
  66. */
  67. #define AUTOFS_INF_PENDING (1<<2) /* dentry pending mount */
  68. struct autofs_wait_queue {
  69. wait_queue_head_t queue;
  70. struct autofs_wait_queue *next;
  71. autofs_wqt_t wait_queue_token;
  72. /* We use the following to see what we are waiting for */
  73. struct qstr name;
  74. u32 dev;
  75. u64 ino;
  76. kuid_t uid;
  77. kgid_t gid;
  78. pid_t pid;
  79. pid_t tgid;
  80. /* This is for status reporting upon return */
  81. int status;
  82. unsigned int wait_ctr;
  83. };
  84. #define AUTOFS_SBI_MAGIC 0x6d4a556d
  85. #define AUTOFS_SBI_CATATONIC 0x0001
  86. #define AUTOFS_SBI_STRICTEXPIRE 0x0002
  87. #define AUTOFS_SBI_IGNORE 0x0004
  88. struct autofs_sb_info {
  89. u32 magic;
  90. int pipefd;
  91. struct file *pipe;
  92. struct pid *oz_pgrp;
  93. int version;
  94. int sub_version;
  95. int min_proto;
  96. int max_proto;
  97. unsigned int flags;
  98. unsigned long exp_timeout;
  99. unsigned int type;
  100. struct super_block *sb;
  101. struct mutex wq_mutex;
  102. struct mutex pipe_mutex;
  103. spinlock_t fs_lock;
  104. struct autofs_wait_queue *queues; /* Wait queue pointer */
  105. spinlock_t lookup_lock;
  106. struct list_head active_list;
  107. struct list_head expiring_list;
  108. struct rcu_head rcu;
  109. };
  110. static inline struct autofs_sb_info *autofs_sbi(struct super_block *sb)
  111. {
  112. return (struct autofs_sb_info *)(sb->s_fs_info);
  113. }
  114. static inline struct autofs_info *autofs_dentry_ino(struct dentry *dentry)
  115. {
  116. return (struct autofs_info *)(dentry->d_fsdata);
  117. }
  118. /* autofs_oz_mode(): do we see the man behind the curtain? (The
  119. * processes which do manipulations for us in user space sees the raw
  120. * filesystem without "magic".)
  121. */
  122. static inline int autofs_oz_mode(struct autofs_sb_info *sbi)
  123. {
  124. return ((sbi->flags & AUTOFS_SBI_CATATONIC) ||
  125. task_pgrp(current) == sbi->oz_pgrp);
  126. }
  127. struct inode *autofs_get_inode(struct super_block *, umode_t);
  128. void autofs_free_ino(struct autofs_info *);
  129. /* Expiration */
  130. int is_autofs_dentry(struct dentry *);
  131. int autofs_expire_wait(const struct path *path, int rcu_walk);
  132. int autofs_expire_run(struct super_block *, struct vfsmount *,
  133. struct autofs_sb_info *,
  134. struct autofs_packet_expire __user *);
  135. int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
  136. struct autofs_sb_info *sbi, unsigned int how);
  137. int autofs_expire_multi(struct super_block *, struct vfsmount *,
  138. struct autofs_sb_info *, int __user *);
  139. /* Device node initialization */
  140. int autofs_dev_ioctl_init(void);
  141. void autofs_dev_ioctl_exit(void);
  142. /* Operations structures */
  143. extern const struct inode_operations autofs_symlink_inode_operations;
  144. extern const struct inode_operations autofs_dir_inode_operations;
  145. extern const struct file_operations autofs_dir_operations;
  146. extern const struct file_operations autofs_root_operations;
  147. extern const struct dentry_operations autofs_dentry_operations;
  148. /* VFS automount flags management functions */
  149. static inline void __managed_dentry_set_managed(struct dentry *dentry)
  150. {
  151. dentry->d_flags |= (DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT);
  152. }
  153. static inline void managed_dentry_set_managed(struct dentry *dentry)
  154. {
  155. spin_lock(&dentry->d_lock);
  156. __managed_dentry_set_managed(dentry);
  157. spin_unlock(&dentry->d_lock);
  158. }
  159. static inline void __managed_dentry_clear_managed(struct dentry *dentry)
  160. {
  161. dentry->d_flags &= ~(DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT);
  162. }
  163. static inline void managed_dentry_clear_managed(struct dentry *dentry)
  164. {
  165. spin_lock(&dentry->d_lock);
  166. __managed_dentry_clear_managed(dentry);
  167. spin_unlock(&dentry->d_lock);
  168. }
  169. /* Initializing function */
  170. int autofs_fill_super(struct super_block *, void *, int);
  171. struct autofs_info *autofs_new_ino(struct autofs_sb_info *);
  172. void autofs_clean_ino(struct autofs_info *);
  173. static inline int autofs_prepare_pipe(struct file *pipe)
  174. {
  175. if (!(pipe->f_mode & FMODE_CAN_WRITE))
  176. return -EINVAL;
  177. if (!S_ISFIFO(file_inode(pipe)->i_mode))
  178. return -EINVAL;
  179. /* We want a packet pipe */
  180. pipe->f_flags |= O_DIRECT;
  181. /* We don't expect -EAGAIN */
  182. pipe->f_flags &= ~O_NONBLOCK;
  183. return 0;
  184. }
  185. /* Queue management functions */
  186. int autofs_wait(struct autofs_sb_info *,
  187. const struct path *, enum autofs_notify);
  188. int autofs_wait_release(struct autofs_sb_info *, autofs_wqt_t, int);
  189. void autofs_catatonic_mode(struct autofs_sb_info *);
  190. static inline u32 autofs_get_dev(struct autofs_sb_info *sbi)
  191. {
  192. return new_encode_dev(sbi->sb->s_dev);
  193. }
  194. static inline u64 autofs_get_ino(struct autofs_sb_info *sbi)
  195. {
  196. return d_inode(sbi->sb->s_root)->i_ino;
  197. }
  198. static inline void __autofs_add_expiring(struct dentry *dentry)
  199. {
  200. struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
  201. struct autofs_info *ino = autofs_dentry_ino(dentry);
  202. if (ino) {
  203. if (list_empty(&ino->expiring))
  204. list_add(&ino->expiring, &sbi->expiring_list);
  205. }
  206. }
  207. static inline void autofs_add_expiring(struct dentry *dentry)
  208. {
  209. struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
  210. struct autofs_info *ino = autofs_dentry_ino(dentry);
  211. if (ino) {
  212. spin_lock(&sbi->lookup_lock);
  213. if (list_empty(&ino->expiring))
  214. list_add(&ino->expiring, &sbi->expiring_list);
  215. spin_unlock(&sbi->lookup_lock);
  216. }
  217. }
  218. static inline void autofs_del_expiring(struct dentry *dentry)
  219. {
  220. struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
  221. struct autofs_info *ino = autofs_dentry_ino(dentry);
  222. if (ino) {
  223. spin_lock(&sbi->lookup_lock);
  224. if (!list_empty(&ino->expiring))
  225. list_del_init(&ino->expiring);
  226. spin_unlock(&sbi->lookup_lock);
  227. }
  228. }
  229. void autofs_kill_sb(struct super_block *);