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