ioctl.c 3.9 KB

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