blob: 6ca015f92766e9052d444f9e81ba3c307846166c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnd Bergmannf58c4c02007-10-09 13:23:51 +02002#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>
Arnd Bergmannf58c4c02007-10-09 13:23:51 +02008#include <linux/hdreg.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Arnd Bergmannf58c4c02007-10-09 13:23:51 +020010#include <linux/syscalls.h>
Arnd Bergmannf58c4c02007-10-09 13:23:51 +020011#include <linux/types.h>
12#include <linux/uaccess.h>
13
14static int compat_put_ushort(unsigned long arg, unsigned short val)
15{
16 return put_user(val, (unsigned short __user *)compat_ptr(arg));
17}
18
19static int compat_put_int(unsigned long arg, int val)
20{
21 return put_user(val, (compat_int_t __user *)compat_ptr(arg));
22}
23
Martin K. Petersenac481c22009-10-03 20:52:01 +020024static int compat_put_uint(unsigned long arg, unsigned int val)
25{
26 return put_user(val, (compat_uint_t __user *)compat_ptr(arg));
27}
28
Arnd Bergmannf58c4c02007-10-09 13:23:51 +020029static int compat_put_long(unsigned long arg, long val)
30{
31 return put_user(val, (compat_long_t __user *)compat_ptr(arg));
32}
33
34static int compat_put_ulong(unsigned long arg, compat_ulong_t val)
35{
36 return put_user(val, (compat_ulong_t __user *)compat_ptr(arg));
37}
38
39static int compat_put_u64(unsigned long arg, u64 val)
40{
41 return put_user(val, (compat_u64 __user *)compat_ptr(arg));
42}
43
Arnd Bergmann9617db02007-10-09 13:23:55 +020044struct compat_hd_geometry {
45 unsigned char heads;
46 unsigned char sectors;
47 unsigned short cylinders;
48 u32 start;
49};
50
51static int compat_hdio_getgeo(struct gendisk *disk, struct block_device *bdev,
52 struct compat_hd_geometry __user *ugeo)
53{
54 struct hd_geometry geo;
55 int ret;
56
57 if (!ugeo)
58 return -EINVAL;
59 if (!disk->fops->getgeo)
60 return -ENOTTY;
61
Cong Wang8b0d77f2013-07-03 15:01:12 -070062 memset(&geo, 0, sizeof(geo));
Arnd Bergmann9617db02007-10-09 13:23:55 +020063 /*
64 * We need to set the startsect first, the driver may
65 * want to override it.
66 */
67 geo.start = get_start_sect(bdev);
68 ret = disk->fops->getgeo(bdev, &geo);
69 if (ret)
70 return ret;
71
72 ret = copy_to_user(ugeo, &geo, 4);
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -070073 ret |= put_user(geo.start, &ugeo->start);
Arnd Bergmann9617db02007-10-09 13:23:55 +020074 if (ret)
75 ret = -EFAULT;
76
77 return ret;
78}
79
Al Viro33c2dca2008-02-22 22:02:50 -050080static int compat_hdio_ioctl(struct block_device *bdev, fmode_t mode,
81 unsigned int cmd, unsigned long arg)
Arnd Bergmann9617db02007-10-09 13:23:55 +020082{
Bart Van Assche8363dae2017-08-23 15:29:11 -070083 unsigned long __user *p;
Arnd Bergmann9617db02007-10-09 13:23:55 +020084 int error;
85
Al Viro30138382017-06-27 15:51:38 -040086 p = compat_alloc_user_space(sizeof(unsigned long));
Al Viro33c2dca2008-02-22 22:02:50 -050087 error = __blkdev_driver_ioctl(bdev, mode,
Al Viro30138382017-06-27 15:51:38 -040088 cmd, (unsigned long)p);
Arnd Bergmann9617db02007-10-09 13:23:55 +020089 if (error == 0) {
Al Viro30138382017-06-27 15:51:38 -040090 unsigned int __user *uvp = compat_ptr(arg);
91 unsigned long v;
92 if (get_user(v, p) || put_user(v, uvp))
Arnd Bergmann9617db02007-10-09 13:23:55 +020093 error = -EFAULT;
94 }
95 return error;
96}
97
Arnd Bergmannb3087cc2007-10-09 13:23:56 +020098struct compat_cdrom_read_audio {
99 union cdrom_addr addr;
100 u8 addr_format;
101 compat_int_t nframes;
102 compat_caddr_t buf;
103};
104
105struct compat_cdrom_generic_command {
106 unsigned char cmd[CDROM_PACKET_SIZE];
107 compat_caddr_t buffer;
108 compat_uint_t buflen;
109 compat_int_t stat;
110 compat_caddr_t sense;
111 unsigned char data_direction;
112 compat_int_t quiet;
113 compat_int_t timeout;
114 compat_caddr_t reserved[1];
115};
116
Al Viro33c2dca2008-02-22 22:02:50 -0500117static int compat_cdrom_read_audio(struct block_device *bdev, fmode_t mode,
118 unsigned int cmd, unsigned long arg)
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200119{
120 struct cdrom_read_audio __user *cdread_audio;
121 struct compat_cdrom_read_audio __user *cdread_audio32;
122 __u32 data;
123 void __user *datap;
124
125 cdread_audio = compat_alloc_user_space(sizeof(*cdread_audio));
126 cdread_audio32 = compat_ptr(arg);
127
128 if (copy_in_user(&cdread_audio->addr,
129 &cdread_audio32->addr,
130 (sizeof(*cdread_audio32) -
131 sizeof(compat_caddr_t))))
132 return -EFAULT;
133
134 if (get_user(data, &cdread_audio32->buf))
135 return -EFAULT;
136 datap = compat_ptr(data);
137 if (put_user(datap, &cdread_audio->buf))
138 return -EFAULT;
139
Al Viro33c2dca2008-02-22 22:02:50 -0500140 return __blkdev_driver_ioctl(bdev, mode, cmd,
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200141 (unsigned long)cdread_audio);
142}
143
Al Viro33c2dca2008-02-22 22:02:50 -0500144static int compat_cdrom_generic_command(struct block_device *bdev, fmode_t mode,
145 unsigned int cmd, unsigned long arg)
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200146{
147 struct cdrom_generic_command __user *cgc;
148 struct compat_cdrom_generic_command __user *cgc32;
149 u32 data;
150 unsigned char dir;
151 int itmp;
152
153 cgc = compat_alloc_user_space(sizeof(*cgc));
154 cgc32 = compat_ptr(arg);
155
156 if (copy_in_user(&cgc->cmd, &cgc32->cmd, sizeof(cgc->cmd)) ||
157 get_user(data, &cgc32->buffer) ||
158 put_user(compat_ptr(data), &cgc->buffer) ||
159 copy_in_user(&cgc->buflen, &cgc32->buflen,
160 (sizeof(unsigned int) + sizeof(int))) ||
161 get_user(data, &cgc32->sense) ||
162 put_user(compat_ptr(data), &cgc->sense) ||
163 get_user(dir, &cgc32->data_direction) ||
164 put_user(dir, &cgc->data_direction) ||
165 get_user(itmp, &cgc32->quiet) ||
166 put_user(itmp, &cgc->quiet) ||
167 get_user(itmp, &cgc32->timeout) ||
168 put_user(itmp, &cgc->timeout) ||
169 get_user(data, &cgc32->reserved[0]) ||
170 put_user(compat_ptr(data), &cgc->reserved[0]))
171 return -EFAULT;
172
Al Viro33c2dca2008-02-22 22:02:50 -0500173 return __blkdev_driver_ioctl(bdev, mode, cmd, (unsigned long)cgc);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200174}
175
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200176struct compat_blkpg_ioctl_arg {
177 compat_int_t op;
178 compat_int_t flags;
179 compat_int_t datalen;
180 compat_caddr_t data;
181};
182
Al Viro56b26ad2008-09-19 03:17:36 -0400183static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200184 unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32)
185{
186 struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a));
187 compat_caddr_t udata;
188 compat_int_t n;
189 int err;
190
191 err = get_user(n, &ua32->op);
192 err |= put_user(n, &a->op);
193 err |= get_user(n, &ua32->flags);
194 err |= put_user(n, &a->flags);
195 err |= get_user(n, &ua32->datalen);
196 err |= put_user(n, &a->datalen);
197 err |= get_user(udata, &ua32->data);
198 err |= put_user(compat_ptr(udata), &a->data);
199 if (err)
200 return err;
201
Al Viro56b26ad2008-09-19 03:17:36 -0400202 return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a);
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200203}
204
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200205#define BLKBSZGET_32 _IOR(0x12, 112, int)
206#define BLKBSZSET_32 _IOW(0x12, 113, int)
207#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
208
Al Viro33c2dca2008-02-22 22:02:50 -0500209static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
210 unsigned cmd, unsigned long arg)
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200211{
Philip Langdale33013a82007-10-27 13:34:10 +0200212 switch (cmd) {
Arnd Bergmann9617db02007-10-09 13:23:55 +0200213 case HDIO_GET_UNMASKINTR:
214 case HDIO_GET_MULTCOUNT:
215 case HDIO_GET_KEEPSETTINGS:
216 case HDIO_GET_32BIT:
217 case HDIO_GET_NOWERR:
218 case HDIO_GET_DMA:
219 case HDIO_GET_NICE:
220 case HDIO_GET_WCACHE:
221 case HDIO_GET_ACOUSTIC:
222 case HDIO_GET_ADDRESS:
223 case HDIO_GET_BUSSTATE:
Al Viro33c2dca2008-02-22 22:02:50 -0500224 return compat_hdio_ioctl(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200225 case CDROMREADAUDIO:
Al Viro33c2dca2008-02-22 22:02:50 -0500226 return compat_cdrom_read_audio(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200227 case CDROM_SEND_PACKET:
Al Viro33c2dca2008-02-22 22:02:50 -0500228 return compat_cdrom_generic_command(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200229
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200230 /*
231 * No handler required for the ones below, we just need to
232 * convert arg to a 64 bit pointer.
233 */
234 case BLKSECTSET:
235 /*
236 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
237 * Some need translations, these do not.
238 */
239 case HDIO_GET_IDENTITY:
240 case HDIO_DRIVE_TASK:
241 case HDIO_DRIVE_CMD:
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200242 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
243 case 0x330:
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200244 /* CDROM stuff */
245 case CDROMPAUSE:
246 case CDROMRESUME:
247 case CDROMPLAYMSF:
248 case CDROMPLAYTRKIND:
249 case CDROMREADTOCHDR:
250 case CDROMREADTOCENTRY:
251 case CDROMSTOP:
252 case CDROMSTART:
253 case CDROMEJECT:
254 case CDROMVOLCTRL:
255 case CDROMSUBCHNL:
256 case CDROMMULTISESSION:
257 case CDROM_GET_MCN:
258 case CDROMRESET:
259 case CDROMVOLREAD:
260 case CDROMSEEK:
261 case CDROMPLAYBLK:
262 case CDROMCLOSETRAY:
263 case CDROM_DISC_STATUS:
264 case CDROM_CHANGER_NSLOTS:
265 case CDROM_GET_CAPABILITY:
266 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
267 * not take a struct cdrom_read, instead they take a struct cdrom_msf
268 * which is compatible.
269 */
270 case CDROMREADMODE2:
271 case CDROMREADMODE1:
272 case CDROMREADRAW:
273 case CDROMREADCOOKED:
274 case CDROMREADALL:
275 /* DVD ioctls */
276 case DVD_READ_STRUCT:
277 case DVD_WRITE_STRUCT:
278 case DVD_AUTH:
279 arg = (unsigned long)compat_ptr(arg);
Andreas Schwab1c9256042008-10-31 22:39:46 +0100280 /* These intepret arg as an unsigned long, not as a pointer,
281 * so we must not do compat_ptr() conversion. */
282 case HDIO_SET_MULTCOUNT:
283 case HDIO_SET_UNMASKINTR:
284 case HDIO_SET_KEEPSETTINGS:
285 case HDIO_SET_32BIT:
286 case HDIO_SET_NOWERR:
287 case HDIO_SET_DMA:
288 case HDIO_SET_PIO_MODE:
289 case HDIO_SET_NICE:
290 case HDIO_SET_WCACHE:
291 case HDIO_SET_ACOUSTIC:
292 case HDIO_SET_BUSSTATE:
293 case HDIO_SET_ADDRESS:
294 case CDROMEJECT_SW:
295 case CDROM_SET_OPTIONS:
296 case CDROM_CLEAR_OPTIONS:
297 case CDROM_SELECT_SPEED:
298 case CDROM_SELECT_DISC:
299 case CDROM_MEDIA_CHANGED:
300 case CDROM_DRIVE_STATUS:
301 case CDROM_LOCKDOOR:
302 case CDROM_DEBUG:
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200303 break;
304 default:
305 /* unknown ioctl number */
306 return -ENOIOCTLCMD;
307 }
308
Al Viro33c2dca2008-02-22 22:02:50 -0500309 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200310}
311
Al Viro45048d02008-09-18 15:53:24 -0400312/* Most of the generic ioctls are handled in the normal fallback path.
313 This assumes the blkdev's low level compat_ioctl always returns
314 ENOIOCTLCMD for unknown ioctls. */
315long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200316{
Al Viro45048d02008-09-18 15:53:24 -0400317 int ret = -ENOIOCTLCMD;
318 struct inode *inode = file->f_mapping->host;
319 struct block_device *bdev = inode->i_bdev;
320 struct gendisk *disk = bdev->bd_disk;
321 fmode_t mode = file->f_mode;
Al Viro45048d02008-09-18 15:53:24 -0400322 loff_t size;
Akinobu Mita63f26492014-05-25 21:43:33 +0900323 unsigned int max_sectors;
Al Viro45048d02008-09-18 15:53:24 -0400324
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +0100325 /*
326 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
327 * to updated it before every ioctl.
328 */
Al Viro45048d02008-09-18 15:53:24 -0400329 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +0100330 mode |= FMODE_NDELAY;
331 else
332 mode &= ~FMODE_NDELAY;
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200333
334 switch (cmd) {
Al Viro45048d02008-09-18 15:53:24 -0400335 case HDIO_GETGEO:
336 return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200337 case BLKPBSZGET:
338 return compat_put_uint(arg, bdev_physical_block_size(bdev));
339 case BLKIOMIN:
340 return compat_put_uint(arg, bdev_io_min(bdev));
341 case BLKIOOPT:
342 return compat_put_uint(arg, bdev_io_opt(bdev));
343 case BLKALIGNOFF:
344 return compat_put_int(arg, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100345 case BLKDISCARDZEROES:
Christoph Hellwig48920ff2017-04-05 19:21:23 +0200346 return compat_put_uint(arg, 0);
Al Viro45048d02008-09-18 15:53:24 -0400347 case BLKFLSBUF:
348 case BLKROSET:
349 case BLKDISCARD:
Adrian Hunter8d57a982010-08-11 14:17:49 -0700350 case BLKSECDISCARD:
Mikulas Patocka3b3a1812014-07-02 12:46:23 -0400351 case BLKZEROOUT:
Al Viro45048d02008-09-18 15:53:24 -0400352 /*
353 * the ones below are implemented in blkdev_locked_ioctl,
354 * but we call blkdev_ioctl, which gets the lock for us
355 */
356 case BLKRRPART:
Al Viro56b26ad2008-09-19 03:17:36 -0400357 return blkdev_ioctl(bdev, mode, cmd,
Al Viro45048d02008-09-18 15:53:24 -0400358 (unsigned long)compat_ptr(arg));
359 case BLKBSZSET_32:
Al Viro56b26ad2008-09-19 03:17:36 -0400360 return blkdev_ioctl(bdev, mode, BLKBSZSET,
Al Viro45048d02008-09-18 15:53:24 -0400361 (unsigned long)compat_ptr(arg));
362 case BLKPG:
Al Viro56b26ad2008-09-19 03:17:36 -0400363 return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200364 case BLKRAGET:
365 case BLKFRAGET:
366 if (!arg)
367 return -EINVAL;
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200368 return compat_put_long(arg,
Jan Karaefa7c9f2017-02-02 15:56:53 +0100369 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200370 case BLKROGET: /* compatible */
371 return compat_put_int(arg, bdev_read_only(bdev) != 0);
372 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
373 return compat_put_int(arg, block_size(bdev));
374 case BLKSSZGET: /* get block device hardware sector size */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400375 return compat_put_int(arg, bdev_logical_block_size(bdev));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200376 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900377 max_sectors = min_t(unsigned int, USHRT_MAX,
378 queue_max_sectors(bdev_get_queue(bdev)));
379 return compat_put_ushort(arg, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100380 case BLKROTATIONAL:
381 return compat_put_ushort(arg,
382 !blk_queue_nonrot(bdev_get_queue(bdev)));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200383 case BLKRASET: /* compatible, but no compat_ptr (!) */
384 case BLKFRASET:
385 if (!capable(CAP_SYS_ADMIN))
386 return -EACCES;
Jan Karaefa7c9f2017-02-02 15:56:53 +0100387 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200388 return 0;
389 case BLKGETSIZE:
Mike Snitzer77304d22010-11-08 14:39:12 +0100390 size = i_size_read(bdev->bd_inode);
Al Viro45048d02008-09-18 15:53:24 -0400391 if ((size >> 9) > ~0UL)
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200392 return -EFBIG;
Al Viro45048d02008-09-18 15:53:24 -0400393 return compat_put_ulong(arg, size >> 9);
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200394
395 case BLKGETSIZE64_32:
Mike Snitzer77304d22010-11-08 14:39:12 +0100396 return compat_put_u64(arg, i_size_read(bdev->bd_inode));
Arnd Bergmann171044d42007-10-09 13:23:53 +0200397
398 case BLKTRACESETUP32:
Arnd Bergmann171044d42007-10-09 13:23:53 +0200399 case BLKTRACESTART: /* compatible */
400 case BLKTRACESTOP: /* compatible */
401 case BLKTRACETEARDOWN: /* compatible */
Al Viro45048d02008-09-18 15:53:24 -0400402 ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200403 return ret;
Al Viro45048d02008-09-18 15:53:24 -0400404 default:
405 if (disk->fops->compat_ioctl)
406 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
407 if (ret == -ENOIOCTLCMD)
408 ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg);
409 return ret;
410 }
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200411}