blob: c8a80dffb4557d80999b71c66b94cb4a7d2b75fe [file] [log] [blame]
Ian Kent8d7b48e2008-10-15 22:02:54 -07001/*
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/compat.h>
21#include <linux/syscalls.h>
Ian Kent8d7b48e2008-10-15 22:02:54 -070022#include <linux/magic.h>
23#include <linux/dcache.h>
24#include <linux/uaccess.h>
25
26#include "autofs_i.h"
27
28/*
29 * This module implements an interface for routing autofs ioctl control
30 * commands via a miscellaneous device file.
31 *
32 * The alternate interface is needed because we need to be able open
33 * an ioctl file descriptor on an autofs mount that may be covered by
34 * another mount. This situation arises when starting automount(8)
35 * or other user space daemon which uses direct mounts or offset
36 * mounts (used for autofs lazy mount/umount of nested mount trees),
37 * which have been left busy at at service shutdown.
38 */
39
40#define AUTOFS_DEV_IOCTL_SIZE sizeof(struct autofs_dev_ioctl)
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 */
Al Viro3eac8772009-04-07 11:12:46 -040056static int invalid_str(char *str, size_t size)
Ian Kent8d7b48e2008-10-15 22:02:54 -070057{
Al Viro3eac8772009-04-07 11:12:46 -040058 if (memchr(str, 0, size))
59 return 0;
Ian Kent8d7b48e2008-10-15 22:02:54 -070060 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 ((AUTOFS_DEV_IOCTL_VERSION_MAJOR != param->ver_major) ||
75 (AUTOFS_DEV_IOCTL_VERSION_MINOR < param->ver_minor)) {
76 AUTOFS_WARN("ioctl control interface version mismatch: "
77 "kernel(%u.%u), user(%u.%u), cmd(%d)",
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 *copy_dev_ioctl(struct autofs_dev_ioctl __user *in)
96{
97 struct autofs_dev_ioctl tmp, *ads;
98
99 if (copy_from_user(&tmp, in, sizeof(tmp)))
100 return ERR_PTR(-EFAULT);
101
102 if (tmp.size < sizeof(tmp))
103 return ERR_PTR(-EINVAL);
104
105 ads = kmalloc(tmp.size, GFP_KERNEL);
106 if (!ads)
107 return ERR_PTR(-ENOMEM);
108
109 if (copy_from_user(ads, in, tmp.size)) {
110 kfree(ads);
111 return ERR_PTR(-EFAULT);
112 }
113
114 return ads;
115}
116
117static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
118{
119 kfree(param);
120 return;
121}
122
123/*
124 * Check sanity of parameter control fields and if a path is present
Ian Kentbae8ec62009-01-06 14:42:09 -0800125 * check that it is terminated and contains at least one "/".
Ian Kent8d7b48e2008-10-15 22:02:54 -0700126 */
127static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param)
128{
Ian Kent96b03172008-11-06 12:53:23 -0800129 int err;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700130
Ian Kent96b03172008-11-06 12:53:23 -0800131 err = check_dev_ioctl_version(cmd, param);
132 if (err) {
Ian Kent8d7b48e2008-10-15 22:02:54 -0700133 AUTOFS_WARN("invalid device control module version "
134 "supplied for cmd(0x%08x)", cmd);
135 goto out;
136 }
137
138 if (param->size > sizeof(*param)) {
Al Viro3eac8772009-04-07 11:12:46 -0400139 err = invalid_str(param->path, param->size - sizeof(*param));
Ian Kent8d7b48e2008-10-15 22:02:54 -0700140 if (err) {
Ian Kentbae8ec62009-01-06 14:42:09 -0800141 AUTOFS_WARN(
142 "path string terminator missing for cmd(0x%08x)",
143 cmd);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700144 goto out;
145 }
146
Ian Kentbae8ec62009-01-06 14:42:09 -0800147 err = check_name(param->path);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700148 if (err) {
149 AUTOFS_WARN("invalid path supplied for cmd(0x%08x)",
150 cmd);
151 goto out;
152 }
153 }
154
155 err = 0;
156out:
157 return err;
158}
159
160/*
161 * Get the autofs super block info struct from the file opened on
162 * the autofs mount point.
163 */
164static struct autofs_sb_info *autofs_dev_ioctl_sbi(struct file *f)
165{
166 struct autofs_sb_info *sbi = NULL;
167 struct inode *inode;
168
169 if (f) {
170 inode = f->f_path.dentry->d_inode;
171 sbi = autofs4_sbi(inode->i_sb);
172 }
173 return sbi;
174}
175
176/* Return autofs module protocol version */
177static int autofs_dev_ioctl_protover(struct file *fp,
178 struct autofs_sb_info *sbi,
179 struct autofs_dev_ioctl *param)
180{
Ian Kent730c9ee2009-01-06 14:42:06 -0800181 param->protover.version = sbi->version;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700182 return 0;
183}
184
185/* Return autofs module protocol sub version */
186static int autofs_dev_ioctl_protosubver(struct file *fp,
187 struct autofs_sb_info *sbi,
188 struct autofs_dev_ioctl *param)
189{
Ian Kent730c9ee2009-01-06 14:42:06 -0800190 param->protosubver.sub_version = sbi->sub_version;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700191 return 0;
192}
193
Al Viro4e44b682009-04-07 11:08:56 -0400194static int find_autofs_mount(const char *pathname,
195 struct path *res,
196 int test(struct path *path, void *data),
197 void *data)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700198{
Al Viro4e44b682009-04-07 11:08:56 -0400199 struct path path;
200 int err = kern_path(pathname, 0, &path);
201 if (err)
202 return err;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700203 err = -ENOENT;
Al Viro4e44b682009-04-07 11:08:56 -0400204 while (path.dentry == path.mnt->mnt_root) {
205 if (path.mnt->mnt_sb->s_magic == AUTOFS_SUPER_MAGIC) {
206 if (test(&path, data)) {
207 path_get(&path);
208 if (!err) /* already found some */
209 path_put(res);
210 *res = path;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700211 err = 0;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700212 }
213 }
Al Virobab77eb2009-04-18 03:26:48 -0400214 if (!follow_up(&path))
Al Viro4e44b682009-04-07 11:08:56 -0400215 break;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700216 }
Al Viro4e44b682009-04-07 11:08:56 -0400217 path_put(&path);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700218 return err;
219}
220
Al Viro4e44b682009-04-07 11:08:56 -0400221static int test_by_dev(struct path *path, void *p)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700222{
Al Viro4e44b682009-04-07 11:08:56 -0400223 return path->mnt->mnt_sb->s_dev == *(dev_t *)p;
224}
Ian Kent8d7b48e2008-10-15 22:02:54 -0700225
Al Viro4e44b682009-04-07 11:08:56 -0400226static int test_by_type(struct path *path, void *p)
227{
228 struct autofs_info *ino = autofs4_dentry_ino(path->dentry);
229 return ino && ino->sbi->type & *(unsigned *)p;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700230}
231
232static void autofs_dev_ioctl_fd_install(unsigned int fd, struct file *file)
233{
234 struct files_struct *files = current->files;
235 struct fdtable *fdt;
236
237 spin_lock(&files->file_lock);
238 fdt = files_fdtable(files);
239 BUG_ON(fdt->fd[fd] != NULL);
240 rcu_assign_pointer(fdt->fd[fd], file);
241 FD_SET(fd, fdt->close_on_exec);
242 spin_unlock(&files->file_lock);
243}
244
245
246/*
247 * Open a file descriptor on the autofs mount point corresponding
248 * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
249 */
Al Viro4e44b682009-04-07 11:08:56 -0400250static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700251{
Ian Kent8d7b48e2008-10-15 22:02:54 -0700252 int err, fd;
253
254 fd = get_unused_fd();
255 if (likely(fd >= 0)) {
Al Viro4e44b682009-04-07 11:08:56 -0400256 struct file *filp;
257 struct path path;
258
259 err = find_autofs_mount(name, &path, test_by_dev, &devid);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700260 if (err)
261 goto out;
262
263 /*
Al Viro4e44b682009-04-07 11:08:56 -0400264 * Find autofs super block that has the device number
Ian Kent8d7b48e2008-10-15 22:02:54 -0700265 * corresponding to the autofs fs we want to open.
266 */
Ian Kent8d7b48e2008-10-15 22:02:54 -0700267
Al Viro4e44b682009-04-07 11:08:56 -0400268 filp = dentry_open(path.dentry, path.mnt, O_RDONLY,
David Howells745ca242008-11-14 10:39:22 +1100269 current_cred());
Ian Kent8d7b48e2008-10-15 22:02:54 -0700270 if (IS_ERR(filp)) {
271 err = PTR_ERR(filp);
272 goto out;
273 }
274
275 autofs_dev_ioctl_fd_install(fd, filp);
276 }
277
278 return fd;
279
280out:
281 put_unused_fd(fd);
282 return err;
283}
284
285/* Open a file descriptor on an autofs mount point */
286static int autofs_dev_ioctl_openmount(struct file *fp,
287 struct autofs_sb_info *sbi,
288 struct autofs_dev_ioctl *param)
289{
290 const char *path;
291 dev_t devid;
292 int err, fd;
293
294 /* param->path has already been checked */
Ian Kent730c9ee2009-01-06 14:42:06 -0800295 if (!param->openmount.devid)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700296 return -EINVAL;
297
298 param->ioctlfd = -1;
299
300 path = param->path;
Al Viro4e44b682009-04-07 11:08:56 -0400301 devid = new_decode_dev(param->openmount.devid);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700302
303 err = 0;
304 fd = autofs_dev_ioctl_open_mountpoint(path, devid);
305 if (unlikely(fd < 0)) {
306 err = fd;
307 goto out;
308 }
309
310 param->ioctlfd = fd;
311out:
312 return err;
313}
314
315/* Close file descriptor allocated above (user can also use close(2)). */
316static int autofs_dev_ioctl_closemount(struct file *fp,
317 struct autofs_sb_info *sbi,
318 struct autofs_dev_ioctl *param)
319{
320 return sys_close(param->ioctlfd);
321}
322
323/*
324 * Send "ready" status for an existing wait (either a mount or an expire
325 * request).
326 */
327static int autofs_dev_ioctl_ready(struct file *fp,
328 struct autofs_sb_info *sbi,
329 struct autofs_dev_ioctl *param)
330{
331 autofs_wqt_t token;
332
Ian Kent730c9ee2009-01-06 14:42:06 -0800333 token = (autofs_wqt_t) param->ready.token;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700334 return autofs4_wait_release(sbi, token, 0);
335}
336
337/*
338 * Send "fail" status for an existing wait (either a mount or an expire
339 * request).
340 */
341static int autofs_dev_ioctl_fail(struct file *fp,
342 struct autofs_sb_info *sbi,
343 struct autofs_dev_ioctl *param)
344{
345 autofs_wqt_t token;
346 int status;
347
Ian Kent730c9ee2009-01-06 14:42:06 -0800348 token = (autofs_wqt_t) param->fail.token;
349 status = param->fail.status ? param->fail.status : -ENOENT;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700350 return autofs4_wait_release(sbi, token, status);
351}
352
353/*
354 * Set the pipe fd for kernel communication to the daemon.
355 *
356 * Normally this is set at mount using an option but if we
357 * are reconnecting to a busy mount then we need to use this
358 * to tell the autofs mount about the new kernel pipe fd. In
359 * order to protect mounts against incorrectly setting the
360 * pipefd we also require that the autofs mount be catatonic.
361 *
362 * This also sets the process group id used to identify the
363 * controlling process (eg. the owning automount(8) daemon).
364 */
365static int autofs_dev_ioctl_setpipefd(struct file *fp,
366 struct autofs_sb_info *sbi,
367 struct autofs_dev_ioctl *param)
368{
369 int pipefd;
370 int err = 0;
371
Ian Kent730c9ee2009-01-06 14:42:06 -0800372 if (param->setpipefd.pipefd == -1)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700373 return -EINVAL;
374
Ian Kent730c9ee2009-01-06 14:42:06 -0800375 pipefd = param->setpipefd.pipefd;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700376
377 mutex_lock(&sbi->wq_mutex);
378 if (!sbi->catatonic) {
379 mutex_unlock(&sbi->wq_mutex);
380 return -EBUSY;
381 } else {
382 struct file *pipe = fget(pipefd);
383 if (!pipe->f_op || !pipe->f_op->write) {
384 err = -EPIPE;
385 fput(pipe);
386 goto out;
387 }
388 sbi->oz_pgrp = task_pgrp_nr(current);
389 sbi->pipefd = pipefd;
390 sbi->pipe = pipe;
391 sbi->catatonic = 0;
392 }
393out:
394 mutex_unlock(&sbi->wq_mutex);
395 return err;
396}
397
398/*
399 * Make the autofs mount point catatonic, no longer responsive to
400 * mount requests. Also closes the kernel pipe file descriptor.
401 */
402static int autofs_dev_ioctl_catatonic(struct file *fp,
403 struct autofs_sb_info *sbi,
404 struct autofs_dev_ioctl *param)
405{
406 autofs4_catatonic_mode(sbi);
407 return 0;
408}
409
410/* Set the autofs mount timeout */
411static int autofs_dev_ioctl_timeout(struct file *fp,
412 struct autofs_sb_info *sbi,
413 struct autofs_dev_ioctl *param)
414{
415 unsigned long timeout;
416
Ian Kent730c9ee2009-01-06 14:42:06 -0800417 timeout = param->timeout.timeout;
418 param->timeout.timeout = sbi->exp_timeout / HZ;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700419 sbi->exp_timeout = timeout * HZ;
420 return 0;
421}
422
423/*
424 * Return the uid and gid of the last request for the mount
425 *
426 * When reconstructing an autofs mount tree with active mounts
427 * we need to re-connect to mounts that may have used the original
428 * process uid and gid (or string variations of them) for mount
429 * lookups within the map entry.
430 */
431static int autofs_dev_ioctl_requester(struct file *fp,
432 struct autofs_sb_info *sbi,
433 struct autofs_dev_ioctl *param)
434{
435 struct autofs_info *ino;
Al Viro4e44b682009-04-07 11:08:56 -0400436 struct path path;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700437 dev_t devid;
438 int err = -ENOENT;
439
440 if (param->size <= sizeof(*param)) {
441 err = -EINVAL;
442 goto out;
443 }
444
Al Viro4e44b682009-04-07 11:08:56 -0400445 devid = sbi->sb->s_dev;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700446
Ian Kent730c9ee2009-01-06 14:42:06 -0800447 param->requester.uid = param->requester.gid = -1;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700448
Al Viro4e44b682009-04-07 11:08:56 -0400449 err = find_autofs_mount(param->path, &path, test_by_dev, &devid);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700450 if (err)
451 goto out;
452
Al Viro4e44b682009-04-07 11:08:56 -0400453 ino = autofs4_dentry_ino(path.dentry);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700454 if (ino) {
455 err = 0;
Al Viro4e44b682009-04-07 11:08:56 -0400456 autofs4_expire_wait(path.dentry);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700457 spin_lock(&sbi->fs_lock);
Ian Kent730c9ee2009-01-06 14:42:06 -0800458 param->requester.uid = ino->uid;
459 param->requester.gid = ino->gid;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700460 spin_unlock(&sbi->fs_lock);
461 }
Al Viro4e44b682009-04-07 11:08:56 -0400462 path_put(&path);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700463out:
464 return err;
465}
466
467/*
468 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
469 * more that can be done.
470 */
471static int autofs_dev_ioctl_expire(struct file *fp,
472 struct autofs_sb_info *sbi,
473 struct autofs_dev_ioctl *param)
474{
Ian Kent8d7b48e2008-10-15 22:02:54 -0700475 struct vfsmount *mnt;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700476 int how;
477
Ian Kent730c9ee2009-01-06 14:42:06 -0800478 how = param->expire.how;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700479 mnt = fp->f_path.mnt;
480
Ian Kent56fcef72009-03-31 15:24:43 -0700481 return autofs4_do_expire_multi(sbi->sb, mnt, sbi, how);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700482}
483
484/* Check if autofs mount point is in use */
485static int autofs_dev_ioctl_askumount(struct file *fp,
486 struct autofs_sb_info *sbi,
487 struct autofs_dev_ioctl *param)
488{
Ian Kent730c9ee2009-01-06 14:42:06 -0800489 param->askumount.may_umount = 0;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700490 if (may_umount(fp->f_path.mnt))
Ian Kent730c9ee2009-01-06 14:42:06 -0800491 param->askumount.may_umount = 1;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700492 return 0;
493}
494
495/*
496 * Check if the given path is a mountpoint.
497 *
498 * If we are supplied with the file descriptor of an autofs
499 * mount we're looking for a specific mount. In this case
500 * the path is considered a mountpoint if it is itself a
501 * mountpoint or contains a mount, such as a multi-mount
502 * without a root mount. In this case we return 1 if the
503 * path is a mount point and the super magic of the covering
504 * mount if there is one or 0 if it isn't a mountpoint.
505 *
506 * If we aren't supplied with a file descriptor then we
507 * lookup the nameidata of the path and check if it is the
508 * root of a mount. If a type is given we are looking for
509 * a particular autofs mount and if we don't find a match
510 * we return fail. If the located nameidata path is the
511 * root of a mount we return 1 along with the super magic
512 * of the mount or 0 otherwise.
513 *
514 * In both cases the the device number (as returned by
515 * new_encode_dev()) is also returned.
516 */
517static int autofs_dev_ioctl_ismountpoint(struct file *fp,
518 struct autofs_sb_info *sbi,
519 struct autofs_dev_ioctl *param)
520{
Al Viro4e44b682009-04-07 11:08:56 -0400521 struct path path;
522 const char *name;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700523 unsigned int type;
Ian Kent730c9ee2009-01-06 14:42:06 -0800524 unsigned int devid, magic;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700525 int err = -ENOENT;
526
527 if (param->size <= sizeof(*param)) {
528 err = -EINVAL;
529 goto out;
530 }
531
Al Viro4e44b682009-04-07 11:08:56 -0400532 name = param->path;
Ian Kent730c9ee2009-01-06 14:42:06 -0800533 type = param->ismountpoint.in.type;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700534
Ian Kent730c9ee2009-01-06 14:42:06 -0800535 param->ismountpoint.out.devid = devid = 0;
536 param->ismountpoint.out.magic = magic = 0;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700537
538 if (!fp || param->ioctlfd == -1) {
Al Viro4e44b682009-04-07 11:08:56 -0400539 if (autofs_type_any(type))
540 err = kern_path(name, LOOKUP_FOLLOW, &path);
541 else
542 err = find_autofs_mount(name, &path, test_by_type, &type);
543 if (err)
544 goto out;
545 devid = new_encode_dev(path.mnt->mnt_sb->s_dev);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700546 err = 0;
Al Virof598f9f2010-01-23 20:08:53 -0500547 if (path.mnt->mnt_root == path.dentry) {
Ian Kent8d7b48e2008-10-15 22:02:54 -0700548 err = 1;
Al Virof598f9f2010-01-23 20:08:53 -0500549 magic = path.mnt->mnt_sb->s_magic;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700550 }
551 } else {
Al Viro4e44b682009-04-07 11:08:56 -0400552 dev_t dev = sbi->sb->s_dev;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700553
Al Viro4e44b682009-04-07 11:08:56 -0400554 err = find_autofs_mount(name, &path, test_by_dev, &dev);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700555 if (err)
556 goto out;
557
Al Viro4e44b682009-04-07 11:08:56 -0400558 devid = new_encode_dev(dev);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700559
Al Viro4e44b682009-04-07 11:08:56 -0400560 err = have_submounts(path.dentry);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700561
Al Virof598f9f2010-01-23 20:08:53 -0500562 if (follow_down(&path))
563 magic = path.mnt->mnt_sb->s_magic;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700564 }
565
Ian Kent730c9ee2009-01-06 14:42:06 -0800566 param->ismountpoint.out.devid = devid;
567 param->ismountpoint.out.magic = magic;
Al Viro4e44b682009-04-07 11:08:56 -0400568 path_put(&path);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700569out:
570 return err;
571}
572
573/*
574 * Our range of ioctl numbers isn't 0 based so we need to shift
575 * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
576 * lookup.
577 */
578#define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
579
580static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
581{
582 static struct {
583 int cmd;
584 ioctl_fn fn;
585 } _ioctls[] = {
586 {cmd_idx(AUTOFS_DEV_IOCTL_VERSION_CMD), NULL},
587 {cmd_idx(AUTOFS_DEV_IOCTL_PROTOVER_CMD),
588 autofs_dev_ioctl_protover},
589 {cmd_idx(AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD),
590 autofs_dev_ioctl_protosubver},
591 {cmd_idx(AUTOFS_DEV_IOCTL_OPENMOUNT_CMD),
592 autofs_dev_ioctl_openmount},
593 {cmd_idx(AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD),
594 autofs_dev_ioctl_closemount},
595 {cmd_idx(AUTOFS_DEV_IOCTL_READY_CMD),
596 autofs_dev_ioctl_ready},
597 {cmd_idx(AUTOFS_DEV_IOCTL_FAIL_CMD),
598 autofs_dev_ioctl_fail},
599 {cmd_idx(AUTOFS_DEV_IOCTL_SETPIPEFD_CMD),
600 autofs_dev_ioctl_setpipefd},
601 {cmd_idx(AUTOFS_DEV_IOCTL_CATATONIC_CMD),
602 autofs_dev_ioctl_catatonic},
603 {cmd_idx(AUTOFS_DEV_IOCTL_TIMEOUT_CMD),
604 autofs_dev_ioctl_timeout},
605 {cmd_idx(AUTOFS_DEV_IOCTL_REQUESTER_CMD),
606 autofs_dev_ioctl_requester},
607 {cmd_idx(AUTOFS_DEV_IOCTL_EXPIRE_CMD),
608 autofs_dev_ioctl_expire},
609 {cmd_idx(AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD),
610 autofs_dev_ioctl_askumount},
611 {cmd_idx(AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD),
612 autofs_dev_ioctl_ismountpoint}
613 };
614 unsigned int idx = cmd_idx(cmd);
615
616 return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx].fn;
617}
618
619/* ioctl dispatcher */
620static int _autofs_dev_ioctl(unsigned int command, struct autofs_dev_ioctl __user *user)
621{
622 struct autofs_dev_ioctl *param;
623 struct file *fp;
624 struct autofs_sb_info *sbi;
625 unsigned int cmd_first, cmd;
626 ioctl_fn fn = NULL;
627 int err = 0;
628
629 /* only root can play with this */
630 if (!capable(CAP_SYS_ADMIN))
631 return -EPERM;
632
633 cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
634 cmd = _IOC_NR(command);
635
636 if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
637 cmd - cmd_first >= AUTOFS_DEV_IOCTL_IOC_COUNT) {
638 return -ENOTTY;
639 }
640
641 /* Copy the parameters into kernel space. */
642 param = copy_dev_ioctl(user);
643 if (IS_ERR(param))
644 return PTR_ERR(param);
645
646 err = validate_dev_ioctl(command, param);
647 if (err)
648 goto out;
649
650 /* The validate routine above always sets the version */
651 if (cmd == AUTOFS_DEV_IOCTL_VERSION_CMD)
652 goto done;
653
654 fn = lookup_dev_ioctl(cmd);
655 if (!fn) {
656 AUTOFS_WARN("unknown command 0x%08x", command);
657 return -ENOTTY;
658 }
659
660 fp = NULL;
661 sbi = NULL;
662
663 /*
664 * For obvious reasons the openmount can't have a file
665 * descriptor yet. We don't take a reference to the
666 * file during close to allow for immediate release.
667 */
668 if (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 if (!fp->f_op) {
679 err = -ENOTTY;
680 fput(fp);
681 goto out;
682 }
683
684 sbi = autofs_dev_ioctl_sbi(fp);
685 if (!sbi || sbi->magic != AUTOFS_SBI_MAGIC) {
686 err = -EINVAL;
687 fput(fp);
688 goto out;
689 }
690
691 /*
692 * Admin needs to be able to set the mount catatonic in
693 * order to be able to perform the re-open.
694 */
695 if (!autofs4_oz_mode(sbi) &&
696 cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
697 err = -EACCES;
698 fput(fp);
699 goto out;
700 }
701 }
702cont:
703 err = fn(fp, sbi, param);
704
705 if (fp)
706 fput(fp);
707done:
708 if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
709 err = -EFAULT;
710out:
711 free_dev_ioctl(param);
712 return err;
713}
714
715static long autofs_dev_ioctl(struct file *file, uint command, ulong u)
716{
717 int err;
718 err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
719 return (long) err;
720}
721
722#ifdef CONFIG_COMPAT
723static long autofs_dev_ioctl_compat(struct file *file, uint command, ulong u)
724{
725 return (long) autofs_dev_ioctl(file, command, (ulong) compat_ptr(u));
726}
727#else
728#define autofs_dev_ioctl_compat NULL
729#endif
730
731static const struct file_operations _dev_ioctl_fops = {
732 .unlocked_ioctl = autofs_dev_ioctl,
733 .compat_ioctl = autofs_dev_ioctl_compat,
734 .owner = THIS_MODULE,
735};
736
737static struct miscdevice _autofs_dev_ioctl_misc = {
738 .minor = MISC_DYNAMIC_MINOR,
739 .name = AUTOFS_DEVICE_NAME,
740 .fops = &_dev_ioctl_fops
741};
742
743/* Register/deregister misc character device */
744int autofs_dev_ioctl_init(void)
745{
746 int r;
747
748 r = misc_register(&_autofs_dev_ioctl_misc);
749 if (r) {
750 AUTOFS_ERROR("misc_register failed for control device");
751 return r;
752 }
753
754 return 0;
755}
756
757void autofs_dev_ioctl_exit(void)
758{
759 misc_deregister(&_autofs_dev_ioctl_misc);
760 return;
761}
762