compat_ioctl: move HDIO ioctl handling into drivers/ide
[linux-2.6-block.git] / block / compat_ioctl.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
f58c4c0a
AB
2#include <linux/blkdev.h>
3#include <linux/blkpg.h>
4#include <linux/blktrace_api.h>
5#include <linux/cdrom.h>
6#include <linux/compat.h>
7#include <linux/elevator.h>
f58c4c0a 8#include <linux/hdreg.h>
b2c0fcd2 9#include <linux/pr.h>
5a0e3ad6 10#include <linux/slab.h>
f58c4c0a 11#include <linux/syscalls.h>
f58c4c0a
AB
12#include <linux/types.h>
13#include <linux/uaccess.h>
14
15static int compat_put_ushort(unsigned long arg, unsigned short val)
16{
17 return put_user(val, (unsigned short __user *)compat_ptr(arg));
18}
19
20static int compat_put_int(unsigned long arg, int val)
21{
22 return put_user(val, (compat_int_t __user *)compat_ptr(arg));
23}
24
ac481c20
MP
25static int compat_put_uint(unsigned long arg, unsigned int val)
26{
27 return put_user(val, (compat_uint_t __user *)compat_ptr(arg));
28}
29
f58c4c0a
AB
30static int compat_put_long(unsigned long arg, long val)
31{
32 return put_user(val, (compat_long_t __user *)compat_ptr(arg));
33}
34
35static int compat_put_ulong(unsigned long arg, compat_ulong_t val)
36{
37 return put_user(val, (compat_ulong_t __user *)compat_ptr(arg));
38}
39
40static int compat_put_u64(unsigned long arg, u64 val)
41{
42 return put_user(val, (compat_u64 __user *)compat_ptr(arg));
43}
44
9617db08
AB
45struct compat_hd_geometry {
46 unsigned char heads;
47 unsigned char sectors;
48 unsigned short cylinders;
49 u32 start;
50};
51
52static int compat_hdio_getgeo(struct gendisk *disk, struct block_device *bdev,
53 struct compat_hd_geometry __user *ugeo)
54{
55 struct hd_geometry geo;
56 int ret;
57
58 if (!ugeo)
59 return -EINVAL;
60 if (!disk->fops->getgeo)
61 return -ENOTTY;
62
8b0d77f1 63 memset(&geo, 0, sizeof(geo));
9617db08
AB
64 /*
65 * We need to set the startsect first, the driver may
66 * want to override it.
67 */
68 geo.start = get_start_sect(bdev);
69 ret = disk->fops->getgeo(bdev, &geo);
70 if (ret)
71 return ret;
72
73 ret = copy_to_user(ugeo, &geo, 4);
3ddc5b46 74 ret |= put_user(geo.start, &ugeo->start);
9617db08
AB
75 if (ret)
76 ret = -EFAULT;
77
78 return ret;
79}
80
18cf7f87
AB
81struct compat_blkpg_ioctl_arg {
82 compat_int_t op;
83 compat_int_t flags;
84 compat_int_t datalen;
85 compat_caddr_t data;
86};
87
56b26add 88static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,
18cf7f87
AB
89 unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32)
90{
91 struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a));
92 compat_caddr_t udata;
93 compat_int_t n;
94 int err;
95
96 err = get_user(n, &ua32->op);
97 err |= put_user(n, &a->op);
98 err |= get_user(n, &ua32->flags);
99 err |= put_user(n, &a->flags);
100 err |= get_user(n, &ua32->datalen);
101 err |= put_user(n, &a->datalen);
102 err |= get_user(udata, &ua32->data);
103 err |= put_user(compat_ptr(udata), &a->data);
104 if (err)
105 return err;
106
56b26add 107 return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a);
18cf7f87
AB
108}
109
f58c4c0a
AB
110#define BLKBSZGET_32 _IOR(0x12, 112, int)
111#define BLKBSZSET_32 _IOW(0x12, 113, int)
112#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
113
45048d09
AV
114/* Most of the generic ioctls are handled in the normal fallback path.
115 This assumes the blkdev's low level compat_ioctl always returns
116 ENOIOCTLCMD for unknown ioctls. */
117long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
f58c4c0a 118{
45048d09
AV
119 int ret = -ENOIOCTLCMD;
120 struct inode *inode = file->f_mapping->host;
121 struct block_device *bdev = inode->i_bdev;
122 struct gendisk *disk = bdev->bd_disk;
123 fmode_t mode = file->f_mode;
45048d09 124 loff_t size;
63f26496 125 unsigned int max_sectors;
45048d09 126
fd4ce1ac
CH
127 /*
128 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
129 * to updated it before every ioctl.
130 */
45048d09 131 if (file->f_flags & O_NDELAY)
fd4ce1ac
CH
132 mode |= FMODE_NDELAY;
133 else
134 mode &= ~FMODE_NDELAY;
f58c4c0a
AB
135
136 switch (cmd) {
45048d09
AV
137 case HDIO_GETGEO:
138 return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
ac481c20
MP
139 case BLKPBSZGET:
140 return compat_put_uint(arg, bdev_physical_block_size(bdev));
141 case BLKIOMIN:
142 return compat_put_uint(arg, bdev_io_min(bdev));
143 case BLKIOOPT:
144 return compat_put_uint(arg, bdev_io_opt(bdev));
145 case BLKALIGNOFF:
146 return compat_put_int(arg, bdev_alignment_offset(bdev));
98262f27 147 case BLKDISCARDZEROES:
48920ff2 148 return compat_put_uint(arg, 0);
45048d09
AV
149 case BLKFLSBUF:
150 case BLKROSET:
151 case BLKDISCARD:
8d57a98c 152 case BLKSECDISCARD:
3b3a1814 153 case BLKZEROOUT:
45048d09
AV
154 /*
155 * the ones below are implemented in blkdev_locked_ioctl,
156 * but we call blkdev_ioctl, which gets the lock for us
157 */
158 case BLKRRPART:
673bdf8c
AB
159 case BLKREPORTZONE:
160 case BLKRESETZONE:
4b43f31d
AB
161 case BLKOPENZONE:
162 case BLKCLOSEZONE:
163 case BLKFINISHZONE:
21d37340
AB
164 case BLKGETZONESZ:
165 case BLKGETNRZONES:
56b26add 166 return blkdev_ioctl(bdev, mode, cmd,
45048d09
AV
167 (unsigned long)compat_ptr(arg));
168 case BLKBSZSET_32:
56b26add 169 return blkdev_ioctl(bdev, mode, BLKBSZSET,
45048d09
AV
170 (unsigned long)compat_ptr(arg));
171 case BLKPG:
56b26add 172 return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
f58c4c0a
AB
173 case BLKRAGET:
174 case BLKFRAGET:
175 if (!arg)
176 return -EINVAL;
f58c4c0a 177 return compat_put_long(arg,
efa7c9f9 178 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
f58c4c0a
AB
179 case BLKROGET: /* compatible */
180 return compat_put_int(arg, bdev_read_only(bdev) != 0);
181 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
182 return compat_put_int(arg, block_size(bdev));
183 case BLKSSZGET: /* get block device hardware sector size */
e1defc4f 184 return compat_put_int(arg, bdev_logical_block_size(bdev));
f58c4c0a 185 case BLKSECTGET:
63f26496
AM
186 max_sectors = min_t(unsigned int, USHRT_MAX,
187 queue_max_sectors(bdev_get_queue(bdev)));
188 return compat_put_ushort(arg, max_sectors);
ef00f59c
MP
189 case BLKROTATIONAL:
190 return compat_put_ushort(arg,
191 !blk_queue_nonrot(bdev_get_queue(bdev)));
f58c4c0a
AB
192 case BLKRASET: /* compatible, but no compat_ptr (!) */
193 case BLKFRASET:
194 if (!capable(CAP_SYS_ADMIN))
195 return -EACCES;
efa7c9f9 196 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
f58c4c0a
AB
197 return 0;
198 case BLKGETSIZE:
77304d2a 199 size = i_size_read(bdev->bd_inode);
45048d09 200 if ((size >> 9) > ~0UL)
f58c4c0a 201 return -EFBIG;
45048d09 202 return compat_put_ulong(arg, size >> 9);
f58c4c0a
AB
203
204 case BLKGETSIZE64_32:
77304d2a 205 return compat_put_u64(arg, i_size_read(bdev->bd_inode));
171044d4
AB
206
207 case BLKTRACESETUP32:
171044d4
AB
208 case BLKTRACESTART: /* compatible */
209 case BLKTRACESTOP: /* compatible */
210 case BLKTRACETEARDOWN: /* compatible */
45048d09 211 ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
7199d4cd 212 return ret;
b2c0fcd2
AB
213 case IOC_PR_REGISTER:
214 case IOC_PR_RESERVE:
215 case IOC_PR_RELEASE:
216 case IOC_PR_PREEMPT:
217 case IOC_PR_PREEMPT_ABORT:
218 case IOC_PR_CLEAR:
219 return blkdev_ioctl(bdev, mode, cmd,
220 (unsigned long)compat_ptr(arg));
45048d09
AV
221 default:
222 if (disk->fops->compat_ioctl)
223 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
45048d09
AV
224 return ret;
225 }
f58c4c0a 226}