dev-ioctl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 ((AUTOFS_DEV_IOCTL_VERSION_MAJOR != param->ver_major) ||
  67. (AUTOFS_DEV_IOCTL_VERSION_MINOR < param->ver_minor)) {
  68. AUTOFS_WARN("ioctl control interface version mismatch: "
  69. "kernel(%u.%u), user(%u.%u), cmd(%d)",
  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 *copy_dev_ioctl(struct autofs_dev_ioctl __user *in)
  85. {
  86. struct autofs_dev_ioctl tmp, *ads;
  87. if (copy_from_user(&tmp, in, sizeof(tmp)))
  88. return ERR_PTR(-EFAULT);
  89. if (tmp.size < sizeof(tmp))
  90. return ERR_PTR(-EINVAL);
  91. ads = kmalloc(tmp.size, GFP_KERNEL);
  92. if (!ads)
  93. return ERR_PTR(-ENOMEM);
  94. if (copy_from_user(ads, in, tmp.size)) {
  95. kfree(ads);
  96. return ERR_PTR(-EFAULT);
  97. }
  98. return ads;
  99. }
  100. static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
  101. {
  102. kfree(param);
  103. return;
  104. }
  105. /*
  106. * Check sanity of parameter control fields and if a path is present
  107. * check that it is terminated and contains at least one "/".
  108. */
  109. static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param)
  110. {
  111. int err;
  112. err = check_dev_ioctl_version(cmd, param);
  113. if (err) {
  114. AUTOFS_WARN("invalid device control module version "
  115. "supplied for cmd(0x%08x)", cmd);
  116. goto out;
  117. }
  118. if (param->size > sizeof(*param)) {
  119. err = invalid_str(param->path, param->size - sizeof(*param));
  120. if (err) {
  121. AUTOFS_WARN(
  122. "path string terminator missing for cmd(0x%08x)",
  123. cmd);
  124. goto out;
  125. }
  126. err = check_name(param->path);
  127. if (err) {
  128. AUTOFS_WARN("invalid path supplied for cmd(0x%08x)",
  129. cmd);
  130. goto out;
  131. }
  132. }
  133. err = 0;
  134. out:
  135. return err;
  136. }
  137. /*
  138. * Get the autofs super block info struct from the file opened on
  139. * the autofs mount point.
  140. */
  141. static struct autofs_sb_info *autofs_dev_ioctl_sbi(struct file *f)
  142. {
  143. struct autofs_sb_info *sbi = NULL;
  144. struct inode *inode;
  145. if (f) {
  146. inode = f->f_path.dentry->d_inode;
  147. sbi = autofs4_sbi(inode->i_sb);
  148. }
  149. return sbi;
  150. }
  151. /* Return autofs module protocol version */
  152. static int autofs_dev_ioctl_protover(struct file *fp,
  153. struct autofs_sb_info *sbi,
  154. struct autofs_dev_ioctl *param)
  155. {
  156. param->protover.version = sbi->version;
  157. return 0;
  158. }
  159. /* Return autofs module protocol sub version */
  160. static int autofs_dev_ioctl_protosubver(struct file *fp,
  161. struct autofs_sb_info *sbi,
  162. struct autofs_dev_ioctl *param)
  163. {
  164. param->protosubver.sub_version = sbi->sub_version;
  165. return 0;
  166. }
  167. static int find_autofs_mount(const char *pathname,
  168. struct path *res,
  169. int test(struct path *path, void *data),
  170. void *data)
  171. {
  172. struct path path;
  173. int err = kern_path(pathname, 0, &path);
  174. if (err)
  175. return err;
  176. err = -ENOENT;
  177. while (path.dentry == path.mnt->mnt_root) {
  178. if (path.mnt->mnt_sb->s_magic == AUTOFS_SUPER_MAGIC) {
  179. if (test(&path, data)) {
  180. path_get(&path);
  181. if (!err) /* already found some */
  182. path_put(res);
  183. *res = path;
  184. err = 0;
  185. }
  186. }
  187. if (!follow_up(&path))
  188. break;
  189. }
  190. path_put(&path);
  191. return err;
  192. }
  193. static int test_by_dev(struct path *path, void *p)
  194. {
  195. return path->mnt->mnt_sb->s_dev == *(dev_t *)p;
  196. }
  197. static int test_by_type(struct path *path, void *p)
  198. {
  199. struct autofs_info *ino = autofs4_dentry_ino(path->dentry);
  200. return ino && ino->sbi->type & *(unsigned *)p;
  201. }
  202. static void autofs_dev_ioctl_fd_install(unsigned int fd, struct file *file)
  203. {
  204. struct files_struct *files = current->files;
  205. struct fdtable *fdt;
  206. spin_lock(&files->file_lock);
  207. fdt = files_fdtable(files);
  208. BUG_ON(fdt->fd[fd] != NULL);
  209. rcu_assign_pointer(fdt->fd[fd], file);
  210. FD_SET(fd, fdt->close_on_exec);
  211. spin_unlock(&files->file_lock);
  212. }
  213. /*
  214. * Open a file descriptor on the autofs mount point corresponding
  215. * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
  216. */
  217. static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid)
  218. {
  219. int err, fd;
  220. fd = get_unused_fd();
  221. if (likely(fd >= 0)) {
  222. struct file *filp;
  223. struct path path;
  224. err = find_autofs_mount(name, &path, test_by_dev, &devid);
  225. if (err)
  226. goto out;
  227. /*
  228. * Find autofs super block that has the device number
  229. * corresponding to the autofs fs we want to open.
  230. */
  231. filp = dentry_open(path.dentry, path.mnt, O_RDONLY,
  232. current_cred());
  233. if (IS_ERR(filp)) {
  234. err = PTR_ERR(filp);
  235. goto out;
  236. }
  237. autofs_dev_ioctl_fd_install(fd, filp);
  238. }
  239. return fd;
  240. out:
  241. put_unused_fd(fd);
  242. return err;
  243. }
  244. /* Open a file descriptor on an autofs mount point */
  245. static int autofs_dev_ioctl_openmount(struct file *fp,
  246. struct autofs_sb_info *sbi,
  247. struct autofs_dev_ioctl *param)
  248. {
  249. const char *path;
  250. dev_t devid;
  251. int err, fd;
  252. /* param->path has already been checked */
  253. if (!param->openmount.devid)
  254. return -EINVAL;
  255. param->ioctlfd = -1;
  256. path = param->path;
  257. devid = new_decode_dev(param->openmount.devid);
  258. err = 0;
  259. fd = autofs_dev_ioctl_open_mountpoint(path, devid);
  260. if (unlikely(fd < 0)) {
  261. err = fd;
  262. goto out;
  263. }
  264. param->ioctlfd = fd;
  265. out:
  266. return err;
  267. }
  268. /* Close file descriptor allocated above (user can also use close(2)). */
  269. static int autofs_dev_ioctl_closemount(struct file *fp,
  270. struct autofs_sb_info *sbi,
  271. struct autofs_dev_ioctl *param)
  272. {
  273. return sys_close(param->ioctlfd);
  274. }
  275. /*
  276. * Send "ready" status for an existing wait (either a mount or an expire
  277. * request).
  278. */
  279. static int autofs_dev_ioctl_ready(struct file *fp,
  280. struct autofs_sb_info *sbi,
  281. struct autofs_dev_ioctl *param)
  282. {
  283. autofs_wqt_t token;
  284. token = (autofs_wqt_t) param->ready.token;
  285. return autofs4_wait_release(sbi, token, 0);
  286. }
  287. /*
  288. * Send "fail" status for an existing wait (either a mount or an expire
  289. * request).
  290. */
  291. static int autofs_dev_ioctl_fail(struct file *fp,
  292. struct autofs_sb_info *sbi,
  293. struct autofs_dev_ioctl *param)
  294. {
  295. autofs_wqt_t token;
  296. int status;
  297. token = (autofs_wqt_t) param->fail.token;
  298. status = param->fail.status ? param->fail.status : -ENOENT;
  299. return autofs4_wait_release(sbi, token, status);
  300. }
  301. /*
  302. * Set the pipe fd for kernel communication to the daemon.
  303. *
  304. * Normally this is set at mount using an option but if we
  305. * are reconnecting to a busy mount then we need to use this
  306. * to tell the autofs mount about the new kernel pipe fd. In
  307. * order to protect mounts against incorrectly setting the
  308. * pipefd we also require that the autofs mount be catatonic.
  309. *
  310. * This also sets the process group id used to identify the
  311. * controlling process (eg. the owning automount(8) daemon).
  312. */
  313. static int autofs_dev_ioctl_setpipefd(struct file *fp,
  314. struct autofs_sb_info *sbi,
  315. struct autofs_dev_ioctl *param)
  316. {
  317. int pipefd;
  318. int err = 0;
  319. if (param->setpipefd.pipefd == -1)
  320. return -EINVAL;
  321. pipefd = param->setpipefd.pipefd;
  322. mutex_lock(&sbi->wq_mutex);
  323. if (!sbi->catatonic) {
  324. mutex_unlock(&sbi->wq_mutex);
  325. return -EBUSY;
  326. } else {
  327. struct file *pipe = fget(pipefd);
  328. if (!pipe->f_op || !pipe->f_op->write) {
  329. err = -EPIPE;
  330. fput(pipe);
  331. goto out;
  332. }
  333. sbi->oz_pgrp = task_pgrp_nr(current);
  334. sbi->pipefd = pipefd;
  335. sbi->pipe = pipe;
  336. sbi->catatonic = 0;
  337. }
  338. out:
  339. mutex_unlock(&sbi->wq_mutex);
  340. return err;
  341. }
  342. /*
  343. * Make the autofs mount point catatonic, no longer responsive to
  344. * mount requests. Also closes the kernel pipe file descriptor.
  345. */
  346. static int autofs_dev_ioctl_catatonic(struct file *fp,
  347. struct autofs_sb_info *sbi,
  348. struct autofs_dev_ioctl *param)
  349. {
  350. autofs4_catatonic_mode(sbi);
  351. return 0;
  352. }
  353. /* Set the autofs mount timeout */
  354. static int autofs_dev_ioctl_timeout(struct file *fp,
  355. struct autofs_sb_info *sbi,
  356. struct autofs_dev_ioctl *param)
  357. {
  358. unsigned long timeout;
  359. timeout = param->timeout.timeout;
  360. param->timeout.timeout = sbi->exp_timeout / HZ;
  361. sbi->exp_timeout = timeout * HZ;
  362. return 0;
  363. }
  364. /*
  365. * Return the uid and gid of the last request for the mount
  366. *
  367. * When reconstructing an autofs mount tree with active mounts
  368. * we need to re-connect to mounts that may have used the original
  369. * process uid and gid (or string variations of them) for mount
  370. * lookups within the map entry.
  371. */
  372. static int autofs_dev_ioctl_requester(struct file *fp,
  373. struct autofs_sb_info *sbi,
  374. struct autofs_dev_ioctl *param)
  375. {
  376. struct autofs_info *ino;
  377. struct path path;
  378. dev_t devid;
  379. int err = -ENOENT;
  380. if (param->size <= sizeof(*param)) {
  381. err = -EINVAL;
  382. goto out;
  383. }
  384. devid = sbi->sb->s_dev;
  385. param->requester.uid = param->requester.gid = -1;
  386. err = find_autofs_mount(param->path, &path, test_by_dev, &devid);
  387. if (err)
  388. goto out;
  389. ino = autofs4_dentry_ino(path.dentry);
  390. if (ino) {
  391. err = 0;
  392. autofs4_expire_wait(path.dentry);
  393. spin_lock(&sbi->fs_lock);
  394. param->requester.uid = ino->uid;
  395. param->requester.gid = ino->gid;
  396. spin_unlock(&sbi->fs_lock);
  397. }
  398. path_put(&path);
  399. out:
  400. return err;
  401. }
  402. /*
  403. * Call repeatedly until it returns -EAGAIN, meaning there's nothing
  404. * more that can be done.
  405. */
  406. static int autofs_dev_ioctl_expire(struct file *fp,
  407. struct autofs_sb_info *sbi,
  408. struct autofs_dev_ioctl *param)
  409. {
  410. struct vfsmount *mnt;
  411. int how;
  412. how = param->expire.how;
  413. mnt = fp->f_path.mnt;
  414. return autofs4_do_expire_multi(sbi->sb, mnt, sbi, how);
  415. }
  416. /* Check if autofs mount point is in use */
  417. static int autofs_dev_ioctl_askumount(struct file *fp,
  418. struct autofs_sb_info *sbi,
  419. struct autofs_dev_ioctl *param)
  420. {
  421. param->askumount.may_umount = 0;
  422. if (may_umount(fp->f_path.mnt))
  423. param->askumount.may_umount = 1;
  424. return 0;
  425. }
  426. /*
  427. * Check if the given path is a mountpoint.
  428. *
  429. * If we are supplied with the file descriptor of an autofs
  430. * mount we're looking for a specific mount. In this case
  431. * the path is considered a mountpoint if it is itself a
  432. * mountpoint or contains a mount, such as a multi-mount
  433. * without a root mount. In this case we return 1 if the
  434. * path is a mount point and the super magic of the covering
  435. * mount if there is one or 0 if it isn't a mountpoint.
  436. *
  437. * If we aren't supplied with a file descriptor then we
  438. * lookup the nameidata of the path and check if it is the
  439. * root of a mount. If a type is given we are looking for
  440. * a particular autofs mount and if we don't find a match
  441. * we return fail. If the located nameidata path is the
  442. * root of a mount we return 1 along with the super magic
  443. * of the mount or 0 otherwise.
  444. *
  445. * In both cases the the device number (as returned by
  446. * new_encode_dev()) is also returned.
  447. */
  448. static int autofs_dev_ioctl_ismountpoint(struct file *fp,
  449. struct autofs_sb_info *sbi,
  450. struct autofs_dev_ioctl *param)
  451. {
  452. struct path path;
  453. const char *name;
  454. unsigned int type;
  455. unsigned int devid, magic;
  456. int err = -ENOENT;
  457. if (param->size <= sizeof(*param)) {
  458. err = -EINVAL;
  459. goto out;
  460. }
  461. name = param->path;
  462. type = param->ismountpoint.in.type;
  463. param->ismountpoint.out.devid = devid = 0;
  464. param->ismountpoint.out.magic = magic = 0;
  465. if (!fp || param->ioctlfd == -1) {
  466. if (autofs_type_any(type))
  467. err = kern_path(name, LOOKUP_FOLLOW, &path);
  468. else
  469. err = find_autofs_mount(name, &path, test_by_type, &type);
  470. if (err)
  471. goto out;
  472. devid = new_encode_dev(path.mnt->mnt_sb->s_dev);
  473. err = 0;
  474. if (path.mnt->mnt_root == path.dentry) {
  475. err = 1;
  476. magic = path.mnt->mnt_sb->s_magic;
  477. }
  478. } else {
  479. dev_t dev = sbi->sb->s_dev;
  480. err = find_autofs_mount(name, &path, test_by_dev, &dev);
  481. if (err)
  482. goto out;
  483. devid = new_encode_dev(dev);
  484. err = have_submounts(path.dentry);
  485. if (follow_down(&path))
  486. magic = path.mnt->mnt_sb->s_magic;
  487. }
  488. param->ismountpoint.out.devid = devid;
  489. param->ismountpoint.out.magic = magic;
  490. path_put(&path);
  491. out:
  492. return err;
  493. }
  494. /*
  495. * Our range of ioctl numbers isn't 0 based so we need to shift
  496. * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
  497. * lookup.
  498. */
  499. #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
  500. static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
  501. {
  502. static struct {
  503. int cmd;
  504. ioctl_fn fn;
  505. } _ioctls[] = {
  506. {cmd_idx(AUTOFS_DEV_IOCTL_VERSION_CMD), NULL},
  507. {cmd_idx(AUTOFS_DEV_IOCTL_PROTOVER_CMD),
  508. autofs_dev_ioctl_protover},
  509. {cmd_idx(AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD),
  510. autofs_dev_ioctl_protosubver},
  511. {cmd_idx(AUTOFS_DEV_IOCTL_OPENMOUNT_CMD),
  512. autofs_dev_ioctl_openmount},
  513. {cmd_idx(AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD),
  514. autofs_dev_ioctl_closemount},
  515. {cmd_idx(AUTOFS_DEV_IOCTL_READY_CMD),
  516. autofs_dev_ioctl_ready},
  517. {cmd_idx(AUTOFS_DEV_IOCTL_FAIL_CMD),
  518. autofs_dev_ioctl_fail},
  519. {cmd_idx(AUTOFS_DEV_IOCTL_SETPIPEFD_CMD),
  520. autofs_dev_ioctl_setpipefd},
  521. {cmd_idx(AUTOFS_DEV_IOCTL_CATATONIC_CMD),
  522. autofs_dev_ioctl_catatonic},
  523. {cmd_idx(AUTOFS_DEV_IOCTL_TIMEOUT_CMD),
  524. autofs_dev_ioctl_timeout},
  525. {cmd_idx(AUTOFS_DEV_IOCTL_REQUESTER_CMD),
  526. autofs_dev_ioctl_requester},
  527. {cmd_idx(AUTOFS_DEV_IOCTL_EXPIRE_CMD),
  528. autofs_dev_ioctl_expire},
  529. {cmd_idx(AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD),
  530. autofs_dev_ioctl_askumount},
  531. {cmd_idx(AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD),
  532. autofs_dev_ioctl_ismountpoint}
  533. };
  534. unsigned int idx = cmd_idx(cmd);
  535. return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx].fn;
  536. }
  537. /* ioctl dispatcher */
  538. static int _autofs_dev_ioctl(unsigned int command, struct autofs_dev_ioctl __user *user)
  539. {
  540. struct autofs_dev_ioctl *param;
  541. struct file *fp;
  542. struct autofs_sb_info *sbi;
  543. unsigned int cmd_first, cmd;
  544. ioctl_fn fn = NULL;
  545. int err = 0;
  546. /* only root can play with this */
  547. if (!capable(CAP_SYS_ADMIN))
  548. return -EPERM;
  549. cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
  550. cmd = _IOC_NR(command);
  551. if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
  552. cmd - cmd_first >= AUTOFS_DEV_IOCTL_IOC_COUNT) {
  553. return -ENOTTY;
  554. }
  555. /* Copy the parameters into kernel space. */
  556. param = copy_dev_ioctl(user);
  557. if (IS_ERR(param))
  558. return PTR_ERR(param);
  559. err = validate_dev_ioctl(command, param);
  560. if (err)
  561. goto out;
  562. /* The validate routine above always sets the version */
  563. if (cmd == AUTOFS_DEV_IOCTL_VERSION_CMD)
  564. goto done;
  565. fn = lookup_dev_ioctl(cmd);
  566. if (!fn) {
  567. AUTOFS_WARN("unknown command 0x%08x", command);
  568. return -ENOTTY;
  569. }
  570. fp = NULL;
  571. sbi = NULL;
  572. /*
  573. * For obvious reasons the openmount can't have a file
  574. * descriptor yet. We don't take a reference to the
  575. * file during close to allow for immediate release.
  576. */
  577. if (cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
  578. cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
  579. fp = fget(param->ioctlfd);
  580. if (!fp) {
  581. if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
  582. goto cont;
  583. err = -EBADF;
  584. goto out;
  585. }
  586. if (!fp->f_op) {
  587. err = -ENOTTY;
  588. fput(fp);
  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. };
  638. static struct miscdevice _autofs_dev_ioctl_misc = {
  639. .minor = MISC_DYNAMIC_MINOR,
  640. .name = AUTOFS_DEVICE_NAME,
  641. .fops = &_dev_ioctl_fops
  642. };
  643. /* Register/deregister misc character device */
  644. int autofs_dev_ioctl_init(void)
  645. {
  646. int r;
  647. r = misc_register(&_autofs_dev_ioctl_misc);
  648. if (r) {
  649. AUTOFS_ERROR("misc_register failed for control device");
  650. return r;
  651. }
  652. return 0;
  653. }
  654. void autofs_dev_ioctl_exit(void)
  655. {
  656. misc_deregister(&_autofs_dev_ioctl_misc);
  657. return;
  658. }