dev-ioctl.c 18 KB

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