dev-ioctl.c 17 KB

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