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