dev-ioctl.c 20 KB

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