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