ioctl.c 3.9 KB

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