blob: 3c9fdd6983aa34f5283fae2e13f2523cfc0171e6 [file] [log] [blame]
Arnd Bergmannf58c4c02007-10-09 13:23:51 +02001#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>
Arnd Bergmannf58c4c02007-10-09 13:23:51 +02007#include <linux/hdreg.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Arnd Bergmannf58c4c02007-10-09 13:23:51 +02009#include <linux/syscalls.h>
Arnd Bergmannf58c4c02007-10-09 13:23:51 +020010#include <linux/types.h>
11#include <linux/uaccess.h>
12
13static int compat_put_ushort(unsigned long arg, unsigned short val)
14{
15 return put_user(val, (unsigned short __user *)compat_ptr(arg));
16}
17
18static int compat_put_int(unsigned long arg, int val)
19{
20 return put_user(val, (compat_int_t __user *)compat_ptr(arg));
21}
22
Martin K. Petersenac481c22009-10-03 20:52:01 +020023static int compat_put_uint(unsigned long arg, unsigned int val)
24{
25 return put_user(val, (compat_uint_t __user *)compat_ptr(arg));
26}
27
Arnd Bergmannf58c4c02007-10-09 13:23:51 +020028static int compat_put_long(unsigned long arg, long val)
29{
30 return put_user(val, (compat_long_t __user *)compat_ptr(arg));
31}
32
33static int compat_put_ulong(unsigned long arg, compat_ulong_t val)
34{
35 return put_user(val, (compat_ulong_t __user *)compat_ptr(arg));
36}
37
38static int compat_put_u64(unsigned long arg, u64 val)
39{
40 return put_user(val, (compat_u64 __user *)compat_ptr(arg));
41}
42
Arnd Bergmann9617db02007-10-09 13:23:55 +020043struct compat_hd_geometry {
44 unsigned char heads;
45 unsigned char sectors;
46 unsigned short cylinders;
47 u32 start;
48};
49
50static int compat_hdio_getgeo(struct gendisk *disk, struct block_device *bdev,
51 struct compat_hd_geometry __user *ugeo)
52{
53 struct hd_geometry geo;
54 int ret;
55
56 if (!ugeo)
57 return -EINVAL;
58 if (!disk->fops->getgeo)
59 return -ENOTTY;
60
Cong Wang8b0d77f2013-07-03 15:01:12 -070061 memset(&geo, 0, sizeof(geo));
Arnd Bergmann9617db02007-10-09 13:23:55 +020062 /*
63 * We need to set the startsect first, the driver may
64 * want to override it.
65 */
66 geo.start = get_start_sect(bdev);
67 ret = disk->fops->getgeo(bdev, &geo);
68 if (ret)
69 return ret;
70
71 ret = copy_to_user(ugeo, &geo, 4);
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -070072 ret |= put_user(geo.start, &ugeo->start);
Arnd Bergmann9617db02007-10-09 13:23:55 +020073 if (ret)
74 ret = -EFAULT;
75
76 return ret;
77}
78
Al Viro33c2dca2008-02-22 22:02:50 -050079static int compat_hdio_ioctl(struct block_device *bdev, fmode_t mode,
80 unsigned int cmd, unsigned long arg)
Arnd Bergmann9617db02007-10-09 13:23:55 +020081{
82 mm_segment_t old_fs = get_fs();
83 unsigned long kval;
84 unsigned int __user *uvp;
85 int error;
86
87 set_fs(KERNEL_DS);
Al Viro33c2dca2008-02-22 22:02:50 -050088 error = __blkdev_driver_ioctl(bdev, mode,
Arnd Bergmann9617db02007-10-09 13:23:55 +020089 cmd, (unsigned long)(&kval));
90 set_fs(old_fs);
91
92 if (error == 0) {
93 uvp = compat_ptr(arg);
94 if (put_user(kval, uvp))
95 error = -EFAULT;
96 }
97 return error;
98}
99
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200100struct compat_cdrom_read_audio {
101 union cdrom_addr addr;
102 u8 addr_format;
103 compat_int_t nframes;
104 compat_caddr_t buf;
105};
106
107struct compat_cdrom_generic_command {
108 unsigned char cmd[CDROM_PACKET_SIZE];
109 compat_caddr_t buffer;
110 compat_uint_t buflen;
111 compat_int_t stat;
112 compat_caddr_t sense;
113 unsigned char data_direction;
114 compat_int_t quiet;
115 compat_int_t timeout;
116 compat_caddr_t reserved[1];
117};
118
Al Viro33c2dca2008-02-22 22:02:50 -0500119static int compat_cdrom_read_audio(struct block_device *bdev, fmode_t mode,
120 unsigned int cmd, unsigned long arg)
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200121{
122 struct cdrom_read_audio __user *cdread_audio;
123 struct compat_cdrom_read_audio __user *cdread_audio32;
124 __u32 data;
125 void __user *datap;
126
127 cdread_audio = compat_alloc_user_space(sizeof(*cdread_audio));
128 cdread_audio32 = compat_ptr(arg);
129
130 if (copy_in_user(&cdread_audio->addr,
131 &cdread_audio32->addr,
132 (sizeof(*cdread_audio32) -
133 sizeof(compat_caddr_t))))
134 return -EFAULT;
135
136 if (get_user(data, &cdread_audio32->buf))
137 return -EFAULT;
138 datap = compat_ptr(data);
139 if (put_user(datap, &cdread_audio->buf))
140 return -EFAULT;
141
Al Viro33c2dca2008-02-22 22:02:50 -0500142 return __blkdev_driver_ioctl(bdev, mode, cmd,
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200143 (unsigned long)cdread_audio);
144}
145
Al Viro33c2dca2008-02-22 22:02:50 -0500146static int compat_cdrom_generic_command(struct block_device *bdev, fmode_t mode,
147 unsigned int cmd, unsigned long arg)
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200148{
149 struct cdrom_generic_command __user *cgc;
150 struct compat_cdrom_generic_command __user *cgc32;
151 u32 data;
152 unsigned char dir;
153 int itmp;
154
155 cgc = compat_alloc_user_space(sizeof(*cgc));
156 cgc32 = compat_ptr(arg);
157
158 if (copy_in_user(&cgc->cmd, &cgc32->cmd, sizeof(cgc->cmd)) ||
159 get_user(data, &cgc32->buffer) ||
160 put_user(compat_ptr(data), &cgc->buffer) ||
161 copy_in_user(&cgc->buflen, &cgc32->buflen,
162 (sizeof(unsigned int) + sizeof(int))) ||
163 get_user(data, &cgc32->sense) ||
164 put_user(compat_ptr(data), &cgc->sense) ||
165 get_user(dir, &cgc32->data_direction) ||
166 put_user(dir, &cgc->data_direction) ||
167 get_user(itmp, &cgc32->quiet) ||
168 put_user(itmp, &cgc->quiet) ||
169 get_user(itmp, &cgc32->timeout) ||
170 put_user(itmp, &cgc->timeout) ||
171 get_user(data, &cgc32->reserved[0]) ||
172 put_user(compat_ptr(data), &cgc->reserved[0]))
173 return -EFAULT;
174
Al Viro33c2dca2008-02-22 22:02:50 -0500175 return __blkdev_driver_ioctl(bdev, mode, cmd, (unsigned long)cgc);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200176}
177
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200178struct compat_blkpg_ioctl_arg {
179 compat_int_t op;
180 compat_int_t flags;
181 compat_int_t datalen;
182 compat_caddr_t data;
183};
184
Al Viro56b26ad2008-09-19 03:17:36 -0400185static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200186 unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32)
187{
188 struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a));
189 compat_caddr_t udata;
190 compat_int_t n;
191 int err;
192
193 err = get_user(n, &ua32->op);
194 err |= put_user(n, &a->op);
195 err |= get_user(n, &ua32->flags);
196 err |= put_user(n, &a->flags);
197 err |= get_user(n, &ua32->datalen);
198 err |= put_user(n, &a->datalen);
199 err |= get_user(udata, &ua32->data);
200 err |= put_user(compat_ptr(udata), &a->data);
201 if (err)
202 return err;
203
Al Viro56b26ad2008-09-19 03:17:36 -0400204 return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a);
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200205}
206
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200207#define BLKBSZGET_32 _IOR(0x12, 112, int)
208#define BLKBSZSET_32 _IOW(0x12, 113, int)
209#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
210
Al Viro33c2dca2008-02-22 22:02:50 -0500211static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
212 unsigned cmd, unsigned long arg)
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200213{
Philip Langdale33013a82007-10-27 13:34:10 +0200214 switch (cmd) {
Arnd Bergmann9617db02007-10-09 13:23:55 +0200215 case HDIO_GET_UNMASKINTR:
216 case HDIO_GET_MULTCOUNT:
217 case HDIO_GET_KEEPSETTINGS:
218 case HDIO_GET_32BIT:
219 case HDIO_GET_NOWERR:
220 case HDIO_GET_DMA:
221 case HDIO_GET_NICE:
222 case HDIO_GET_WCACHE:
223 case HDIO_GET_ACOUSTIC:
224 case HDIO_GET_ADDRESS:
225 case HDIO_GET_BUSSTATE:
Al Viro33c2dca2008-02-22 22:02:50 -0500226 return compat_hdio_ioctl(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200227 case CDROMREADAUDIO:
Al Viro33c2dca2008-02-22 22:02:50 -0500228 return compat_cdrom_read_audio(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200229 case CDROM_SEND_PACKET:
Al Viro33c2dca2008-02-22 22:02:50 -0500230 return compat_cdrom_generic_command(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200231
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200232 /*
233 * No handler required for the ones below, we just need to
234 * convert arg to a 64 bit pointer.
235 */
236 case BLKSECTSET:
237 /*
238 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
239 * Some need translations, these do not.
240 */
241 case HDIO_GET_IDENTITY:
242 case HDIO_DRIVE_TASK:
243 case HDIO_DRIVE_CMD:
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200244 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
245 case 0x330:
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200246 /* CDROM stuff */
247 case CDROMPAUSE:
248 case CDROMRESUME:
249 case CDROMPLAYMSF:
250 case CDROMPLAYTRKIND:
251 case CDROMREADTOCHDR:
252 case CDROMREADTOCENTRY:
253 case CDROMSTOP:
254 case CDROMSTART:
255 case CDROMEJECT:
256 case CDROMVOLCTRL:
257 case CDROMSUBCHNL:
258 case CDROMMULTISESSION:
259 case CDROM_GET_MCN:
260 case CDROMRESET:
261 case CDROMVOLREAD:
262 case CDROMSEEK:
263 case CDROMPLAYBLK:
264 case CDROMCLOSETRAY:
265 case CDROM_DISC_STATUS:
266 case CDROM_CHANGER_NSLOTS:
267 case CDROM_GET_CAPABILITY:
268 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
269 * not take a struct cdrom_read, instead they take a struct cdrom_msf
270 * which is compatible.
271 */
272 case CDROMREADMODE2:
273 case CDROMREADMODE1:
274 case CDROMREADRAW:
275 case CDROMREADCOOKED:
276 case CDROMREADALL:
277 /* DVD ioctls */
278 case DVD_READ_STRUCT:
279 case DVD_WRITE_STRUCT:
280 case DVD_AUTH:
281 arg = (unsigned long)compat_ptr(arg);
Andreas Schwab1c925602008-10-31 22:39:46 +0100282 /* These intepret arg as an unsigned long, not as a pointer,
283 * so we must not do compat_ptr() conversion. */
284 case HDIO_SET_MULTCOUNT:
285 case HDIO_SET_UNMASKINTR:
286 case HDIO_SET_KEEPSETTINGS:
287 case HDIO_SET_32BIT:
288 case HDIO_SET_NOWERR:
289 case HDIO_SET_DMA:
290 case HDIO_SET_PIO_MODE:
291 case HDIO_SET_NICE:
292 case HDIO_SET_WCACHE:
293 case HDIO_SET_ACOUSTIC:
294 case HDIO_SET_BUSSTATE:
295 case HDIO_SET_ADDRESS:
296 case CDROMEJECT_SW:
297 case CDROM_SET_OPTIONS:
298 case CDROM_CLEAR_OPTIONS:
299 case CDROM_SELECT_SPEED:
300 case CDROM_SELECT_DISC:
301 case CDROM_MEDIA_CHANGED:
302 case CDROM_DRIVE_STATUS:
303 case CDROM_LOCKDOOR:
304 case CDROM_DEBUG:
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200305 break;
306 default:
307 /* unknown ioctl number */
308 return -ENOIOCTLCMD;
309 }
310
Al Viro33c2dca2008-02-22 22:02:50 -0500311 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200312}
313
Al Viro45048d02008-09-18 15:53:24 -0400314/* Most of the generic ioctls are handled in the normal fallback path.
315 This assumes the blkdev's low level compat_ioctl always returns
316 ENOIOCTLCMD for unknown ioctls. */
317long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200318{
Al Viro45048d02008-09-18 15:53:24 -0400319 int ret = -ENOIOCTLCMD;
320 struct inode *inode = file->f_mapping->host;
321 struct block_device *bdev = inode->i_bdev;
322 struct gendisk *disk = bdev->bd_disk;
323 fmode_t mode = file->f_mode;
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200324 struct backing_dev_info *bdi;
Al Viro45048d02008-09-18 15:53:24 -0400325 loff_t size;
Akinobu Mita63f26492014-05-25 21:43:33 +0900326 unsigned int max_sectors;
Al Viro45048d02008-09-18 15:53:24 -0400327
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +0100328 /*
329 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
330 * to updated it before every ioctl.
331 */
Al Viro45048d02008-09-18 15:53:24 -0400332 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +0100333 mode |= FMODE_NDELAY;
334 else
335 mode &= ~FMODE_NDELAY;
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200336
337 switch (cmd) {
Al Viro45048d02008-09-18 15:53:24 -0400338 case HDIO_GETGEO:
339 return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200340 case BLKPBSZGET:
341 return compat_put_uint(arg, bdev_physical_block_size(bdev));
342 case BLKIOMIN:
343 return compat_put_uint(arg, bdev_io_min(bdev));
344 case BLKIOOPT:
345 return compat_put_uint(arg, bdev_io_opt(bdev));
346 case BLKALIGNOFF:
347 return compat_put_int(arg, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100348 case BLKDISCARDZEROES:
349 return compat_put_uint(arg, bdev_discard_zeroes_data(bdev));
Al Viro45048d02008-09-18 15:53:24 -0400350 case BLKFLSBUF:
351 case BLKROSET:
352 case BLKDISCARD:
Adrian Hunter8d57a982010-08-11 14:17:49 -0700353 case BLKSECDISCARD:
Mikulas Patocka3b3a1812014-07-02 12:46:23 -0400354 case BLKZEROOUT:
Al Viro45048d02008-09-18 15:53:24 -0400355 /*
356 * the ones below are implemented in blkdev_locked_ioctl,
357 * but we call blkdev_ioctl, which gets the lock for us
358 */
359 case BLKRRPART:
Al Viro56b26ad2008-09-19 03:17:36 -0400360 return blkdev_ioctl(bdev, mode, cmd,
Al Viro45048d02008-09-18 15:53:24 -0400361 (unsigned long)compat_ptr(arg));
362 case BLKBSZSET_32:
Al Viro56b26ad2008-09-19 03:17:36 -0400363 return blkdev_ioctl(bdev, mode, BLKBSZSET,
Al Viro45048d02008-09-18 15:53:24 -0400364 (unsigned long)compat_ptr(arg));
365 case BLKPG:
Al Viro56b26ad2008-09-19 03:17:36 -0400366 return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200367 case BLKRAGET:
368 case BLKFRAGET:
369 if (!arg)
370 return -EINVAL;
371 bdi = blk_get_backing_dev_info(bdev);
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200372 return compat_put_long(arg,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300373 (bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200374 case BLKROGET: /* compatible */
375 return compat_put_int(arg, bdev_read_only(bdev) != 0);
376 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
377 return compat_put_int(arg, block_size(bdev));
378 case BLKSSZGET: /* get block device hardware sector size */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400379 return compat_put_int(arg, bdev_logical_block_size(bdev));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200380 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900381 max_sectors = min_t(unsigned int, USHRT_MAX,
382 queue_max_sectors(bdev_get_queue(bdev)));
383 return compat_put_ushort(arg, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100384 case BLKROTATIONAL:
385 return compat_put_ushort(arg,
386 !blk_queue_nonrot(bdev_get_queue(bdev)));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200387 case BLKRASET: /* compatible, but no compat_ptr (!) */
388 case BLKFRASET:
389 if (!capable(CAP_SYS_ADMIN))
390 return -EACCES;
391 bdi = blk_get_backing_dev_info(bdev);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300392 bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200393 return 0;
394 case BLKGETSIZE:
Mike Snitzer77304d22010-11-08 14:39:12 +0100395 size = i_size_read(bdev->bd_inode);
Al Viro45048d02008-09-18 15:53:24 -0400396 if ((size >> 9) > ~0UL)
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200397 return -EFBIG;
Al Viro45048d02008-09-18 15:53:24 -0400398 return compat_put_ulong(arg, size >> 9);
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200399
400 case BLKGETSIZE64_32:
Mike Snitzer77304d22010-11-08 14:39:12 +0100401 return compat_put_u64(arg, i_size_read(bdev->bd_inode));
Arnd Bergmann171044d42007-10-09 13:23:53 +0200402
403 case BLKTRACESETUP32:
Arnd Bergmann171044d42007-10-09 13:23:53 +0200404 case BLKTRACESTART: /* compatible */
405 case BLKTRACESTOP: /* compatible */
406 case BLKTRACETEARDOWN: /* compatible */
Al Viro45048d02008-09-18 15:53:24 -0400407 ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200408 return ret;
Al Viro45048d02008-09-18 15:53:24 -0400409 default:
410 if (disk->fops->compat_ioctl)
411 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
412 if (ret == -ENOIOCTLCMD)
413 ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg);
414 return ret;
415 }
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200416}