dev-ioctl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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/smp_lock.h>
  22. #include <linux/magic.h>
  23. #include <linux/dcache.h>
  24. #include <linux/uaccess.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. /*
  168. * Walk down the mount stack looking for an autofs mount that
  169. * has the requested device number (aka. new_encode_dev(sb->s_dev).
  170. */
  171. static int autofs_dev_ioctl_find_super(struct nameidata *nd, dev_t devno)
  172. {
  173. struct dentry *dentry;
  174. struct inode *inode;
  175. struct super_block *sb;
  176. dev_t s_dev;
  177. unsigned int err;
  178. err = -ENOENT;
  179. /* Lookup the dentry name at the base of our mount point */
  180. dentry = d_lookup(nd->path.dentry, &nd->last);
  181. if (!dentry)
  182. goto out;
  183. dput(nd->path.dentry);
  184. nd->path.dentry = dentry;
  185. /* And follow the mount stack looking for our autofs mount */
  186. while (follow_down(&nd->path.mnt, &nd->path.dentry)) {
  187. inode = nd->path.dentry->d_inode;
  188. if (!inode)
  189. break;
  190. sb = inode->i_sb;
  191. s_dev = new_encode_dev(sb->s_dev);
  192. if (devno == s_dev) {
  193. if (sb->s_magic == AUTOFS_SUPER_MAGIC) {
  194. err = 0;
  195. break;
  196. }
  197. }
  198. }
  199. out:
  200. return err;
  201. }
  202. /*
  203. * Walk down the mount stack looking for an autofs mount that
  204. * has the requested mount type (ie. indirect, direct or offset).
  205. */
  206. static int autofs_dev_ioctl_find_sbi_type(struct nameidata *nd, unsigned int type)
  207. {
  208. struct dentry *dentry;
  209. struct autofs_info *ino;
  210. unsigned int err;
  211. err = -ENOENT;
  212. /* Lookup the dentry name at the base of our mount point */
  213. dentry = d_lookup(nd->path.dentry, &nd->last);
  214. if (!dentry)
  215. goto out;
  216. dput(nd->path.dentry);
  217. nd->path.dentry = dentry;
  218. /* And follow the mount stack looking for our autofs mount */
  219. while (follow_down(&nd->path.mnt, &nd->path.dentry)) {
  220. ino = autofs4_dentry_ino(nd->path.dentry);
  221. if (ino && ino->sbi->type & type) {
  222. err = 0;
  223. break;
  224. }
  225. }
  226. out:
  227. return err;
  228. }
  229. static void autofs_dev_ioctl_fd_install(unsigned int fd, struct file *file)
  230. {
  231. struct files_struct *files = current->files;
  232. struct fdtable *fdt;
  233. spin_lock(&files->file_lock);
  234. fdt = files_fdtable(files);
  235. BUG_ON(fdt->fd[fd] != NULL);
  236. rcu_assign_pointer(fdt->fd[fd], file);
  237. FD_SET(fd, fdt->close_on_exec);
  238. spin_unlock(&files->file_lock);
  239. }
  240. /*
  241. * Open a file descriptor on the autofs mount point corresponding
  242. * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
  243. */
  244. static int autofs_dev_ioctl_open_mountpoint(const char *path, dev_t devid)
  245. {
  246. struct file *filp;
  247. struct nameidata nd;
  248. int err, fd;
  249. fd = get_unused_fd();
  250. if (likely(fd >= 0)) {
  251. /* Get nameidata of the parent directory */
  252. err = path_lookup(path, LOOKUP_PARENT, &nd);
  253. if (err)
  254. goto out;
  255. /*
  256. * Search down, within the parent, looking for an
  257. * autofs super block that has the device number
  258. * corresponding to the autofs fs we want to open.
  259. */
  260. err = autofs_dev_ioctl_find_super(&nd, devid);
  261. if (err) {
  262. path_put(&nd.path);
  263. goto out;
  264. }
  265. filp = dentry_open(nd.path.dentry, nd.path.mnt, O_RDONLY,
  266. current_cred());
  267. if (IS_ERR(filp)) {
  268. err = PTR_ERR(filp);
  269. goto out;
  270. }
  271. autofs_dev_ioctl_fd_install(fd, filp);
  272. }
  273. return fd;
  274. out:
  275. put_unused_fd(fd);
  276. return err;
  277. }
  278. /* Open a file descriptor on an autofs mount point */
  279. static int autofs_dev_ioctl_openmount(struct file *fp,
  280. struct autofs_sb_info *sbi,
  281. struct autofs_dev_ioctl *param)
  282. {
  283. const char *path;
  284. dev_t devid;
  285. int err, fd;
  286. /* param->path has already been checked */
  287. if (!param->openmount.devid)
  288. return -EINVAL;
  289. param->ioctlfd = -1;
  290. path = param->path;
  291. devid = param->openmount.devid;
  292. err = 0;
  293. fd = autofs_dev_ioctl_open_mountpoint(path, devid);
  294. if (unlikely(fd < 0)) {
  295. err = fd;
  296. goto out;
  297. }
  298. param->ioctlfd = fd;
  299. out:
  300. return err;
  301. }
  302. /* Close file descriptor allocated above (user can also use close(2)). */
  303. static int autofs_dev_ioctl_closemount(struct file *fp,
  304. struct autofs_sb_info *sbi,
  305. struct autofs_dev_ioctl *param)
  306. {
  307. return sys_close(param->ioctlfd);
  308. }
  309. /*
  310. * Send "ready" status for an existing wait (either a mount or an expire
  311. * request).
  312. */
  313. static int autofs_dev_ioctl_ready(struct file *fp,
  314. struct autofs_sb_info *sbi,
  315. struct autofs_dev_ioctl *param)
  316. {
  317. autofs_wqt_t token;
  318. token = (autofs_wqt_t) param->ready.token;
  319. return autofs4_wait_release(sbi, token, 0);
  320. }
  321. /*
  322. * Send "fail" status for an existing wait (either a mount or an expire
  323. * request).
  324. */
  325. static int autofs_dev_ioctl_fail(struct file *fp,
  326. struct autofs_sb_info *sbi,
  327. struct autofs_dev_ioctl *param)
  328. {
  329. autofs_wqt_t token;
  330. int status;
  331. token = (autofs_wqt_t) param->fail.token;
  332. status = param->fail.status ? param->fail.status : -ENOENT;
  333. return autofs4_wait_release(sbi, token, status);
  334. }
  335. /*
  336. * Set the pipe fd for kernel communication to the daemon.
  337. *
  338. * Normally this is set at mount using an option but if we
  339. * are reconnecting to a busy mount then we need to use this
  340. * to tell the autofs mount about the new kernel pipe fd. In
  341. * order to protect mounts against incorrectly setting the
  342. * pipefd we also require that the autofs mount be catatonic.
  343. *
  344. * This also sets the process group id used to identify the
  345. * controlling process (eg. the owning automount(8) daemon).
  346. */
  347. static int autofs_dev_ioctl_setpipefd(struct file *fp,
  348. struct autofs_sb_info *sbi,
  349. struct autofs_dev_ioctl *param)
  350. {
  351. int pipefd;
  352. int err = 0;
  353. if (param->setpipefd.pipefd == -1)
  354. return -EINVAL;
  355. pipefd = param->setpipefd.pipefd;
  356. mutex_lock(&sbi->wq_mutex);
  357. if (!sbi->catatonic) {
  358. mutex_unlock(&sbi->wq_mutex);
  359. return -EBUSY;
  360. } else {
  361. struct file *pipe = fget(pipefd);
  362. if (!pipe->f_op || !pipe->f_op->write) {
  363. err = -EPIPE;
  364. fput(pipe);
  365. goto out;
  366. }
  367. sbi->oz_pgrp = task_pgrp_nr(current);
  368. sbi->pipefd = pipefd;
  369. sbi->pipe = pipe;
  370. sbi->catatonic = 0;
  371. }
  372. out:
  373. mutex_unlock(&sbi->wq_mutex);
  374. return err;
  375. }
  376. /*
  377. * Make the autofs mount point catatonic, no longer responsive to
  378. * mount requests. Also closes the kernel pipe file descriptor.
  379. */
  380. static int autofs_dev_ioctl_catatonic(struct file *fp,
  381. struct autofs_sb_info *sbi,
  382. struct autofs_dev_ioctl *param)
  383. {
  384. autofs4_catatonic_mode(sbi);
  385. return 0;
  386. }
  387. /* Set the autofs mount timeout */
  388. static int autofs_dev_ioctl_timeout(struct file *fp,
  389. struct autofs_sb_info *sbi,
  390. struct autofs_dev_ioctl *param)
  391. {
  392. unsigned long timeout;
  393. timeout = param->timeout.timeout;
  394. param->timeout.timeout = sbi->exp_timeout / HZ;
  395. sbi->exp_timeout = timeout * HZ;
  396. return 0;
  397. }
  398. /*
  399. * Return the uid and gid of the last request for the mount
  400. *
  401. * When reconstructing an autofs mount tree with active mounts
  402. * we need to re-connect to mounts that may have used the original
  403. * process uid and gid (or string variations of them) for mount
  404. * lookups within the map entry.
  405. */
  406. static int autofs_dev_ioctl_requester(struct file *fp,
  407. struct autofs_sb_info *sbi,
  408. struct autofs_dev_ioctl *param)
  409. {
  410. struct autofs_info *ino;
  411. struct nameidata nd;
  412. const char *path;
  413. dev_t devid;
  414. int err = -ENOENT;
  415. if (param->size <= sizeof(*param)) {
  416. err = -EINVAL;
  417. goto out;
  418. }
  419. path = param->path;
  420. devid = new_encode_dev(sbi->sb->s_dev);
  421. param->requester.uid = param->requester.gid = -1;
  422. /* Get nameidata of the parent directory */
  423. err = path_lookup(path, LOOKUP_PARENT, &nd);
  424. if (err)
  425. goto out;
  426. err = autofs_dev_ioctl_find_super(&nd, devid);
  427. if (err)
  428. goto out_release;
  429. ino = autofs4_dentry_ino(nd.path.dentry);
  430. if (ino) {
  431. err = 0;
  432. autofs4_expire_wait(nd.path.dentry);
  433. spin_lock(&sbi->fs_lock);
  434. param->requester.uid = ino->uid;
  435. param->requester.gid = ino->gid;
  436. spin_unlock(&sbi->fs_lock);
  437. }
  438. out_release:
  439. path_put(&nd.path);
  440. out:
  441. return err;
  442. }
  443. /*
  444. * Call repeatedly until it returns -EAGAIN, meaning there's nothing
  445. * more that can be done.
  446. */
  447. static int autofs_dev_ioctl_expire(struct file *fp,
  448. struct autofs_sb_info *sbi,
  449. struct autofs_dev_ioctl *param)
  450. {
  451. struct vfsmount *mnt;
  452. int how;
  453. how = param->expire.how;
  454. mnt = fp->f_path.mnt;
  455. return autofs4_do_expire_multi(sbi->sb, mnt, sbi, how);
  456. }
  457. /* Check if autofs mount point is in use */
  458. static int autofs_dev_ioctl_askumount(struct file *fp,
  459. struct autofs_sb_info *sbi,
  460. struct autofs_dev_ioctl *param)
  461. {
  462. param->askumount.may_umount = 0;
  463. if (may_umount(fp->f_path.mnt))
  464. param->askumount.may_umount = 1;
  465. return 0;
  466. }
  467. /*
  468. * Check if the given path is a mountpoint.
  469. *
  470. * If we are supplied with the file descriptor of an autofs
  471. * mount we're looking for a specific mount. In this case
  472. * the path is considered a mountpoint if it is itself a
  473. * mountpoint or contains a mount, such as a multi-mount
  474. * without a root mount. In this case we return 1 if the
  475. * path is a mount point and the super magic of the covering
  476. * mount if there is one or 0 if it isn't a mountpoint.
  477. *
  478. * If we aren't supplied with a file descriptor then we
  479. * lookup the nameidata of the path and check if it is the
  480. * root of a mount. If a type is given we are looking for
  481. * a particular autofs mount and if we don't find a match
  482. * we return fail. If the located nameidata path is the
  483. * root of a mount we return 1 along with the super magic
  484. * of the mount or 0 otherwise.
  485. *
  486. * In both cases the the device number (as returned by
  487. * new_encode_dev()) is also returned.
  488. */
  489. static int autofs_dev_ioctl_ismountpoint(struct file *fp,
  490. struct autofs_sb_info *sbi,
  491. struct autofs_dev_ioctl *param)
  492. {
  493. struct nameidata nd;
  494. const char *path;
  495. unsigned int type;
  496. unsigned int devid, magic;
  497. int err = -ENOENT;
  498. if (param->size <= sizeof(*param)) {
  499. err = -EINVAL;
  500. goto out;
  501. }
  502. path = param->path;
  503. type = param->ismountpoint.in.type;
  504. param->ismountpoint.out.devid = devid = 0;
  505. param->ismountpoint.out.magic = magic = 0;
  506. if (!fp || param->ioctlfd == -1) {
  507. if (autofs_type_any(type)) {
  508. struct super_block *sb;
  509. err = path_lookup(path, LOOKUP_FOLLOW, &nd);
  510. if (err)
  511. goto out;
  512. sb = nd.path.dentry->d_sb;
  513. devid = new_encode_dev(sb->s_dev);
  514. } else {
  515. struct autofs_info *ino;
  516. err = path_lookup(path, LOOKUP_PARENT, &nd);
  517. if (err)
  518. goto out;
  519. err = autofs_dev_ioctl_find_sbi_type(&nd, type);
  520. if (err)
  521. goto out_release;
  522. ino = autofs4_dentry_ino(nd.path.dentry);
  523. devid = autofs4_get_dev(ino->sbi);
  524. }
  525. err = 0;
  526. if (nd.path.dentry->d_inode &&
  527. nd.path.mnt->mnt_root == nd.path.dentry) {
  528. err = 1;
  529. magic = nd.path.dentry->d_inode->i_sb->s_magic;
  530. }
  531. } else {
  532. dev_t dev = autofs4_get_dev(sbi);
  533. err = path_lookup(path, LOOKUP_PARENT, &nd);
  534. if (err)
  535. goto out;
  536. err = autofs_dev_ioctl_find_super(&nd, dev);
  537. if (err)
  538. goto out_release;
  539. devid = dev;
  540. err = have_submounts(nd.path.dentry);
  541. if (nd.path.mnt->mnt_mountpoint != nd.path.mnt->mnt_root) {
  542. if (follow_down(&nd.path.mnt, &nd.path.dentry)) {
  543. struct inode *inode = nd.path.dentry->d_inode;
  544. magic = inode->i_sb->s_magic;
  545. }
  546. }
  547. }
  548. param->ismountpoint.out.devid = devid;
  549. param->ismountpoint.out.magic = magic;
  550. out_release:
  551. path_put(&nd.path);
  552. out:
  553. return err;
  554. }
  555. /*
  556. * Our range of ioctl numbers isn't 0 based so we need to shift
  557. * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
  558. * lookup.
  559. */
  560. #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
  561. static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
  562. {
  563. static struct {
  564. int cmd;
  565. ioctl_fn fn;
  566. } _ioctls[] = {
  567. {cmd_idx(AUTOFS_DEV_IOCTL_VERSION_CMD), NULL},
  568. {cmd_idx(AUTOFS_DEV_IOCTL_PROTOVER_CMD),
  569. autofs_dev_ioctl_protover},
  570. {cmd_idx(AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD),
  571. autofs_dev_ioctl_protosubver},
  572. {cmd_idx(AUTOFS_DEV_IOCTL_OPENMOUNT_CMD),
  573. autofs_dev_ioctl_openmount},
  574. {cmd_idx(AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD),
  575. autofs_dev_ioctl_closemount},
  576. {cmd_idx(AUTOFS_DEV_IOCTL_READY_CMD),
  577. autofs_dev_ioctl_ready},
  578. {cmd_idx(AUTOFS_DEV_IOCTL_FAIL_CMD),
  579. autofs_dev_ioctl_fail},
  580. {cmd_idx(AUTOFS_DEV_IOCTL_SETPIPEFD_CMD),
  581. autofs_dev_ioctl_setpipefd},
  582. {cmd_idx(AUTOFS_DEV_IOCTL_CATATONIC_CMD),
  583. autofs_dev_ioctl_catatonic},
  584. {cmd_idx(AUTOFS_DEV_IOCTL_TIMEOUT_CMD),
  585. autofs_dev_ioctl_timeout},
  586. {cmd_idx(AUTOFS_DEV_IOCTL_REQUESTER_CMD),
  587. autofs_dev_ioctl_requester},
  588. {cmd_idx(AUTOFS_DEV_IOCTL_EXPIRE_CMD),
  589. autofs_dev_ioctl_expire},
  590. {cmd_idx(AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD),
  591. autofs_dev_ioctl_askumount},
  592. {cmd_idx(AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD),
  593. autofs_dev_ioctl_ismountpoint}
  594. };
  595. unsigned int idx = cmd_idx(cmd);
  596. return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx].fn;
  597. }
  598. /* ioctl dispatcher */
  599. static int _autofs_dev_ioctl(unsigned int command, struct autofs_dev_ioctl __user *user)
  600. {
  601. struct autofs_dev_ioctl *param;
  602. struct file *fp;
  603. struct autofs_sb_info *sbi;
  604. unsigned int cmd_first, cmd;
  605. ioctl_fn fn = NULL;
  606. int err = 0;
  607. /* only root can play with this */
  608. if (!capable(CAP_SYS_ADMIN))
  609. return -EPERM;
  610. cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
  611. cmd = _IOC_NR(command);
  612. if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
  613. cmd - cmd_first >= AUTOFS_DEV_IOCTL_IOC_COUNT) {
  614. return -ENOTTY;
  615. }
  616. /* Copy the parameters into kernel space. */
  617. param = copy_dev_ioctl(user);
  618. if (IS_ERR(param))
  619. return PTR_ERR(param);
  620. err = validate_dev_ioctl(command, param);
  621. if (err)
  622. goto out;
  623. /* The validate routine above always sets the version */
  624. if (cmd == AUTOFS_DEV_IOCTL_VERSION_CMD)
  625. goto done;
  626. fn = lookup_dev_ioctl(cmd);
  627. if (!fn) {
  628. AUTOFS_WARN("unknown command 0x%08x", command);
  629. return -ENOTTY;
  630. }
  631. fp = NULL;
  632. sbi = NULL;
  633. /*
  634. * For obvious reasons the openmount can't have a file
  635. * descriptor yet. We don't take a reference to the
  636. * file during close to allow for immediate release.
  637. */
  638. if (cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
  639. cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
  640. fp = fget(param->ioctlfd);
  641. if (!fp) {
  642. if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
  643. goto cont;
  644. err = -EBADF;
  645. goto out;
  646. }
  647. if (!fp->f_op) {
  648. err = -ENOTTY;
  649. fput(fp);
  650. goto out;
  651. }
  652. sbi = autofs_dev_ioctl_sbi(fp);
  653. if (!sbi || sbi->magic != AUTOFS_SBI_MAGIC) {
  654. err = -EINVAL;
  655. fput(fp);
  656. goto out;
  657. }
  658. /*
  659. * Admin needs to be able to set the mount catatonic in
  660. * order to be able to perform the re-open.
  661. */
  662. if (!autofs4_oz_mode(sbi) &&
  663. cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
  664. err = -EACCES;
  665. fput(fp);
  666. goto out;
  667. }
  668. }
  669. cont:
  670. err = fn(fp, sbi, param);
  671. if (fp)
  672. fput(fp);
  673. done:
  674. if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
  675. err = -EFAULT;
  676. out:
  677. free_dev_ioctl(param);
  678. return err;
  679. }
  680. static long autofs_dev_ioctl(struct file *file, uint command, ulong u)
  681. {
  682. int err;
  683. err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
  684. return (long) err;
  685. }
  686. #ifdef CONFIG_COMPAT
  687. static long autofs_dev_ioctl_compat(struct file *file, uint command, ulong u)
  688. {
  689. return (long) autofs_dev_ioctl(file, command, (ulong) compat_ptr(u));
  690. }
  691. #else
  692. #define autofs_dev_ioctl_compat NULL
  693. #endif
  694. static const struct file_operations _dev_ioctl_fops = {
  695. .unlocked_ioctl = autofs_dev_ioctl,
  696. .compat_ioctl = autofs_dev_ioctl_compat,
  697. .owner = THIS_MODULE,
  698. };
  699. static struct miscdevice _autofs_dev_ioctl_misc = {
  700. .minor = MISC_DYNAMIC_MINOR,
  701. .name = AUTOFS_DEVICE_NAME,
  702. .fops = &_dev_ioctl_fops
  703. };
  704. /* Register/deregister misc character device */
  705. int autofs_dev_ioctl_init(void)
  706. {
  707. int r;
  708. r = misc_register(&_autofs_dev_ioctl_misc);
  709. if (r) {
  710. AUTOFS_ERROR("misc_register failed for control device");
  711. return r;
  712. }
  713. return 0;
  714. }
  715. void autofs_dev_ioctl_exit(void)
  716. {
  717. misc_deregister(&_autofs_dev_ioctl_misc);
  718. return;
  719. }