ioctl.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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: [in] open file to invoke ioctl method on
  19. * @cmd: [in] ioctl command to execute
  20. * @arg: [in/out] 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 file_ioctl(struct file *filp, unsigned int cmd,
  49. unsigned long arg)
  50. {
  51. int error;
  52. int block;
  53. struct inode *inode = filp->f_path.dentry->d_inode;
  54. int __user *p = (int __user *)arg;
  55. switch (cmd) {
  56. case FIBMAP:
  57. {
  58. struct address_space *mapping = filp->f_mapping;
  59. int res;
  60. /* do we support this mess? */
  61. if (!mapping->a_ops->bmap)
  62. return -EINVAL;
  63. if (!capable(CAP_SYS_RAWIO))
  64. return -EPERM;
  65. error = get_user(block, p);
  66. if (error)
  67. return error;
  68. lock_kernel();
  69. res = mapping->a_ops->bmap(mapping, block);
  70. unlock_kernel();
  71. return put_user(res, p);
  72. }
  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. /*
  81. * When you add any new common ioctls to the switches above and below
  82. * please update compat_sys_ioctl() too.
  83. *
  84. * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  85. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  86. */
  87. int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
  88. unsigned long arg)
  89. {
  90. unsigned int flag;
  91. int on, error = 0;
  92. switch (cmd) {
  93. case FIOCLEX:
  94. set_close_on_exec(fd, 1);
  95. break;
  96. case FIONCLEX:
  97. set_close_on_exec(fd, 0);
  98. break;
  99. case FIONBIO:
  100. error = get_user(on, (int __user *)arg);
  101. if (error)
  102. break;
  103. flag = O_NONBLOCK;
  104. #ifdef __sparc__
  105. /* SunOS compatibility item. */
  106. if (O_NONBLOCK != O_NDELAY)
  107. flag |= O_NDELAY;
  108. #endif
  109. if (on)
  110. filp->f_flags |= flag;
  111. else
  112. filp->f_flags &= ~flag;
  113. break;
  114. case FIOASYNC:
  115. error = get_user(on, (int __user *)arg);
  116. if (error)
  117. break;
  118. flag = on ? FASYNC : 0;
  119. /* Did FASYNC state change ? */
  120. if ((flag ^ filp->f_flags) & FASYNC) {
  121. if (filp->f_op && filp->f_op->fasync) {
  122. lock_kernel();
  123. error = filp->f_op->fasync(fd, filp, on);
  124. unlock_kernel();
  125. } else
  126. error = -ENOTTY;
  127. }
  128. if (error != 0)
  129. break;
  130. if (on)
  131. filp->f_flags |= FASYNC;
  132. else
  133. filp->f_flags &= ~FASYNC;
  134. break;
  135. case FIOQSIZE:
  136. if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
  137. S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
  138. S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
  139. loff_t res =
  140. inode_get_bytes(filp->f_path.dentry->d_inode);
  141. error = copy_to_user((loff_t __user *)arg, &res,
  142. sizeof(res)) ? -EFAULT : 0;
  143. } else
  144. error = -ENOTTY;
  145. break;
  146. default:
  147. if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
  148. error = file_ioctl(filp, cmd, arg);
  149. else
  150. error = vfs_ioctl(filp, cmd, arg);
  151. break;
  152. }
  153. return error;
  154. }
  155. asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
  156. {
  157. struct file *filp;
  158. int error = -EBADF;
  159. int fput_needed;
  160. filp = fget_light(fd, &fput_needed);
  161. if (!filp)
  162. goto out;
  163. error = security_file_ioctl(filp, cmd, arg);
  164. if (error)
  165. goto out_fput;
  166. error = do_vfs_ioctl(filp, fd, cmd, arg);
  167. out_fput:
  168. fput_light(filp, fput_needed);
  169. out:
  170. return error;
  171. }