summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIan Kent <raven@themaw.net>2018-06-07 20:11:13 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2018-06-07 20:34:39 -0400
commitebc921ca9b92a3cf304d99bd7b7f373ec78c7ed7 (patch)
treeb8e0a20632c0461a25e604d3023f598f4c458fa2
parent47206e012a0ad05f358342c083cc6021160b61f9 (diff)
autofs: copy autofs4 to autofs
Copy source files from the autofs4 directory to the autofs directory. Link: http://lkml.kernel.org/r/152626705013.28589.931913083997578251.stgit@pluto.themaw.net Signed-off-by: Ian Kent <raven@themaw.net> Cc: Al Viro <viro@ZenIV.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--fs/autofs/autofs_i.h273
-rw-r--r--fs/autofs/dev-ioctl.c761
-rw-r--r--fs/autofs/expire.c631
-rw-r--r--fs/autofs/init.c48
-rw-r--r--fs/autofs/inode.c375
-rw-r--r--fs/autofs/root.c942
-rw-r--r--fs/autofs/symlink.c29
-rw-r--r--fs/autofs/waitq.c559
8 files changed, 3618 insertions, 0 deletions
diff --git a/fs/autofs/autofs_i.h b/fs/autofs/autofs_i.h
new file mode 100644
index 000000000000..9110b66c7ef1
--- /dev/null
+++ b/fs/autofs/autofs_i.h
@@ -0,0 +1,273 @@
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/mount.h>
22#include <linux/namei.h>
23#include <linux/uaccess.h>
24#include <linux/mutex.h>
25#include <linux/spinlock.h>
26#include <linux/list.h>
27#include <linux/completion.h>
28#include <asm/current.h>
29
30/* This is the range of ioctl() numbers we claim as ours */
31#define AUTOFS_IOC_FIRST AUTOFS_IOC_READY
32#define AUTOFS_IOC_COUNT 32
33
34#define AUTOFS_DEV_IOCTL_IOC_FIRST (AUTOFS_DEV_IOCTL_VERSION)
35#define AUTOFS_DEV_IOCTL_IOC_COUNT \
36 (AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD - AUTOFS_DEV_IOCTL_VERSION_CMD)
37
38#ifdef pr_fmt
39#undef pr_fmt
40#endif
41#define pr_fmt(fmt) KBUILD_MODNAME ":pid:%d:%s: " fmt, current->pid, __func__
42
43/*
44 * Unified info structure. This is pointed to by both the dentry and
45 * inode structures. Each file in the filesystem has an instance of this
46 * structure. It holds a reference to the dentry, so dentries are never
47 * flushed while the file exists. All name lookups are dealt with at the
48 * dentry level, although the filesystem can interfere in the validation
49 * process. Readdir is implemented by traversing the dentry lists.
50 */
51struct autofs_info {
52 struct dentry *dentry;
53 struct inode *inode;
54
55 int flags;
56
57 struct completion expire_complete;
58
59 struct list_head active;
60 int active_count;
61
62 struct list_head expiring;
63
64 struct autofs_sb_info *sbi;
65 unsigned long last_used;
66 atomic_t count;
67
68 kuid_t uid;
69 kgid_t gid;
70};
71
72#define AUTOFS_INF_EXPIRING (1<<0) /* dentry in the process of expiring */
73#define AUTOFS_INF_WANT_EXPIRE (1<<1) /* the dentry is being considered
74 * for expiry, so RCU_walk is
75 * not permitted. If it progresses to
76 * actual expiry attempt, the flag is
77 * not cleared when EXPIRING is set -
78 * in that case it gets cleared only
79 * when it comes to clearing EXPIRING.
80 */
81#define AUTOFS_INF_PENDING (1<<2) /* dentry pending mount */
82
83struct autofs_wait_queue {
84 wait_queue_head_t queue;
85 struct autofs_wait_queue *next;
86 autofs_wqt_t wait_queue_token;
87 /* We use the following to see what we are waiting for */
88 struct qstr name;
89 u32 dev;
90 u64 ino;
91 kuid_t uid;
92 kgid_t gid;
93 pid_t pid;
94 pid_t tgid;
95 /* This is for status reporting upon return */
96 int status;
97 unsigned int wait_ctr;
98};
99
100#define AUTOFS_SBI_MAGIC 0x6d4a556d
101
102struct autofs_sb_info {
103 u32 magic;
104 int pipefd;
105 struct file *pipe;
106 struct pid *oz_pgrp;
107 int catatonic;
108 int version;
109 int sub_version;
110 int min_proto;
111 int max_proto;
112 unsigned long exp_timeout;
113 unsigned int type;
114 struct super_block *sb;
115 struct mutex wq_mutex;
116 struct mutex pipe_mutex;
117 spinlock_t fs_lock;
118 struct autofs_wait_queue *queues; /* Wait queue pointer */
119 spinlock_t lookup_lock;
120 struct list_head active_list;
121 struct list_head expiring_list;
122 struct rcu_head rcu;
123};
124
125static inline struct autofs_sb_info *autofs_sbi(struct super_block *sb)
126{
127 return (struct autofs_sb_info *)(sb->s_fs_info);
128}
129
130static inline struct autofs_info *autofs_dentry_ino(struct dentry *dentry)
131{
132 return (struct autofs_info *)(dentry->d_fsdata);
133}
134
135/* autofs_oz_mode(): do we see the man behind the curtain? (The
136 * processes which do manipulations for us in user space sees the raw
137 * filesystem without "magic".)
138 */
139static inline int autofs_oz_mode(struct autofs_sb_info *sbi)
140{
141 return sbi->catatonic || task_pgrp(current) == sbi->oz_pgrp;
142}
143
144struct inode *autofs_get_inode(struct super_block *, umode_t);
145void autofs_free_ino(struct autofs_info *);
146
147/* Expiration */
148int is_autofs_dentry(struct dentry *);
149int autofs_expire_wait(const struct path *path, int rcu_walk);
150int autofs_expire_run(struct super_block *, struct vfsmount *,
151 struct autofs_sb_info *,
152 struct autofs_packet_expire __user *);
153int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
154 struct autofs_sb_info *sbi, int when);
155int autofs_expire_multi(struct super_block *, struct vfsmount *,
156 struct autofs_sb_info *, int __user *);
157struct dentry *autofs_expire_direct(struct super_block *sb,
158 struct vfsmount *mnt,
159 struct autofs_sb_info *sbi, int how);
160struct dentry *autofs_expire_indirect(struct super_block *sb,
161 struct vfsmount *mnt,
162 struct autofs_sb_info *sbi, int how);
163
164/* Device node initialization */
165
166int autofs_dev_ioctl_init(void);
167void autofs_dev_ioctl_exit(void);
168
169/* Operations structures */
170
171extern const struct inode_operations autofs_symlink_inode_operations;
172extern const struct inode_operations autofs_dir_inode_operations;
173extern const struct file_operations autofs_dir_operations;
174extern const struct file_operations autofs_root_operations;
175extern const struct dentry_operations autofs_dentry_operations;
176
177/* VFS automount flags management functions */
178static inline void __managed_dentry_set_managed(struct dentry *dentry)
179{
180 dentry->d_flags |= (DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT);
181}
182
183static 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
190static inline void __managed_dentry_clear_managed(struct dentry *dentry)
191{
192 dentry->d_flags &= ~(DCACHE_NEED_AUTOMOUNT|DCACHE_MANAGE_TRANSIT);
193}
194
195static 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
204int autofs_fill_super(struct super_block *, void *, int);
205struct autofs_info *autofs_new_ino(struct autofs_sb_info *);
206void autofs_clean_ino(struct autofs_info *);
207
208static 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 return 0;
217}
218
219/* Queue management functions */
220
221int autofs_wait(struct autofs_sb_info *,
222 const struct path *, enum autofs_notify);
223int autofs_wait_release(struct autofs_sb_info *, autofs_wqt_t, int);
224void autofs_catatonic_mode(struct autofs_sb_info *);
225
226static inline u32 autofs_get_dev(struct autofs_sb_info *sbi)
227{
228 return new_encode_dev(sbi->sb->s_dev);
229}
230
231static inline u64 autofs_get_ino(struct autofs_sb_info *sbi)
232{
233 return d_inode(sbi->sb->s_root)->i_ino;
234}
235
236static inline void __autofs_add_expiring(struct dentry *dentry)
237{
238 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
239 struct autofs_info *ino = autofs_dentry_ino(dentry);
240
241 if (ino) {
242 if (list_empty(&ino->expiring))
243 list_add(&ino->expiring, &sbi->expiring_list);
244 }
245}
246
247static inline void autofs_add_expiring(struct dentry *dentry)
248{
249 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
250 struct autofs_info *ino = autofs_dentry_ino(dentry);
251
252 if (ino) {
253 spin_lock(&sbi->lookup_lock);
254 if (list_empty(&ino->expiring))
255 list_add(&ino->expiring, &sbi->expiring_list);
256 spin_unlock(&sbi->lookup_lock);
257 }
258}
259
260static inline void autofs_del_expiring(struct dentry *dentry)
261{
262 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
263 struct autofs_info *ino = autofs_dentry_ino(dentry);
264
265 if (ino) {
266 spin_lock(&sbi->lookup_lock);
267 if (!list_empty(&ino->expiring))
268 list_del_init(&ino->expiring);
269 spin_unlock(&sbi->lookup_lock);
270 }
271}
272
273void autofs_kill_sb(struct super_block *);
diff --git a/fs/autofs/dev-ioctl.c b/fs/autofs/dev-ioctl.c
new file mode 100644
index 000000000000..a2281ab2b957
--- /dev/null
+++ b/fs/autofs/dev-ioctl.c
@@ -0,0 +1,761 @@
1/*
2 * Copyright 2008 Red Hat, Inc. All rights reserved.
3 * Copyright 2008 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#include <linux/module.h>
11#include <linux/vmalloc.h>
12#include <linux/miscdevice.h>
13#include <linux/init.h>
14#include <linux/wait.h>
15#include <linux/namei.h>
16#include <linux/fcntl.h>
17#include <linux/file.h>
18#include <linux/fdtable.h>
19#include <linux/sched.h>
20#include <linux/cred.h>
21#include <linux/compat.h>
22#include <linux/syscalls.h>
23#include <linux/magic.h>
24#include <linux/dcache.h>
25#include <linux/uaccess.h>
26#include <linux/slab.h>
27
28#include "autofs_i.h"
29
30/*
31 * This module implements an interface for routing autofs ioctl control
32 * commands via a miscellaneous device file.
33 *
34 * The alternate interface is needed because we need to be able open
35 * an ioctl file descriptor on an autofs mount that may be covered by
36 * another mount. This situation arises when starting automount(8)
37 * or other user space daemon which uses direct mounts or offset
38 * mounts (used for autofs lazy mount/umount of nested mount trees),
39 * which have been left busy at at service shutdown.
40 */
41
42typedef int (*ioctl_fn)(struct file *, struct autofs_sb_info *,
43 struct autofs_dev_ioctl *);
44
45static int check_name(const char *name)
46{
47 if (!strchr(name, '/'))
48 return -EINVAL;
49 return 0;
50}
51
52/*
53 * Check a string doesn't overrun the chunk of
54 * memory we copied from user land.
55 */
56static int invalid_str(char *str, size_t size)
57{
58 if (memchr(str, 0, size))
59 return 0;
60 return -EINVAL;
61}
62
63/*
64 * Check that the user compiled against correct version of autofs
65 * misc device code.
66 *
67 * As well as checking the version compatibility this always copies
68 * the kernel interface version out.
69 */
70static int check_dev_ioctl_version(int cmd, struct autofs_dev_ioctl *param)
71{
72 int err = 0;
73
74 if ((param->ver_major != AUTOFS_DEV_IOCTL_VERSION_MAJOR) ||
75 (param->ver_minor > AUTOFS_DEV_IOCTL_VERSION_MINOR)) {
76 pr_warn("ioctl control interface version mismatch: "
77 "kernel(%u.%u), user(%u.%u), cmd(0x%08x)\n",
78 AUTOFS_DEV_IOCTL_VERSION_MAJOR,
79 AUTOFS_DEV_IOCTL_VERSION_MINOR,
80 param->ver_major, param->ver_minor, cmd);
81 err = -EINVAL;
82 }
83
84 /* Fill in the kernel version. */
85 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
86 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
87
88 return err;
89}
90
91/*
92 * Copy parameter control struct, including a possible path allocated
93 * at the end of the struct.
94 */
95static struct autofs_dev_ioctl *
96copy_dev_ioctl(struct autofs_dev_ioctl __user *in)
97{
98 struct autofs_dev_ioctl tmp, *res;
99
100 if (copy_from_user(&tmp, in, AUTOFS_DEV_IOCTL_SIZE))
101 return ERR_PTR(-EFAULT);
102
103 if (tmp.size < AUTOFS_DEV_IOCTL_SIZE)
104 return ERR_PTR(-EINVAL);
105
106 if (tmp.size > AUTOFS_DEV_IOCTL_SIZE + PATH_MAX)
107 return ERR_PTR(-ENAMETOOLONG);
108
109 res = memdup_user(in, tmp.size);
110 if (!IS_ERR(res))
111 res->size = tmp.size;
112
113 return res;
114}
115
116static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
117{
118 kfree(param);
119}
120
121/*
122 * Check sanity of parameter control fields and if a path is present
123 * check that it is terminated and contains at least one "/".
124 */
125static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param)
126{
127 int err;
128
129 err = check_dev_ioctl_version(cmd, param);
130 if (err) {
131 pr_warn("invalid device control module version "
132 "supplied for cmd(0x%08x)\n", cmd);
133 goto out;
134 }
135
136 if (param->size > AUTOFS_DEV_IOCTL_SIZE) {
137 err = invalid_str(param->path, param->size - AUTOFS_DEV_IOCTL_SIZE);
138 if (err) {
139 pr_warn(
140 "path string terminator missing for cmd(0x%08x)\n",
141 cmd);
142 goto out;
143 }
144
145 err = check_name(param->path);
146 if (err) {
147 pr_warn("invalid path supplied for cmd(0x%08x)\n",
148 cmd);
149 goto out;
150 }
151 }
152
153 err = 0;
154out:
155 return err;
156}
157
158/*
159 * Get the autofs super block info struct from the file opened on
160 * the autofs mount point.
161 */
162static struct autofs_sb_info *autofs_dev_ioctl_sbi(struct file *f)
163{
164 struct autofs_sb_info *sbi = NULL;
165 struct inode *inode;
166
167 if (f) {
168 inode = file_inode(f);
169 sbi = autofs_sbi(inode->i_sb);
170 }
171 return sbi;
172}
173
174/* Return autofs dev ioctl version */
175static int autofs_dev_ioctl_version(struct file *fp,
176 struct autofs_sb_info *sbi,
177 struct autofs_dev_ioctl *param)
178{
179 /* This should have already been set. */
180 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
181 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
182 return 0;
183}
184
185/* Return autofs module protocol version */
186static int autofs_dev_ioctl_protover(struct file *fp,
187 struct autofs_sb_info *sbi,
188 struct autofs_dev_ioctl *param)
189{
190 param->protover.version = sbi->version;
191 return 0;
192}
193
194/* Return autofs module protocol sub version */
195static int autofs_dev_ioctl_protosubver(struct file *fp,
196 struct autofs_sb_info *sbi,
197 struct autofs_dev_ioctl *param)
198{
199 param->protosubver.sub_version = sbi->sub_version;
200 return 0;
201}
202
203/* Find the topmost mount satisfying test() */
204static int find_autofs_mount(const char *pathname,
205 struct path *res,
206 int test(const struct path *path, void *data),
207 void *data)
208{
209 struct path path;
210 int err;
211
212 err = kern_path_mountpoint(AT_FDCWD, pathname, &path, 0);
213 if (err)
214 return err;
215 err = -ENOENT;
216 while (path.dentry == path.mnt->mnt_root) {
217 if (path.dentry->d_sb->s_magic == AUTOFS_SUPER_MAGIC) {
218 if (test(&path, data)) {
219 path_get(&path);
220 *res = path;
221 err = 0;
222 break;
223 }
224 }
225 if (!follow_up(&path))
226 break;
227 }
228 path_put(&path);
229 return err;
230}
231
232static int test_by_dev(const struct path *path, void *p)
233{
234 return path->dentry->d_sb->s_dev == *(dev_t *)p;
235}
236
237static int test_by_type(const struct path *path, void *p)
238{
239 struct autofs_info *ino = autofs_dentry_ino(path->dentry);
240
241 return ino && ino->sbi->type & *(unsigned *)p;
242}
243
244/*
245 * Open a file descriptor on the autofs mount point corresponding
246 * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
247 */
248static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid)
249{
250 int err, fd;
251
252 fd = get_unused_fd_flags(O_CLOEXEC);
253 if (likely(fd >= 0)) {
254 struct file *filp;
255 struct path path;
256
257 err = find_autofs_mount(name, &path, test_by_dev, &devid);
258 if (err)
259 goto out;
260
261 filp = dentry_open(&path, O_RDONLY, current_cred());
262 path_put(&path);
263 if (IS_ERR(filp)) {
264 err = PTR_ERR(filp);
265 goto out;
266 }
267
268 fd_install(fd, filp);
269 }
270
271 return fd;
272
273out:
274 put_unused_fd(fd);
275 return err;
276}
277
278/* Open a file descriptor on an autofs mount point */
279static int autofs_dev_ioctl_openmount(struct file *fp,
280 struct autofs_sb_info *sbi,
281 struct autofs_dev_ioctl *param)
282{
283 const char *path;
284 dev_t devid;
285 int err, fd;
286
287 /* param->path has already been checked */
288 if (!param->openmount.devid)
289 return -EINVAL;
290
291 param->ioctlfd = -1;
292
293 path = param->path;
294 devid = new_decode_dev(param->openmount.devid);
295
296 err = 0;
297 fd = autofs_dev_ioctl_open_mountpoint(path, devid);
298 if (unlikely(fd < 0)) {
299 err = fd;
300 goto out;
301 }
302
303 param->ioctlfd = fd;
304out:
305 return err;
306}
307
308/* Close file descriptor allocated above (user can also use close(2)). */
309static int autofs_dev_ioctl_closemount(struct file *fp,
310 struct autofs_sb_info *sbi,
311 struct autofs_dev_ioctl *param)
312{
313 return ksys_close(param->ioctlfd);
314}
315
316/*
317 * Send "ready" status for an existing wait (either a mount or an expire
318 * request).
319 */
320static int autofs_dev_ioctl_ready(struct file *fp,
321 struct autofs_sb_info *sbi,
322 struct autofs_dev_ioctl *param)
323{
324 autofs_wqt_t token;
325
326 token = (autofs_wqt_t) param->ready.token;
327 return autofs_wait_release(sbi, token, 0);
328}
329
330/*
331 * Send "fail" status for an existing wait (either a mount or an expire
332 * request).
333 */
334static int autofs_dev_ioctl_fail(struct file *fp,
335 struct autofs_sb_info *sbi,
336 struct autofs_dev_ioctl *param)
337{
338 autofs_wqt_t token;
339 int status;
340
341 token = (autofs_wqt_t) param->fail.token;
342 status = param->fail.status < 0 ? param->fail.status : -ENOENT;
343 return autofs_wait_release(sbi, token, status);
344}
345
346/*
347 * Set the pipe fd for kernel communication to the daemon.
348 *
349 * Normally this is set at mount using an option but if we
350 * are reconnecting to a busy mount then we need to use this
351 * to tell the autofs mount about the new kernel pipe fd. In
352 * order to protect mounts against incorrectly setting the
353 * pipefd we also require that the autofs mount be catatonic.
354 *
355 * This also sets the process group id used to identify the
356 * controlling process (eg. the owning automount(8) daemon).
357 */
358static int autofs_dev_ioctl_setpipefd(struct file *fp,
359 struct autofs_sb_info *sbi,
360 struct autofs_dev_ioctl *param)
361{
362 int pipefd;
363 int err = 0;
364 struct pid *new_pid = NULL;
365
366 if (param->setpipefd.pipefd == -1)
367 return -EINVAL;
368
369 pipefd = param->setpipefd.pipefd;
370
371 mutex_lock(&sbi->wq_mutex);
372 if (!sbi->catatonic) {
373 mutex_unlock(&sbi->wq_mutex);
374 return -EBUSY;
375 } else {
376 struct file *pipe;
377
378 new_pid = get_task_pid(current, PIDTYPE_PGID);
379
380 if (ns_of_pid(new_pid) != ns_of_pid(sbi->oz_pgrp)) {
381 pr_warn("not allowed to change PID namespace\n");
382 err = -EINVAL;
383 goto out;
384 }
385
386 pipe = fget(pipefd);
387 if (!pipe) {
388 err = -EBADF;
389 goto out;
390 }
391 if (autofs_prepare_pipe(pipe) < 0) {
392 err = -EPIPE;
393 fput(pipe);
394 goto out;
395 }
396 swap(sbi->oz_pgrp, new_pid);
397 sbi->pipefd = pipefd;
398 sbi->pipe = pipe;
399 sbi->catatonic = 0;
400 }
401out:
402 put_pid(new_pid);
403 mutex_unlock(&sbi->wq_mutex);
404 return err;
405}
406
407/*
408 * Make the autofs mount point catatonic, no longer responsive to
409 * mount requests. Also closes the kernel pipe file descriptor.
410 */
411static int autofs_dev_ioctl_catatonic(struct file *fp,
412 struct autofs_sb_info *sbi,
413 struct autofs_dev_ioctl *param)
414{
415 autofs_catatonic_mode(sbi);
416 return 0;
417}
418
419/* Set the autofs mount timeout */
420static int autofs_dev_ioctl_timeout(struct file *fp,
421 struct autofs_sb_info *sbi,
422 struct autofs_dev_ioctl *param)
423{
424 unsigned long timeout;
425
426 timeout = param->timeout.timeout;
427 param->timeout.timeout = sbi->exp_timeout / HZ;
428 sbi->exp_timeout = timeout * HZ;
429 return 0;
430}
431
432/*
433 * Return the uid and gid of the last request for the mount
434 *
435 * When reconstructing an autofs mount tree with active mounts
436 * we need to re-connect to mounts that may have used the original
437 * process uid and gid (or string variations of them) for mount
438 * lookups within the map entry.
439 */
440static int autofs_dev_ioctl_requester(struct file *fp,
441 struct autofs_sb_info *sbi,
442 struct autofs_dev_ioctl *param)
443{
444 struct autofs_info *ino;
445 struct path path;
446 dev_t devid;
447 int err = -ENOENT;
448
449 if (param->size <= AUTOFS_DEV_IOCTL_SIZE) {
450 err = -EINVAL;
451 goto out;
452 }
453
454 devid = sbi->sb->s_dev;
455
456 param->requester.uid = param->requester.gid = -1;
457
458 err = find_autofs_mount(param->path, &path, test_by_dev, &devid);
459 if (err)
460 goto out;
461
462 ino = autofs_dentry_ino(path.dentry);
463 if (ino) {
464 err = 0;
465 autofs_expire_wait(&path, 0);
466 spin_lock(&sbi->fs_lock);
467 param->requester.uid =
468 from_kuid_munged(current_user_ns(), ino->uid);
469 param->requester.gid =
470 from_kgid_munged(current_user_ns(), ino->gid);
471 spin_unlock(&sbi->fs_lock);
472 }
473 path_put(&path);
474out:
475 return err;
476}
477
478/*
479 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
480 * more that can be done.
481 */
482static int autofs_dev_ioctl_expire(struct file *fp,
483 struct autofs_sb_info *sbi,
484 struct autofs_dev_ioctl *param)
485{
486 struct vfsmount *mnt;
487 int how;
488
489 how = param->expire.how;
490 mnt = fp->f_path.mnt;
491
492 return autofs_do_expire_multi(sbi->sb, mnt, sbi, how);
493}
494
495/* Check if autofs mount point is in use */
496static int autofs_dev_ioctl_askumount(struct file *fp,
497 struct autofs_sb_info *sbi,
498 struct autofs_dev_ioctl *param)
499{
500 param->askumount.may_umount = 0;
501 if (may_umount(fp->f_path.mnt))
502 param->askumount.may_umount = 1;
503 return 0;
504}
505
506/*
507 * Check if the given path is a mountpoint.
508 *
509 * If we are supplied with the file descriptor of an autofs
510 * mount we're looking for a specific mount. In this case
511 * the path is considered a mountpoint if it is itself a
512 * mountpoint or contains a mount, such as a multi-mount
513 * without a root mount. In this case we return 1 if the
514 * path is a mount point and the super magic of the covering
515 * mount if there is one or 0 if it isn't a mountpoint.
516 *
517 * If we aren't supplied with a file descriptor then we
518 * lookup the path and check if it is the root of a mount.
519 * If a type is given we are looking for a particular autofs
520 * mount and if we don't find a match we return fail. If the
521 * located path is the root of a mount we return 1 along with
522 * the super magic of the mount or 0 otherwise.
523 *
524 * In both cases the the device number (as returned by
525 * new_encode_dev()) is also returned.
526 */
527static int autofs_dev_ioctl_ismountpoint(struct file *fp,
528 struct autofs_sb_info *sbi,
529 struct autofs_dev_ioctl *param)
530{
531 struct path path;
532 const char *name;
533 unsigned int type;
534 unsigned int devid, magic;
535 int err = -ENOENT;
536
537 if (param->size <= AUTOFS_DEV_IOCTL_SIZE) {
538 err = -EINVAL;
539 goto out;
540 }
541
542 name = param->path;
543 type = param->ismountpoint.in.type;
544
545 param->ismountpoint.out.devid = devid = 0;
546 param->ismountpoint.out.magic = magic = 0;
547
548 if (!fp || param->ioctlfd == -1) {
549 if (autofs_type_any(type))
550 err = kern_path_mountpoint(AT_FDCWD,
551 name, &path, LOOKUP_FOLLOW);
552 else
553 err = find_autofs_mount(name, &path,
554 test_by_type, &type);
555 if (err)
556 goto out;
557 devid = new_encode_dev(path.dentry->d_sb->s_dev);
558 err = 0;
559 if (path.mnt->mnt_root == path.dentry) {
560 err = 1;
561 magic = path.dentry->d_sb->s_magic;
562 }
563 } else {
564 dev_t dev = sbi->sb->s_dev;
565
566 err = find_autofs_mount(name, &path, test_by_dev, &dev);
567 if (err)
568 goto out;
569
570 devid = new_encode_dev(dev);
571
572 err = path_has_submounts(&path);
573
574 if (follow_down_one(&path))
575 magic = path.dentry->d_sb->s_magic;
576 }
577
578 param->ismountpoint.out.devid = devid;
579 param->ismountpoint.out.magic = magic;
580 path_put(&path);
581out:
582 return err;
583}
584
585/*
586 * Our range of ioctl numbers isn't 0 based so we need to shift
587 * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
588 * lookup.
589 */
590#define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
591
592static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
593{
594 static ioctl_fn _ioctls[] = {
595 autofs_dev_ioctl_version,
596 autofs_dev_ioctl_protover,
597 autofs_dev_ioctl_protosubver,
598 autofs_dev_ioctl_openmount,
599 autofs_dev_ioctl_closemount,
600 autofs_dev_ioctl_ready,
601 autofs_dev_ioctl_fail,
602 autofs_dev_ioctl_setpipefd,
603 autofs_dev_ioctl_catatonic,
604 autofs_dev_ioctl_timeout,
605 autofs_dev_ioctl_requester,
606 autofs_dev_ioctl_expire,
607 autofs_dev_ioctl_askumount,
608 autofs_dev_ioctl_ismountpoint,
609 };
610 unsigned int idx = cmd_idx(cmd);
611
612 return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx];
613}
614
615/* ioctl dispatcher */
616static int _autofs_dev_ioctl(unsigned int command,
617 struct autofs_dev_ioctl __user *user)
618{
619 struct autofs_dev_ioctl *param;
620 struct file *fp;
621 struct autofs_sb_info *sbi;
622 unsigned int cmd_first, cmd;
623 ioctl_fn fn = NULL;
624 int err = 0;
625
626 cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
627 cmd = _IOC_NR(command);
628
629 if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
630 cmd - cmd_first > AUTOFS_DEV_IOCTL_IOC_COUNT) {
631 return -ENOTTY;
632 }
633
634 /* Only root can use ioctls other than AUTOFS_DEV_IOCTL_VERSION_CMD
635 * and AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
636 */
637 if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
638 cmd != AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD &&
639 !capable(CAP_SYS_ADMIN))
640 return -EPERM;
641
642 /* Copy the parameters into kernel space. */
643 param = copy_dev_ioctl(user);
644 if (IS_ERR(param))
645 return PTR_ERR(param);
646
647 err = validate_dev_ioctl(command, param);
648 if (err)
649 goto out;
650
651 fn = lookup_dev_ioctl(cmd);
652 if (!fn) {
653 pr_warn("unknown command 0x%08x\n", command);
654 err = -ENOTTY;
655 goto out;
656 }
657
658 fp = NULL;
659 sbi = NULL;
660
661 /*
662 * For obvious reasons the openmount can't have a file
663 * descriptor yet. We don't take a reference to the
664 * file during close to allow for immediate release,
665 * and the same for retrieving ioctl version.
666 */
667 if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
668 cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
669 cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
670 fp = fget(param->ioctlfd);
671 if (!fp) {
672 if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
673 goto cont;
674 err = -EBADF;
675 goto out;
676 }
677
678 sbi = autofs_dev_ioctl_sbi(fp);
679 if (!sbi || sbi->magic != AUTOFS_SBI_MAGIC) {
680 err = -EINVAL;
681 fput(fp);
682 goto out;
683 }
684
685 /*
686 * Admin needs to be able to set the mount catatonic in
687 * order to be able to perform the re-open.
688 */
689 if (!autofs_oz_mode(sbi) &&
690 cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
691 err = -EACCES;
692 fput(fp);
693 goto out;
694 }
695 }
696cont:
697 err = fn(fp, sbi, param);
698
699 if (fp)
700 fput(fp);
701 if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
702 err = -EFAULT;
703out:
704 free_dev_ioctl(param);
705 return err;
706}
707
708static long autofs_dev_ioctl(struct file *file, unsigned int command,
709 unsigned long u)
710{
711 int err;
712
713 err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
714 return (long) err;
715}
716
717#ifdef CONFIG_COMPAT
718static long autofs_dev_ioctl_compat(struct file *file, unsigned int command,
719 unsigned long u)
720{
721 return autofs_dev_ioctl(file, command, (unsigned long) compat_ptr(u));
722}
723#else
724#define autofs_dev_ioctl_compat NULL
725#endif
726
727static const struct file_operations _dev_ioctl_fops = {
728 .unlocked_ioctl = autofs_dev_ioctl,
729 .compat_ioctl = autofs_dev_ioctl_compat,
730 .owner = THIS_MODULE,
731 .llseek = noop_llseek,
732};
733
734static struct miscdevice _autofs_dev_ioctl_misc = {
735 .minor = AUTOFS_MINOR,
736 .name = AUTOFS_DEVICE_NAME,
737 .fops = &_dev_ioctl_fops,
738 .mode = 0644,
739};
740
741MODULE_ALIAS_MISCDEV(AUTOFS_MINOR);
742MODULE_ALIAS("devname:autofs");
743
744/* Register/deregister misc character device */
745int __init autofs_dev_ioctl_init(void)
746{
747 int r;
748
749 r = misc_register(&_autofs_dev_ioctl_misc);
750 if (r) {
751 pr_err("misc_register failed for control device\n");
752 return r;
753 }
754
755 return 0;
756}
757
758void autofs_dev_ioctl_exit(void)
759{
760 misc_deregister(&_autofs_dev_ioctl_misc);
761}
diff --git a/fs/autofs/expire.c b/fs/autofs/expire.c
new file mode 100644
index 000000000000..b332d3f6e730
--- /dev/null
+++ b/fs/autofs/expire.c
@@ -0,0 +1,631 @@
1/*
2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
4 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
5 *
6 * This file is part of the Linux kernel and is made available under
7 * the terms of the GNU General Public License, version 2, or at your
8 * option, any later version, incorporated herein by reference.
9 */
10
11#include "autofs_i.h"
12
13static unsigned long now;
14
15/* Check if a dentry can be expired */
16static inline int autofs_can_expire(struct dentry *dentry,
17 unsigned long timeout, int do_now)
18{
19 struct autofs_info *ino = autofs_dentry_ino(dentry);
20
21 /* dentry in the process of being deleted */
22 if (ino == NULL)
23 return 0;
24
25 if (!do_now) {
26 /* Too young to die */
27 if (!timeout || time_after(ino->last_used + timeout, now))
28 return 0;
29 }
30 return 1;
31}
32
33/* Check a mount point for busyness */
34static int autofs_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
35{
36 struct dentry *top = dentry;
37 struct path path = {.mnt = mnt, .dentry = dentry};
38 int status = 1;
39
40 pr_debug("dentry %p %pd\n", dentry, dentry);
41
42 path_get(&path);
43
44 if (!follow_down_one(&path))
45 goto done;
46
47 if (is_autofs_dentry(path.dentry)) {
48 struct autofs_sb_info *sbi = autofs_sbi(path.dentry->d_sb);
49
50 /* This is an autofs submount, we can't expire it */
51 if (autofs_type_indirect(sbi->type))
52 goto done;
53 }
54
55 /* Update the expiry counter if fs is busy */
56 if (!may_umount_tree(path.mnt)) {
57 struct autofs_info *ino;
58
59 ino = autofs_dentry_ino(top);
60 ino->last_used = jiffies;
61 goto done;
62 }
63
64 status = 0;
65done:
66 pr_debug("returning = %d\n", status);
67 path_put(&path);
68 return status;
69}
70
71/*
72 * Calculate and dget next entry in the subdirs list under root.
73 */
74static struct dentry *get_next_positive_subdir(struct dentry *prev,
75 struct dentry *root)
76{
77 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
78 struct list_head *next;
79 struct dentry *q;
80
81 spin_lock(&sbi->lookup_lock);
82 spin_lock(&root->d_lock);
83
84 if (prev)
85 next = prev->d_child.next;
86 else {
87 prev = dget_dlock(root);
88 next = prev->d_subdirs.next;
89 }
90
91cont:
92 if (next == &root->d_subdirs) {
93 spin_unlock(&root->d_lock);
94 spin_unlock(&sbi->lookup_lock);
95 dput(prev);
96 return NULL;
97 }
98
99 q = list_entry(next, struct dentry, d_child);
100
101 spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
102 /* Already gone or negative dentry (under construction) - try next */
103 if (!d_count(q) || !simple_positive(q)) {
104 spin_unlock(&q->d_lock);
105 next = q->d_child.next;
106 goto cont;
107 }
108 dget_dlock(q);
109 spin_unlock(&q->d_lock);
110 spin_unlock(&root->d_lock);
111 spin_unlock(&sbi->lookup_lock);
112
113 dput(prev);
114
115 return q;
116}
117
118/*
119 * Calculate and dget next entry in top down tree traversal.
120 */
121static struct dentry *get_next_positive_dentry(struct dentry *prev,
122 struct dentry *root)
123{
124 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
125 struct list_head *next;
126 struct dentry *p, *ret;
127
128 if (prev == NULL)
129 return dget(root);
130
131 spin_lock(&sbi->lookup_lock);
132relock:
133 p = prev;
134 spin_lock(&p->d_lock);
135again:
136 next = p->d_subdirs.next;
137 if (next == &p->d_subdirs) {
138 while (1) {
139 struct dentry *parent;
140
141 if (p == root) {
142 spin_unlock(&p->d_lock);
143 spin_unlock(&sbi->lookup_lock);
144 dput(prev);
145 return NULL;
146 }
147
148 parent = p->d_parent;
149 if (!spin_trylock(&parent->d_lock)) {
150 spin_unlock(&p->d_lock);
151 cpu_relax();
152 goto relock;
153 }
154 spin_unlock(&p->d_lock);
155 next = p->d_child.next;
156 p = parent;
157 if (next != &parent->d_subdirs)
158 break;
159 }
160 }
161 ret = list_entry(next, struct dentry, d_child);
162
163 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
164 /* Negative dentry - try next */
165 if (!simple_positive(ret)) {
166 spin_unlock(&p->d_lock);
167 lock_set_subclass(&ret->d_lock.dep_map, 0, _RET_IP_);
168 p = ret;
169 goto again;
170 }
171 dget_dlock(ret);
172 spin_unlock(&ret->d_lock);
173 spin_unlock(&p->d_lock);
174 spin_unlock(&sbi->lookup_lock);
175
176 dput(prev);
177
178 return ret;
179}
180
181/*
182 * Check a direct mount point for busyness.
183 * Direct mounts have similar expiry semantics to tree mounts.
184 * The tree is not busy iff no mountpoints are busy and there are no
185 * autofs submounts.
186 */
187static int autofs_direct_busy(struct vfsmount *mnt,
188 struct dentry *top,
189 unsigned long timeout,
190 int do_now)
191{
192 pr_debug("top %p %pd\n", top, top);
193
194 /* If it's busy update the expiry counters */
195 if (!may_umount_tree(mnt)) {
196 struct autofs_info *ino;
197
198 ino = autofs_dentry_ino(top);
199 if (ino)
200 ino->last_used = jiffies;
201 return 1;
202 }
203
204 /* Timeout of a direct mount is determined by its top dentry */
205 if (!autofs_can_expire(top, timeout, do_now))
206 return 1;
207
208 return 0;
209}
210
211/*
212 * Check a directory tree of mount points for busyness
213 * The tree is not busy iff no mountpoints are busy
214 */
215static int autofs_tree_busy(struct vfsmount *mnt,
216 struct dentry *top,
217 unsigned long timeout,
218 int do_now)
219{
220 struct autofs_info *top_ino = autofs_dentry_ino(top);
221 struct dentry *p;
222
223 pr_debug("top %p %pd\n", top, top);
224
225 /* Negative dentry - give up */
226 if (!simple_positive(top))
227 return 1;
228
229 p = NULL;
230 while ((p = get_next_positive_dentry(p, top))) {
231 pr_debug("dentry %p %pd\n", p, p);
232
233 /*
234 * Is someone visiting anywhere in the subtree ?
235 * If there's no mount we need to check the usage
236 * count for the autofs dentry.
237 * If the fs is busy update the expiry counter.
238 */
239 if (d_mountpoint(p)) {
240 if (autofs_mount_busy(mnt, p)) {
241 top_ino->last_used = jiffies;
242 dput(p);
243 return 1;
244 }
245 } else {
246 struct autofs_info *ino = autofs_dentry_ino(p);
247 unsigned int ino_count = atomic_read(&ino->count);
248
249 /* allow for dget above and top is already dgot */
250 if (p == top)
251 ino_count += 2;
252 else
253 ino_count++;
254
255 if (d_count(p) > ino_count) {
256 top_ino->last_used = jiffies;
257 dput(p);
258 return 1;
259 }
260 }
261 }
262
263 /* Timeout of a tree mount is ultimately determined by its top dentry */
264 if (!autofs_can_expire(top, timeout, do_now))
265 return 1;
266
267 return 0;
268}
269
270static struct dentry *autofs_check_leaves(struct vfsmount *mnt,
271 struct dentry *parent,
272 unsigned long timeout,
273 int do_now)
274{
275 struct dentry *p;
276
277 pr_debug("parent %p %pd\n", parent, parent);
278
279 p = NULL;
280 while ((p = get_next_positive_dentry(p, parent))) {
281 pr_debug("dentry %p %pd\n", p, p);
282
283 if (d_mountpoint(p)) {
284 /* Can we umount this guy */
285 if (autofs_mount_busy(mnt, p))
286 continue;
287
288 /* Can we expire this guy */
289 if (autofs_can_expire(p, timeout, do_now))
290 return p;
291 }
292 }
293 return NULL;
294}
295
296/* Check if we can expire a direct mount (possibly a tree) */
297struct dentry *autofs_expire_direct(struct super_block *sb,
298 struct vfsmount *mnt,
299 struct autofs_sb_info *sbi,
300 int how)
301{
302 unsigned long timeout;
303 struct dentry *root = dget(sb->s_root);
304 int do_now = how & AUTOFS_EXP_IMMEDIATE;
305 struct autofs_info *ino;
306
307 if (!root)
308 return NULL;
309
310 now = jiffies;
311 timeout = sbi->exp_timeout;
312
313 if (!autofs_direct_busy(mnt, root, timeout, do_now)) {
314 spin_lock(&sbi->fs_lock);
315 ino = autofs_dentry_ino(root);
316 /* No point expiring a pending mount */
317 if (ino->flags & AUTOFS_INF_PENDING) {
318 spin_unlock(&sbi->fs_lock);
319 goto out;
320 }
321 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
322 spin_unlock(&sbi->fs_lock);
323 synchronize_rcu();
324 if (!autofs_direct_busy(mnt, root, timeout, do_now)) {
325 spin_lock(&sbi->fs_lock);
326 ino->flags |= AUTOFS_INF_EXPIRING;
327 init_completion(&ino->expire_complete);
328 spin_unlock(&sbi->fs_lock);
329 return root;
330 }
331 spin_lock(&sbi->fs_lock);
332 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
333 spin_unlock(&sbi->fs_lock);
334 }
335out:
336 dput(root);
337
338 return NULL;
339}
340
341/* Check if 'dentry' should expire, or return a nearby
342 * dentry that is suitable.
343 * If returned dentry is different from arg dentry,
344 * then a dget() reference was taken, else not.
345 */
346static struct dentry *should_expire(struct dentry *dentry,
347 struct vfsmount *mnt,
348 unsigned long timeout,
349 int how)
350{
351 int do_now = how & AUTOFS_EXP_IMMEDIATE;
352 int exp_leaves = how & AUTOFS_EXP_LEAVES;
353 struct autofs_info *ino = autofs_dentry_ino(dentry);
354 unsigned int ino_count;
355
356 /* No point expiring a pending mount */
357 if (ino->flags & AUTOFS_INF_PENDING)
358 return NULL;
359
360 /*
361 * Case 1: (i) indirect mount or top level pseudo direct mount
362 * (autofs-4.1).
363 * (ii) indirect mount with offset mount, check the "/"
364 * offset (autofs-5.0+).
365 */
366 if (d_mountpoint(dentry)) {
367 pr_debug("checking mountpoint %p %pd\n", dentry, dentry);
368
369 /* Can we umount this guy */
370 if (autofs_mount_busy(mnt, dentry))
371 return NULL;
372
373 /* Can we expire this guy */
374 if (autofs_can_expire(dentry, timeout, do_now))
375 return dentry;
376 return NULL;
377 }
378
379 if (d_really_is_positive(dentry) && d_is_symlink(dentry)) {
380 pr_debug("checking symlink %p %pd\n", dentry, dentry);
381 /*
382 * A symlink can't be "busy" in the usual sense so
383 * just check last used for expire timeout.
384 */
385 if (autofs_can_expire(dentry, timeout, do_now))
386 return dentry;
387 return NULL;
388 }
389
390 if (simple_empty(dentry))
391 return NULL;
392
393 /* Case 2: tree mount, expire iff entire tree is not busy */
394 if (!exp_leaves) {
395 /* Path walk currently on this dentry? */
396 ino_count = atomic_read(&ino->count) + 1;
397 if (d_count(dentry) > ino_count)
398 return NULL;
399
400 if (!autofs_tree_busy(mnt, dentry, timeout, do_now))
401 return dentry;
402 /*
403 * Case 3: pseudo direct mount, expire individual leaves
404 * (autofs-4.1).
405 */
406 } else {
407 /* Path walk currently on this dentry? */
408 struct dentry *expired;
409
410 ino_count = atomic_read(&ino->count) + 1;
411 if (d_count(dentry) > ino_count)
412 return NULL;
413
414 expired = autofs_check_leaves(mnt, dentry, timeout, do_now);
415 if (expired) {
416 if (expired == dentry)
417 dput(dentry);
418 return expired;
419 }
420 }
421 return NULL;
422}
423
424/*
425 * Find an eligible tree to time-out
426 * A tree is eligible if :-
427 * - it is unused by any user process
428 * - it has been unused for exp_timeout time
429 */
430struct dentry *autofs_expire_indirect(struct super_block *sb,
431 struct vfsmount *mnt,
432 struct autofs_sb_info *sbi,
433 int how)
434{
435 unsigned long timeout;
436 struct dentry *root = sb->s_root;
437 struct dentry *dentry;
438 struct dentry *expired;
439 struct dentry *found;
440 struct autofs_info *ino;
441
442 if (!root)
443 return NULL;
444
445 now = jiffies;
446 timeout = sbi->exp_timeout;
447
448 dentry = NULL;
449 while ((dentry = get_next_positive_subdir(dentry, root))) {
450 int flags = how;
451
452 spin_lock(&sbi->fs_lock);
453 ino = autofs_dentry_ino(dentry);
454 if (ino->flags & AUTOFS_INF_WANT_EXPIRE) {
455 spin_unlock(&sbi->fs_lock);
456 continue;
457 }
458 spin_unlock(&sbi->fs_lock);
459
460 expired = should_expire(dentry, mnt, timeout, flags);
461 if (!expired)
462 continue;
463
464 spin_lock(&sbi->fs_lock);
465 ino = autofs_dentry_ino(expired);
466 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
467 spin_unlock(&sbi->fs_lock);
468 synchronize_rcu();
469
470 /* Make sure a reference is not taken on found if
471 * things have changed.
472 */
473 flags &= ~AUTOFS_EXP_LEAVES;
474 found = should_expire(expired, mnt, timeout, how);
475 if (!found || found != expired)
476 /* Something has changed, continue */
477 goto next;
478
479 if (expired != dentry)
480 dput(dentry);
481
482 spin_lock(&sbi->fs_lock);
483 goto found;
484next:
485 spin_lock(&sbi->fs_lock);
486 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
487 spin_unlock(&sbi->fs_lock);
488 if (expired != dentry)
489 dput(expired);
490 }
491 return NULL;
492
493found:
494 pr_debug("returning %p %pd\n", expired, expired);
495 ino->flags |= AUTOFS_INF_EXPIRING;
496 init_completion(&ino->expire_complete);
497 spin_unlock(&sbi->fs_lock);
498 return expired;
499}
500
501int autofs_expire_wait(const struct path *path, int rcu_walk)
502{
503 struct dentry *dentry = path->dentry;
504 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
505 struct autofs_info *ino = autofs_dentry_ino(dentry);
506 int status;
507 int state;
508
509 /* Block on any pending expire */
510 if (!(ino->flags & AUTOFS_INF_WANT_EXPIRE))
511 return 0;
512 if (rcu_walk)
513 return -ECHILD;
514
515retry:
516 spin_lock(&sbi->fs_lock);
517 state = ino->flags & (AUTOFS_INF_WANT_EXPIRE | AUTOFS_INF_EXPIRING);
518 if (state == AUTOFS_INF_WANT_EXPIRE) {
519 spin_unlock(&sbi->fs_lock);
520 /*
521 * Possibly being selected for expire, wait until
522 * it's selected or not.
523 */
524 schedule_timeout_uninterruptible(HZ/10);
525 goto retry;
526 }
527 if (state & AUTOFS_INF_EXPIRING) {
528 spin_unlock(&sbi->fs_lock);
529
530 pr_debug("waiting for expire %p name=%pd\n", dentry, dentry);
531
532 status = autofs_wait(sbi, path, NFY_NONE);
533 wait_for_completion(&ino->expire_complete);
534
535 pr_debug("expire done status=%d\n", status);
536
537 if (d_unhashed(dentry))
538 return -EAGAIN;
539
540 return status;
541 }
542 spin_unlock(&sbi->fs_lock);
543
544 return 0;
545}
546
547/* Perform an expiry operation */
548int autofs_expire_run(struct super_block *sb,
549 struct vfsmount *mnt,
550 struct autofs_sb_info *sbi,
551 struct autofs_packet_expire __user *pkt_p)
552{
553 struct autofs_packet_expire pkt;
554 struct autofs_info *ino;
555 struct dentry *dentry;
556 int ret = 0;
557
558 memset(&pkt, 0, sizeof(pkt));
559
560 pkt.hdr.proto_version = sbi->version;
561 pkt.hdr.type = autofs_ptype_expire;
562
563 dentry = autofs_expire_indirect(sb, mnt, sbi, 0);
564 if (!dentry)
565 return -EAGAIN;
566
567 pkt.len = dentry->d_name.len;
568 memcpy(pkt.name, dentry->d_name.name, pkt.len);
569 pkt.name[pkt.len] = '\0';
570 dput(dentry);
571
572 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
573 ret = -EFAULT;
574
575 spin_lock(&sbi->fs_lock);
576 ino = autofs_dentry_ino(dentry);
577 /* avoid rapid-fire expire attempts if expiry fails */
578 ino->last_used = now;
579 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
580 complete_all(&ino->expire_complete);
581 spin_unlock(&sbi->fs_lock);
582
583 return ret;
584}
585
586int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
587 struct autofs_sb_info *sbi, int when)
588{
589 struct dentry *dentry;
590 int ret = -EAGAIN;
591
592 if (autofs_type_trigger(sbi->type))
593 dentry = autofs_expire_direct(sb, mnt, sbi, when);
594 else
595 dentry = autofs_expire_indirect(sb, mnt, sbi, when);
596
597 if (dentry) {
598 struct autofs_info *ino = autofs_dentry_ino(dentry);
599 const struct path path = { .mnt = mnt, .dentry = dentry };
600
601 /* This is synchronous because it makes the daemon a
602 * little easier
603 */
604 ret = autofs_wait(sbi, &path, NFY_EXPIRE);
605
606 spin_lock(&sbi->fs_lock);
607 /* avoid rapid-fire expire attempts if expiry fails */
608 ino->last_used = now;
609 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
610 complete_all(&ino->expire_complete);
611 spin_unlock(&sbi->fs_lock);
612 dput(dentry);
613 }
614
615 return ret;
616}
617
618/*
619 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
620 * more to be done.
621 */
622int autofs_expire_multi(struct super_block *sb, struct vfsmount *mnt,
623 struct autofs_sb_info *sbi, int __user *arg)
624{
625 int do_now = 0;
626
627 if (arg && get_user(do_now, arg))
628 return -EFAULT;
629
630 return autofs_do_expire_multi(sb, mnt, sbi, do_now);
631}
diff --git a/fs/autofs/init.c b/fs/autofs/init.c
new file mode 100644
index 000000000000..16fb61315843
--- /dev/null
+++ b/fs/autofs/init.c
@@ -0,0 +1,48 @@
1/*
2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 *
4 * This file is part of the Linux kernel and is made available under
5 * the terms of the GNU General Public License, version 2, or at your
6 * option, any later version, incorporated herein by reference.
7 */
8
9#include <linux/module.h>
10#include <linux/init.h>
11#include "autofs_i.h"
12
13static struct dentry *autofs_mount(struct file_system_type *fs_type,
14 int flags, const char *dev_name, void *data)
15{
16 return mount_nodev(fs_type, flags, data, autofs_fill_super);
17}
18
19static struct file_system_type autofs_fs_type = {
20 .owner = THIS_MODULE,
21 .name = "autofs",
22 .mount = autofs_mount,
23 .kill_sb = autofs_kill_sb,
24};
25MODULE_ALIAS_FS("autofs");
26
27static int __init init_autofs_fs(void)
28{
29 int err;
30
31 autofs_dev_ioctl_init();
32
33 err = register_filesystem(&autofs_fs_type);
34 if (err)
35 autofs_dev_ioctl_exit();
36
37 return err;
38}
39
40static void __exit exit_autofs_fs(void)
41{
42 autofs_dev_ioctl_exit();
43 unregister_filesystem(&autofs_fs_type);
44}
45
46module_init(init_autofs_fs)
47module_exit(exit_autofs_fs)
48MODULE_LICENSE("GPL");
diff --git a/fs/autofs/inode.c b/fs/autofs/inode.c
new file mode 100644
index 000000000000..6262819ede45
--- /dev/null
+++ b/fs/autofs/inode.c
@@ -0,0 +1,375 @@
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#include <linux/kernel.h>
11#include <linux/slab.h>
12#include <linux/file.h>
13#include <linux/seq_file.h>
14#include <linux/pagemap.h>
15#include <linux/parser.h>
16#include <linux/bitops.h>
17#include <linux/magic.h>
18#include "autofs_i.h"
19#include <linux/module.h>
20
21struct autofs_info *autofs_new_ino(struct autofs_sb_info *sbi)
22{
23 struct autofs_info *ino;
24
25 ino = kzalloc(sizeof(*ino), GFP_KERNEL);
26 if (ino) {
27 INIT_LIST_HEAD(&ino->active);
28 INIT_LIST_HEAD(&ino->expiring);
29 ino->last_used = jiffies;
30 ino->sbi = sbi;
31 }
32 return ino;
33}
34
35void autofs_clean_ino(struct autofs_info *ino)
36{
37 ino->uid = GLOBAL_ROOT_UID;
38 ino->gid = GLOBAL_ROOT_GID;
39 ino->last_used = jiffies;
40}
41
42void autofs_free_ino(struct autofs_info *ino)
43{
44 kfree(ino);
45}
46
47void autofs_kill_sb(struct super_block *sb)
48{
49 struct autofs_sb_info *sbi = autofs_sbi(sb);
50
51 /*
52 * In the event of a failure in get_sb_nodev the superblock
53 * info is not present so nothing else has been setup, so
54 * just call kill_anon_super when we are called from
55 * deactivate_super.
56 */
57 if (sbi) {
58 /* Free wait queues, close pipe */
59 autofs_catatonic_mode(sbi);
60 put_pid(sbi->oz_pgrp);
61 }
62
63 pr_debug("shutting down\n");
64 kill_litter_super(sb);
65 if (sbi)
66 kfree_rcu(sbi, rcu);
67}
68
69static int autofs_show_options(struct seq_file *m, struct dentry *root)
70{
71 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
72 struct inode *root_inode = d_inode(root->d_sb->s_root);
73
74 if (!sbi)
75 return 0;
76
77 seq_printf(m, ",fd=%d", sbi->pipefd);
78 if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID))
79 seq_printf(m, ",uid=%u",
80 from_kuid_munged(&init_user_ns, root_inode->i_uid));
81 if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID))
82 seq_printf(m, ",gid=%u",
83 from_kgid_munged(&init_user_ns, root_inode->i_gid));
84 seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp));
85 seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
86 seq_printf(m, ",minproto=%d", sbi->min_proto);
87 seq_printf(m, ",maxproto=%d", sbi->max_proto);
88
89 if (autofs_type_offset(sbi->type))
90 seq_printf(m, ",offset");
91 else if (autofs_type_direct(sbi->type))
92 seq_printf(m, ",direct");
93 else
94 seq_printf(m, ",indirect");
95#ifdef CONFIG_CHECKPOINT_RESTORE
96 if (sbi->pipe)
97 seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino);
98 else
99 seq_printf(m, ",pipe_ino=-1");
100#endif
101 return 0;
102}
103
104static void autofs_evict_inode(struct inode *inode)
105{
106 clear_inode(inode);
107 kfree(inode->i_private);
108}
109
110static const struct super_operations autofs_sops = {
111 .statfs = simple_statfs,
112 .show_options = autofs_show_options,
113 .evict_inode = autofs_evict_inode,
114};
115
116enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
117 Opt_indirect, Opt_direct, Opt_offset};
118
119static const match_table_t tokens = {
120 {Opt_fd, "fd=%u"},
121 {Opt_uid, "uid=%u"},
122 {Opt_gid, "gid=%u"},
123 {Opt_pgrp, "pgrp=%u"},
124 {Opt_minproto, "minproto=%u"},
125 {Opt_maxproto, "maxproto=%u"},
126 {Opt_indirect, "indirect"},
127 {Opt_direct, "direct"},
128 {Opt_offset, "offset"},
129 {Opt_err, NULL}
130};
131
132static int parse_options(char *options, int *pipefd, kuid_t *uid, kgid_t *gid,
133 int *pgrp, bool *pgrp_set, unsigned int *type,
134 int *minproto, int *maxproto)
135{
136 char *p;
137 substring_t args[MAX_OPT_ARGS];
138 int option;
139
140 *uid = current_uid();
141 *gid = current_gid();
142
143 *minproto = AUTOFS_MIN_PROTO_VERSION;
144 *maxproto = AUTOFS_MAX_PROTO_VERSION;
145
146 *pipefd = -1;
147
148 if (!options)
149 return 1;
150
151 while ((p = strsep(&options, ",")) != NULL) {
152 int token;
153
154 if (!*p)
155 continue;
156
157 token = match_token(p, tokens, args);
158 switch (token) {
159 case Opt_fd:
160 if (match_int(args, pipefd))
161 return 1;
162 break;
163 case Opt_uid:
164 if (match_int(args, &option))
165 return 1;
166 *uid = make_kuid(current_user_ns(), option);
167 if (!uid_valid(*uid))
168 return 1;
169 break;
170 case Opt_gid:
171 if (match_int(args, &option))
172 return 1;
173 *gid = make_kgid(current_user_ns(), option);
174 if (!gid_valid(*gid))
175 return 1;
176 break;
177 case Opt_pgrp:
178 if (match_int(args, &option))
179 return 1;
180 *pgrp = option;
181 *pgrp_set = true;
182 break;
183 case Opt_minproto:
184 if (match_int(args, &option))
185 return 1;
186 *minproto = option;
187 break;
188 case Opt_maxproto:
189 if (match_int(args, &option))
190 return 1;
191 *maxproto = option;
192 break;
193 case Opt_indirect:
194 set_autofs_type_indirect(type);
195 break;
196 case Opt_direct:
197 set_autofs_type_direct(type);
198 break;
199 case Opt_offset:
200 set_autofs_type_offset(type);
201 break;
202 default:
203 return 1;
204 }
205 }
206 return (*pipefd < 0);
207}
208
209int autofs_fill_super(struct super_block *s, void *data, int silent)
210{
211 struct inode *root_inode;
212 struct dentry *root;
213 struct file *pipe;
214 int pipefd;
215 struct autofs_sb_info *sbi;
216 struct autofs_info *ino;
217 int pgrp = 0;
218 bool pgrp_set = false;
219 int ret = -EINVAL;
220
221 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
222 if (!sbi)
223 return -ENOMEM;
224 pr_debug("starting up, sbi = %p\n", sbi);
225
226 s->s_fs_info = sbi;
227 sbi->magic = AUTOFS_SBI_MAGIC;
228 sbi->pipefd = -1;
229 sbi->pipe = NULL;
230 sbi->catatonic = 1;
231 sbi->exp_timeout = 0;
232 sbi->oz_pgrp = NULL;
233 sbi->sb = s;
234 sbi->version = 0;
235 sbi->sub_version = 0;
236 set_autofs_type_indirect(&sbi->type);
237 sbi->min_proto = 0;
238 sbi->max_proto = 0;
239 mutex_init(&sbi->wq_mutex);
240 mutex_init(&sbi->pipe_mutex);
241 spin_lock_init(&sbi->fs_lock);
242 sbi->queues = NULL;
243 spin_lock_init(&sbi->lookup_lock);
244 INIT_LIST_HEAD(&sbi->active_list);
245 INIT_LIST_HEAD(&sbi->expiring_list);
246 s->s_blocksize = 1024;
247 s->s_blocksize_bits = 10;
248 s->s_magic = AUTOFS_SUPER_MAGIC;
249 s->s_op = &autofs_sops;
250 s->s_d_op = &autofs_dentry_operations;
251 s->s_time_gran = 1;
252
253 /*
254 * Get the root inode and dentry, but defer checking for errors.
255 */
256 ino = autofs_new_ino(sbi);
257 if (!ino) {
258 ret = -ENOMEM;
259 goto fail_free;
260 }
261 root_inode = autofs_get_inode(s, S_IFDIR | 0755);
262 root = d_make_root(root_inode);
263 if (!root)
264 goto fail_ino;
265 pipe = NULL;
266
267 root->d_fsdata = ino;
268
269 /* Can this call block? */
270 if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
271 &pgrp, &pgrp_set, &sbi->type, &sbi->min_proto,
272 &sbi->max_proto)) {
273 pr_err("called with bogus options\n");
274 goto fail_dput;
275 }
276
277 /* Test versions first */
278 if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
279 sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
280 pr_err("kernel does not match daemon version "
281 "daemon (%d, %d) kernel (%d, %d)\n",
282 sbi->min_proto, sbi->max_proto,
283 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
284 goto fail_dput;
285 }
286
287 /* Establish highest kernel protocol version */
288 if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
289 sbi->version = AUTOFS_MAX_PROTO_VERSION;
290 else
291 sbi->version = sbi->max_proto;
292 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
293
294 if (pgrp_set) {
295 sbi->oz_pgrp = find_get_pid(pgrp);
296 if (!sbi->oz_pgrp) {
297 pr_err("could not find process group %d\n",
298 pgrp);
299 goto fail_dput;
300 }
301 } else {
302 sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
303 }
304
305 if (autofs_type_trigger(sbi->type))
306 __managed_dentry_set_managed(root);
307
308 root_inode->i_fop = &autofs_root_operations;
309 root_inode->i_op = &autofs_dir_inode_operations;
310
311 pr_debug("pipe fd = %d, pgrp = %u\n", pipefd, pid_nr(sbi->oz_pgrp));
312 pipe = fget(pipefd);
313
314 if (!pipe) {
315 pr_err("could not open pipe file descriptor\n");
316 goto fail_put_pid;
317 }
318 ret = autofs_prepare_pipe(pipe);
319 if (ret < 0)
320 goto fail_fput;
321 sbi->pipe = pipe;
322 sbi->pipefd = pipefd;
323 sbi->catatonic = 0;
324
325 /*
326 * Success! Install the root dentry now to indicate completion.
327 */
328 s->s_root = root;
329 return 0;
330
331 /*
332 * Failure ... clean up.
333 */
334fail_fput:
335 pr_err("pipe file descriptor does not contain proper ops\n");
336 fput(pipe);
337fail_put_pid:
338 put_pid(sbi->oz_pgrp);
339fail_dput:
340 dput(root);
341 goto fail_free;
342fail_ino:
343 autofs_free_ino(ino);
344fail_free:
345 kfree(sbi);
346 s->s_fs_info = NULL;
347 return ret;
348}
349
350struct inode *autofs_get_inode(struct super_block *sb, umode_t mode)
351{
352 struct inode *inode = new_inode(sb);
353
354 if (inode == NULL)
355 return NULL;
356
357 inode->i_mode = mode;
358 if (sb->s_root) {
359 inode->i_uid = d_inode(sb->s_root)->i_uid;
360 inode->i_gid = d_inode(sb->s_root)->i_gid;
361 }
362 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
363 inode->i_ino = get_next_ino();
364
365 if (S_ISDIR(mode)) {
366 set_nlink(inode, 2);
367 inode->i_op = &autofs_dir_inode_operations;
368 inode->i_fop = &autofs_dir_operations;
369 } else if (S_ISLNK(mode)) {
370 inode->i_op = &autofs_symlink_inode_operations;
371 } else
372 WARN_ON(1);
373
374 return inode;
375}
diff --git a/fs/autofs/root.c b/fs/autofs/root.c
new file mode 100644
index 000000000000..a4b36e44f73c
--- /dev/null
+++ b/fs/autofs/root.c
@@ -0,0 +1,942 @@
1/*
2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
4 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
5 *
6 * This file is part of the Linux kernel and is made available under
7 * the terms of the GNU General Public License, version 2, or at your
8 * option, any later version, incorporated herein by reference.
9 */
10
11#include <linux/capability.h>
12#include <linux/errno.h>
13#include <linux/stat.h>
14#include <linux/slab.h>
15#include <linux/param.h>
16#include <linux/time.h>
17#include <linux/compat.h>
18#include <linux/mutex.h>
19
20#include "autofs_i.h"
21
22static int autofs_dir_symlink(struct inode *, struct dentry *, const char *);
23static int autofs_dir_unlink(struct inode *, struct dentry *);
24static int autofs_dir_rmdir(struct inode *, struct dentry *);
25static int autofs_dir_mkdir(struct inode *, struct dentry *, umode_t);
26static long autofs_root_ioctl(struct file *, unsigned int, unsigned long);
27#ifdef CONFIG_COMPAT
28static long autofs_root_compat_ioctl(struct file *,
29 unsigned int, unsigned long);
30#endif
31static int autofs_dir_open(struct inode *inode, struct file *file);
32static struct dentry *autofs_lookup(struct inode *,
33 struct dentry *, unsigned int);
34static struct vfsmount *autofs_d_automount(struct path *);
35static int autofs_d_manage(const struct path *, bool);
36static void autofs_dentry_release(struct dentry *);
37
38const struct file_operations autofs_root_operations = {
39 .open = dcache_dir_open,
40 .release = dcache_dir_close,
41 .read = generic_read_dir,
42 .iterate_shared = dcache_readdir,
43 .llseek = dcache_dir_lseek,
44 .unlocked_ioctl = autofs_root_ioctl,
45#ifdef CONFIG_COMPAT
46 .compat_ioctl = autofs_root_compat_ioctl,
47#endif
48};
49
50const struct file_operations autofs_dir_operations = {
51 .open = autofs_dir_open,
52 .release = dcache_dir_close,
53 .read = generic_read_dir,
54 .iterate_shared = dcache_readdir,
55 .llseek = dcache_dir_lseek,
56};
57
58const struct inode_operations autofs_dir_inode_operations = {
59 .lookup = autofs_lookup,
60 .unlink = autofs_dir_unlink,
61 .symlink = autofs_dir_symlink,
62 .mkdir = autofs_dir_mkdir,
63 .rmdir = autofs_dir_rmdir,
64};
65
66const struct dentry_operations autofs_dentry_operations = {
67 .d_automount = autofs_d_automount,
68 .d_manage = autofs_d_manage,
69 .d_release = autofs_dentry_release,
70};
71
72static void autofs_add_active(struct dentry *dentry)
73{
74 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
75 struct autofs_info *ino;
76
77 ino = autofs_dentry_ino(dentry);
78 if (ino) {
79 spin_lock(&sbi->lookup_lock);
80 if (!ino->active_count) {
81 if (list_empty(&ino->active))
82 list_add(&ino->active, &sbi->active_list);
83 }
84 ino->active_count++;
85 spin_unlock(&sbi->lookup_lock);
86 }
87}
88
89static void autofs_del_active(struct dentry *dentry)
90{
91 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
92 struct autofs_info *ino;
93
94 ino = autofs_dentry_ino(dentry);
95 if (ino) {
96 spin_lock(&sbi->lookup_lock);
97 ino->active_count--;
98 if (!ino->active_count) {
99 if (!list_empty(&ino->active))
100 list_del_init(&ino->active);
101 }
102 spin_unlock(&sbi->lookup_lock);
103 }
104}
105
106static int autofs_dir_open(struct inode *inode, struct file *file)
107{
108 struct dentry *dentry = file->f_path.dentry;
109 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
110
111 pr_debug("file=%p dentry=%p %pd\n", file, dentry, dentry);
112
113 if (autofs_oz_mode(sbi))
114 goto out;
115
116 /*
117 * An empty directory in an autofs file system is always a
118 * mount point. The daemon must have failed to mount this
119 * during lookup so it doesn't exist. This can happen, for
120 * example, if user space returns an incorrect status for a
121 * mount request. Otherwise we're doing a readdir on the
122 * autofs file system so just let the libfs routines handle
123 * it.
124 */
125 spin_lock(&sbi->lookup_lock);
126 if (!path_is_mountpoint(&file->f_path) && simple_empty(dentry)) {
127 spin_unlock(&sbi->lookup_lock);
128 return -ENOENT;
129 }
130 spin_unlock(&sbi->lookup_lock);
131
132out:
133 return dcache_dir_open(inode, file);
134}
135
136static void autofs_dentry_release(struct dentry *de)
137{
138 struct autofs_info *ino = autofs_dentry_ino(de);
139 struct autofs_sb_info *sbi = autofs_sbi(de->d_sb);
140
141 pr_debug("releasing %p\n", de);
142
143 if (!ino)
144 return;
145
146 if (sbi) {
147 spin_lock(&sbi->lookup_lock);
148 if (!list_empty(&ino->active))
149 list_del(&ino->active);
150 if (!list_empty(&ino->expiring))
151 list_del(&ino->expiring);
152 spin_unlock(&sbi->lookup_lock);
153 }
154
155 autofs_free_ino(ino);
156}
157
158static struct dentry *autofs_lookup_active(struct dentry *dentry)
159{
160 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
161 struct dentry *parent = dentry->d_parent;
162 const struct qstr *name = &dentry->d_name;
163 unsigned int len = name->len;
164 unsigned int hash = name->hash;
165 const unsigned char *str = name->name;
166 struct list_head *p, *head;
167
168 head = &sbi->active_list;
169 if (list_empty(head))
170 return NULL;
171 spin_lock(&sbi->lookup_lock);
172 list_for_each(p, head) {
173 struct autofs_info *ino;
174 struct dentry *active;
175 const struct qstr *qstr;
176
177 ino = list_entry(p, struct autofs_info, active);
178 active = ino->dentry;
179
180 spin_lock(&active->d_lock);
181
182 /* Already gone? */
183 if ((int) d_count(active) <= 0)
184 goto next;
185
186 qstr = &active->d_name;
187
188 if (active->d_name.hash != hash)
189 goto next;
190 if (active->d_parent != parent)
191 goto next;
192
193 if (qstr->len != len)
194 goto next;
195 if (memcmp(qstr->name, str, len))
196 goto next;
197
198 if (d_unhashed(active)) {
199 dget_dlock(active);
200 spin_unlock(&active->d_lock);
201 spin_unlock(&sbi->lookup_lock);
202 return active;
203 }
204next:
205 spin_unlock(&active->d_lock);
206 }
207 spin_unlock(&sbi->lookup_lock);
208
209 return NULL;
210}
211
212static struct dentry *autofs_lookup_expiring(struct dentry *dentry,
213 bool rcu_walk)
214{
215 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
216 struct dentry *parent = dentry->d_parent;
217 const struct qstr *name = &dentry->d_name;
218 unsigned int len = name->len;
219 unsigned int hash = name->hash;
220 const unsigned char *str = name->name;
221 struct list_head *p, *head;
222
223 head = &sbi->expiring_list;
224 if (list_empty(head))
225 return NULL;
226 spin_lock(&sbi->lookup_lock);
227 list_for_each(p, head) {
228 struct autofs_info *ino;
229 struct dentry *expiring;
230 const struct qstr *qstr;
231
232 if (rcu_walk) {
233 spin_unlock(&sbi->lookup_lock);
234 return ERR_PTR(-ECHILD);
235 }
236
237 ino = list_entry(p, struct autofs_info, expiring);
238 expiring = ino->dentry;
239
240 spin_lock(&expiring->d_lock);
241
242 /* We've already been dentry_iput or unlinked */
243 if (d_really_is_negative(expiring))
244 goto next;
245
246 qstr = &expiring->d_name;
247
248 if (expiring->d_name.hash != hash)
249 goto next;
250 if (expiring->d_parent != parent)
251 goto next;
252
253 if (qstr->len != len)
254 goto next;
255 if (memcmp(qstr->name, str, len))
256 goto next;
257
258 if (d_unhashed(expiring)) {
259 dget_dlock(expiring);
260 spin_unlock(&expiring->d_lock);
261 spin_unlock(&sbi->lookup_lock);
262 return expiring;
263 }
264next:
265 spin_unlock(&expiring->d_lock);
266 }
267 spin_unlock(&sbi->lookup_lock);
268
269 return NULL;
270}
271
272static int autofs_mount_wait(const struct path *path, bool rcu_walk)
273{
274 struct autofs_sb_info *sbi = autofs_sbi(path->dentry->d_sb);
275 struct autofs_info *ino = autofs_dentry_ino(path->dentry);
276 int status = 0;
277
278 if (ino->flags & AUTOFS_INF_PENDING) {
279 if (rcu_walk)
280 return -ECHILD;
281 pr_debug("waiting for mount name=%pd\n", path->dentry);
282 status = autofs_wait(sbi, path, NFY_MOUNT);
283 pr_debug("mount wait done status=%d\n", status);
284 }
285 ino->last_used = jiffies;
286 return status;
287}
288
289static int do_expire_wait(const struct path *path, bool rcu_walk)
290{
291 struct dentry *dentry = path->dentry;
292 struct dentry *expiring;
293
294 expiring = autofs_lookup_expiring(dentry, rcu_walk);
295 if (IS_ERR(expiring))
296 return PTR_ERR(expiring);
297 if (!expiring)
298 return autofs_expire_wait(path, rcu_walk);
299 else {
300 const struct path this = { .mnt = path->mnt, .dentry = expiring };
301 /*
302 * If we are racing with expire the request might not
303 * be quite complete, but the directory has been removed
304 * so it must have been successful, just wait for it.
305 */
306 autofs_expire_wait(&this, 0);
307 autofs_del_expiring(expiring);
308 dput(expiring);
309 }
310 return 0;
311}
312
313static struct dentry *autofs_mountpoint_changed(struct path *path)
314{
315 struct dentry *dentry = path->dentry;
316 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
317
318 /*
319 * If this is an indirect mount the dentry could have gone away
320 * as a result of an expire and a new one created.
321 */
322 if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
323 struct dentry *parent = dentry->d_parent;
324 struct autofs_info *ino;
325 struct dentry *new;
326
327 new = d_lookup(parent, &dentry->d_name);
328 if (!new)
329 return NULL;
330 ino = autofs_dentry_ino(new);
331 ino->last_used = jiffies;
332 dput(path->dentry);
333 path->dentry = new;
334 }
335 return path->dentry;
336}
337
338static struct vfsmount *autofs_d_automount(struct path *path)
339{
340 struct dentry *dentry = path->dentry;
341 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
342 struct autofs_info *ino = autofs_dentry_ino(dentry);
343 int status;
344
345 pr_debug("dentry=%p %pd\n", dentry, dentry);
346
347 /* The daemon never triggers a mount. */
348 if (autofs_oz_mode(sbi))
349 return NULL;
350
351 /*
352 * If an expire request is pending everyone must wait.
353 * If the expire fails we're still mounted so continue
354 * the follow and return. A return of -EAGAIN (which only
355 * happens with indirect mounts) means the expire completed
356 * and the directory was removed, so just go ahead and try
357 * the mount.
358 */
359 status = do_expire_wait(path, 0);
360 if (status && status != -EAGAIN)
361 return NULL;
362
363 /* Callback to the daemon to perform the mount or wait */
364 spin_lock(&sbi->fs_lock);
365 if (ino->flags & AUTOFS_INF_PENDING) {
366 spin_unlock(&sbi->fs_lock);
367 status = autofs_mount_wait(path, 0);
368 if (status)
369 return ERR_PTR(status);
370 goto done;
371 }
372
373 /*
374 * If the dentry is a symlink it's equivalent to a directory
375 * having path_is_mountpoint() true, so there's no need to call
376 * back to the daemon.
377 */
378 if (d_really_is_positive(dentry) && d_is_symlink(dentry)) {
379 spin_unlock(&sbi->fs_lock);
380 goto done;
381 }
382
383 if (!path_is_mountpoint(path)) {
384 /*
385 * It's possible that user space hasn't removed directories
386 * after umounting a rootless multi-mount, although it
387 * should. For v5 path_has_submounts() is sufficient to
388 * handle this because the leaves of the directory tree under
389 * the mount never trigger mounts themselves (they have an
390 * autofs trigger mount mounted on them). But v4 pseudo direct
391 * mounts do need the leaves to trigger mounts. In this case
392 * we have no choice but to use the list_empty() check and
393 * require user space behave.
394 */
395 if (sbi->version > 4) {
396 if (path_has_submounts(path)) {
397 spin_unlock(&sbi->fs_lock);
398 goto done;
399 }
400 } else {
401 if (!simple_empty(dentry)) {
402 spin_unlock(&sbi->fs_lock);
403 goto done;
404 }
405 }
406 ino->flags |= AUTOFS_INF_PENDING;
407 spin_unlock(&sbi->fs_lock);
408 status = autofs_mount_wait(path, 0);
409 spin_lock(&sbi->fs_lock);
410 ino->flags &= ~AUTOFS_INF_PENDING;
411 if (status) {
412 spin_unlock(&sbi->fs_lock);
413 return ERR_PTR(status);
414 }
415 }
416 spin_unlock(&sbi->fs_lock);
417done:
418 /* Mount succeeded, check if we ended up with a new dentry */
419 dentry = autofs_mountpoint_changed(path);
420 if (!dentry)
421 return ERR_PTR(-ENOENT);
422
423 return NULL;
424}
425
426static int autofs_d_manage(const struct path *path, bool rcu_walk)
427{
428 struct dentry *dentry = path->dentry;
429 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
430 struct autofs_info *ino = autofs_dentry_ino(dentry);
431 int status;
432
433 pr_debug("dentry=%p %pd\n", dentry, dentry);
434
435 /* The daemon never waits. */
436 if (autofs_oz_mode(sbi)) {
437 if (!path_is_mountpoint(path))
438 return -EISDIR;
439 return 0;
440 }
441
442 /* Wait for pending expires */
443 if (do_expire_wait(path, rcu_walk) == -ECHILD)
444 return -ECHILD;
445
446 /*
447 * This dentry may be under construction so wait on mount
448 * completion.
449 */
450 status = autofs_mount_wait(path, rcu_walk);
451 if (status)
452 return status;
453
454 if (rcu_walk) {
455 /* We don't need fs_lock in rcu_walk mode,
456 * just testing 'AUTOFS_INFO_NO_RCU' is enough.
457 * simple_empty() takes a spinlock, so leave it
458 * to last.
459 * We only return -EISDIR when certain this isn't
460 * a mount-trap.
461 */
462 struct inode *inode;
463
464 if (ino->flags & AUTOFS_INF_WANT_EXPIRE)
465 return 0;
466 if (path_is_mountpoint(path))
467 return 0;
468 inode = d_inode_rcu(dentry);
469 if (inode && S_ISLNK(inode->i_mode))
470 return -EISDIR;
471 if (list_empty(&dentry->d_subdirs))
472 return 0;
473 if (!simple_empty(dentry))
474 return -EISDIR;
475 return 0;
476 }
477
478 spin_lock(&sbi->fs_lock);
479 /*
480 * If the dentry has been selected for expire while we slept
481 * on the lock then it might go away. We'll deal with that in
482 * ->d_automount() and wait on a new mount if the expire
483 * succeeds or return here if it doesn't (since there's no
484 * mount to follow with a rootless multi-mount).
485 */
486 if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
487 /*
488 * Any needed mounting has been completed and the path
489 * updated so check if this is a rootless multi-mount so
490 * we can avoid needless calls ->d_automount() and avoid
491 * an incorrect ELOOP error return.
492 */
493 if ((!path_is_mountpoint(path) && !simple_empty(dentry)) ||
494 (d_really_is_positive(dentry) && d_is_symlink(dentry)))
495 status = -EISDIR;
496 }
497 spin_unlock(&sbi->fs_lock);
498
499 return status;
500}
501
502/* Lookups in the root directory */
503static struct dentry *autofs_lookup(struct inode *dir,
504 struct dentry *dentry, unsigned int flags)
505{
506 struct autofs_sb_info *sbi;
507 struct autofs_info *ino;
508 struct dentry *active;
509
510 pr_debug("name = %pd\n", dentry);
511
512 /* File name too long to exist */
513 if (dentry->d_name.len > NAME_MAX)
514 return ERR_PTR(-ENAMETOOLONG);
515
516 sbi = autofs_sbi(dir->i_sb);
517
518 pr_debug("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
519 current->pid, task_pgrp_nr(current), sbi->catatonic,
520 autofs_oz_mode(sbi));
521
522 active = autofs_lookup_active(dentry);
523 if (active)
524 return active;
525 else {
526 /*
527 * A dentry that is not within the root can never trigger a
528 * mount operation, unless the directory already exists, so we
529 * can return fail immediately. The daemon however does need
530 * to create directories within the file system.
531 */
532 if (!autofs_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
533 return ERR_PTR(-ENOENT);
534
535 /* Mark entries in the root as mount triggers */
536 if (IS_ROOT(dentry->d_parent) &&
537 autofs_type_indirect(sbi->type))
538 __managed_dentry_set_managed(dentry);
539
540 ino = autofs_new_ino(sbi);
541 if (!ino)
542 return ERR_PTR(-ENOMEM);
543
544 dentry->d_fsdata = ino;
545 ino->dentry = dentry;
546
547 autofs_add_active(dentry);
548 }
549 return NULL;
550}
551
552static int autofs_dir_symlink(struct inode *dir,
553 struct dentry *dentry,
554 const char *symname)
555{
556 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
557 struct autofs_info *ino = autofs_dentry_ino(dentry);
558 struct autofs_info *p_ino;
559 struct inode *inode;
560 size_t size = strlen(symname);
561 char *cp;
562
563 pr_debug("%s <- %pd\n", symname, dentry);
564
565 if (!autofs_oz_mode(sbi))
566 return -EACCES;
567
568 BUG_ON(!ino);
569
570 autofs_clean_ino(ino);
571
572 autofs_del_active(dentry);
573
574 cp = kmalloc(size + 1, GFP_KERNEL);
575 if (!cp)
576 return -ENOMEM;
577
578 strcpy(cp, symname);
579
580 inode = autofs_get_inode(dir->i_sb, S_IFLNK | 0555);
581 if (!inode) {
582 kfree(cp);
583 return -ENOMEM;
584 }
585 inode->i_private = cp;
586 inode->i_size = size;
587 d_add(dentry, inode);
588
589 dget(dentry);
590 atomic_inc(&ino->count);
591 p_ino = autofs_dentry_ino(dentry->d_parent);
592 if (p_ino && !IS_ROOT(dentry))
593 atomic_inc(&p_ino->count);
594
595 dir->i_mtime = current_time(dir);
596
597 return 0;
598}
599
600/*
601 * NOTE!
602 *
603 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
604 * that the file no longer exists. However, doing that means that the
605 * VFS layer can turn the dentry into a negative dentry. We don't want
606 * this, because the unlink is probably the result of an expire.
607 * We simply d_drop it and add it to a expiring list in the super block,
608 * which allows the dentry lookup to check for an incomplete expire.
609 *
610 * If a process is blocked on the dentry waiting for the expire to finish,
611 * it will invalidate the dentry and try to mount with a new one.
612 *
613 * Also see autofs_dir_rmdir()..
614 */
615static int autofs_dir_unlink(struct inode *dir, struct dentry *dentry)
616{
617 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
618 struct autofs_info *ino = autofs_dentry_ino(dentry);
619 struct autofs_info *p_ino;
620
621 /* This allows root to remove symlinks */
622 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
623 return -EPERM;
624
625 if (atomic_dec_and_test(&ino->count)) {
626 p_ino = autofs_dentry_ino(dentry->d_parent);
627 if (p_ino && !IS_ROOT(dentry))
628 atomic_dec(&p_ino->count);
629 }
630 dput(ino->dentry);
631
632 d_inode(dentry)->i_size = 0;
633 clear_nlink(d_inode(dentry));
634
635 dir->i_mtime = current_time(dir);
636
637 spin_lock(&sbi->lookup_lock);
638 __autofs_add_expiring(dentry);
639 d_drop(dentry);
640 spin_unlock(&sbi->lookup_lock);
641
642 return 0;
643}
644
645/*
646 * Version 4 of autofs provides a pseudo direct mount implementation
647 * that relies on directories at the leaves of a directory tree under
648 * an indirect mount to trigger mounts. To allow for this we need to
649 * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves
650 * of the directory tree. There is no need to clear the automount flag
651 * following a mount or restore it after an expire because these mounts
652 * are always covered. However, it is necessary to ensure that these
653 * flags are clear on non-empty directories to avoid unnecessary calls
654 * during path walks.
655 */
656static void autofs_set_leaf_automount_flags(struct dentry *dentry)
657{
658 struct dentry *parent;
659
660 /* root and dentrys in the root are already handled */
661 if (IS_ROOT(dentry->d_parent))
662 return;
663
664 managed_dentry_set_managed(dentry);
665
666 parent = dentry->d_parent;
667 /* only consider parents below dentrys in the root */
668 if (IS_ROOT(parent->d_parent))
669 return;
670 managed_dentry_clear_managed(parent);
671}
672
673static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
674{
675 struct list_head *d_child;
676 struct dentry *parent;
677
678 /* flags for dentrys in the root are handled elsewhere */
679 if (IS_ROOT(dentry->d_parent))
680 return;
681
682 managed_dentry_clear_managed(dentry);
683
684 parent = dentry->d_parent;
685 /* only consider parents below dentrys in the root */
686 if (IS_ROOT(parent->d_parent))
687 return;
688 d_child = &dentry->d_child;
689 /* Set parent managed if it's becoming empty */
690 if (d_child->next == &parent->d_subdirs &&
691 d_child->prev == &parent->d_subdirs)
692 managed_dentry_set_managed(parent);
693}
694
695static int autofs_dir_rmdir(struct inode *dir, struct dentry *dentry)
696{
697 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
698 struct autofs_info *ino = autofs_dentry_ino(dentry);
699 struct autofs_info *p_ino;
700
701 pr_debug("dentry %p, removing %pd\n", dentry, dentry);
702
703 if (!autofs_oz_mode(sbi))
704 return -EACCES;
705
706 spin_lock(&sbi->lookup_lock);
707 if (!simple_empty(dentry)) {
708 spin_unlock(&sbi->lookup_lock);
709 return -ENOTEMPTY;
710 }
711 __autofs_add_expiring(dentry);
712 d_drop(dentry);
713 spin_unlock(&sbi->lookup_lock);
714
715 if (sbi->version < 5)
716 autofs_clear_leaf_automount_flags(dentry);
717
718 if (atomic_dec_and_test(&ino->count)) {
719 p_ino = autofs_dentry_ino(dentry->d_parent);
720 if (p_ino && dentry->d_parent != dentry)
721 atomic_dec(&p_ino->count);
722 }
723 dput(ino->dentry);
724 d_inode(dentry)->i_size = 0;
725 clear_nlink(d_inode(dentry));
726
727 if (dir->i_nlink)
728 drop_nlink(dir);
729
730 return 0;
731}
732
733static int autofs_dir_mkdir(struct inode *dir,
734 struct dentry *dentry, umode_t mode)
735{
736 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
737 struct autofs_info *ino = autofs_dentry_ino(dentry);
738 struct autofs_info *p_ino;
739 struct inode *inode;
740
741 if (!autofs_oz_mode(sbi))
742 return -EACCES;
743
744 pr_debug("dentry %p, creating %pd\n", dentry, dentry);
745
746 BUG_ON(!ino);
747
748 autofs_clean_ino(ino);
749
750 autofs_del_active(dentry);
751
752 inode = autofs_get_inode(dir->i_sb, S_IFDIR | mode);
753 if (!inode)
754 return -ENOMEM;
755 d_add(dentry, inode);
756
757 if (sbi->version < 5)
758 autofs_set_leaf_automount_flags(dentry);
759
760 dget(dentry);
761 atomic_inc(&ino->count);
762 p_ino = autofs_dentry_ino(dentry->d_parent);
763 if (p_ino && !IS_ROOT(dentry))
764 atomic_inc(&p_ino->count);
765 inc_nlink(dir);
766 dir->i_mtime = current_time(dir);
767
768 return 0;
769}
770
771/* Get/set timeout ioctl() operation */
772#ifdef CONFIG_COMPAT
773static inline int autofs_compat_get_set_timeout(struct autofs_sb_info *sbi,
774 compat_ulong_t __user *p)
775{
776 unsigned long ntimeout;
777 int rv;
778
779 rv = get_user(ntimeout, p);
780 if (rv)
781 goto error;
782
783 rv = put_user(sbi->exp_timeout/HZ, p);
784 if (rv)
785 goto error;
786
787 if (ntimeout > UINT_MAX/HZ)
788 sbi->exp_timeout = 0;
789 else
790 sbi->exp_timeout = ntimeout * HZ;
791
792 return 0;
793error:
794 return rv;
795}
796#endif
797
798static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
799 unsigned long __user *p)
800{
801 unsigned long ntimeout;
802 int rv;
803
804 rv = get_user(ntimeout, p);
805 if (rv)
806 goto error;
807
808 rv = put_user(sbi->exp_timeout/HZ, p);
809 if (rv)
810 goto error;
811
812 if (ntimeout > ULONG_MAX/HZ)
813 sbi->exp_timeout = 0;
814 else
815 sbi->exp_timeout = ntimeout * HZ;
816
817 return 0;
818error:
819 return rv;
820}
821
822/* Return protocol version */
823static inline int autofs_get_protover(struct autofs_sb_info *sbi,
824 int __user *p)
825{
826 return put_user(sbi->version, p);
827}
828
829/* Return protocol sub version */
830static inline int autofs_get_protosubver(struct autofs_sb_info *sbi,
831 int __user *p)
832{
833 return put_user(sbi->sub_version, p);
834}
835
836/*
837* Tells the daemon whether it can umount the autofs mount.
838*/
839static inline int autofs_ask_umount(struct vfsmount *mnt, int __user *p)
840{
841 int status = 0;
842
843 if (may_umount(mnt))
844 status = 1;
845
846 pr_debug("may umount %d\n", status);
847
848 status = put_user(status, p);
849
850 return status;
851}
852
853/* Identify autofs_dentries - this is so we can tell if there's
854 * an extra dentry refcount or not. We only hold a refcount on the
855 * dentry if its non-negative (ie, d_inode != NULL)
856 */
857int is_autofs_dentry(struct dentry *dentry)
858{
859 return dentry && d_really_is_positive(dentry) &&
860 dentry->d_op == &autofs_dentry_operations &&
861 dentry->d_fsdata != NULL;
862}
863
864/*
865 * ioctl()'s on the root directory is the chief method for the daemon to
866 * generate kernel reactions
867 */
868static int autofs_root_ioctl_unlocked(struct inode *inode, struct file *filp,
869 unsigned int cmd, unsigned long arg)
870{
871 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
872 void __user *p = (void __user *)arg;
873
874 pr_debug("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",
875 cmd, arg, sbi, task_pgrp_nr(current));
876
877 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
878 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
879 return -ENOTTY;
880
881 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
882 return -EPERM;
883
884 switch (cmd) {
885 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
886 return autofs_wait_release(sbi, (autofs_wqt_t) arg, 0);
887 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
888 return autofs_wait_release(sbi, (autofs_wqt_t) arg, -ENOENT);
889 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
890 autofs_catatonic_mode(sbi);
891 return 0;
892 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
893 return autofs_get_protover(sbi, p);
894 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
895 return autofs_get_protosubver(sbi, p);
896 case AUTOFS_IOC_SETTIMEOUT:
897 return autofs_get_set_timeout(sbi, p);
898#ifdef CONFIG_COMPAT
899 case AUTOFS_IOC_SETTIMEOUT32:
900 return autofs_compat_get_set_timeout(sbi, p);
901#endif
902
903 case AUTOFS_IOC_ASKUMOUNT:
904 return autofs_ask_umount(filp->f_path.mnt, p);
905
906 /* return a single thing to expire */
907 case AUTOFS_IOC_EXPIRE:
908 return autofs_expire_run(inode->i_sb, filp->f_path.mnt, sbi, p);
909 /* same as above, but can send multiple expires through pipe */
910 case AUTOFS_IOC_EXPIRE_MULTI:
911 return autofs_expire_multi(inode->i_sb,
912 filp->f_path.mnt, sbi, p);
913
914 default:
915 return -EINVAL;
916 }
917}
918
919static long autofs_root_ioctl(struct file *filp,
920 unsigned int cmd, unsigned long arg)
921{
922 struct inode *inode = file_inode(filp);
923
924 return autofs_root_ioctl_unlocked(inode, filp, cmd, arg);
925}
926
927#ifdef CONFIG_COMPAT
928static long autofs_root_compat_ioctl(struct file *filp,
929 unsigned int cmd, unsigned long arg)
930{
931 struct inode *inode = file_inode(filp);
932 int ret;
933
934 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
935 ret = autofs_root_ioctl_unlocked(inode, filp, cmd, arg);
936 else
937 ret = autofs_root_ioctl_unlocked(inode, filp, cmd,
938 (unsigned long) compat_ptr(arg));
939
940 return ret;
941}
942#endif
diff --git a/fs/autofs/symlink.c b/fs/autofs/symlink.c
new file mode 100644
index 000000000000..aad3902c0cc1
--- /dev/null
+++ b/fs/autofs/symlink.c
@@ -0,0 +1,29 @@
1/*
2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 *
4 * This file is part of the Linux kernel and is made available under
5 * the terms of the GNU General Public License, version 2, or at your
6 * option, any later version, incorporated herein by reference.
7 */
8
9#include "autofs_i.h"
10
11static const char *autofs_get_link(struct dentry *dentry,
12 struct inode *inode,
13 struct delayed_call *done)
14{
15 struct autofs_sb_info *sbi;
16 struct autofs_info *ino;
17
18 if (!dentry)
19 return ERR_PTR(-ECHILD);
20 sbi = autofs_sbi(dentry->d_sb);
21 ino = autofs_dentry_ino(dentry);
22 if (ino && !autofs_oz_mode(sbi))
23 ino->last_used = jiffies;
24 return d_inode(dentry)->i_private;
25}
26
27const struct inode_operations autofs_symlink_inode_operations = {
28 .get_link = autofs_get_link
29};
diff --git a/fs/autofs/waitq.c b/fs/autofs/waitq.c
new file mode 100644
index 000000000000..8a566fa66afe
--- /dev/null
+++ b/fs/autofs/waitq.c
@@ -0,0 +1,559 @@
1/*
2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 * Copyright 2001-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#include <linux/slab.h>
11#include <linux/time.h>
12#include <linux/signal.h>
13#include <linux/sched/signal.h>
14#include <linux/file.h>
15#include "autofs_i.h"
16
17/* We make this a static variable rather than a part of the superblock; it
18 * is better if we don't reassign numbers easily even across filesystems
19 */
20static autofs_wqt_t autofs_next_wait_queue = 1;
21
22void autofs_catatonic_mode(struct autofs_sb_info *sbi)
23{
24 struct autofs_wait_queue *wq, *nwq;
25
26 mutex_lock(&sbi->wq_mutex);
27 if (sbi->catatonic) {
28 mutex_unlock(&sbi->wq_mutex);
29 return;
30 }
31
32 pr_debug("entering catatonic mode\n");
33
34 sbi->catatonic = 1;
35 wq = sbi->queues;
36 sbi->queues = NULL; /* Erase all wait queues */
37 while (wq) {
38 nwq = wq->next;
39 wq->status = -ENOENT; /* Magic is gone - report failure */
40 kfree(wq->name.name);
41 wq->name.name = NULL;
42 wq->wait_ctr--;
43 wake_up_interruptible(&wq->queue);
44 wq = nwq;
45 }
46 fput(sbi->pipe); /* Close the pipe */
47 sbi->pipe = NULL;
48 sbi->pipefd = -1;
49 mutex_unlock(&sbi->wq_mutex);
50}
51
52static int autofs_write(struct autofs_sb_info *sbi,
53 struct file *file, const void *addr, int bytes)
54{
55 unsigned long sigpipe, flags;
56 const char *data = (const char *)addr;
57 ssize_t wr = 0;
58
59 sigpipe = sigismember(&current->pending.signal, SIGPIPE);
60
61 mutex_lock(&sbi->pipe_mutex);
62 while (bytes) {
63 wr = __kernel_write(file, data, bytes, &file->f_pos);
64 if (wr <= 0)
65 break;
66 data += wr;
67 bytes -= wr;
68 }
69 mutex_unlock(&sbi->pipe_mutex);
70
71 /* Keep the currently executing process from receiving a
72 * SIGPIPE unless it was already supposed to get one
73 */
74 if (wr == -EPIPE && !sigpipe) {
75 spin_lock_irqsave(&current->sighand->siglock, flags);
76 sigdelset(&current->pending.signal, SIGPIPE);
77 recalc_sigpending();
78 spin_unlock_irqrestore(&current->sighand->siglock, flags);
79 }
80
81 /* if 'wr' returned 0 (impossible) we assume -EIO (safe) */
82 return bytes == 0 ? 0 : wr < 0 ? wr : -EIO;
83}
84
85static void autofs_notify_daemon(struct autofs_sb_info *sbi,
86 struct autofs_wait_queue *wq,
87 int type)
88{
89 union {
90 struct autofs_packet_hdr hdr;
91 union autofs_packet_union v4_pkt;
92 union autofs_v5_packet_union v5_pkt;
93 } pkt;
94 struct file *pipe = NULL;
95 size_t pktsz;
96 int ret;
97
98 pr_debug("wait id = 0x%08lx, name = %.*s, type=%d\n",
99 (unsigned long) wq->wait_queue_token,
100 wq->name.len, wq->name.name, type);
101
102 memset(&pkt, 0, sizeof(pkt)); /* For security reasons */
103
104 pkt.hdr.proto_version = sbi->version;
105 pkt.hdr.type = type;
106
107 switch (type) {
108 /* Kernel protocol v4 missing and expire packets */
109 case autofs_ptype_missing:
110 {
111 struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
112
113 pktsz = sizeof(*mp);
114
115 mp->wait_queue_token = wq->wait_queue_token;
116 mp->len = wq->name.len;
117 memcpy(mp->name, wq->name.name, wq->name.len);
118 mp->name[wq->name.len] = '\0';
119 break;
120 }
121 case autofs_ptype_expire_multi:
122 {
123 struct autofs_packet_expire_multi *ep =
124 &pkt.v4_pkt.expire_multi;
125
126 pktsz = sizeof(*ep);
127
128 ep->wait_queue_token = wq->wait_queue_token;
129 ep->len = wq->name.len;
130 memcpy(ep->name, wq->name.name, wq->name.len);
131 ep->name[wq->name.len] = '\0';
132 break;
133 }
134 /*
135 * Kernel protocol v5 packet for handling indirect and direct
136 * mount missing and expire requests
137 */
138 case autofs_ptype_missing_indirect:
139 case autofs_ptype_expire_indirect:
140 case autofs_ptype_missing_direct:
141 case autofs_ptype_expire_direct:
142 {
143 struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
144 struct user_namespace *user_ns = sbi->pipe->f_cred->user_ns;
145
146 pktsz = sizeof(*packet);
147
148 packet->wait_queue_token = wq->wait_queue_token;
149 packet->len = wq->name.len;
150 memcpy(packet->name, wq->name.name, wq->name.len);
151 packet->name[wq->name.len] = '\0';
152 packet->dev = wq->dev;
153 packet->ino = wq->ino;
154 packet->uid = from_kuid_munged(user_ns, wq->uid);
155 packet->gid = from_kgid_munged(user_ns, wq->gid);
156 packet->pid = wq->pid;
157 packet->tgid = wq->tgid;
158 break;
159 }
160 default:
161 pr_warn("bad type %d!\n", type);
162 mutex_unlock(&sbi->wq_mutex);
163 return;
164 }
165
166 pipe = get_file(sbi->pipe);
167
168 mutex_unlock(&sbi->wq_mutex);
169
170 switch (ret = autofs_write(sbi, pipe, &pkt, pktsz)) {
171 case 0:
172 break;
173 case -ENOMEM:
174 case -ERESTARTSYS:
175 /* Just fail this one */
176 autofs_wait_release(sbi, wq->wait_queue_token, ret);
177 break;
178 default:
179 autofs_catatonic_mode(sbi);
180 break;
181 }
182 fput(pipe);
183}
184
185static int autofs_getpath(struct autofs_sb_info *sbi,
186 struct dentry *dentry, char **name)
187{
188 struct dentry *root = sbi->sb->s_root;
189 struct dentry *tmp;
190 char *buf;
191 char *p;
192 int len;
193 unsigned seq;
194
195rename_retry:
196 buf = *name;
197 len = 0;
198
199 seq = read_seqbegin(&rename_lock);
200 rcu_read_lock();
201 spin_lock(&sbi->fs_lock);
202 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
203 len += tmp->d_name.len + 1;
204
205 if (!len || --len > NAME_MAX) {
206 spin_unlock(&sbi->fs_lock);
207 rcu_read_unlock();
208 if (read_seqretry(&rename_lock, seq))
209 goto rename_retry;
210 return 0;
211 }
212
213 *(buf + len) = '\0';
214 p = buf + len - dentry->d_name.len;
215 strncpy(p, dentry->d_name.name, dentry->d_name.len);
216
217 for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
218 *(--p) = '/';
219 p -= tmp->d_name.len;
220 strncpy(p, tmp->d_name.name, tmp->d_name.len);
221 }
222 spin_unlock(&sbi->fs_lock);
223 rcu_read_unlock();
224 if (read_seqretry(&rename_lock, seq))
225 goto rename_retry;
226
227 return len;
228}
229
230static struct autofs_wait_queue *
231autofs_find_wait(struct autofs_sb_info *sbi, const struct qstr *qstr)
232{
233 struct autofs_wait_queue *wq;
234
235 for (wq = sbi->queues; wq; wq = wq->next) {
236 if (wq->name.hash == qstr->hash &&
237 wq->name.len == qstr->len &&
238 wq->name.name &&
239 !memcmp(wq->name.name, qstr->name, qstr->len))
240 break;
241 }
242 return wq;
243}
244
245/*
246 * Check if we have a valid request.
247 * Returns
248 * 1 if the request should continue.
249 * In this case we can return an autofs_wait_queue entry if one is
250 * found or NULL to idicate a new wait needs to be created.
251 * 0 or a negative errno if the request shouldn't continue.
252 */
253static int validate_request(struct autofs_wait_queue **wait,
254 struct autofs_sb_info *sbi,
255 const struct qstr *qstr,
256 const struct path *path, enum autofs_notify notify)
257{
258 struct dentry *dentry = path->dentry;
259 struct autofs_wait_queue *wq;
260 struct autofs_info *ino;
261
262 if (sbi->catatonic)
263 return -ENOENT;
264
265 /* Wait in progress, continue; */
266 wq = autofs_find_wait(sbi, qstr);
267 if (wq) {
268 *wait = wq;
269 return 1;
270 }
271
272 *wait = NULL;
273
274 /* If we don't yet have any info this is a new request */
275 ino = autofs_dentry_ino(dentry);
276 if (!ino)
277 return 1;
278
279 /*
280 * If we've been asked to wait on an existing expire (NFY_NONE)
281 * but there is no wait in the queue ...
282 */
283 if (notify == NFY_NONE) {
284 /*
285 * Either we've betean the pending expire to post it's
286 * wait or it finished while we waited on the mutex.
287 * So we need to wait till either, the wait appears
288 * or the expire finishes.
289 */
290
291 while (ino->flags & AUTOFS_INF_EXPIRING) {
292 mutex_unlock(&sbi->wq_mutex);
293 schedule_timeout_interruptible(HZ/10);
294 if (mutex_lock_interruptible(&sbi->wq_mutex))
295 return -EINTR;
296
297 if (sbi->catatonic)
298 return -ENOENT;
299
300 wq = autofs_find_wait(sbi, qstr);
301 if (wq) {
302 *wait = wq;
303 return 1;
304 }
305 }
306
307 /*
308 * Not ideal but the status has already gone. Of the two
309 * cases where we wait on NFY_NONE neither depend on the
310 * return status of the wait.
311 */
312 return 0;
313 }
314
315 /*
316 * If we've been asked to trigger a mount and the request
317 * completed while we waited on the mutex ...
318 */
319 if (notify == NFY_MOUNT) {
320 struct dentry *new = NULL;
321 struct path this;
322 int valid = 1;
323
324 /*
325 * If the dentry was successfully mounted while we slept
326 * on the wait queue mutex we can return success. If it
327 * isn't mounted (doesn't have submounts for the case of
328 * a multi-mount with no mount at it's base) we can
329 * continue on and create a new request.
330 */
331 if (!IS_ROOT(dentry)) {
332 if (d_unhashed(dentry) &&
333 d_really_is_positive(dentry)) {
334 struct dentry *parent = dentry->d_parent;
335
336 new = d_lookup(parent, &dentry->d_name);
337 if (new)
338 dentry = new;
339 }
340 }
341 this.mnt = path->mnt;
342 this.dentry = dentry;
343 if (path_has_submounts(&this))
344 valid = 0;
345
346 if (new)
347 dput(new);
348 return valid;
349 }
350
351 return 1;
352}
353
354int autofs_wait(struct autofs_sb_info *sbi,
355 const struct path *path, enum autofs_notify notify)
356{
357 struct dentry *dentry = path->dentry;
358 struct autofs_wait_queue *wq;
359 struct qstr qstr;
360 char *name;
361 int status, ret, type;
362 pid_t pid;
363 pid_t tgid;
364
365 /* In catatonic mode, we don't wait for nobody */
366 if (sbi->catatonic)
367 return -ENOENT;
368
369 /*
370 * Try translating pids to the namespace of the daemon.
371 *
372 * Zero means failure: we are in an unrelated pid namespace.
373 */
374 pid = task_pid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
375 tgid = task_tgid_nr_ns(current, ns_of_pid(sbi->oz_pgrp));
376 if (pid == 0 || tgid == 0)
377 return -ENOENT;
378
379 if (d_really_is_negative(dentry)) {
380 /*
381 * A wait for a negative dentry is invalid for certain
382 * cases. A direct or offset mount "always" has its mount
383 * point directory created and so the request dentry must
384 * be positive or the map key doesn't exist. The situation
385 * is very similar for indirect mounts except only dentrys
386 * in the root of the autofs file system may be negative.
387 */
388 if (autofs_type_trigger(sbi->type))
389 return -ENOENT;
390 else if (!IS_ROOT(dentry->d_parent))
391 return -ENOENT;
392 }
393
394 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
395 if (!name)
396 return -ENOMEM;
397
398 /* If this is a direct mount request create a dummy name */
399 if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type))
400 qstr.len = sprintf(name, "%p", dentry);
401 else {
402 qstr.len = autofs_getpath(sbi, dentry, &name);
403 if (!qstr.len) {
404 kfree(name);
405 return -ENOENT;
406 }
407 }
408 qstr.name = name;
409 qstr.hash = full_name_hash(dentry, name, qstr.len);
410
411 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
412 kfree(qstr.name);
413 return -EINTR;
414 }
415
416 ret = validate_request(&wq, sbi, &qstr, path, notify);
417 if (ret <= 0) {
418 if (ret != -EINTR)
419 mutex_unlock(&sbi->wq_mutex);
420 kfree(qstr.name);
421 return ret;
422 }
423
424 if (!wq) {
425 /* Create a new wait queue */
426 wq = kmalloc(sizeof(struct autofs_wait_queue), GFP_KERNEL);
427 if (!wq) {
428 kfree(qstr.name);
429 mutex_unlock(&sbi->wq_mutex);
430 return -ENOMEM;
431 }
432
433 wq->wait_queue_token = autofs_next_wait_queue;
434 if (++autofs_next_wait_queue == 0)
435 autofs_next_wait_queue = 1;
436 wq->next = sbi->queues;
437 sbi->queues = wq;
438 init_waitqueue_head(&wq->queue);
439 memcpy(&wq->name, &qstr, sizeof(struct qstr));
440 wq->dev = autofs_get_dev(sbi);
441 wq->ino = autofs_get_ino(sbi);
442 wq->uid = current_uid();
443 wq->gid = current_gid();
444 wq->pid = pid;
445 wq->tgid = tgid;
446 wq->status = -EINTR; /* Status return if interrupted */
447 wq->wait_ctr = 2;
448
449 if (sbi->version < 5) {
450 if (notify == NFY_MOUNT)
451 type = autofs_ptype_missing;
452 else
453 type = autofs_ptype_expire_multi;
454 } else {
455 if (notify == NFY_MOUNT)
456 type = autofs_type_trigger(sbi->type) ?
457 autofs_ptype_missing_direct :
458 autofs_ptype_missing_indirect;
459 else
460 type = autofs_type_trigger(sbi->type) ?
461 autofs_ptype_expire_direct :
462 autofs_ptype_expire_indirect;
463 }
464
465 pr_debug("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
466 (unsigned long) wq->wait_queue_token, wq->name.len,
467 wq->name.name, notify);
468
469 /*
470 * autofs_notify_daemon() may block; it will unlock ->wq_mutex
471 */
472 autofs_notify_daemon(sbi, wq, type);
473 } else {
474 wq->wait_ctr++;
475 pr_debug("existing wait id = 0x%08lx, name = %.*s, nfy=%d\n",
476 (unsigned long) wq->wait_queue_token, wq->name.len,
477 wq->name.name, notify);
478 mutex_unlock(&sbi->wq_mutex);
479 kfree(qstr.name);
480 }
481
482 /*
483 * wq->name.name is NULL iff the lock is already released
484 * or the mount has been made catatonic.
485 */
486 wait_event_killable(wq->queue, wq->name.name == NULL);
487 status = wq->status;
488
489 /*
490 * For direct and offset mounts we need to track the requester's
491 * uid and gid in the dentry info struct. This is so it can be
492 * supplied, on request, by the misc device ioctl interface.
493 * This is needed during daemon resatart when reconnecting
494 * to existing, active, autofs mounts. The uid and gid (and
495 * related string values) may be used for macro substitution
496 * in autofs mount maps.
497 */
498 if (!status) {
499 struct autofs_info *ino;
500 struct dentry *de = NULL;
501
502 /* direct mount or browsable map */
503 ino = autofs_dentry_ino(dentry);
504 if (!ino) {
505 /* If not lookup actual dentry used */
506 de = d_lookup(dentry->d_parent, &dentry->d_name);
507 if (de)
508 ino = autofs_dentry_ino(de);
509 }
510
511 /* Set mount requester */
512 if (ino) {
513 spin_lock(&sbi->fs_lock);
514 ino->uid = wq->uid;
515 ino->gid = wq->gid;
516 spin_unlock(&sbi->fs_lock);
517 }
518
519 if (de)
520 dput(de);
521 }
522
523 /* Are we the last process to need status? */
524 mutex_lock(&sbi->wq_mutex);
525 if (!--wq->wait_ctr)
526 kfree(wq);
527 mutex_unlock(&sbi->wq_mutex);
528
529 return status;
530}
531
532
533int autofs_wait_release(struct autofs_sb_info *sbi,
534 autofs_wqt_t wait_queue_token, int status)
535{
536 struct autofs_wait_queue *wq, **wql;
537
538 mutex_lock(&sbi->wq_mutex);
539 for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
540 if (wq->wait_queue_token == wait_queue_token)
541 break;
542 }
543
544 if (!wq) {
545 mutex_unlock(&sbi->wq_mutex);
546 return -EINVAL;
547 }
548
549 *wql = wq->next; /* Unlink from chain */
550 kfree(wq->name.name);
551 wq->name.name = NULL; /* Do not wait on this queue */
552 wq->status = status;
553 wake_up(&wq->queue);
554 if (!--wq->wait_ctr)
555 kfree(wq);
556 mutex_unlock(&sbi->wq_mutex);
557
558 return 0;
559}