dev-ioctl.c 18 KB

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