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