dev-ioctl.c 20 KB

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