compat_ioctl.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <linux/blkdev.h>
  2. #include <linux/blkpg.h>
  3. #include <linux/blktrace_api.h>
  4. #include <linux/cdrom.h>
  5. #include <linux/compat.h>
  6. #include <linux/elevator.h>
  7. #include <linux/fd.h>
  8. #include <linux/hdreg.h>
  9. #include <linux/syscalls.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/types.h>
  12. #include <linux/uaccess.h>
  13. static int compat_put_ushort(unsigned long arg, unsigned short val)
  14. {
  15. return put_user(val, (unsigned short __user *)compat_ptr(arg));
  16. }
  17. static int compat_put_int(unsigned long arg, int val)
  18. {
  19. return put_user(val, (compat_int_t __user *)compat_ptr(arg));
  20. }
  21. static int compat_put_long(unsigned long arg, long val)
  22. {
  23. return put_user(val, (compat_long_t __user *)compat_ptr(arg));
  24. }
  25. static int compat_put_ulong(unsigned long arg, compat_ulong_t val)
  26. {
  27. return put_user(val, (compat_ulong_t __user *)compat_ptr(arg));
  28. }
  29. static int compat_put_u64(unsigned long arg, u64 val)
  30. {
  31. return put_user(val, (compat_u64 __user *)compat_ptr(arg));
  32. }
  33. #define BLKBSZGET_32 _IOR(0x12, 112, int)
  34. #define BLKBSZSET_32 _IOW(0x12, 113, int)
  35. #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
  36. static int compat_blkdev_locked_ioctl(struct inode *inode, struct file *file,
  37. struct block_device *bdev,
  38. unsigned cmd, unsigned long arg)
  39. {
  40. struct backing_dev_info *bdi;
  41. switch (cmd) {
  42. case BLKRAGET:
  43. case BLKFRAGET:
  44. if (!arg)
  45. return -EINVAL;
  46. bdi = blk_get_backing_dev_info(bdev);
  47. if (bdi == NULL)
  48. return -ENOTTY;
  49. return compat_put_long(arg,
  50. (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
  51. case BLKROGET: /* compatible */
  52. return compat_put_int(arg, bdev_read_only(bdev) != 0);
  53. case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
  54. return compat_put_int(arg, block_size(bdev));
  55. case BLKSSZGET: /* get block device hardware sector size */
  56. return compat_put_int(arg, bdev_hardsect_size(bdev));
  57. case BLKSECTGET:
  58. return compat_put_ushort(arg,
  59. bdev_get_queue(bdev)->max_sectors);
  60. case BLKRASET: /* compatible, but no compat_ptr (!) */
  61. case BLKFRASET:
  62. if (!capable(CAP_SYS_ADMIN))
  63. return -EACCES;
  64. bdi = blk_get_backing_dev_info(bdev);
  65. if (bdi == NULL)
  66. return -ENOTTY;
  67. bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
  68. return 0;
  69. case BLKGETSIZE:
  70. if ((bdev->bd_inode->i_size >> 9) > ~0UL)
  71. return -EFBIG;
  72. return compat_put_ulong(arg, bdev->bd_inode->i_size >> 9);
  73. case BLKGETSIZE64_32:
  74. return compat_put_u64(arg, bdev->bd_inode->i_size);
  75. }
  76. return -ENOIOCTLCMD;
  77. }
  78. /* Most of the generic ioctls are handled in the normal fallback path.
  79. This assumes the blkdev's low level compat_ioctl always returns
  80. ENOIOCTLCMD for unknown ioctls. */
  81. long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  82. {
  83. int ret = -ENOIOCTLCMD;
  84. struct inode *inode = file->f_mapping->host;
  85. struct block_device *bdev = inode->i_bdev;
  86. struct gendisk *disk = bdev->bd_disk;
  87. switch (cmd) {
  88. case BLKFLSBUF:
  89. case BLKROSET:
  90. /*
  91. * the ones below are implemented in blkdev_locked_ioctl,
  92. * but we call blkdev_ioctl, which gets the lock for us
  93. */
  94. case BLKRRPART:
  95. return blkdev_ioctl(inode, file, cmd,
  96. (unsigned long)compat_ptr(arg));
  97. case BLKBSZSET_32:
  98. return blkdev_ioctl(inode, file, BLKBSZSET,
  99. (unsigned long)compat_ptr(arg));
  100. }
  101. lock_kernel();
  102. ret = compat_blkdev_locked_ioctl(inode, file, bdev, cmd, arg);
  103. /* FIXME: why do we assume -> compat_ioctl needs the BKL? */
  104. if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
  105. ret = disk->fops->compat_ioctl(file, cmd, arg);
  106. unlock_kernel();
  107. return ret;
  108. }