dev-ioctl.c 20 KB

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