Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 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/compat.h> |
| 21 | #include <linux/syscalls.h> |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 22 | #include <linux/magic.h> |
| 23 | #include <linux/dcache.h> |
| 24 | #include <linux/uaccess.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 26 | |
| 27 | #include "autofs_i.h" |
| 28 | |
| 29 | /* |
| 30 | * This module implements an interface for routing autofs ioctl control |
| 31 | * commands via a miscellaneous device file. |
| 32 | * |
| 33 | * The alternate interface is needed because we need to be able open |
| 34 | * an ioctl file descriptor on an autofs mount that may be covered by |
| 35 | * another mount. This situation arises when starting automount(8) |
| 36 | * or other user space daemon which uses direct mounts or offset |
| 37 | * mounts (used for autofs lazy mount/umount of nested mount trees), |
| 38 | * which have been left busy at at service shutdown. |
| 39 | */ |
| 40 | |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 41 | typedef int (*ioctl_fn)(struct file *, struct autofs_sb_info *, |
| 42 | struct autofs_dev_ioctl *); |
| 43 | |
| 44 | static int check_name(const char *name) |
| 45 | { |
| 46 | if (!strchr(name, '/')) |
| 47 | return -EINVAL; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * Check a string doesn't overrun the chunk of |
| 53 | * memory we copied from user land. |
| 54 | */ |
Al Viro | 3eac877 | 2009-04-07 11:12:46 -0400 | [diff] [blame] | 55 | static int invalid_str(char *str, size_t size) |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 56 | { |
Al Viro | 3eac877 | 2009-04-07 11:12:46 -0400 | [diff] [blame] | 57 | if (memchr(str, 0, size)) |
| 58 | return 0; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 59 | return -EINVAL; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Check that the user compiled against correct version of autofs |
| 64 | * misc device code. |
| 65 | * |
| 66 | * As well as checking the version compatibility this always copies |
| 67 | * the kernel interface version out. |
| 68 | */ |
| 69 | static int check_dev_ioctl_version(int cmd, struct autofs_dev_ioctl *param) |
| 70 | { |
| 71 | int err = 0; |
| 72 | |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 73 | if ((param->ver_major != AUTOFS_DEV_IOCTL_VERSION_MAJOR) || |
| 74 | (param->ver_minor > AUTOFS_DEV_IOCTL_VERSION_MINOR)) { |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 75 | pr_warn("ioctl control interface version mismatch: " |
Tomohiro Kusumi | 3908555 | 2016-10-11 13:53:08 -0700 | [diff] [blame] | 76 | "kernel(%u.%u), user(%u.%u), cmd(0x%08x)\n", |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 77 | AUTOFS_DEV_IOCTL_VERSION_MAJOR, |
| 78 | AUTOFS_DEV_IOCTL_VERSION_MINOR, |
| 79 | param->ver_major, param->ver_minor, cmd); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 80 | err = -EINVAL; |
| 81 | } |
| 82 | |
| 83 | /* Fill in the kernel version. */ |
| 84 | param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR; |
| 85 | param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR; |
| 86 | |
| 87 | return err; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Copy parameter control struct, including a possible path allocated |
| 92 | * at the end of the struct. |
| 93 | */ |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 94 | static struct autofs_dev_ioctl * |
| 95 | copy_dev_ioctl(struct autofs_dev_ioctl __user *in) |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 96 | { |
Al Viro | 0a28096 | 2015-02-21 22:19:57 -0500 | [diff] [blame] | 97 | struct autofs_dev_ioctl tmp, *res; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 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 | |
Sasha Levin | e53d77e | 2014-04-08 16:04:11 -0700 | [diff] [blame] | 105 | if (tmp.size > (PATH_MAX + sizeof(tmp))) |
| 106 | return ERR_PTR(-ENAMETOOLONG); |
| 107 | |
Al Viro | 0a28096 | 2015-02-21 22:19:57 -0500 | [diff] [blame] | 108 | res = memdup_user(in, tmp.size); |
| 109 | if (!IS_ERR(res)) |
| 110 | res->size = tmp.size; |
| 111 | |
| 112 | return res; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static inline void free_dev_ioctl(struct autofs_dev_ioctl *param) |
| 116 | { |
| 117 | kfree(param); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Check sanity of parameter control fields and if a path is present |
Ian Kent | bae8ec6 | 2009-01-06 14:42:09 -0800 | [diff] [blame] | 122 | * check that it is terminated and contains at least one "/". |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 123 | */ |
| 124 | static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param) |
| 125 | { |
Ian Kent | 96b0317 | 2008-11-06 12:53:23 -0800 | [diff] [blame] | 126 | int err; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 127 | |
Ian Kent | 96b0317 | 2008-11-06 12:53:23 -0800 | [diff] [blame] | 128 | err = check_dev_ioctl_version(cmd, param); |
| 129 | if (err) { |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 130 | pr_warn("invalid device control module version " |
| 131 | "supplied for cmd(0x%08x)\n", cmd); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 132 | goto out; |
| 133 | } |
| 134 | |
| 135 | if (param->size > sizeof(*param)) { |
Al Viro | 3eac877 | 2009-04-07 11:12:46 -0400 | [diff] [blame] | 136 | err = invalid_str(param->path, param->size - sizeof(*param)); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 137 | if (err) { |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 138 | pr_warn( |
Ian Kent | 90967c8 | 2016-03-15 14:58:42 -0700 | [diff] [blame] | 139 | "path string terminator missing for cmd(0x%08x)\n", |
Ian Kent | bae8ec6 | 2009-01-06 14:42:09 -0800 | [diff] [blame] | 140 | cmd); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 141 | goto out; |
| 142 | } |
| 143 | |
Ian Kent | bae8ec6 | 2009-01-06 14:42:09 -0800 | [diff] [blame] | 144 | err = check_name(param->path); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 145 | if (err) { |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 146 | pr_warn("invalid path supplied for cmd(0x%08x)\n", |
| 147 | cmd); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 148 | goto out; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | err = 0; |
| 153 | out: |
| 154 | return err; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Get the autofs super block info struct from the file opened on |
| 159 | * the autofs mount point. |
| 160 | */ |
| 161 | static struct autofs_sb_info *autofs_dev_ioctl_sbi(struct file *f) |
| 162 | { |
| 163 | struct autofs_sb_info *sbi = NULL; |
| 164 | struct inode *inode; |
| 165 | |
| 166 | if (f) { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 167 | inode = file_inode(f); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 168 | sbi = autofs4_sbi(inode->i_sb); |
| 169 | } |
| 170 | return sbi; |
| 171 | } |
| 172 | |
Ian Kent | d9e1923 | 2016-10-11 13:53:05 -0700 | [diff] [blame] | 173 | /* Return autofs dev ioctl version */ |
| 174 | static int autofs_dev_ioctl_version(struct file *fp, |
| 175 | struct autofs_sb_info *sbi, |
| 176 | struct autofs_dev_ioctl *param) |
| 177 | { |
| 178 | /* This should have already been set. */ |
| 179 | param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR; |
| 180 | param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR; |
| 181 | return 0; |
| 182 | } |
| 183 | |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 184 | /* Return autofs module protocol version */ |
| 185 | static int autofs_dev_ioctl_protover(struct file *fp, |
| 186 | struct autofs_sb_info *sbi, |
| 187 | struct autofs_dev_ioctl *param) |
| 188 | { |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 189 | param->protover.version = sbi->version; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | /* Return autofs module protocol sub version */ |
| 194 | static int autofs_dev_ioctl_protosubver(struct file *fp, |
| 195 | struct autofs_sb_info *sbi, |
| 196 | struct autofs_dev_ioctl *param) |
| 197 | { |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 198 | param->protosubver.sub_version = sbi->sub_version; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
Ian Kent | ac83871 | 2013-09-08 16:47:23 +0800 | [diff] [blame] | 202 | /* Find the topmost mount satisfying test() */ |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 203 | static int find_autofs_mount(const char *pathname, |
| 204 | struct path *res, |
Al Viro | 5b5577e | 2016-11-20 19:33:32 -0500 | [diff] [blame] | 205 | int test(const struct path *path, void *data), |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 206 | void *data) |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 207 | { |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 208 | struct path path; |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 209 | int err; |
| 210 | |
| 211 | err = kern_path_mountpoint(AT_FDCWD, pathname, &path, 0); |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 212 | if (err) |
| 213 | return err; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 214 | err = -ENOENT; |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 215 | while (path.dentry == path.mnt->mnt_root) { |
Al Viro | d8c9584 | 2011-12-07 18:16:57 -0500 | [diff] [blame] | 216 | if (path.dentry->d_sb->s_magic == AUTOFS_SUPER_MAGIC) { |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 217 | if (test(&path, data)) { |
| 218 | path_get(&path); |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 219 | *res = path; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 220 | err = 0; |
Ian Kent | ac83871 | 2013-09-08 16:47:23 +0800 | [diff] [blame] | 221 | break; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 224 | if (!follow_up(&path)) |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 225 | break; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 226 | } |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 227 | path_put(&path); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 228 | return err; |
| 229 | } |
| 230 | |
Al Viro | 5b5577e | 2016-11-20 19:33:32 -0500 | [diff] [blame] | 231 | static int test_by_dev(const struct path *path, void *p) |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 232 | { |
Al Viro | d8c9584 | 2011-12-07 18:16:57 -0500 | [diff] [blame] | 233 | return path->dentry->d_sb->s_dev == *(dev_t *)p; |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 234 | } |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 235 | |
Al Viro | 5b5577e | 2016-11-20 19:33:32 -0500 | [diff] [blame] | 236 | static int test_by_type(const struct path *path, void *p) |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 237 | { |
| 238 | struct autofs_info *ino = autofs4_dentry_ino(path->dentry); |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 239 | |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 240 | return ino && ino->sbi->type & *(unsigned *)p; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 243 | /* |
| 244 | * Open a file descriptor on the autofs mount point corresponding |
| 245 | * to the given path and device number (aka. new_encode_dev(sb->s_dev)). |
| 246 | */ |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 247 | static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid) |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 248 | { |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 249 | int err, fd; |
| 250 | |
Al Viro | c921b40 | 2012-08-12 18:04:37 -0400 | [diff] [blame] | 251 | fd = get_unused_fd_flags(O_CLOEXEC); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 252 | if (likely(fd >= 0)) { |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 253 | struct file *filp; |
| 254 | struct path path; |
| 255 | |
| 256 | err = find_autofs_mount(name, &path, test_by_dev, &devid); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 257 | if (err) |
| 258 | goto out; |
| 259 | |
| 260 | /* |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 261 | * Find autofs super block that has the device number |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 262 | * corresponding to the autofs fs we want to open. |
| 263 | */ |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 264 | |
Al Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 265 | filp = dentry_open(&path, O_RDONLY, current_cred()); |
| 266 | path_put(&path); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 267 | if (IS_ERR(filp)) { |
| 268 | err = PTR_ERR(filp); |
| 269 | goto out; |
| 270 | } |
| 271 | |
Al Viro | c921b40 | 2012-08-12 18:04:37 -0400 | [diff] [blame] | 272 | fd_install(fd, filp); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | return fd; |
| 276 | |
| 277 | out: |
| 278 | put_unused_fd(fd); |
| 279 | return err; |
| 280 | } |
| 281 | |
| 282 | /* Open a file descriptor on an autofs mount point */ |
| 283 | static int autofs_dev_ioctl_openmount(struct file *fp, |
| 284 | struct autofs_sb_info *sbi, |
| 285 | struct autofs_dev_ioctl *param) |
| 286 | { |
| 287 | const char *path; |
| 288 | dev_t devid; |
| 289 | int err, fd; |
| 290 | |
| 291 | /* param->path has already been checked */ |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 292 | if (!param->openmount.devid) |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 293 | return -EINVAL; |
| 294 | |
| 295 | param->ioctlfd = -1; |
| 296 | |
| 297 | path = param->path; |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 298 | devid = new_decode_dev(param->openmount.devid); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 299 | |
| 300 | err = 0; |
| 301 | fd = autofs_dev_ioctl_open_mountpoint(path, devid); |
| 302 | if (unlikely(fd < 0)) { |
| 303 | err = fd; |
| 304 | goto out; |
| 305 | } |
| 306 | |
| 307 | param->ioctlfd = fd; |
| 308 | out: |
| 309 | return err; |
| 310 | } |
| 311 | |
| 312 | /* Close file descriptor allocated above (user can also use close(2)). */ |
| 313 | static int autofs_dev_ioctl_closemount(struct file *fp, |
| 314 | struct autofs_sb_info *sbi, |
| 315 | struct autofs_dev_ioctl *param) |
| 316 | { |
| 317 | return sys_close(param->ioctlfd); |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Send "ready" status for an existing wait (either a mount or an expire |
| 322 | * request). |
| 323 | */ |
| 324 | static int autofs_dev_ioctl_ready(struct file *fp, |
| 325 | struct autofs_sb_info *sbi, |
| 326 | struct autofs_dev_ioctl *param) |
| 327 | { |
| 328 | autofs_wqt_t token; |
| 329 | |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 330 | token = (autofs_wqt_t) param->ready.token; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 331 | return autofs4_wait_release(sbi, token, 0); |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Send "fail" status for an existing wait (either a mount or an expire |
| 336 | * request). |
| 337 | */ |
| 338 | static int autofs_dev_ioctl_fail(struct file *fp, |
| 339 | struct autofs_sb_info *sbi, |
| 340 | struct autofs_dev_ioctl *param) |
| 341 | { |
| 342 | autofs_wqt_t token; |
| 343 | int status; |
| 344 | |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 345 | token = (autofs_wqt_t) param->fail.token; |
| 346 | status = param->fail.status ? param->fail.status : -ENOENT; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 347 | return autofs4_wait_release(sbi, token, status); |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Set the pipe fd for kernel communication to the daemon. |
| 352 | * |
| 353 | * Normally this is set at mount using an option but if we |
| 354 | * are reconnecting to a busy mount then we need to use this |
| 355 | * to tell the autofs mount about the new kernel pipe fd. In |
| 356 | * order to protect mounts against incorrectly setting the |
| 357 | * pipefd we also require that the autofs mount be catatonic. |
| 358 | * |
| 359 | * This also sets the process group id used to identify the |
| 360 | * controlling process (eg. the owning automount(8) daemon). |
| 361 | */ |
| 362 | static int autofs_dev_ioctl_setpipefd(struct file *fp, |
| 363 | struct autofs_sb_info *sbi, |
| 364 | struct autofs_dev_ioctl *param) |
| 365 | { |
| 366 | int pipefd; |
| 367 | int err = 0; |
Sukadev Bhattiprolu | 6eaba35 | 2014-01-23 15:54:57 -0800 | [diff] [blame] | 368 | struct pid *new_pid = NULL; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 369 | |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 370 | if (param->setpipefd.pipefd == -1) |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 371 | return -EINVAL; |
| 372 | |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 373 | pipefd = param->setpipefd.pipefd; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 374 | |
| 375 | mutex_lock(&sbi->wq_mutex); |
| 376 | if (!sbi->catatonic) { |
| 377 | mutex_unlock(&sbi->wq_mutex); |
| 378 | return -EBUSY; |
| 379 | } else { |
Sukadev Bhattiprolu | 6eaba35 | 2014-01-23 15:54:57 -0800 | [diff] [blame] | 380 | struct file *pipe; |
| 381 | |
| 382 | new_pid = get_task_pid(current, PIDTYPE_PGID); |
| 383 | |
| 384 | if (ns_of_pid(new_pid) != ns_of_pid(sbi->oz_pgrp)) { |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 385 | pr_warn("not allowed to change PID namespace\n"); |
Sukadev Bhattiprolu | 6eaba35 | 2014-01-23 15:54:57 -0800 | [diff] [blame] | 386 | err = -EINVAL; |
| 387 | goto out; |
| 388 | } |
| 389 | |
| 390 | pipe = fget(pipefd); |
Jesper Juhl | 3dc8fe4 | 2011-03-25 01:51:37 +0800 | [diff] [blame] | 391 | if (!pipe) { |
| 392 | err = -EBADF; |
| 393 | goto out; |
| 394 | } |
Linus Torvalds | 64f371b | 2012-04-29 13:30:08 -0700 | [diff] [blame] | 395 | if (autofs_prepare_pipe(pipe) < 0) { |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 396 | err = -EPIPE; |
| 397 | fput(pipe); |
| 398 | goto out; |
| 399 | } |
Sukadev Bhattiprolu | 6eaba35 | 2014-01-23 15:54:57 -0800 | [diff] [blame] | 400 | swap(sbi->oz_pgrp, new_pid); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 401 | sbi->pipefd = pipefd; |
| 402 | sbi->pipe = pipe; |
| 403 | sbi->catatonic = 0; |
| 404 | } |
| 405 | out: |
Sukadev Bhattiprolu | 6eaba35 | 2014-01-23 15:54:57 -0800 | [diff] [blame] | 406 | put_pid(new_pid); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 407 | mutex_unlock(&sbi->wq_mutex); |
| 408 | return err; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Make the autofs mount point catatonic, no longer responsive to |
| 413 | * mount requests. Also closes the kernel pipe file descriptor. |
| 414 | */ |
| 415 | static int autofs_dev_ioctl_catatonic(struct file *fp, |
| 416 | struct autofs_sb_info *sbi, |
| 417 | struct autofs_dev_ioctl *param) |
| 418 | { |
| 419 | autofs4_catatonic_mode(sbi); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | /* Set the autofs mount timeout */ |
| 424 | static int autofs_dev_ioctl_timeout(struct file *fp, |
| 425 | struct autofs_sb_info *sbi, |
| 426 | struct autofs_dev_ioctl *param) |
| 427 | { |
| 428 | unsigned long timeout; |
| 429 | |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 430 | timeout = param->timeout.timeout; |
| 431 | param->timeout.timeout = sbi->exp_timeout / HZ; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 432 | sbi->exp_timeout = timeout * HZ; |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Return the uid and gid of the last request for the mount |
| 438 | * |
| 439 | * When reconstructing an autofs mount tree with active mounts |
| 440 | * we need to re-connect to mounts that may have used the original |
| 441 | * process uid and gid (or string variations of them) for mount |
| 442 | * lookups within the map entry. |
| 443 | */ |
| 444 | static int autofs_dev_ioctl_requester(struct file *fp, |
| 445 | struct autofs_sb_info *sbi, |
| 446 | struct autofs_dev_ioctl *param) |
| 447 | { |
| 448 | struct autofs_info *ino; |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 449 | struct path path; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 450 | dev_t devid; |
| 451 | int err = -ENOENT; |
| 452 | |
| 453 | if (param->size <= sizeof(*param)) { |
| 454 | err = -EINVAL; |
| 455 | goto out; |
| 456 | } |
| 457 | |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 458 | devid = sbi->sb->s_dev; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 459 | |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 460 | param->requester.uid = param->requester.gid = -1; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 461 | |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 462 | err = find_autofs_mount(param->path, &path, test_by_dev, &devid); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 463 | if (err) |
| 464 | goto out; |
| 465 | |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 466 | ino = autofs4_dentry_ino(path.dentry); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 467 | if (ino) { |
| 468 | err = 0; |
Ian Kent | 74f504c | 2016-11-24 08:03:41 +1100 | [diff] [blame] | 469 | autofs4_expire_wait(&path, 0); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 470 | spin_lock(&sbi->fs_lock); |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 471 | param->requester.uid = |
| 472 | from_kuid_munged(current_user_ns(), ino->uid); |
| 473 | param->requester.gid = |
| 474 | from_kgid_munged(current_user_ns(), ino->gid); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 475 | spin_unlock(&sbi->fs_lock); |
| 476 | } |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 477 | path_put(&path); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 478 | out: |
| 479 | return err; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Call repeatedly until it returns -EAGAIN, meaning there's nothing |
| 484 | * more that can be done. |
| 485 | */ |
| 486 | static int autofs_dev_ioctl_expire(struct file *fp, |
| 487 | struct autofs_sb_info *sbi, |
| 488 | struct autofs_dev_ioctl *param) |
| 489 | { |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 490 | struct vfsmount *mnt; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 491 | int how; |
| 492 | |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 493 | how = param->expire.how; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 494 | mnt = fp->f_path.mnt; |
| 495 | |
Ian Kent | 56fcef7 | 2009-03-31 15:24:43 -0700 | [diff] [blame] | 496 | return autofs4_do_expire_multi(sbi->sb, mnt, sbi, how); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /* Check if autofs mount point is in use */ |
| 500 | static int autofs_dev_ioctl_askumount(struct file *fp, |
| 501 | struct autofs_sb_info *sbi, |
| 502 | struct autofs_dev_ioctl *param) |
| 503 | { |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 504 | param->askumount.may_umount = 0; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 505 | if (may_umount(fp->f_path.mnt)) |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 506 | param->askumount.may_umount = 1; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Check if the given path is a mountpoint. |
| 512 | * |
| 513 | * If we are supplied with the file descriptor of an autofs |
| 514 | * mount we're looking for a specific mount. In this case |
| 515 | * the path is considered a mountpoint if it is itself a |
| 516 | * mountpoint or contains a mount, such as a multi-mount |
| 517 | * without a root mount. In this case we return 1 if the |
| 518 | * path is a mount point and the super magic of the covering |
| 519 | * mount if there is one or 0 if it isn't a mountpoint. |
| 520 | * |
| 521 | * If we aren't supplied with a file descriptor then we |
Ian Kent | ac83871 | 2013-09-08 16:47:23 +0800 | [diff] [blame] | 522 | * lookup the path and check if it is the root of a mount. |
| 523 | * If a type is given we are looking for a particular autofs |
| 524 | * mount and if we don't find a match we return fail. If the |
| 525 | * located path is the root of a mount we return 1 along with |
| 526 | * the super magic of the mount or 0 otherwise. |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 527 | * |
| 528 | * In both cases the the device number (as returned by |
| 529 | * new_encode_dev()) is also returned. |
| 530 | */ |
| 531 | static int autofs_dev_ioctl_ismountpoint(struct file *fp, |
| 532 | struct autofs_sb_info *sbi, |
| 533 | struct autofs_dev_ioctl *param) |
| 534 | { |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 535 | struct path path; |
| 536 | const char *name; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 537 | unsigned int type; |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 538 | unsigned int devid, magic; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 539 | int err = -ENOENT; |
| 540 | |
| 541 | if (param->size <= sizeof(*param)) { |
| 542 | err = -EINVAL; |
| 543 | goto out; |
| 544 | } |
| 545 | |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 546 | name = param->path; |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 547 | type = param->ismountpoint.in.type; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 548 | |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 549 | param->ismountpoint.out.devid = devid = 0; |
| 550 | param->ismountpoint.out.magic = magic = 0; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 551 | |
| 552 | if (!fp || param->ioctlfd == -1) { |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 553 | if (autofs_type_any(type)) |
Ian Kent | ac83871 | 2013-09-08 16:47:23 +0800 | [diff] [blame] | 554 | err = kern_path_mountpoint(AT_FDCWD, |
| 555 | name, &path, LOOKUP_FOLLOW); |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 556 | else |
Ian Kent | ac83871 | 2013-09-08 16:47:23 +0800 | [diff] [blame] | 557 | err = find_autofs_mount(name, &path, |
| 558 | test_by_type, &type); |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 559 | if (err) |
| 560 | goto out; |
Al Viro | d8c9584 | 2011-12-07 18:16:57 -0500 | [diff] [blame] | 561 | devid = new_encode_dev(path.dentry->d_sb->s_dev); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 562 | err = 0; |
Al Viro | f598f9f | 2010-01-23 20:08:53 -0500 | [diff] [blame] | 563 | if (path.mnt->mnt_root == path.dentry) { |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 564 | err = 1; |
Al Viro | d8c9584 | 2011-12-07 18:16:57 -0500 | [diff] [blame] | 565 | magic = path.dentry->d_sb->s_magic; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 566 | } |
| 567 | } else { |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 568 | dev_t dev = sbi->sb->s_dev; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 569 | |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 570 | err = find_autofs_mount(name, &path, test_by_dev, &dev); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 571 | if (err) |
| 572 | goto out; |
| 573 | |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 574 | devid = new_encode_dev(dev); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 575 | |
Ian Kent | 6035974 | 2016-11-24 08:03:42 +1100 | [diff] [blame] | 576 | err = path_has_submounts(&path); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 577 | |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 578 | if (follow_down_one(&path)) |
Al Viro | d8c9584 | 2011-12-07 18:16:57 -0500 | [diff] [blame] | 579 | magic = path.dentry->d_sb->s_magic; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Ian Kent | 730c9ee | 2009-01-06 14:42:06 -0800 | [diff] [blame] | 582 | param->ismountpoint.out.devid = devid; |
| 583 | param->ismountpoint.out.magic = magic; |
Al Viro | 4e44b68 | 2009-04-07 11:08:56 -0400 | [diff] [blame] | 584 | path_put(&path); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 585 | out: |
| 586 | return err; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Our range of ioctl numbers isn't 0 based so we need to shift |
| 591 | * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table |
| 592 | * lookup. |
| 593 | */ |
| 594 | #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST)) |
| 595 | |
| 596 | static ioctl_fn lookup_dev_ioctl(unsigned int cmd) |
| 597 | { |
Tomohiro Kusumi | fcc2453 | 2016-10-11 13:53:19 -0700 | [diff] [blame] | 598 | static ioctl_fn _ioctls[] = { |
| 599 | autofs_dev_ioctl_version, |
| 600 | autofs_dev_ioctl_protover, |
| 601 | autofs_dev_ioctl_protosubver, |
| 602 | autofs_dev_ioctl_openmount, |
| 603 | autofs_dev_ioctl_closemount, |
| 604 | autofs_dev_ioctl_ready, |
| 605 | autofs_dev_ioctl_fail, |
| 606 | autofs_dev_ioctl_setpipefd, |
| 607 | autofs_dev_ioctl_catatonic, |
| 608 | autofs_dev_ioctl_timeout, |
| 609 | autofs_dev_ioctl_requester, |
| 610 | autofs_dev_ioctl_expire, |
| 611 | autofs_dev_ioctl_askumount, |
| 612 | autofs_dev_ioctl_ismountpoint, |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 613 | }; |
| 614 | unsigned int idx = cmd_idx(cmd); |
| 615 | |
Tomohiro Kusumi | fcc2453 | 2016-10-11 13:53:19 -0700 | [diff] [blame] | 616 | return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx]; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* ioctl dispatcher */ |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 620 | static int _autofs_dev_ioctl(unsigned int command, |
| 621 | struct autofs_dev_ioctl __user *user) |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 622 | { |
| 623 | struct autofs_dev_ioctl *param; |
| 624 | struct file *fp; |
| 625 | struct autofs_sb_info *sbi; |
| 626 | unsigned int cmd_first, cmd; |
| 627 | ioctl_fn fn = NULL; |
| 628 | int err = 0; |
| 629 | |
| 630 | /* only root can play with this */ |
| 631 | if (!capable(CAP_SYS_ADMIN)) |
| 632 | return -EPERM; |
| 633 | |
| 634 | cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST); |
| 635 | cmd = _IOC_NR(command); |
| 636 | |
| 637 | if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) || |
Ian Kent | aa84193 | 2016-10-11 13:53:02 -0700 | [diff] [blame] | 638 | cmd - cmd_first > AUTOFS_DEV_IOCTL_IOC_COUNT) { |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 639 | return -ENOTTY; |
| 640 | } |
| 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 | |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 651 | fn = lookup_dev_ioctl(cmd); |
| 652 | if (!fn) { |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 653 | pr_warn("unknown command 0x%08x\n", command); |
Tomohiro Kusumi | 41a4497 | 2016-10-11 13:52:48 -0700 | [diff] [blame] | 654 | err = -ENOTTY; |
| 655 | goto out; |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 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 |
Ian Kent | d9e1923 | 2016-10-11 13:53:05 -0700 | [diff] [blame] | 664 | * file during close to allow for immediate release, |
| 665 | * and the same for retrieving ioctl version. |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 666 | */ |
Ian Kent | d9e1923 | 2016-10-11 13:53:05 -0700 | [diff] [blame] | 667 | if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD && |
| 668 | cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD && |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 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 | |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 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 (!autofs4_oz_mode(sbi) && |
| 690 | cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) { |
| 691 | err = -EACCES; |
| 692 | fput(fp); |
| 693 | goto out; |
| 694 | } |
| 695 | } |
| 696 | cont: |
| 697 | err = fn(fp, sbi, param); |
| 698 | |
| 699 | if (fp) |
| 700 | fput(fp); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 701 | if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE)) |
| 702 | err = -EFAULT; |
| 703 | out: |
| 704 | free_dev_ioctl(param); |
| 705 | return err; |
| 706 | } |
| 707 | |
| 708 | static long autofs_dev_ioctl(struct file *file, uint command, ulong u) |
| 709 | { |
| 710 | int err; |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 711 | |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 712 | err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u); |
| 713 | return (long) err; |
| 714 | } |
| 715 | |
| 716 | #ifdef CONFIG_COMPAT |
| 717 | static long autofs_dev_ioctl_compat(struct file *file, uint command, ulong u) |
| 718 | { |
| 719 | return (long) autofs_dev_ioctl(file, command, (ulong) compat_ptr(u)); |
| 720 | } |
| 721 | #else |
| 722 | #define autofs_dev_ioctl_compat NULL |
| 723 | #endif |
| 724 | |
| 725 | static const struct file_operations _dev_ioctl_fops = { |
| 726 | .unlocked_ioctl = autofs_dev_ioctl, |
| 727 | .compat_ioctl = autofs_dev_ioctl_compat, |
| 728 | .owner = THIS_MODULE, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 729 | .llseek = noop_llseek, |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 730 | }; |
| 731 | |
| 732 | static struct miscdevice _autofs_dev_ioctl_misc = { |
Kay Sievers | 578454f | 2010-05-20 18:07:20 +0200 | [diff] [blame] | 733 | .minor = AUTOFS_MINOR, |
Ian Kent | e9a7c2f | 2016-03-15 14:58:25 -0700 | [diff] [blame] | 734 | .name = AUTOFS_DEVICE_NAME, |
| 735 | .fops = &_dev_ioctl_fops |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 736 | }; |
| 737 | |
Kay Sievers | 578454f | 2010-05-20 18:07:20 +0200 | [diff] [blame] | 738 | MODULE_ALIAS_MISCDEV(AUTOFS_MINOR); |
| 739 | MODULE_ALIAS("devname:autofs"); |
| 740 | |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 741 | /* Register/deregister misc character device */ |
Fabian Frederick | 3ff6db3 | 2014-06-04 16:12:21 -0700 | [diff] [blame] | 742 | int __init autofs_dev_ioctl_init(void) |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 743 | { |
| 744 | int r; |
| 745 | |
| 746 | r = misc_register(&_autofs_dev_ioctl_misc); |
| 747 | if (r) { |
Ian Kent | 8a78d59 | 2016-03-15 14:58:45 -0700 | [diff] [blame] | 748 | pr_err("misc_register failed for control device\n"); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 749 | return r; |
| 750 | } |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | void autofs_dev_ioctl_exit(void) |
| 756 | { |
| 757 | misc_deregister(&_autofs_dev_ioctl_misc); |
Ian Kent | 8d7b48e | 2008-10-15 22:02:54 -0700 | [diff] [blame] | 758 | } |