dev-ioctl.c 18 KB

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