compat_ioctl: move CDROMREADADIO to cdrom.c
[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
33c2dca4
AV
81static int compat_hdio_ioctl(struct block_device *bdev, fmode_t mode,
82 unsigned int cmd, unsigned long arg)
9617db08 83{
8363dae2 84 unsigned long __user *p;
9617db08
AB
85 int error;
86
30138384 87 p = compat_alloc_user_space(sizeof(unsigned long));
33c2dca4 88 error = __blkdev_driver_ioctl(bdev, mode,
30138384 89 cmd, (unsigned long)p);
9617db08 90 if (error == 0) {
30138384
AV
91 unsigned int __user *uvp = compat_ptr(arg);
92 unsigned long v;
93 if (get_user(v, p) || put_user(v, uvp))
9617db08
AB
94 error = -EFAULT;
95 }
96 return error;
97}
98
18cf7f87
AB
99struct compat_blkpg_ioctl_arg {
100 compat_int_t op;
101 compat_int_t flags;
102 compat_int_t datalen;
103 compat_caddr_t data;
104};
105
56b26add 106static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,
18cf7f87
AB
107 unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32)
108{
109 struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a));
110 compat_caddr_t udata;
111 compat_int_t n;
112 int err;
113
114 err = get_user(n, &ua32->op);
115 err |= put_user(n, &a->op);
116 err |= get_user(n, &ua32->flags);
117 err |= put_user(n, &a->flags);
118 err |= get_user(n, &ua32->datalen);
119 err |= put_user(n, &a->datalen);
120 err |= get_user(udata, &ua32->data);
121 err |= put_user(compat_ptr(udata), &a->data);
122 if (err)
123 return err;
124
56b26add 125 return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a);
18cf7f87
AB
126}
127
f58c4c0a
AB
128#define BLKBSZGET_32 _IOR(0x12, 112, int)
129#define BLKBSZSET_32 _IOW(0x12, 113, int)
130#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
131
33c2dca4
AV
132static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
133 unsigned cmd, unsigned long arg)
7199d4cd 134{
33013a88 135 switch (cmd) {
9617db08
AB
136 case HDIO_GET_UNMASKINTR:
137 case HDIO_GET_MULTCOUNT:
138 case HDIO_GET_KEEPSETTINGS:
139 case HDIO_GET_32BIT:
140 case HDIO_GET_NOWERR:
141 case HDIO_GET_DMA:
142 case HDIO_GET_NICE:
143 case HDIO_GET_WCACHE:
144 case HDIO_GET_ACOUSTIC:
145 case HDIO_GET_ADDRESS:
146 case HDIO_GET_BUSSTATE:
33c2dca4 147 return compat_hdio_ioctl(bdev, mode, cmd, arg);
b3087cc4 148
7199d4cd
AB
149 /*
150 * No handler required for the ones below, we just need to
151 * convert arg to a 64 bit pointer.
152 */
153 case BLKSECTSET:
154 /*
155 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
156 * Some need translations, these do not.
157 */
158 case HDIO_GET_IDENTITY:
159 case HDIO_DRIVE_TASK:
160 case HDIO_DRIVE_CMD:
7199d4cd
AB
161 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
162 case 0x330:
7199d4cd
AB
163 /* CDROM stuff */
164 case CDROMPAUSE:
165 case CDROMRESUME:
166 case CDROMPLAYMSF:
167 case CDROMPLAYTRKIND:
168 case CDROMREADTOCHDR:
169 case CDROMREADTOCENTRY:
170 case CDROMSTOP:
171 case CDROMSTART:
172 case CDROMEJECT:
173 case CDROMVOLCTRL:
174 case CDROMSUBCHNL:
175 case CDROMMULTISESSION:
176 case CDROM_GET_MCN:
177 case CDROMRESET:
178 case CDROMVOLREAD:
179 case CDROMSEEK:
180 case CDROMPLAYBLK:
181 case CDROMCLOSETRAY:
182 case CDROM_DISC_STATUS:
183 case CDROM_CHANGER_NSLOTS:
184 case CDROM_GET_CAPABILITY:
f3ee6e63 185 case CDROM_SEND_PACKET:
7199d4cd
AB
186 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
187 * not take a struct cdrom_read, instead they take a struct cdrom_msf
188 * which is compatible.
189 */
190 case CDROMREADMODE2:
191 case CDROMREADMODE1:
192 case CDROMREADRAW:
193 case CDROMREADCOOKED:
194 case CDROMREADALL:
195 /* DVD ioctls */
196 case DVD_READ_STRUCT:
197 case DVD_WRITE_STRUCT:
198 case DVD_AUTH:
199 arg = (unsigned long)compat_ptr(arg);
1c925604
AS
200 /* These intepret arg as an unsigned long, not as a pointer,
201 * so we must not do compat_ptr() conversion. */
202 case HDIO_SET_MULTCOUNT:
203 case HDIO_SET_UNMASKINTR:
204 case HDIO_SET_KEEPSETTINGS:
205 case HDIO_SET_32BIT:
206 case HDIO_SET_NOWERR:
207 case HDIO_SET_DMA:
208 case HDIO_SET_PIO_MODE:
209 case HDIO_SET_NICE:
210 case HDIO_SET_WCACHE:
211 case HDIO_SET_ACOUSTIC:
212 case HDIO_SET_BUSSTATE:
213 case HDIO_SET_ADDRESS:
214 case CDROMEJECT_SW:
215 case CDROM_SET_OPTIONS:
216 case CDROM_CLEAR_OPTIONS:
217 case CDROM_SELECT_SPEED:
218 case CDROM_SELECT_DISC:
219 case CDROM_MEDIA_CHANGED:
220 case CDROM_DRIVE_STATUS:
221 case CDROM_LOCKDOOR:
222 case CDROM_DEBUG:
7199d4cd
AB
223 break;
224 default:
225 /* unknown ioctl number */
226 return -ENOIOCTLCMD;
227 }
228
33c2dca4 229 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
7199d4cd
AB
230}
231
45048d09
AV
232/* Most of the generic ioctls are handled in the normal fallback path.
233 This assumes the blkdev's low level compat_ioctl always returns
234 ENOIOCTLCMD for unknown ioctls. */
235long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
f58c4c0a 236{
45048d09
AV
237 int ret = -ENOIOCTLCMD;
238 struct inode *inode = file->f_mapping->host;
239 struct block_device *bdev = inode->i_bdev;
240 struct gendisk *disk = bdev->bd_disk;
241 fmode_t mode = file->f_mode;
45048d09 242 loff_t size;
63f26496 243 unsigned int max_sectors;
45048d09 244
fd4ce1ac
CH
245 /*
246 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
247 * to updated it before every ioctl.
248 */
45048d09 249 if (file->f_flags & O_NDELAY)
fd4ce1ac
CH
250 mode |= FMODE_NDELAY;
251 else
252 mode &= ~FMODE_NDELAY;
f58c4c0a
AB
253
254 switch (cmd) {
45048d09
AV
255 case HDIO_GETGEO:
256 return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
ac481c20
MP
257 case BLKPBSZGET:
258 return compat_put_uint(arg, bdev_physical_block_size(bdev));
259 case BLKIOMIN:
260 return compat_put_uint(arg, bdev_io_min(bdev));
261 case BLKIOOPT:
262 return compat_put_uint(arg, bdev_io_opt(bdev));
263 case BLKALIGNOFF:
264 return compat_put_int(arg, bdev_alignment_offset(bdev));
98262f27 265 case BLKDISCARDZEROES:
48920ff2 266 return compat_put_uint(arg, 0);
45048d09
AV
267 case BLKFLSBUF:
268 case BLKROSET:
269 case BLKDISCARD:
8d57a98c 270 case BLKSECDISCARD:
3b3a1814 271 case BLKZEROOUT:
45048d09
AV
272 /*
273 * the ones below are implemented in blkdev_locked_ioctl,
274 * but we call blkdev_ioctl, which gets the lock for us
275 */
276 case BLKRRPART:
673bdf8c
AB
277 case BLKREPORTZONE:
278 case BLKRESETZONE:
4b43f31d
AB
279 case BLKOPENZONE:
280 case BLKCLOSEZONE:
281 case BLKFINISHZONE:
21d37340
AB
282 case BLKGETZONESZ:
283 case BLKGETNRZONES:
56b26add 284 return blkdev_ioctl(bdev, mode, cmd,
45048d09
AV
285 (unsigned long)compat_ptr(arg));
286 case BLKBSZSET_32:
56b26add 287 return blkdev_ioctl(bdev, mode, BLKBSZSET,
45048d09
AV
288 (unsigned long)compat_ptr(arg));
289 case BLKPG:
56b26add 290 return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
f58c4c0a
AB
291 case BLKRAGET:
292 case BLKFRAGET:
293 if (!arg)
294 return -EINVAL;
f58c4c0a 295 return compat_put_long(arg,
efa7c9f9 296 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
f58c4c0a
AB
297 case BLKROGET: /* compatible */
298 return compat_put_int(arg, bdev_read_only(bdev) != 0);
299 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
300 return compat_put_int(arg, block_size(bdev));
301 case BLKSSZGET: /* get block device hardware sector size */
e1defc4f 302 return compat_put_int(arg, bdev_logical_block_size(bdev));
f58c4c0a 303 case BLKSECTGET:
63f26496
AM
304 max_sectors = min_t(unsigned int, USHRT_MAX,
305 queue_max_sectors(bdev_get_queue(bdev)));
306 return compat_put_ushort(arg, max_sectors);
ef00f59c
MP
307 case BLKROTATIONAL:
308 return compat_put_ushort(arg,
309 !blk_queue_nonrot(bdev_get_queue(bdev)));
f58c4c0a
AB
310 case BLKRASET: /* compatible, but no compat_ptr (!) */
311 case BLKFRASET:
312 if (!capable(CAP_SYS_ADMIN))
313 return -EACCES;
efa7c9f9 314 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
f58c4c0a
AB
315 return 0;
316 case BLKGETSIZE:
77304d2a 317 size = i_size_read(bdev->bd_inode);
45048d09 318 if ((size >> 9) > ~0UL)
f58c4c0a 319 return -EFBIG;
45048d09 320 return compat_put_ulong(arg, size >> 9);
f58c4c0a
AB
321
322 case BLKGETSIZE64_32:
77304d2a 323 return compat_put_u64(arg, i_size_read(bdev->bd_inode));
171044d4
AB
324
325 case BLKTRACESETUP32:
171044d4
AB
326 case BLKTRACESTART: /* compatible */
327 case BLKTRACESTOP: /* compatible */
328 case BLKTRACETEARDOWN: /* compatible */
45048d09 329 ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
7199d4cd 330 return ret;
b2c0fcd2
AB
331 case IOC_PR_REGISTER:
332 case IOC_PR_RESERVE:
333 case IOC_PR_RELEASE:
334 case IOC_PR_PREEMPT:
335 case IOC_PR_PREEMPT_ABORT:
336 case IOC_PR_CLEAR:
337 return blkdev_ioctl(bdev, mode, cmd,
338 (unsigned long)compat_ptr(arg));
45048d09
AV
339 default:
340 if (disk->fops->compat_ioctl)
341 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
342 if (ret == -ENOIOCTLCMD)
343 ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg);
344 return ret;
345 }
f58c4c0a 346}