dev-ioctl.c 18 KB

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