ioctl.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * linux/fs/ioctl.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/syscalls.h>
  7. #include <linux/mm.h>
  8. #include <linux/smp_lock.h>
  9. #include <linux/capability.h>
  10. #include <linux/file.h>
  11. #include <linux/fs.h>
  12. #include <linux/security.h>
  13. #include <linux/module.h>
  14. #include <linux/uaccess.h>
  15. #include <asm/ioctls.h>
  16. /**
  17. * vfs_ioctl - call filesystem specific ioctl methods
  18. * @filp: open file to invoke ioctl method on
  19. * @cmd: ioctl command to execute
  20. * @arg: command-specific argument for ioctl
  21. *
  22. * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
  23. * invokes filesystem specific ->ioctl method. If neither method exists,
  24. * returns -ENOTTY.
  25. *
  26. * Returns 0 on success, -errno on error.
  27. */
  28. long vfs_ioctl(struct file *filp, unsigned int cmd,
  29. unsigned long arg)
  30. {
  31. int error = -ENOTTY;
  32. if (!filp->f_op)
  33. goto out;
  34. if (filp->f_op->unlocked_ioctl) {
  35. error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
  36. if (error == -ENOIOCTLCMD)
  37. error = -EINVAL;
  38. goto out;
  39. } else if (filp->f_op->ioctl) {
  40. lock_kernel();
  41. error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
  42. filp, cmd, arg);
  43. unlock_kernel();
  44. }
  45. out:
  46. return error;
  47. }
  48. static int ioctl_fibmap(struct file *filp, int __user *p)
  49. {
  50. struct address_space *mapping = filp->f_mapping;
  51. int res, block;
  52. /* do we support this mess? */
  53. if (!mapping->a_ops->bmap)
  54. return -EINVAL;
  55. if (!capable(CAP_SYS_RAWIO))
  56. return -EPERM;
  57. res = get_user(block, p);
  58. if (res)
  59. return res;
  60. lock_kernel();
  61. res = mapping->a_ops->bmap(mapping, block);
  62. unlock_kernel();
  63. return put_user(res, p);
  64. }
  65. static int file_ioctl(struct file *filp, unsigned int cmd,
  66. unsigned long arg)
  67. {
  68. struct inode *inode = filp->f_path.dentry->d_inode;
  69. int __user *p = (int __user *)arg;
  70. switch (cmd) {
  71. case FIBMAP:
  72. return ioctl_fibmap(filp, p);
  73. case FIGETBSZ:
  74. return put_user(inode->i_sb->s_blocksize, p);
  75. case FIONREAD:
  76. return put_user(i_size_read(inode) - filp->f_pos, p);
  77. }
  78. return vfs_ioctl(filp, cmd, arg);
  79. }
  80. static int ioctl_fionbio(struct file *filp, int __user *argp)
  81. {
  82. unsigned int flag;
  83. int on, error;
  84. error = get_user(on, argp);
  85. if (error)
  86. return error;
  87. flag = O_NONBLOCK;
  88. #ifdef __sparc__
  89. /* SunOS compatibility item. */
  90. if (O_NONBLOCK != O_NDELAY)
  91. flag |= O_NDELAY;
  92. #endif
  93. if (on)
  94. filp->f_flags |= flag;
  95. else
  96. filp->f_flags &= ~flag;
  97. return error;
  98. }
  99. static int ioctl_fioasync(unsigned int fd, struct file *filp,
  100. int __user *argp)
  101. {
  102. unsigned int flag;
  103. int on, error;
  104. error = get_user(on, argp);
  105. if (error)
  106. return error;
  107. flag = on ? FASYNC : 0;
  108. /* Did FASYNC state change ? */
  109. if ((flag ^ filp->f_flags) & FASYNC) {
  110. if (filp->f_op && filp->f_op->fasync) {
  111. lock_kernel();
  112. error = filp->f_op->fasync(fd, filp, on);
  113. unlock_kernel();
  114. } else
  115. error = -ENOTTY;
  116. }
  117. if (error)
  118. return error;
  119. if (on)
  120. filp->f_flags |= FASYNC;
  121. else
  122. filp->f_flags &= ~FASYNC;
  123. return error;
  124. }
  125. /*
  126. * When you add any new common ioctls to the switches above and below
  127. * please update compat_sys_ioctl() too.
  128. *
  129. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  130. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  131. */
  132. int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
  133. unsigned long arg)
  134. {
  135. int error = 0;
  136. int __user *argp = (int __user *)arg;
  137. switch (cmd) {
  138. case FIOCLEX:
  139. set_close_on_exec(fd, 1);
  140. break;
  141. case FIONCLEX:
  142. set_close_on_exec(fd, 0);
  143. break;
  144. case FIONBIO:
  145. error = ioctl_fionbio(filp, argp);
  146. break;
  147. case FIOASYNC:
  148. error = ioctl_fioasync(fd, filp, argp);
  149. break;
  150. case FIOQSIZE:
  151. if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
  152. S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
  153. S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
  154. loff_t res =
  155. inode_get_bytes(filp->f_path.dentry->d_inode);
  156. error = copy_to_user((loff_t __user *)arg, &res,
  157. sizeof(res)) ? -EFAULT : 0;
  158. } else
  159. error = -ENOTTY;
  160. break;
  161. default:
  162. if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
  163. error = file_ioctl(filp, cmd, arg);
  164. else
  165. error = vfs_ioctl(filp, cmd, arg);
  166. break;
  167. }
  168. return error;
  169. }
  170. asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
  171. {
  172. struct file *filp;
  173. int error = -EBADF;
  174. int fput_needed;
  175. filp = fget_light(fd, &fput_needed);
  176. if (!filp)
  177. goto out;
  178. error = security_file_ioctl(filp, cmd, arg);
  179. if (error)
  180. goto out_fput;
  181. error = do_vfs_ioctl(filp, fd, cmd, arg);
  182. out_fput:
  183. fput_light(filp, fput_needed);
  184. out:
  185. return error;
  186. }