blob: abaf9d78a2061d8d307a3f05858afbc8445c15d7 [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{
Bart Van Assche8363dae2017-08-23 15:29:11 -070082 unsigned long __user *p;
Arnd Bergmann9617db02007-10-09 13:23:55 +020083 int error;
84
Al Viro30138382017-06-27 15:51:38 -040085 p = compat_alloc_user_space(sizeof(unsigned long));
Al Viro33c2dca2008-02-22 22:02:50 -050086 error = __blkdev_driver_ioctl(bdev, mode,
Al Viro30138382017-06-27 15:51:38 -040087 cmd, (unsigned long)p);
Arnd Bergmann9617db02007-10-09 13:23:55 +020088 if (error == 0) {
Al Viro30138382017-06-27 15:51:38 -040089 unsigned int __user *uvp = compat_ptr(arg);
90 unsigned long v;
91 if (get_user(v, p) || put_user(v, uvp))
Arnd Bergmann9617db02007-10-09 13:23:55 +020092 error = -EFAULT;
93 }
94 return error;
95}
96
Arnd Bergmannb3087cc2007-10-09 13:23:56 +020097struct compat_cdrom_read_audio {
98 union cdrom_addr addr;
99 u8 addr_format;
100 compat_int_t nframes;
101 compat_caddr_t buf;
102};
103
104struct compat_cdrom_generic_command {
105 unsigned char cmd[CDROM_PACKET_SIZE];
106 compat_caddr_t buffer;
107 compat_uint_t buflen;
108 compat_int_t stat;
109 compat_caddr_t sense;
110 unsigned char data_direction;
111 compat_int_t quiet;
112 compat_int_t timeout;
113 compat_caddr_t reserved[1];
114};
115
Al Viro33c2dca2008-02-22 22:02:50 -0500116static int compat_cdrom_read_audio(struct block_device *bdev, fmode_t mode,
117 unsigned int cmd, unsigned long arg)
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200118{
119 struct cdrom_read_audio __user *cdread_audio;
120 struct compat_cdrom_read_audio __user *cdread_audio32;
121 __u32 data;
122 void __user *datap;
123
124 cdread_audio = compat_alloc_user_space(sizeof(*cdread_audio));
125 cdread_audio32 = compat_ptr(arg);
126
127 if (copy_in_user(&cdread_audio->addr,
128 &cdread_audio32->addr,
129 (sizeof(*cdread_audio32) -
130 sizeof(compat_caddr_t))))
131 return -EFAULT;
132
133 if (get_user(data, &cdread_audio32->buf))
134 return -EFAULT;
135 datap = compat_ptr(data);
136 if (put_user(datap, &cdread_audio->buf))
137 return -EFAULT;
138
Al Viro33c2dca2008-02-22 22:02:50 -0500139 return __blkdev_driver_ioctl(bdev, mode, cmd,
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200140 (unsigned long)cdread_audio);
141}
142
Al Viro33c2dca2008-02-22 22:02:50 -0500143static int compat_cdrom_generic_command(struct block_device *bdev, fmode_t mode,
144 unsigned int cmd, unsigned long arg)
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200145{
146 struct cdrom_generic_command __user *cgc;
147 struct compat_cdrom_generic_command __user *cgc32;
148 u32 data;
149 unsigned char dir;
150 int itmp;
151
152 cgc = compat_alloc_user_space(sizeof(*cgc));
153 cgc32 = compat_ptr(arg);
154
155 if (copy_in_user(&cgc->cmd, &cgc32->cmd, sizeof(cgc->cmd)) ||
156 get_user(data, &cgc32->buffer) ||
157 put_user(compat_ptr(data), &cgc->buffer) ||
158 copy_in_user(&cgc->buflen, &cgc32->buflen,
159 (sizeof(unsigned int) + sizeof(int))) ||
160 get_user(data, &cgc32->sense) ||
161 put_user(compat_ptr(data), &cgc->sense) ||
162 get_user(dir, &cgc32->data_direction) ||
163 put_user(dir, &cgc->data_direction) ||
164 get_user(itmp, &cgc32->quiet) ||
165 put_user(itmp, &cgc->quiet) ||
166 get_user(itmp, &cgc32->timeout) ||
167 put_user(itmp, &cgc->timeout) ||
168 get_user(data, &cgc32->reserved[0]) ||
169 put_user(compat_ptr(data), &cgc->reserved[0]))
170 return -EFAULT;
171
Al Viro33c2dca2008-02-22 22:02:50 -0500172 return __blkdev_driver_ioctl(bdev, mode, cmd, (unsigned long)cgc);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200173}
174
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200175struct compat_blkpg_ioctl_arg {
176 compat_int_t op;
177 compat_int_t flags;
178 compat_int_t datalen;
179 compat_caddr_t data;
180};
181
Al Viro56b26ad2008-09-19 03:17:36 -0400182static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200183 unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32)
184{
185 struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a));
186 compat_caddr_t udata;
187 compat_int_t n;
188 int err;
189
190 err = get_user(n, &ua32->op);
191 err |= put_user(n, &a->op);
192 err |= get_user(n, &ua32->flags);
193 err |= put_user(n, &a->flags);
194 err |= get_user(n, &ua32->datalen);
195 err |= put_user(n, &a->datalen);
196 err |= get_user(udata, &ua32->data);
197 err |= put_user(compat_ptr(udata), &a->data);
198 if (err)
199 return err;
200
Al Viro56b26ad2008-09-19 03:17:36 -0400201 return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a);
Arnd Bergmann18cf7f82007-10-09 13:23:56 +0200202}
203
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200204#define BLKBSZGET_32 _IOR(0x12, 112, int)
205#define BLKBSZSET_32 _IOW(0x12, 113, int)
206#define BLKGETSIZE64_32 _IOR(0x12, 114, int)
207
Al Viro33c2dca2008-02-22 22:02:50 -0500208static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
209 unsigned cmd, unsigned long arg)
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200210{
Philip Langdale33013a82007-10-27 13:34:10 +0200211 switch (cmd) {
Arnd Bergmann9617db02007-10-09 13:23:55 +0200212 case HDIO_GET_UNMASKINTR:
213 case HDIO_GET_MULTCOUNT:
214 case HDIO_GET_KEEPSETTINGS:
215 case HDIO_GET_32BIT:
216 case HDIO_GET_NOWERR:
217 case HDIO_GET_DMA:
218 case HDIO_GET_NICE:
219 case HDIO_GET_WCACHE:
220 case HDIO_GET_ACOUSTIC:
221 case HDIO_GET_ADDRESS:
222 case HDIO_GET_BUSSTATE:
Al Viro33c2dca2008-02-22 22:02:50 -0500223 return compat_hdio_ioctl(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200224 case CDROMREADAUDIO:
Al Viro33c2dca2008-02-22 22:02:50 -0500225 return compat_cdrom_read_audio(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200226 case CDROM_SEND_PACKET:
Al Viro33c2dca2008-02-22 22:02:50 -0500227 return compat_cdrom_generic_command(bdev, mode, cmd, arg);
Arnd Bergmannb3087cc2007-10-09 13:23:56 +0200228
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200229 /*
230 * No handler required for the ones below, we just need to
231 * convert arg to a 64 bit pointer.
232 */
233 case BLKSECTSET:
234 /*
235 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
236 * Some need translations, these do not.
237 */
238 case HDIO_GET_IDENTITY:
239 case HDIO_DRIVE_TASK:
240 case HDIO_DRIVE_CMD:
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200241 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
242 case 0x330:
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200243 /* CDROM stuff */
244 case CDROMPAUSE:
245 case CDROMRESUME:
246 case CDROMPLAYMSF:
247 case CDROMPLAYTRKIND:
248 case CDROMREADTOCHDR:
249 case CDROMREADTOCENTRY:
250 case CDROMSTOP:
251 case CDROMSTART:
252 case CDROMEJECT:
253 case CDROMVOLCTRL:
254 case CDROMSUBCHNL:
255 case CDROMMULTISESSION:
256 case CDROM_GET_MCN:
257 case CDROMRESET:
258 case CDROMVOLREAD:
259 case CDROMSEEK:
260 case CDROMPLAYBLK:
261 case CDROMCLOSETRAY:
262 case CDROM_DISC_STATUS:
263 case CDROM_CHANGER_NSLOTS:
264 case CDROM_GET_CAPABILITY:
265 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
266 * not take a struct cdrom_read, instead they take a struct cdrom_msf
267 * which is compatible.
268 */
269 case CDROMREADMODE2:
270 case CDROMREADMODE1:
271 case CDROMREADRAW:
272 case CDROMREADCOOKED:
273 case CDROMREADALL:
274 /* DVD ioctls */
275 case DVD_READ_STRUCT:
276 case DVD_WRITE_STRUCT:
277 case DVD_AUTH:
278 arg = (unsigned long)compat_ptr(arg);
Andreas Schwab1c9256042008-10-31 22:39:46 +0100279 /* These intepret arg as an unsigned long, not as a pointer,
280 * so we must not do compat_ptr() conversion. */
281 case HDIO_SET_MULTCOUNT:
282 case HDIO_SET_UNMASKINTR:
283 case HDIO_SET_KEEPSETTINGS:
284 case HDIO_SET_32BIT:
285 case HDIO_SET_NOWERR:
286 case HDIO_SET_DMA:
287 case HDIO_SET_PIO_MODE:
288 case HDIO_SET_NICE:
289 case HDIO_SET_WCACHE:
290 case HDIO_SET_ACOUSTIC:
291 case HDIO_SET_BUSSTATE:
292 case HDIO_SET_ADDRESS:
293 case CDROMEJECT_SW:
294 case CDROM_SET_OPTIONS:
295 case CDROM_CLEAR_OPTIONS:
296 case CDROM_SELECT_SPEED:
297 case CDROM_SELECT_DISC:
298 case CDROM_MEDIA_CHANGED:
299 case CDROM_DRIVE_STATUS:
300 case CDROM_LOCKDOOR:
301 case CDROM_DEBUG:
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200302 break;
303 default:
304 /* unknown ioctl number */
305 return -ENOIOCTLCMD;
306 }
307
Al Viro33c2dca2008-02-22 22:02:50 -0500308 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200309}
310
Al Viro45048d02008-09-18 15:53:24 -0400311/* Most of the generic ioctls are handled in the normal fallback path.
312 This assumes the blkdev's low level compat_ioctl always returns
313 ENOIOCTLCMD for unknown ioctls. */
314long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200315{
Al Viro45048d02008-09-18 15:53:24 -0400316 int ret = -ENOIOCTLCMD;
317 struct inode *inode = file->f_mapping->host;
318 struct block_device *bdev = inode->i_bdev;
319 struct gendisk *disk = bdev->bd_disk;
320 fmode_t mode = file->f_mode;
Al Viro45048d02008-09-18 15:53:24 -0400321 loff_t size;
Akinobu Mita63f26492014-05-25 21:43:33 +0900322 unsigned int max_sectors;
Al Viro45048d02008-09-18 15:53:24 -0400323
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +0100324 /*
325 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
326 * to updated it before every ioctl.
327 */
Al Viro45048d02008-09-18 15:53:24 -0400328 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +0100329 mode |= FMODE_NDELAY;
330 else
331 mode &= ~FMODE_NDELAY;
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200332
333 switch (cmd) {
Al Viro45048d02008-09-18 15:53:24 -0400334 case HDIO_GETGEO:
335 return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
Martin K. Petersenac481c22009-10-03 20:52:01 +0200336 case BLKPBSZGET:
337 return compat_put_uint(arg, bdev_physical_block_size(bdev));
338 case BLKIOMIN:
339 return compat_put_uint(arg, bdev_io_min(bdev));
340 case BLKIOOPT:
341 return compat_put_uint(arg, bdev_io_opt(bdev));
342 case BLKALIGNOFF:
343 return compat_put_int(arg, bdev_alignment_offset(bdev));
Martin K. Petersen98262f22009-12-03 09:24:48 +0100344 case BLKDISCARDZEROES:
Christoph Hellwig48920ff2017-04-05 19:21:23 +0200345 return compat_put_uint(arg, 0);
Al Viro45048d02008-09-18 15:53:24 -0400346 case BLKFLSBUF:
347 case BLKROSET:
348 case BLKDISCARD:
Adrian Hunter8d57a982010-08-11 14:17:49 -0700349 case BLKSECDISCARD:
Mikulas Patocka3b3a1812014-07-02 12:46:23 -0400350 case BLKZEROOUT:
Al Viro45048d02008-09-18 15:53:24 -0400351 /*
352 * the ones below are implemented in blkdev_locked_ioctl,
353 * but we call blkdev_ioctl, which gets the lock for us
354 */
355 case BLKRRPART:
Al Viro56b26ad2008-09-19 03:17:36 -0400356 return blkdev_ioctl(bdev, mode, cmd,
Al Viro45048d02008-09-18 15:53:24 -0400357 (unsigned long)compat_ptr(arg));
358 case BLKBSZSET_32:
Al Viro56b26ad2008-09-19 03:17:36 -0400359 return blkdev_ioctl(bdev, mode, BLKBSZSET,
Al Viro45048d02008-09-18 15:53:24 -0400360 (unsigned long)compat_ptr(arg));
361 case BLKPG:
Al Viro56b26ad2008-09-19 03:17:36 -0400362 return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200363 case BLKRAGET:
364 case BLKFRAGET:
365 if (!arg)
366 return -EINVAL;
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200367 return compat_put_long(arg,
Jan Karaefa7c9f2017-02-02 15:56:53 +0100368 (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512);
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200369 case BLKROGET: /* compatible */
370 return compat_put_int(arg, bdev_read_only(bdev) != 0);
371 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
372 return compat_put_int(arg, block_size(bdev));
373 case BLKSSZGET: /* get block device hardware sector size */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400374 return compat_put_int(arg, bdev_logical_block_size(bdev));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200375 case BLKSECTGET:
Akinobu Mita63f26492014-05-25 21:43:33 +0900376 max_sectors = min_t(unsigned int, USHRT_MAX,
377 queue_max_sectors(bdev_get_queue(bdev)));
378 return compat_put_ushort(arg, max_sectors);
Martin K. Petersenef00f592012-01-11 16:29:31 +0100379 case BLKROTATIONAL:
380 return compat_put_ushort(arg,
381 !blk_queue_nonrot(bdev_get_queue(bdev)));
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200382 case BLKRASET: /* compatible, but no compat_ptr (!) */
383 case BLKFRASET:
384 if (!capable(CAP_SYS_ADMIN))
385 return -EACCES;
Jan Karaefa7c9f2017-02-02 15:56:53 +0100386 bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE;
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200387 return 0;
388 case BLKGETSIZE:
Mike Snitzer77304d22010-11-08 14:39:12 +0100389 size = i_size_read(bdev->bd_inode);
Al Viro45048d02008-09-18 15:53:24 -0400390 if ((size >> 9) > ~0UL)
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200391 return -EFBIG;
Al Viro45048d02008-09-18 15:53:24 -0400392 return compat_put_ulong(arg, size >> 9);
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200393
394 case BLKGETSIZE64_32:
Mike Snitzer77304d22010-11-08 14:39:12 +0100395 return compat_put_u64(arg, i_size_read(bdev->bd_inode));
Arnd Bergmann171044d42007-10-09 13:23:53 +0200396
397 case BLKTRACESETUP32:
Arnd Bergmann171044d42007-10-09 13:23:53 +0200398 case BLKTRACESTART: /* compatible */
399 case BLKTRACESTOP: /* compatible */
400 case BLKTRACETEARDOWN: /* compatible */
Al Viro45048d02008-09-18 15:53:24 -0400401 ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
Arnd Bergmann7199d4c2007-10-09 13:23:52 +0200402 return ret;
Al Viro45048d02008-09-18 15:53:24 -0400403 default:
404 if (disk->fops->compat_ioctl)
405 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
406 if (ret == -ENOIOCTLCMD)
407 ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg);
408 return ret;
409 }
Arnd Bergmannf58c4c02007-10-09 13:23:51 +0200410}