Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 1 | #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> |
| 7 | #include <linux/fd.h> |
| 8 | #include <linux/hdreg.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 10 | #include <linux/syscalls.h> |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/uaccess.h> |
| 13 | |
| 14 | static int compat_put_ushort(unsigned long arg, unsigned short val) |
| 15 | { |
| 16 | return put_user(val, (unsigned short __user *)compat_ptr(arg)); |
| 17 | } |
| 18 | |
| 19 | static 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. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 24 | static 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 Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 29 | static int compat_put_long(unsigned long arg, long val) |
| 30 | { |
| 31 | return put_user(val, (compat_long_t __user *)compat_ptr(arg)); |
| 32 | } |
| 33 | |
| 34 | static 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 | |
| 39 | static int compat_put_u64(unsigned long arg, u64 val) |
| 40 | { |
| 41 | return put_user(val, (compat_u64 __user *)compat_ptr(arg)); |
| 42 | } |
| 43 | |
Arnd Bergmann | 9617db0 | 2007-10-09 13:23:55 +0200 | [diff] [blame] | 44 | struct compat_hd_geometry { |
| 45 | unsigned char heads; |
| 46 | unsigned char sectors; |
| 47 | unsigned short cylinders; |
| 48 | u32 start; |
| 49 | }; |
| 50 | |
| 51 | static 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 Wang | 8b0d77f | 2013-07-03 15:01:12 -0700 | [diff] [blame] | 62 | memset(&geo, 0, sizeof(geo)); |
Arnd Bergmann | 9617db0 | 2007-10-09 13:23:55 +0200 | [diff] [blame] | 63 | /* |
| 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 Desnoyers | 3ddc5b4 | 2013-09-11 14:23:18 -0700 | [diff] [blame] | 73 | ret |= put_user(geo.start, &ugeo->start); |
Arnd Bergmann | 9617db0 | 2007-10-09 13:23:55 +0200 | [diff] [blame] | 74 | if (ret) |
| 75 | ret = -EFAULT; |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 80 | static int compat_hdio_ioctl(struct block_device *bdev, fmode_t mode, |
| 81 | unsigned int cmd, unsigned long arg) |
Arnd Bergmann | 9617db0 | 2007-10-09 13:23:55 +0200 | [diff] [blame] | 82 | { |
| 83 | mm_segment_t old_fs = get_fs(); |
| 84 | unsigned long kval; |
| 85 | unsigned int __user *uvp; |
| 86 | int error; |
| 87 | |
| 88 | set_fs(KERNEL_DS); |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 89 | error = __blkdev_driver_ioctl(bdev, mode, |
Arnd Bergmann | 9617db0 | 2007-10-09 13:23:55 +0200 | [diff] [blame] | 90 | cmd, (unsigned long)(&kval)); |
| 91 | set_fs(old_fs); |
| 92 | |
| 93 | if (error == 0) { |
| 94 | uvp = compat_ptr(arg); |
| 95 | if (put_user(kval, uvp)) |
| 96 | error = -EFAULT; |
| 97 | } |
| 98 | return error; |
| 99 | } |
| 100 | |
Arnd Bergmann | b3087cc | 2007-10-09 13:23:56 +0200 | [diff] [blame] | 101 | struct compat_cdrom_read_audio { |
| 102 | union cdrom_addr addr; |
| 103 | u8 addr_format; |
| 104 | compat_int_t nframes; |
| 105 | compat_caddr_t buf; |
| 106 | }; |
| 107 | |
| 108 | struct compat_cdrom_generic_command { |
| 109 | unsigned char cmd[CDROM_PACKET_SIZE]; |
| 110 | compat_caddr_t buffer; |
| 111 | compat_uint_t buflen; |
| 112 | compat_int_t stat; |
| 113 | compat_caddr_t sense; |
| 114 | unsigned char data_direction; |
| 115 | compat_int_t quiet; |
| 116 | compat_int_t timeout; |
| 117 | compat_caddr_t reserved[1]; |
| 118 | }; |
| 119 | |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 120 | static int compat_cdrom_read_audio(struct block_device *bdev, fmode_t mode, |
| 121 | unsigned int cmd, unsigned long arg) |
Arnd Bergmann | b3087cc | 2007-10-09 13:23:56 +0200 | [diff] [blame] | 122 | { |
| 123 | struct cdrom_read_audio __user *cdread_audio; |
| 124 | struct compat_cdrom_read_audio __user *cdread_audio32; |
| 125 | __u32 data; |
| 126 | void __user *datap; |
| 127 | |
| 128 | cdread_audio = compat_alloc_user_space(sizeof(*cdread_audio)); |
| 129 | cdread_audio32 = compat_ptr(arg); |
| 130 | |
| 131 | if (copy_in_user(&cdread_audio->addr, |
| 132 | &cdread_audio32->addr, |
| 133 | (sizeof(*cdread_audio32) - |
| 134 | sizeof(compat_caddr_t)))) |
| 135 | return -EFAULT; |
| 136 | |
| 137 | if (get_user(data, &cdread_audio32->buf)) |
| 138 | return -EFAULT; |
| 139 | datap = compat_ptr(data); |
| 140 | if (put_user(datap, &cdread_audio->buf)) |
| 141 | return -EFAULT; |
| 142 | |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 143 | return __blkdev_driver_ioctl(bdev, mode, cmd, |
Arnd Bergmann | b3087cc | 2007-10-09 13:23:56 +0200 | [diff] [blame] | 144 | (unsigned long)cdread_audio); |
| 145 | } |
| 146 | |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 147 | static int compat_cdrom_generic_command(struct block_device *bdev, fmode_t mode, |
| 148 | unsigned int cmd, unsigned long arg) |
Arnd Bergmann | b3087cc | 2007-10-09 13:23:56 +0200 | [diff] [blame] | 149 | { |
| 150 | struct cdrom_generic_command __user *cgc; |
| 151 | struct compat_cdrom_generic_command __user *cgc32; |
| 152 | u32 data; |
| 153 | unsigned char dir; |
| 154 | int itmp; |
| 155 | |
| 156 | cgc = compat_alloc_user_space(sizeof(*cgc)); |
| 157 | cgc32 = compat_ptr(arg); |
| 158 | |
| 159 | if (copy_in_user(&cgc->cmd, &cgc32->cmd, sizeof(cgc->cmd)) || |
| 160 | get_user(data, &cgc32->buffer) || |
| 161 | put_user(compat_ptr(data), &cgc->buffer) || |
| 162 | copy_in_user(&cgc->buflen, &cgc32->buflen, |
| 163 | (sizeof(unsigned int) + sizeof(int))) || |
| 164 | get_user(data, &cgc32->sense) || |
| 165 | put_user(compat_ptr(data), &cgc->sense) || |
| 166 | get_user(dir, &cgc32->data_direction) || |
| 167 | put_user(dir, &cgc->data_direction) || |
| 168 | get_user(itmp, &cgc32->quiet) || |
| 169 | put_user(itmp, &cgc->quiet) || |
| 170 | get_user(itmp, &cgc32->timeout) || |
| 171 | put_user(itmp, &cgc->timeout) || |
| 172 | get_user(data, &cgc32->reserved[0]) || |
| 173 | put_user(compat_ptr(data), &cgc->reserved[0])) |
| 174 | return -EFAULT; |
| 175 | |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 176 | return __blkdev_driver_ioctl(bdev, mode, cmd, (unsigned long)cgc); |
Arnd Bergmann | b3087cc | 2007-10-09 13:23:56 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Arnd Bergmann | 18cf7f8 | 2007-10-09 13:23:56 +0200 | [diff] [blame] | 179 | struct compat_blkpg_ioctl_arg { |
| 180 | compat_int_t op; |
| 181 | compat_int_t flags; |
| 182 | compat_int_t datalen; |
| 183 | compat_caddr_t data; |
| 184 | }; |
| 185 | |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 186 | static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode, |
Arnd Bergmann | 18cf7f8 | 2007-10-09 13:23:56 +0200 | [diff] [blame] | 187 | unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32) |
| 188 | { |
| 189 | struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a)); |
| 190 | compat_caddr_t udata; |
| 191 | compat_int_t n; |
| 192 | int err; |
| 193 | |
| 194 | err = get_user(n, &ua32->op); |
| 195 | err |= put_user(n, &a->op); |
| 196 | err |= get_user(n, &ua32->flags); |
| 197 | err |= put_user(n, &a->flags); |
| 198 | err |= get_user(n, &ua32->datalen); |
| 199 | err |= put_user(n, &a->datalen); |
| 200 | err |= get_user(udata, &ua32->data); |
| 201 | err |= put_user(compat_ptr(udata), &a->data); |
| 202 | if (err) |
| 203 | return err; |
| 204 | |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 205 | return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a); |
Arnd Bergmann | 18cf7f8 | 2007-10-09 13:23:56 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 208 | #define BLKBSZGET_32 _IOR(0x12, 112, int) |
| 209 | #define BLKBSZSET_32 _IOW(0x12, 113, int) |
| 210 | #define BLKGETSIZE64_32 _IOR(0x12, 114, int) |
| 211 | |
Arnd Bergmann | 1ca91cd0 | 2007-10-09 13:23:57 +0200 | [diff] [blame] | 212 | struct compat_floppy_drive_params { |
| 213 | char cmos; |
| 214 | compat_ulong_t max_dtr; |
| 215 | compat_ulong_t hlt; |
| 216 | compat_ulong_t hut; |
| 217 | compat_ulong_t srt; |
| 218 | compat_ulong_t spinup; |
| 219 | compat_ulong_t spindown; |
| 220 | unsigned char spindown_offset; |
| 221 | unsigned char select_delay; |
| 222 | unsigned char rps; |
| 223 | unsigned char tracks; |
| 224 | compat_ulong_t timeout; |
| 225 | unsigned char interleave_sect; |
| 226 | struct floppy_max_errors max_errors; |
| 227 | char flags; |
| 228 | char read_track; |
| 229 | short autodetect[8]; |
| 230 | compat_int_t checkfreq; |
| 231 | compat_int_t native_format; |
| 232 | }; |
| 233 | |
| 234 | struct compat_floppy_drive_struct { |
| 235 | signed char flags; |
| 236 | compat_ulong_t spinup_date; |
| 237 | compat_ulong_t select_date; |
| 238 | compat_ulong_t first_read_date; |
| 239 | short probed_format; |
| 240 | short track; |
| 241 | short maxblock; |
| 242 | short maxtrack; |
| 243 | compat_int_t generation; |
| 244 | compat_int_t keep_data; |
| 245 | compat_int_t fd_ref; |
| 246 | compat_int_t fd_device; |
| 247 | compat_int_t last_checked; |
| 248 | compat_caddr_t dmabuf; |
| 249 | compat_int_t bufblocks; |
| 250 | }; |
| 251 | |
| 252 | struct compat_floppy_fdc_state { |
| 253 | compat_int_t spec1; |
| 254 | compat_int_t spec2; |
| 255 | compat_int_t dtr; |
| 256 | unsigned char version; |
| 257 | unsigned char dor; |
| 258 | compat_ulong_t address; |
| 259 | unsigned int rawcmd:2; |
| 260 | unsigned int reset:1; |
| 261 | unsigned int need_configure:1; |
| 262 | unsigned int perp_mode:2; |
| 263 | unsigned int has_fifo:1; |
| 264 | unsigned int driver_version; |
| 265 | unsigned char track[4]; |
| 266 | }; |
| 267 | |
| 268 | struct compat_floppy_write_errors { |
| 269 | unsigned int write_errors; |
| 270 | compat_ulong_t first_error_sector; |
| 271 | compat_int_t first_error_generation; |
| 272 | compat_ulong_t last_error_sector; |
| 273 | compat_int_t last_error_generation; |
| 274 | compat_uint_t badness; |
| 275 | }; |
| 276 | |
| 277 | #define FDSETPRM32 _IOW(2, 0x42, struct compat_floppy_struct) |
| 278 | #define FDDEFPRM32 _IOW(2, 0x43, struct compat_floppy_struct) |
Arnd Bergmann | 1ca91cd0 | 2007-10-09 13:23:57 +0200 | [diff] [blame] | 279 | #define FDSETDRVPRM32 _IOW(2, 0x90, struct compat_floppy_drive_params) |
| 280 | #define FDGETDRVPRM32 _IOR(2, 0x11, struct compat_floppy_drive_params) |
| 281 | #define FDGETDRVSTAT32 _IOR(2, 0x12, struct compat_floppy_drive_struct) |
| 282 | #define FDPOLLDRVSTAT32 _IOR(2, 0x13, struct compat_floppy_drive_struct) |
| 283 | #define FDGETFDCSTAT32 _IOR(2, 0x15, struct compat_floppy_fdc_state) |
| 284 | #define FDWERRORGET32 _IOR(2, 0x17, struct compat_floppy_write_errors) |
| 285 | |
| 286 | static struct { |
| 287 | unsigned int cmd32; |
| 288 | unsigned int cmd; |
| 289 | } fd_ioctl_trans_table[] = { |
| 290 | { FDSETPRM32, FDSETPRM }, |
| 291 | { FDDEFPRM32, FDDEFPRM }, |
| 292 | { FDGETPRM32, FDGETPRM }, |
| 293 | { FDSETDRVPRM32, FDSETDRVPRM }, |
| 294 | { FDGETDRVPRM32, FDGETDRVPRM }, |
| 295 | { FDGETDRVSTAT32, FDGETDRVSTAT }, |
| 296 | { FDPOLLDRVSTAT32, FDPOLLDRVSTAT }, |
| 297 | { FDGETFDCSTAT32, FDGETFDCSTAT }, |
| 298 | { FDWERRORGET32, FDWERRORGET } |
| 299 | }; |
| 300 | |
| 301 | #define NR_FD_IOCTL_TRANS ARRAY_SIZE(fd_ioctl_trans_table) |
| 302 | |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 303 | static int compat_fd_ioctl(struct block_device *bdev, fmode_t mode, |
| 304 | unsigned int cmd, unsigned long arg) |
Arnd Bergmann | 1ca91cd0 | 2007-10-09 13:23:57 +0200 | [diff] [blame] | 305 | { |
| 306 | mm_segment_t old_fs = get_fs(); |
| 307 | void *karg = NULL; |
| 308 | unsigned int kcmd = 0; |
| 309 | int i, err; |
| 310 | |
| 311 | for (i = 0; i < NR_FD_IOCTL_TRANS; i++) |
| 312 | if (cmd == fd_ioctl_trans_table[i].cmd32) { |
| 313 | kcmd = fd_ioctl_trans_table[i].cmd; |
| 314 | break; |
| 315 | } |
| 316 | if (!kcmd) |
| 317 | return -EINVAL; |
| 318 | |
| 319 | switch (cmd) { |
| 320 | case FDSETPRM32: |
| 321 | case FDDEFPRM32: |
| 322 | case FDGETPRM32: |
| 323 | { |
| 324 | compat_uptr_t name; |
| 325 | struct compat_floppy_struct __user *uf; |
| 326 | struct floppy_struct *f; |
| 327 | |
| 328 | uf = compat_ptr(arg); |
| 329 | f = karg = kmalloc(sizeof(struct floppy_struct), GFP_KERNEL); |
| 330 | if (!karg) |
| 331 | return -ENOMEM; |
| 332 | if (cmd == FDGETPRM32) |
| 333 | break; |
| 334 | err = __get_user(f->size, &uf->size); |
| 335 | err |= __get_user(f->sect, &uf->sect); |
| 336 | err |= __get_user(f->head, &uf->head); |
| 337 | err |= __get_user(f->track, &uf->track); |
| 338 | err |= __get_user(f->stretch, &uf->stretch); |
| 339 | err |= __get_user(f->gap, &uf->gap); |
| 340 | err |= __get_user(f->rate, &uf->rate); |
| 341 | err |= __get_user(f->spec1, &uf->spec1); |
| 342 | err |= __get_user(f->fmt_gap, &uf->fmt_gap); |
| 343 | err |= __get_user(name, &uf->name); |
| 344 | f->name = compat_ptr(name); |
| 345 | if (err) { |
| 346 | err = -EFAULT; |
| 347 | goto out; |
| 348 | } |
| 349 | break; |
| 350 | } |
| 351 | case FDSETDRVPRM32: |
| 352 | case FDGETDRVPRM32: |
| 353 | { |
| 354 | struct compat_floppy_drive_params __user *uf; |
| 355 | struct floppy_drive_params *f; |
| 356 | |
| 357 | uf = compat_ptr(arg); |
| 358 | f = karg = kmalloc(sizeof(struct floppy_drive_params), GFP_KERNEL); |
| 359 | if (!karg) |
| 360 | return -ENOMEM; |
| 361 | if (cmd == FDGETDRVPRM32) |
| 362 | break; |
| 363 | err = __get_user(f->cmos, &uf->cmos); |
| 364 | err |= __get_user(f->max_dtr, &uf->max_dtr); |
| 365 | err |= __get_user(f->hlt, &uf->hlt); |
| 366 | err |= __get_user(f->hut, &uf->hut); |
| 367 | err |= __get_user(f->srt, &uf->srt); |
| 368 | err |= __get_user(f->spinup, &uf->spinup); |
| 369 | err |= __get_user(f->spindown, &uf->spindown); |
| 370 | err |= __get_user(f->spindown_offset, &uf->spindown_offset); |
| 371 | err |= __get_user(f->select_delay, &uf->select_delay); |
| 372 | err |= __get_user(f->rps, &uf->rps); |
| 373 | err |= __get_user(f->tracks, &uf->tracks); |
| 374 | err |= __get_user(f->timeout, &uf->timeout); |
| 375 | err |= __get_user(f->interleave_sect, &uf->interleave_sect); |
| 376 | err |= __copy_from_user(&f->max_errors, &uf->max_errors, sizeof(f->max_errors)); |
| 377 | err |= __get_user(f->flags, &uf->flags); |
| 378 | err |= __get_user(f->read_track, &uf->read_track); |
| 379 | err |= __copy_from_user(f->autodetect, uf->autodetect, sizeof(f->autodetect)); |
| 380 | err |= __get_user(f->checkfreq, &uf->checkfreq); |
| 381 | err |= __get_user(f->native_format, &uf->native_format); |
| 382 | if (err) { |
| 383 | err = -EFAULT; |
| 384 | goto out; |
| 385 | } |
| 386 | break; |
| 387 | } |
| 388 | case FDGETDRVSTAT32: |
| 389 | case FDPOLLDRVSTAT32: |
| 390 | karg = kmalloc(sizeof(struct floppy_drive_struct), GFP_KERNEL); |
| 391 | if (!karg) |
| 392 | return -ENOMEM; |
| 393 | break; |
| 394 | case FDGETFDCSTAT32: |
| 395 | karg = kmalloc(sizeof(struct floppy_fdc_state), GFP_KERNEL); |
| 396 | if (!karg) |
| 397 | return -ENOMEM; |
| 398 | break; |
| 399 | case FDWERRORGET32: |
| 400 | karg = kmalloc(sizeof(struct floppy_write_errors), GFP_KERNEL); |
| 401 | if (!karg) |
| 402 | return -ENOMEM; |
| 403 | break; |
| 404 | default: |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | set_fs(KERNEL_DS); |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 408 | err = __blkdev_driver_ioctl(bdev, mode, kcmd, (unsigned long)karg); |
Arnd Bergmann | 1ca91cd0 | 2007-10-09 13:23:57 +0200 | [diff] [blame] | 409 | set_fs(old_fs); |
| 410 | if (err) |
| 411 | goto out; |
| 412 | switch (cmd) { |
| 413 | case FDGETPRM32: |
| 414 | { |
| 415 | struct floppy_struct *f = karg; |
| 416 | struct compat_floppy_struct __user *uf = compat_ptr(arg); |
| 417 | |
| 418 | err = __put_user(f->size, &uf->size); |
| 419 | err |= __put_user(f->sect, &uf->sect); |
| 420 | err |= __put_user(f->head, &uf->head); |
| 421 | err |= __put_user(f->track, &uf->track); |
| 422 | err |= __put_user(f->stretch, &uf->stretch); |
| 423 | err |= __put_user(f->gap, &uf->gap); |
| 424 | err |= __put_user(f->rate, &uf->rate); |
| 425 | err |= __put_user(f->spec1, &uf->spec1); |
| 426 | err |= __put_user(f->fmt_gap, &uf->fmt_gap); |
| 427 | err |= __put_user((u64)f->name, (compat_caddr_t __user *)&uf->name); |
| 428 | break; |
| 429 | } |
| 430 | case FDGETDRVPRM32: |
| 431 | { |
| 432 | struct compat_floppy_drive_params __user *uf; |
| 433 | struct floppy_drive_params *f = karg; |
| 434 | |
| 435 | uf = compat_ptr(arg); |
| 436 | err = __put_user(f->cmos, &uf->cmos); |
| 437 | err |= __put_user(f->max_dtr, &uf->max_dtr); |
| 438 | err |= __put_user(f->hlt, &uf->hlt); |
| 439 | err |= __put_user(f->hut, &uf->hut); |
| 440 | err |= __put_user(f->srt, &uf->srt); |
| 441 | err |= __put_user(f->spinup, &uf->spinup); |
| 442 | err |= __put_user(f->spindown, &uf->spindown); |
| 443 | err |= __put_user(f->spindown_offset, &uf->spindown_offset); |
| 444 | err |= __put_user(f->select_delay, &uf->select_delay); |
| 445 | err |= __put_user(f->rps, &uf->rps); |
| 446 | err |= __put_user(f->tracks, &uf->tracks); |
| 447 | err |= __put_user(f->timeout, &uf->timeout); |
| 448 | err |= __put_user(f->interleave_sect, &uf->interleave_sect); |
| 449 | err |= __copy_to_user(&uf->max_errors, &f->max_errors, sizeof(f->max_errors)); |
| 450 | err |= __put_user(f->flags, &uf->flags); |
| 451 | err |= __put_user(f->read_track, &uf->read_track); |
| 452 | err |= __copy_to_user(uf->autodetect, f->autodetect, sizeof(f->autodetect)); |
| 453 | err |= __put_user(f->checkfreq, &uf->checkfreq); |
| 454 | err |= __put_user(f->native_format, &uf->native_format); |
| 455 | break; |
| 456 | } |
| 457 | case FDGETDRVSTAT32: |
| 458 | case FDPOLLDRVSTAT32: |
| 459 | { |
| 460 | struct compat_floppy_drive_struct __user *uf; |
| 461 | struct floppy_drive_struct *f = karg; |
| 462 | |
| 463 | uf = compat_ptr(arg); |
| 464 | err = __put_user(f->flags, &uf->flags); |
| 465 | err |= __put_user(f->spinup_date, &uf->spinup_date); |
| 466 | err |= __put_user(f->select_date, &uf->select_date); |
| 467 | err |= __put_user(f->first_read_date, &uf->first_read_date); |
| 468 | err |= __put_user(f->probed_format, &uf->probed_format); |
| 469 | err |= __put_user(f->track, &uf->track); |
| 470 | err |= __put_user(f->maxblock, &uf->maxblock); |
| 471 | err |= __put_user(f->maxtrack, &uf->maxtrack); |
| 472 | err |= __put_user(f->generation, &uf->generation); |
| 473 | err |= __put_user(f->keep_data, &uf->keep_data); |
| 474 | err |= __put_user(f->fd_ref, &uf->fd_ref); |
| 475 | err |= __put_user(f->fd_device, &uf->fd_device); |
| 476 | err |= __put_user(f->last_checked, &uf->last_checked); |
| 477 | err |= __put_user((u64)f->dmabuf, &uf->dmabuf); |
| 478 | err |= __put_user((u64)f->bufblocks, &uf->bufblocks); |
| 479 | break; |
| 480 | } |
| 481 | case FDGETFDCSTAT32: |
| 482 | { |
| 483 | struct compat_floppy_fdc_state __user *uf; |
| 484 | struct floppy_fdc_state *f = karg; |
| 485 | |
| 486 | uf = compat_ptr(arg); |
| 487 | err = __put_user(f->spec1, &uf->spec1); |
| 488 | err |= __put_user(f->spec2, &uf->spec2); |
| 489 | err |= __put_user(f->dtr, &uf->dtr); |
| 490 | err |= __put_user(f->version, &uf->version); |
| 491 | err |= __put_user(f->dor, &uf->dor); |
| 492 | err |= __put_user(f->address, &uf->address); |
| 493 | err |= __copy_to_user((char __user *)&uf->address + sizeof(uf->address), |
| 494 | (char *)&f->address + sizeof(f->address), sizeof(int)); |
| 495 | err |= __put_user(f->driver_version, &uf->driver_version); |
| 496 | err |= __copy_to_user(uf->track, f->track, sizeof(f->track)); |
| 497 | break; |
| 498 | } |
| 499 | case FDWERRORGET32: |
| 500 | { |
| 501 | struct compat_floppy_write_errors __user *uf; |
| 502 | struct floppy_write_errors *f = karg; |
| 503 | |
| 504 | uf = compat_ptr(arg); |
| 505 | err = __put_user(f->write_errors, &uf->write_errors); |
| 506 | err |= __put_user(f->first_error_sector, &uf->first_error_sector); |
| 507 | err |= __put_user(f->first_error_generation, &uf->first_error_generation); |
| 508 | err |= __put_user(f->last_error_sector, &uf->last_error_sector); |
| 509 | err |= __put_user(f->last_error_generation, &uf->last_error_generation); |
| 510 | err |= __put_user(f->badness, &uf->badness); |
| 511 | break; |
| 512 | } |
| 513 | default: |
| 514 | break; |
| 515 | } |
| 516 | if (err) |
| 517 | err = -EFAULT; |
| 518 | |
| 519 | out: |
| 520 | kfree(karg); |
| 521 | return err; |
| 522 | } |
| 523 | |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 524 | static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode, |
| 525 | unsigned cmd, unsigned long arg) |
Arnd Bergmann | 7199d4c | 2007-10-09 13:23:52 +0200 | [diff] [blame] | 526 | { |
Philip Langdale | 33013a8 | 2007-10-27 13:34:10 +0200 | [diff] [blame] | 527 | switch (cmd) { |
Arnd Bergmann | 9617db0 | 2007-10-09 13:23:55 +0200 | [diff] [blame] | 528 | case HDIO_GET_UNMASKINTR: |
| 529 | case HDIO_GET_MULTCOUNT: |
| 530 | case HDIO_GET_KEEPSETTINGS: |
| 531 | case HDIO_GET_32BIT: |
| 532 | case HDIO_GET_NOWERR: |
| 533 | case HDIO_GET_DMA: |
| 534 | case HDIO_GET_NICE: |
| 535 | case HDIO_GET_WCACHE: |
| 536 | case HDIO_GET_ACOUSTIC: |
| 537 | case HDIO_GET_ADDRESS: |
| 538 | case HDIO_GET_BUSSTATE: |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 539 | return compat_hdio_ioctl(bdev, mode, cmd, arg); |
Arnd Bergmann | 1ca91cd0 | 2007-10-09 13:23:57 +0200 | [diff] [blame] | 540 | case FDSETPRM32: |
| 541 | case FDDEFPRM32: |
| 542 | case FDGETPRM32: |
| 543 | case FDSETDRVPRM32: |
| 544 | case FDGETDRVPRM32: |
| 545 | case FDGETDRVSTAT32: |
| 546 | case FDPOLLDRVSTAT32: |
| 547 | case FDGETFDCSTAT32: |
| 548 | case FDWERRORGET32: |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 549 | return compat_fd_ioctl(bdev, mode, cmd, arg); |
Arnd Bergmann | b3087cc | 2007-10-09 13:23:56 +0200 | [diff] [blame] | 550 | case CDROMREADAUDIO: |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 551 | return compat_cdrom_read_audio(bdev, mode, cmd, arg); |
Arnd Bergmann | b3087cc | 2007-10-09 13:23:56 +0200 | [diff] [blame] | 552 | case CDROM_SEND_PACKET: |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 553 | return compat_cdrom_generic_command(bdev, mode, cmd, arg); |
Arnd Bergmann | b3087cc | 2007-10-09 13:23:56 +0200 | [diff] [blame] | 554 | |
Arnd Bergmann | 7199d4c | 2007-10-09 13:23:52 +0200 | [diff] [blame] | 555 | /* |
| 556 | * No handler required for the ones below, we just need to |
| 557 | * convert arg to a 64 bit pointer. |
| 558 | */ |
| 559 | case BLKSECTSET: |
| 560 | /* |
| 561 | * 0x03 -- HD/IDE ioctl's used by hdparm and friends. |
| 562 | * Some need translations, these do not. |
| 563 | */ |
| 564 | case HDIO_GET_IDENTITY: |
| 565 | case HDIO_DRIVE_TASK: |
| 566 | case HDIO_DRIVE_CMD: |
Arnd Bergmann | 7199d4c | 2007-10-09 13:23:52 +0200 | [diff] [blame] | 567 | /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */ |
| 568 | case 0x330: |
| 569 | /* 0x02 -- Floppy ioctls */ |
| 570 | case FDMSGON: |
| 571 | case FDMSGOFF: |
| 572 | case FDSETEMSGTRESH: |
| 573 | case FDFLUSH: |
| 574 | case FDWERRORCLR: |
| 575 | case FDSETMAXERRS: |
| 576 | case FDGETMAXERRS: |
| 577 | case FDGETDRVTYP: |
| 578 | case FDEJECT: |
| 579 | case FDCLRPRM: |
| 580 | case FDFMTBEG: |
| 581 | case FDFMTEND: |
| 582 | case FDRESET: |
| 583 | case FDTWADDLE: |
| 584 | case FDFMTTRK: |
| 585 | case FDRAWCMD: |
| 586 | /* CDROM stuff */ |
| 587 | case CDROMPAUSE: |
| 588 | case CDROMRESUME: |
| 589 | case CDROMPLAYMSF: |
| 590 | case CDROMPLAYTRKIND: |
| 591 | case CDROMREADTOCHDR: |
| 592 | case CDROMREADTOCENTRY: |
| 593 | case CDROMSTOP: |
| 594 | case CDROMSTART: |
| 595 | case CDROMEJECT: |
| 596 | case CDROMVOLCTRL: |
| 597 | case CDROMSUBCHNL: |
| 598 | case CDROMMULTISESSION: |
| 599 | case CDROM_GET_MCN: |
| 600 | case CDROMRESET: |
| 601 | case CDROMVOLREAD: |
| 602 | case CDROMSEEK: |
| 603 | case CDROMPLAYBLK: |
| 604 | case CDROMCLOSETRAY: |
| 605 | case CDROM_DISC_STATUS: |
| 606 | case CDROM_CHANGER_NSLOTS: |
| 607 | case CDROM_GET_CAPABILITY: |
| 608 | /* Ignore cdrom.h about these next 5 ioctls, they absolutely do |
| 609 | * not take a struct cdrom_read, instead they take a struct cdrom_msf |
| 610 | * which is compatible. |
| 611 | */ |
| 612 | case CDROMREADMODE2: |
| 613 | case CDROMREADMODE1: |
| 614 | case CDROMREADRAW: |
| 615 | case CDROMREADCOOKED: |
| 616 | case CDROMREADALL: |
| 617 | /* DVD ioctls */ |
| 618 | case DVD_READ_STRUCT: |
| 619 | case DVD_WRITE_STRUCT: |
| 620 | case DVD_AUTH: |
| 621 | arg = (unsigned long)compat_ptr(arg); |
Andreas Schwab | 1c925604 | 2008-10-31 22:39:46 +0100 | [diff] [blame] | 622 | /* These intepret arg as an unsigned long, not as a pointer, |
| 623 | * so we must not do compat_ptr() conversion. */ |
| 624 | case HDIO_SET_MULTCOUNT: |
| 625 | case HDIO_SET_UNMASKINTR: |
| 626 | case HDIO_SET_KEEPSETTINGS: |
| 627 | case HDIO_SET_32BIT: |
| 628 | case HDIO_SET_NOWERR: |
| 629 | case HDIO_SET_DMA: |
| 630 | case HDIO_SET_PIO_MODE: |
| 631 | case HDIO_SET_NICE: |
| 632 | case HDIO_SET_WCACHE: |
| 633 | case HDIO_SET_ACOUSTIC: |
| 634 | case HDIO_SET_BUSSTATE: |
| 635 | case HDIO_SET_ADDRESS: |
| 636 | case CDROMEJECT_SW: |
| 637 | case CDROM_SET_OPTIONS: |
| 638 | case CDROM_CLEAR_OPTIONS: |
| 639 | case CDROM_SELECT_SPEED: |
| 640 | case CDROM_SELECT_DISC: |
| 641 | case CDROM_MEDIA_CHANGED: |
| 642 | case CDROM_DRIVE_STATUS: |
| 643 | case CDROM_LOCKDOOR: |
| 644 | case CDROM_DEBUG: |
Arnd Bergmann | 7199d4c | 2007-10-09 13:23:52 +0200 | [diff] [blame] | 645 | break; |
| 646 | default: |
| 647 | /* unknown ioctl number */ |
| 648 | return -ENOIOCTLCMD; |
| 649 | } |
| 650 | |
Al Viro | 33c2dca | 2008-02-22 22:02:50 -0500 | [diff] [blame] | 651 | return __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
Arnd Bergmann | 7199d4c | 2007-10-09 13:23:52 +0200 | [diff] [blame] | 652 | } |
| 653 | |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 654 | /* Most of the generic ioctls are handled in the normal fallback path. |
| 655 | This assumes the blkdev's low level compat_ioctl always returns |
| 656 | ENOIOCTLCMD for unknown ioctls. */ |
| 657 | long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 658 | { |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 659 | int ret = -ENOIOCTLCMD; |
| 660 | struct inode *inode = file->f_mapping->host; |
| 661 | struct block_device *bdev = inode->i_bdev; |
| 662 | struct gendisk *disk = bdev->bd_disk; |
| 663 | fmode_t mode = file->f_mode; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 664 | loff_t size; |
Akinobu Mita | 63f2649 | 2014-05-25 21:43:33 +0900 | [diff] [blame] | 665 | unsigned int max_sectors; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 666 | |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 667 | /* |
| 668 | * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have |
| 669 | * to updated it before every ioctl. |
| 670 | */ |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 671 | if (file->f_flags & O_NDELAY) |
Christoph Hellwig | fd4ce1a | 2008-11-05 14:58:42 +0100 | [diff] [blame] | 672 | mode |= FMODE_NDELAY; |
| 673 | else |
| 674 | mode &= ~FMODE_NDELAY; |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 675 | |
| 676 | switch (cmd) { |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 677 | case HDIO_GETGEO: |
| 678 | return compat_hdio_getgeo(disk, bdev, compat_ptr(arg)); |
Martin K. Petersen | ac481c2 | 2009-10-03 20:52:01 +0200 | [diff] [blame] | 679 | case BLKPBSZGET: |
| 680 | return compat_put_uint(arg, bdev_physical_block_size(bdev)); |
| 681 | case BLKIOMIN: |
| 682 | return compat_put_uint(arg, bdev_io_min(bdev)); |
| 683 | case BLKIOOPT: |
| 684 | return compat_put_uint(arg, bdev_io_opt(bdev)); |
| 685 | case BLKALIGNOFF: |
| 686 | return compat_put_int(arg, bdev_alignment_offset(bdev)); |
Martin K. Petersen | 98262f2 | 2009-12-03 09:24:48 +0100 | [diff] [blame] | 687 | case BLKDISCARDZEROES: |
| 688 | return compat_put_uint(arg, bdev_discard_zeroes_data(bdev)); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 689 | case BLKFLSBUF: |
| 690 | case BLKROSET: |
| 691 | case BLKDISCARD: |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 692 | case BLKSECDISCARD: |
Mikulas Patocka | 3b3a181 | 2014-07-02 12:46:23 -0400 | [diff] [blame] | 693 | case BLKZEROOUT: |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 694 | /* |
| 695 | * the ones below are implemented in blkdev_locked_ioctl, |
| 696 | * but we call blkdev_ioctl, which gets the lock for us |
| 697 | */ |
| 698 | case BLKRRPART: |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 699 | return blkdev_ioctl(bdev, mode, cmd, |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 700 | (unsigned long)compat_ptr(arg)); |
| 701 | case BLKBSZSET_32: |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 702 | return blkdev_ioctl(bdev, mode, BLKBSZSET, |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 703 | (unsigned long)compat_ptr(arg)); |
| 704 | case BLKPG: |
Al Viro | 56b26ad | 2008-09-19 03:17:36 -0400 | [diff] [blame] | 705 | return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg)); |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 706 | case BLKRAGET: |
| 707 | case BLKFRAGET: |
| 708 | if (!arg) |
| 709 | return -EINVAL; |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 710 | return compat_put_long(arg, |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 711 | (bdev->bd_bdi->ra_pages * PAGE_SIZE) / 512); |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 712 | case BLKROGET: /* compatible */ |
| 713 | return compat_put_int(arg, bdev_read_only(bdev) != 0); |
| 714 | case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */ |
| 715 | return compat_put_int(arg, block_size(bdev)); |
| 716 | case BLKSSZGET: /* get block device hardware sector size */ |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 717 | return compat_put_int(arg, bdev_logical_block_size(bdev)); |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 718 | case BLKSECTGET: |
Akinobu Mita | 63f2649 | 2014-05-25 21:43:33 +0900 | [diff] [blame] | 719 | max_sectors = min_t(unsigned int, USHRT_MAX, |
| 720 | queue_max_sectors(bdev_get_queue(bdev))); |
| 721 | return compat_put_ushort(arg, max_sectors); |
Martin K. Petersen | ef00f59 | 2012-01-11 16:29:31 +0100 | [diff] [blame] | 722 | case BLKROTATIONAL: |
| 723 | return compat_put_ushort(arg, |
| 724 | !blk_queue_nonrot(bdev_get_queue(bdev))); |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 725 | case BLKRASET: /* compatible, but no compat_ptr (!) */ |
| 726 | case BLKFRASET: |
| 727 | if (!capable(CAP_SYS_ADMIN)) |
| 728 | return -EACCES; |
Jan Kara | efa7c9f | 2017-02-02 15:56:53 +0100 | [diff] [blame] | 729 | bdev->bd_bdi->ra_pages = (arg * 512) / PAGE_SIZE; |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 730 | return 0; |
| 731 | case BLKGETSIZE: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 732 | size = i_size_read(bdev->bd_inode); |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 733 | if ((size >> 9) > ~0UL) |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 734 | return -EFBIG; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 735 | return compat_put_ulong(arg, size >> 9); |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 736 | |
| 737 | case BLKGETSIZE64_32: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 738 | return compat_put_u64(arg, i_size_read(bdev->bd_inode)); |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 739 | |
| 740 | case BLKTRACESETUP32: |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 741 | case BLKTRACESTART: /* compatible */ |
| 742 | case BLKTRACESTOP: /* compatible */ |
| 743 | case BLKTRACETEARDOWN: /* compatible */ |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 744 | ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg)); |
Arnd Bergmann | 7199d4c | 2007-10-09 13:23:52 +0200 | [diff] [blame] | 745 | return ret; |
Al Viro | 45048d0 | 2008-09-18 15:53:24 -0400 | [diff] [blame] | 746 | default: |
| 747 | if (disk->fops->compat_ioctl) |
| 748 | ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg); |
| 749 | if (ret == -ENOIOCTLCMD) |
| 750 | ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg); |
| 751 | return ret; |
| 752 | } |
Arnd Bergmann | f58c4c0 | 2007-10-09 13:23:51 +0200 | [diff] [blame] | 753 | } |