dev-ioctl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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. 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->arg1)
  288. return -EINVAL;
  289. param->ioctlfd = -1;
  290. path = param->path;
  291. devid = param->arg1;
  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->arg1;
  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->arg1;
  332. status = param->arg2 ? param->arg2 : -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->arg1 == -1)
  354. return -EINVAL;
  355. pipefd = param->arg1;
  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->arg1;
  394. param->arg1 = 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 = sbi->sb->s_dev;
  421. param->arg1 = param->arg2 = -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->arg1 = ino->uid;
  435. param->arg2 = 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 dentry *dentry;
  452. struct vfsmount *mnt;
  453. int err = -EAGAIN;
  454. int how;
  455. how = param->arg1;
  456. mnt = fp->f_path.mnt;
  457. if (sbi->type & AUTOFS_TYPE_TRIGGER)
  458. dentry = autofs4_expire_direct(sbi->sb, mnt, sbi, how);
  459. else
  460. dentry = autofs4_expire_indirect(sbi->sb, mnt, sbi, how);
  461. if (dentry) {
  462. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  463. /*
  464. * This is synchronous because it makes the daemon a
  465. * little easier
  466. */
  467. err = autofs4_wait(sbi, dentry, NFY_EXPIRE);
  468. spin_lock(&sbi->fs_lock);
  469. if (ino->flags & AUTOFS_INF_MOUNTPOINT) {
  470. ino->flags &= ~AUTOFS_INF_MOUNTPOINT;
  471. sbi->sb->s_root->d_mounted++;
  472. }
  473. ino->flags &= ~AUTOFS_INF_EXPIRING;
  474. complete_all(&ino->expire_complete);
  475. spin_unlock(&sbi->fs_lock);
  476. dput(dentry);
  477. }
  478. return err;
  479. }
  480. /* Check if autofs mount point is in use */
  481. static int autofs_dev_ioctl_askumount(struct file *fp,
  482. struct autofs_sb_info *sbi,
  483. struct autofs_dev_ioctl *param)
  484. {
  485. param->arg1 = 0;
  486. if (may_umount(fp->f_path.mnt))
  487. param->arg1 = 1;
  488. return 0;
  489. }
  490. /*
  491. * Check if the given path is a mountpoint.
  492. *
  493. * If we are supplied with the file descriptor of an autofs
  494. * mount we're looking for a specific mount. In this case
  495. * the path is considered a mountpoint if it is itself a
  496. * mountpoint or contains a mount, such as a multi-mount
  497. * without a root mount. In this case we return 1 if the
  498. * path is a mount point and the super magic of the covering
  499. * mount if there is one or 0 if it isn't a mountpoint.
  500. *
  501. * If we aren't supplied with a file descriptor then we
  502. * lookup the nameidata of the path and check if it is the
  503. * root of a mount. If a type is given we are looking for
  504. * a particular autofs mount and if we don't find a match
  505. * we return fail. If the located nameidata path is the
  506. * root of a mount we return 1 along with the super magic
  507. * of the mount or 0 otherwise.
  508. *
  509. * In both cases the the device number (as returned by
  510. * new_encode_dev()) is also returned.
  511. */
  512. static int autofs_dev_ioctl_ismountpoint(struct file *fp,
  513. struct autofs_sb_info *sbi,
  514. struct autofs_dev_ioctl *param)
  515. {
  516. struct nameidata nd;
  517. const char *path;
  518. unsigned int type;
  519. int err = -ENOENT;
  520. if (param->size <= sizeof(*param)) {
  521. err = -EINVAL;
  522. goto out;
  523. }
  524. path = param->path;
  525. type = param->arg1;
  526. param->arg1 = 0;
  527. param->arg2 = 0;
  528. if (!fp || param->ioctlfd == -1) {
  529. if (type == AUTOFS_TYPE_ANY) {
  530. struct super_block *sb;
  531. err = path_lookup(path, LOOKUP_FOLLOW, &nd);
  532. if (err)
  533. goto out;
  534. sb = nd.path.dentry->d_sb;
  535. param->arg1 = new_encode_dev(sb->s_dev);
  536. } else {
  537. struct autofs_info *ino;
  538. err = path_lookup(path, LOOKUP_PARENT, &nd);
  539. if (err)
  540. goto out;
  541. err = autofs_dev_ioctl_find_sbi_type(&nd, type);
  542. if (err)
  543. goto out_release;
  544. ino = autofs4_dentry_ino(nd.path.dentry);
  545. param->arg1 = autofs4_get_dev(ino->sbi);
  546. }
  547. err = 0;
  548. if (nd.path.dentry->d_inode &&
  549. nd.path.mnt->mnt_root == nd.path.dentry) {
  550. err = 1;
  551. param->arg2 = nd.path.dentry->d_inode->i_sb->s_magic;
  552. }
  553. } else {
  554. dev_t devid = new_encode_dev(sbi->sb->s_dev);
  555. err = path_lookup(path, LOOKUP_PARENT, &nd);
  556. if (err)
  557. goto out;
  558. err = autofs_dev_ioctl_find_super(&nd, devid);
  559. if (err)
  560. goto out_release;
  561. param->arg1 = autofs4_get_dev(sbi);
  562. err = have_submounts(nd.path.dentry);
  563. if (nd.path.mnt->mnt_mountpoint != nd.path.mnt->mnt_root) {
  564. if (follow_down(&nd.path.mnt, &nd.path.dentry)) {
  565. struct inode *inode = nd.path.dentry->d_inode;
  566. param->arg2 = inode->i_sb->s_magic;
  567. }
  568. }
  569. }
  570. out_release:
  571. path_put(&nd.path);
  572. out:
  573. return err;
  574. }
  575. /*
  576. * Our range of ioctl numbers isn't 0 based so we need to shift
  577. * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
  578. * lookup.
  579. */
  580. #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
  581. static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
  582. {
  583. static struct {
  584. int cmd;
  585. ioctl_fn fn;
  586. } _ioctls[] = {
  587. {cmd_idx(AUTOFS_DEV_IOCTL_VERSION_CMD), NULL},
  588. {cmd_idx(AUTOFS_DEV_IOCTL_PROTOVER_CMD),
  589. autofs_dev_ioctl_protover},
  590. {cmd_idx(AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD),
  591. autofs_dev_ioctl_protosubver},
  592. {cmd_idx(AUTOFS_DEV_IOCTL_OPENMOUNT_CMD),
  593. autofs_dev_ioctl_openmount},
  594. {cmd_idx(AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD),
  595. autofs_dev_ioctl_closemount},
  596. {cmd_idx(AUTOFS_DEV_IOCTL_READY_CMD),
  597. autofs_dev_ioctl_ready},
  598. {cmd_idx(AUTOFS_DEV_IOCTL_FAIL_CMD),
  599. autofs_dev_ioctl_fail},
  600. {cmd_idx(AUTOFS_DEV_IOCTL_SETPIPEFD_CMD),
  601. autofs_dev_ioctl_setpipefd},
  602. {cmd_idx(AUTOFS_DEV_IOCTL_CATATONIC_CMD),
  603. autofs_dev_ioctl_catatonic},
  604. {cmd_idx(AUTOFS_DEV_IOCTL_TIMEOUT_CMD),
  605. autofs_dev_ioctl_timeout},
  606. {cmd_idx(AUTOFS_DEV_IOCTL_REQUESTER_CMD),
  607. autofs_dev_ioctl_requester},
  608. {cmd_idx(AUTOFS_DEV_IOCTL_EXPIRE_CMD),
  609. autofs_dev_ioctl_expire},
  610. {cmd_idx(AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD),
  611. autofs_dev_ioctl_askumount},
  612. {cmd_idx(AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD),
  613. autofs_dev_ioctl_ismountpoint}
  614. };
  615. unsigned int idx = cmd_idx(cmd);
  616. return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx].fn;
  617. }
  618. /* ioctl dispatcher */
  619. static int _autofs_dev_ioctl(unsigned int command, struct autofs_dev_ioctl __user *user)
  620. {
  621. struct autofs_dev_ioctl *param;
  622. struct file *fp;
  623. struct autofs_sb_info *sbi;
  624. unsigned int cmd_first, cmd;
  625. ioctl_fn fn = NULL;
  626. int err = 0;
  627. /* only root can play with this */
  628. if (!capable(CAP_SYS_ADMIN))
  629. return -EPERM;
  630. cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
  631. cmd = _IOC_NR(command);
  632. if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
  633. cmd - cmd_first >= AUTOFS_DEV_IOCTL_IOC_COUNT) {
  634. return -ENOTTY;
  635. }
  636. /* Copy the parameters into kernel space. */
  637. param = copy_dev_ioctl(user);
  638. if (IS_ERR(param))
  639. return PTR_ERR(param);
  640. err = validate_dev_ioctl(command, param);
  641. if (err)
  642. goto out;
  643. /* The validate routine above always sets the version */
  644. if (cmd == AUTOFS_DEV_IOCTL_VERSION_CMD)
  645. goto done;
  646. fn = lookup_dev_ioctl(cmd);
  647. if (!fn) {
  648. AUTOFS_WARN("unknown command 0x%08x", command);
  649. return -ENOTTY;
  650. }
  651. fp = NULL;
  652. sbi = NULL;
  653. /*
  654. * For obvious reasons the openmount can't have a file
  655. * descriptor yet. We don't take a reference to the
  656. * file during close to allow for immediate release.
  657. */
  658. if (cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
  659. cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
  660. fp = fget(param->ioctlfd);
  661. if (!fp) {
  662. if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
  663. goto cont;
  664. err = -EBADF;
  665. goto out;
  666. }
  667. if (!fp->f_op) {
  668. err = -ENOTTY;
  669. fput(fp);
  670. goto out;
  671. }
  672. sbi = autofs_dev_ioctl_sbi(fp);
  673. if (!sbi || sbi->magic != AUTOFS_SBI_MAGIC) {
  674. err = -EINVAL;
  675. fput(fp);
  676. goto out;
  677. }
  678. /*
  679. * Admin needs to be able to set the mount catatonic in
  680. * order to be able to perform the re-open.
  681. */
  682. if (!autofs4_oz_mode(sbi) &&
  683. cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
  684. err = -EACCES;
  685. fput(fp);
  686. goto out;
  687. }
  688. }
  689. cont:
  690. err = fn(fp, sbi, param);
  691. if (fp)
  692. fput(fp);
  693. done:
  694. if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
  695. err = -EFAULT;
  696. out:
  697. free_dev_ioctl(param);
  698. return err;
  699. }
  700. static long autofs_dev_ioctl(struct file *file, uint command, ulong u)
  701. {
  702. int err;
  703. err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
  704. return (long) err;
  705. }
  706. #ifdef CONFIG_COMPAT
  707. static long autofs_dev_ioctl_compat(struct file *file, uint command, ulong u)
  708. {
  709. return (long) autofs_dev_ioctl(file, command, (ulong) compat_ptr(u));
  710. }
  711. #else
  712. #define autofs_dev_ioctl_compat NULL
  713. #endif
  714. static const struct file_operations _dev_ioctl_fops = {
  715. .unlocked_ioctl = autofs_dev_ioctl,
  716. .compat_ioctl = autofs_dev_ioctl_compat,
  717. .owner = THIS_MODULE,
  718. };
  719. static struct miscdevice _autofs_dev_ioctl_misc = {
  720. .minor = MISC_DYNAMIC_MINOR,
  721. .name = AUTOFS_DEVICE_NAME,
  722. .fops = &_dev_ioctl_fops
  723. };
  724. /* Register/deregister misc character device */
  725. int autofs_dev_ioctl_init(void)
  726. {
  727. int r;
  728. r = misc_register(&_autofs_dev_ioctl_misc);
  729. if (r) {
  730. AUTOFS_ERROR("misc_register failed for control device");
  731. return r;
  732. }
  733. return 0;
  734. }
  735. void autofs_dev_ioctl_exit(void)
  736. {
  737. misc_deregister(&_autofs_dev_ioctl_misc);
  738. return;
  739. }