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