dev-ioctl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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->protover.version = 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->protosubver.sub_version = 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->openmount.devid)
  289. return -EINVAL;
  290. param->ioctlfd = -1;
  291. path = param->path;
  292. devid = param->openmount.devid;
  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->ready.token;
  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->fail.token;
  333. status = param->fail.status ? param->fail.status : -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->setpipefd.pipefd == -1)
  355. return -EINVAL;
  356. pipefd = param->setpipefd.pipefd;
  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->timeout.timeout;
  395. param->timeout.timeout = 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->requester.uid = param->requester.gid = -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->requester.uid = ino->uid;
  436. param->requester.gid = 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->expire.how;
  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->askumount.may_umount = 0;
  487. if (may_umount(fp->f_path.mnt))
  488. param->askumount.may_umount = 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. unsigned int devid, magic;
  521. int err = -ENOENT;
  522. if (param->size <= sizeof(*param)) {
  523. err = -EINVAL;
  524. goto out;
  525. }
  526. path = param->path;
  527. type = param->ismountpoint.in.type;
  528. param->ismountpoint.out.devid = devid = 0;
  529. param->ismountpoint.out.magic = magic = 0;
  530. if (!fp || param->ioctlfd == -1) {
  531. if (type == AUTOFS_TYPE_ANY) {
  532. struct super_block *sb;
  533. err = path_lookup(path, LOOKUP_FOLLOW, &nd);
  534. if (err)
  535. goto out;
  536. sb = nd.path.dentry->d_sb;
  537. devid = new_encode_dev(sb->s_dev);
  538. } else {
  539. struct autofs_info *ino;
  540. err = path_lookup(path, LOOKUP_PARENT, &nd);
  541. if (err)
  542. goto out;
  543. err = autofs_dev_ioctl_find_sbi_type(&nd, type);
  544. if (err)
  545. goto out_release;
  546. ino = autofs4_dentry_ino(nd.path.dentry);
  547. devid = autofs4_get_dev(ino->sbi);
  548. }
  549. err = 0;
  550. if (nd.path.dentry->d_inode &&
  551. nd.path.mnt->mnt_root == nd.path.dentry) {
  552. err = 1;
  553. magic = nd.path.dentry->d_inode->i_sb->s_magic;
  554. }
  555. } else {
  556. dev_t dev = autofs4_get_dev(sbi);
  557. err = path_lookup(path, LOOKUP_PARENT, &nd);
  558. if (err)
  559. goto out;
  560. err = autofs_dev_ioctl_find_super(&nd, dev);
  561. if (err)
  562. goto out_release;
  563. devid = dev;
  564. err = have_submounts(nd.path.dentry);
  565. if (nd.path.mnt->mnt_mountpoint != nd.path.mnt->mnt_root) {
  566. if (follow_down(&nd.path.mnt, &nd.path.dentry)) {
  567. struct inode *inode = nd.path.dentry->d_inode;
  568. magic = inode->i_sb->s_magic;
  569. }
  570. }
  571. }
  572. param->ismountpoint.out.devid = devid;
  573. param->ismountpoint.out.magic = magic;
  574. out_release:
  575. path_put(&nd.path);
  576. out:
  577. return err;
  578. }
  579. /*
  580. * Our range of ioctl numbers isn't 0 based so we need to shift
  581. * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
  582. * lookup.
  583. */
  584. #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
  585. static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
  586. {
  587. static struct {
  588. int cmd;
  589. ioctl_fn fn;
  590. } _ioctls[] = {
  591. {cmd_idx(AUTOFS_DEV_IOCTL_VERSION_CMD), NULL},
  592. {cmd_idx(AUTOFS_DEV_IOCTL_PROTOVER_CMD),
  593. autofs_dev_ioctl_protover},
  594. {cmd_idx(AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD),
  595. autofs_dev_ioctl_protosubver},
  596. {cmd_idx(AUTOFS_DEV_IOCTL_OPENMOUNT_CMD),
  597. autofs_dev_ioctl_openmount},
  598. {cmd_idx(AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD),
  599. autofs_dev_ioctl_closemount},
  600. {cmd_idx(AUTOFS_DEV_IOCTL_READY_CMD),
  601. autofs_dev_ioctl_ready},
  602. {cmd_idx(AUTOFS_DEV_IOCTL_FAIL_CMD),
  603. autofs_dev_ioctl_fail},
  604. {cmd_idx(AUTOFS_DEV_IOCTL_SETPIPEFD_CMD),
  605. autofs_dev_ioctl_setpipefd},
  606. {cmd_idx(AUTOFS_DEV_IOCTL_CATATONIC_CMD),
  607. autofs_dev_ioctl_catatonic},
  608. {cmd_idx(AUTOFS_DEV_IOCTL_TIMEOUT_CMD),
  609. autofs_dev_ioctl_timeout},
  610. {cmd_idx(AUTOFS_DEV_IOCTL_REQUESTER_CMD),
  611. autofs_dev_ioctl_requester},
  612. {cmd_idx(AUTOFS_DEV_IOCTL_EXPIRE_CMD),
  613. autofs_dev_ioctl_expire},
  614. {cmd_idx(AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD),
  615. autofs_dev_ioctl_askumount},
  616. {cmd_idx(AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD),
  617. autofs_dev_ioctl_ismountpoint}
  618. };
  619. unsigned int idx = cmd_idx(cmd);
  620. return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx].fn;
  621. }
  622. /* ioctl dispatcher */
  623. static int _autofs_dev_ioctl(unsigned int command, struct autofs_dev_ioctl __user *user)
  624. {
  625. struct autofs_dev_ioctl *param;
  626. struct file *fp;
  627. struct autofs_sb_info *sbi;
  628. unsigned int cmd_first, cmd;
  629. ioctl_fn fn = NULL;
  630. int err = 0;
  631. /* only root can play with this */
  632. if (!capable(CAP_SYS_ADMIN))
  633. return -EPERM;
  634. cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
  635. cmd = _IOC_NR(command);
  636. if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
  637. cmd - cmd_first >= AUTOFS_DEV_IOCTL_IOC_COUNT) {
  638. return -ENOTTY;
  639. }
  640. /* Copy the parameters into kernel space. */
  641. param = copy_dev_ioctl(user);
  642. if (IS_ERR(param))
  643. return PTR_ERR(param);
  644. err = validate_dev_ioctl(command, param);
  645. if (err)
  646. goto out;
  647. /* The validate routine above always sets the version */
  648. if (cmd == AUTOFS_DEV_IOCTL_VERSION_CMD)
  649. goto done;
  650. fn = lookup_dev_ioctl(cmd);
  651. if (!fn) {
  652. AUTOFS_WARN("unknown command 0x%08x", command);
  653. return -ENOTTY;
  654. }
  655. fp = NULL;
  656. sbi = NULL;
  657. /*
  658. * For obvious reasons the openmount can't have a file
  659. * descriptor yet. We don't take a reference to the
  660. * file during close to allow for immediate release.
  661. */
  662. if (cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
  663. cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
  664. fp = fget(param->ioctlfd);
  665. if (!fp) {
  666. if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
  667. goto cont;
  668. err = -EBADF;
  669. goto out;
  670. }
  671. if (!fp->f_op) {
  672. err = -ENOTTY;
  673. fput(fp);
  674. goto out;
  675. }
  676. sbi = autofs_dev_ioctl_sbi(fp);
  677. if (!sbi || sbi->magic != AUTOFS_SBI_MAGIC) {
  678. err = -EINVAL;
  679. fput(fp);
  680. goto out;
  681. }
  682. /*
  683. * Admin needs to be able to set the mount catatonic in
  684. * order to be able to perform the re-open.
  685. */
  686. if (!autofs4_oz_mode(sbi) &&
  687. cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
  688. err = -EACCES;
  689. fput(fp);
  690. goto out;
  691. }
  692. }
  693. cont:
  694. err = fn(fp, sbi, param);
  695. if (fp)
  696. fput(fp);
  697. done:
  698. if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
  699. err = -EFAULT;
  700. out:
  701. free_dev_ioctl(param);
  702. return err;
  703. }
  704. static long autofs_dev_ioctl(struct file *file, uint command, ulong u)
  705. {
  706. int err;
  707. err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
  708. return (long) err;
  709. }
  710. #ifdef CONFIG_COMPAT
  711. static long autofs_dev_ioctl_compat(struct file *file, uint command, ulong u)
  712. {
  713. return (long) autofs_dev_ioctl(file, command, (ulong) compat_ptr(u));
  714. }
  715. #else
  716. #define autofs_dev_ioctl_compat NULL
  717. #endif
  718. static const struct file_operations _dev_ioctl_fops = {
  719. .unlocked_ioctl = autofs_dev_ioctl,
  720. .compat_ioctl = autofs_dev_ioctl_compat,
  721. .owner = THIS_MODULE,
  722. };
  723. static struct miscdevice _autofs_dev_ioctl_misc = {
  724. .minor = MISC_DYNAMIC_MINOR,
  725. .name = AUTOFS_DEVICE_NAME,
  726. .fops = &_dev_ioctl_fops
  727. };
  728. /* Register/deregister misc character device */
  729. int autofs_dev_ioctl_init(void)
  730. {
  731. int r;
  732. r = misc_register(&_autofs_dev_ioctl_misc);
  733. if (r) {
  734. AUTOFS_ERROR("misc_register failed for control device");
  735. return r;
  736. }
  737. return 0;
  738. }
  739. void autofs_dev_ioctl_exit(void)
  740. {
  741. misc_deregister(&_autofs_dev_ioctl_misc);
  742. return;
  743. }