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>
8 #include <linux/hdreg.h>
9 #include <linux/slab.h>
10 #include <linux/syscalls.h>
11 #include <linux/types.h>
12 #include <linux/uaccess.h>
14 static int compat_put_ushort(unsigned long arg, unsigned short val)
16 return put_user(val, (unsigned short __user *)compat_ptr(arg));
19 static int compat_put_int(unsigned long arg, int val)
21 return put_user(val, (compat_int_t __user *)compat_ptr(arg));
24 static int compat_put_uint(unsigned long arg, unsigned int val)
26 return put_user(val, (compat_uint_t __user *)compat_ptr(arg));
29 static int compat_put_long(unsigned long arg, long val)
31 return put_user(val, (compat_long_t __user *)compat_ptr(arg));
34 static int compat_put_ulong(unsigned long arg, compat_ulong_t val)
36 return put_user(val, (compat_ulong_t __user *)compat_ptr(arg));
39 static int compat_put_u64(unsigned long arg, u64 val)
41 return put_user(val, (compat_u64 __user *)compat_ptr(arg));
44 struct compat_hd_geometry {
46 unsigned char sectors;
47 unsigned short cylinders;
51 static int compat_hdio_getgeo(struct gendisk *disk, struct block_device *bdev,
52 struct compat_hd_geometry __user *ugeo)
54 struct hd_geometry geo;
59 if (!disk->fops->getgeo)
63 * We need to set the startsect first, the driver may
64 * want to override it.
66 geo.start = get_start_sect(bdev);
67 ret = disk->fops->getgeo(bdev, &geo);
71 ret = copy_to_user(ugeo, &geo, 4);
72 ret |= __put_user(geo.start, &ugeo->start);
79 static int compat_hdio_ioctl(struct block_device *bdev, fmode_t mode,
80 unsigned int cmd, unsigned long arg)
82 mm_segment_t old_fs = get_fs();
84 unsigned int __user *uvp;
88 error = __blkdev_driver_ioctl(bdev, mode,
89 cmd, (unsigned long)(&kval));
93 uvp = compat_ptr(arg);
94 if (put_user(kval, uvp))
100 struct compat_cdrom_read_audio {
101 union cdrom_addr addr;
103 compat_int_t nframes;
107 struct compat_cdrom_generic_command {
108 unsigned char cmd[CDROM_PACKET_SIZE];
109 compat_caddr_t buffer;
110 compat_uint_t buflen;
112 compat_caddr_t sense;
113 unsigned char data_direction;
115 compat_int_t timeout;
116 compat_caddr_t reserved[1];
119 static int compat_cdrom_read_audio(struct block_device *bdev, fmode_t mode,
120 unsigned int cmd, unsigned long arg)
122 struct cdrom_read_audio __user *cdread_audio;
123 struct compat_cdrom_read_audio __user *cdread_audio32;
127 cdread_audio = compat_alloc_user_space(sizeof(*cdread_audio));
128 cdread_audio32 = compat_ptr(arg);
130 if (copy_in_user(&cdread_audio->addr,
131 &cdread_audio32->addr,
132 (sizeof(*cdread_audio32) -
133 sizeof(compat_caddr_t))))
136 if (get_user(data, &cdread_audio32->buf))
138 datap = compat_ptr(data);
139 if (put_user(datap, &cdread_audio->buf))
142 return __blkdev_driver_ioctl(bdev, mode, cmd,
143 (unsigned long)cdread_audio);
146 static int compat_cdrom_generic_command(struct block_device *bdev, fmode_t mode,
147 unsigned int cmd, unsigned long arg)
149 struct cdrom_generic_command __user *cgc;
150 struct compat_cdrom_generic_command __user *cgc32;
155 cgc = compat_alloc_user_space(sizeof(*cgc));
156 cgc32 = compat_ptr(arg);
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]))
175 return __blkdev_driver_ioctl(bdev, mode, cmd, (unsigned long)cgc);
178 struct compat_blkpg_ioctl_arg {
181 compat_int_t datalen;
185 static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,
186 unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32)
188 struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a));
189 compat_caddr_t udata;
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);
204 return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a);
207 #define BLKBSZGET_32 _IOR(0x12, 112, int)
208 #define BLKBSZSET_32 _IOW(0x12, 113, int)
209 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
211 struct compat_floppy_struct {
216 compat_uint_t stretch;
220 unsigned char fmt_gap;
221 const compat_caddr_t name;
224 struct compat_floppy_drive_params {
226 compat_ulong_t max_dtr;
230 compat_ulong_t spinup;
231 compat_ulong_t spindown;
232 unsigned char spindown_offset;
233 unsigned char select_delay;
235 unsigned char tracks;
236 compat_ulong_t timeout;
237 unsigned char interleave_sect;
238 struct floppy_max_errors max_errors;
242 compat_int_t checkfreq;
243 compat_int_t native_format;
246 struct compat_floppy_drive_struct {
248 compat_ulong_t spinup_date;
249 compat_ulong_t select_date;
250 compat_ulong_t first_read_date;
255 compat_int_t generation;
256 compat_int_t keep_data;
258 compat_int_t fd_device;
259 compat_int_t last_checked;
260 compat_caddr_t dmabuf;
261 compat_int_t bufblocks;
264 struct compat_floppy_fdc_state {
268 unsigned char version;
270 compat_ulong_t address;
271 unsigned int rawcmd:2;
272 unsigned int reset:1;
273 unsigned int need_configure:1;
274 unsigned int perp_mode:2;
275 unsigned int has_fifo:1;
276 unsigned int driver_version;
277 unsigned char track[4];
280 struct compat_floppy_write_errors {
281 unsigned int write_errors;
282 compat_ulong_t first_error_sector;
283 compat_int_t first_error_generation;
284 compat_ulong_t last_error_sector;
285 compat_int_t last_error_generation;
286 compat_uint_t badness;
289 #define FDSETPRM32 _IOW(2, 0x42, struct compat_floppy_struct)
290 #define FDDEFPRM32 _IOW(2, 0x43, struct compat_floppy_struct)
291 #define FDGETPRM32 _IOR(2, 0x04, struct compat_floppy_struct)
292 #define FDSETDRVPRM32 _IOW(2, 0x90, struct compat_floppy_drive_params)
293 #define FDGETDRVPRM32 _IOR(2, 0x11, struct compat_floppy_drive_params)
294 #define FDGETDRVSTAT32 _IOR(2, 0x12, struct compat_floppy_drive_struct)
295 #define FDPOLLDRVSTAT32 _IOR(2, 0x13, struct compat_floppy_drive_struct)
296 #define FDGETFDCSTAT32 _IOR(2, 0x15, struct compat_floppy_fdc_state)
297 #define FDWERRORGET32 _IOR(2, 0x17, struct compat_floppy_write_errors)
302 } fd_ioctl_trans_table[] = {
303 { FDSETPRM32, FDSETPRM },
304 { FDDEFPRM32, FDDEFPRM },
305 { FDGETPRM32, FDGETPRM },
306 { FDSETDRVPRM32, FDSETDRVPRM },
307 { FDGETDRVPRM32, FDGETDRVPRM },
308 { FDGETDRVSTAT32, FDGETDRVSTAT },
309 { FDPOLLDRVSTAT32, FDPOLLDRVSTAT },
310 { FDGETFDCSTAT32, FDGETFDCSTAT },
311 { FDWERRORGET32, FDWERRORGET }
314 #define NR_FD_IOCTL_TRANS ARRAY_SIZE(fd_ioctl_trans_table)
316 static int compat_fd_ioctl(struct block_device *bdev, fmode_t mode,
317 unsigned int cmd, unsigned long arg)
319 mm_segment_t old_fs = get_fs();
321 unsigned int kcmd = 0;
324 for (i = 0; i < NR_FD_IOCTL_TRANS; i++)
325 if (cmd == fd_ioctl_trans_table[i].cmd32) {
326 kcmd = fd_ioctl_trans_table[i].cmd;
338 struct compat_floppy_struct __user *uf;
339 struct floppy_struct *f;
341 uf = compat_ptr(arg);
342 f = karg = kmalloc(sizeof(struct floppy_struct), GFP_KERNEL);
345 if (cmd == FDGETPRM32)
347 err = __get_user(f->size, &uf->size);
348 err |= __get_user(f->sect, &uf->sect);
349 err |= __get_user(f->head, &uf->head);
350 err |= __get_user(f->track, &uf->track);
351 err |= __get_user(f->stretch, &uf->stretch);
352 err |= __get_user(f->gap, &uf->gap);
353 err |= __get_user(f->rate, &uf->rate);
354 err |= __get_user(f->spec1, &uf->spec1);
355 err |= __get_user(f->fmt_gap, &uf->fmt_gap);
356 err |= __get_user(name, &uf->name);
357 f->name = compat_ptr(name);
367 struct compat_floppy_drive_params __user *uf;
368 struct floppy_drive_params *f;
370 uf = compat_ptr(arg);
371 f = karg = kmalloc(sizeof(struct floppy_drive_params), GFP_KERNEL);
374 if (cmd == FDGETDRVPRM32)
376 err = __get_user(f->cmos, &uf->cmos);
377 err |= __get_user(f->max_dtr, &uf->max_dtr);
378 err |= __get_user(f->hlt, &uf->hlt);
379 err |= __get_user(f->hut, &uf->hut);
380 err |= __get_user(f->srt, &uf->srt);
381 err |= __get_user(f->spinup, &uf->spinup);
382 err |= __get_user(f->spindown, &uf->spindown);
383 err |= __get_user(f->spindown_offset, &uf->spindown_offset);
384 err |= __get_user(f->select_delay, &uf->select_delay);
385 err |= __get_user(f->rps, &uf->rps);
386 err |= __get_user(f->tracks, &uf->tracks);
387 err |= __get_user(f->timeout, &uf->timeout);
388 err |= __get_user(f->interleave_sect, &uf->interleave_sect);
389 err |= __copy_from_user(&f->max_errors, &uf->max_errors, sizeof(f->max_errors));
390 err |= __get_user(f->flags, &uf->flags);
391 err |= __get_user(f->read_track, &uf->read_track);
392 err |= __copy_from_user(f->autodetect, uf->autodetect, sizeof(f->autodetect));
393 err |= __get_user(f->checkfreq, &uf->checkfreq);
394 err |= __get_user(f->native_format, &uf->native_format);
402 case FDPOLLDRVSTAT32:
403 karg = kmalloc(sizeof(struct floppy_drive_struct), GFP_KERNEL);
408 karg = kmalloc(sizeof(struct floppy_fdc_state), GFP_KERNEL);
413 karg = kmalloc(sizeof(struct floppy_write_errors), GFP_KERNEL);
421 err = __blkdev_driver_ioctl(bdev, mode, kcmd, (unsigned long)karg);
428 struct floppy_struct *f = karg;
429 struct compat_floppy_struct __user *uf = compat_ptr(arg);
431 err = __put_user(f->size, &uf->size);
432 err |= __put_user(f->sect, &uf->sect);
433 err |= __put_user(f->head, &uf->head);
434 err |= __put_user(f->track, &uf->track);
435 err |= __put_user(f->stretch, &uf->stretch);
436 err |= __put_user(f->gap, &uf->gap);
437 err |= __put_user(f->rate, &uf->rate);
438 err |= __put_user(f->spec1, &uf->spec1);
439 err |= __put_user(f->fmt_gap, &uf->fmt_gap);
440 err |= __put_user((u64)f->name, (compat_caddr_t __user *)&uf->name);
445 struct compat_floppy_drive_params __user *uf;
446 struct floppy_drive_params *f = karg;
448 uf = compat_ptr(arg);
449 err = __put_user(f->cmos, &uf->cmos);
450 err |= __put_user(f->max_dtr, &uf->max_dtr);
451 err |= __put_user(f->hlt, &uf->hlt);
452 err |= __put_user(f->hut, &uf->hut);
453 err |= __put_user(f->srt, &uf->srt);
454 err |= __put_user(f->spinup, &uf->spinup);
455 err |= __put_user(f->spindown, &uf->spindown);
456 err |= __put_user(f->spindown_offset, &uf->spindown_offset);
457 err |= __put_user(f->select_delay, &uf->select_delay);
458 err |= __put_user(f->rps, &uf->rps);
459 err |= __put_user(f->tracks, &uf->tracks);
460 err |= __put_user(f->timeout, &uf->timeout);
461 err |= __put_user(f->interleave_sect, &uf->interleave_sect);
462 err |= __copy_to_user(&uf->max_errors, &f->max_errors, sizeof(f->max_errors));
463 err |= __put_user(f->flags, &uf->flags);
464 err |= __put_user(f->read_track, &uf->read_track);
465 err |= __copy_to_user(uf->autodetect, f->autodetect, sizeof(f->autodetect));
466 err |= __put_user(f->checkfreq, &uf->checkfreq);
467 err |= __put_user(f->native_format, &uf->native_format);
471 case FDPOLLDRVSTAT32:
473 struct compat_floppy_drive_struct __user *uf;
474 struct floppy_drive_struct *f = karg;
476 uf = compat_ptr(arg);
477 err = __put_user(f->flags, &uf->flags);
478 err |= __put_user(f->spinup_date, &uf->spinup_date);
479 err |= __put_user(f->select_date, &uf->select_date);
480 err |= __put_user(f->first_read_date, &uf->first_read_date);
481 err |= __put_user(f->probed_format, &uf->probed_format);
482 err |= __put_user(f->track, &uf->track);
483 err |= __put_user(f->maxblock, &uf->maxblock);
484 err |= __put_user(f->maxtrack, &uf->maxtrack);
485 err |= __put_user(f->generation, &uf->generation);
486 err |= __put_user(f->keep_data, &uf->keep_data);
487 err |= __put_user(f->fd_ref, &uf->fd_ref);
488 err |= __put_user(f->fd_device, &uf->fd_device);
489 err |= __put_user(f->last_checked, &uf->last_checked);
490 err |= __put_user((u64)f->dmabuf, &uf->dmabuf);
491 err |= __put_user((u64)f->bufblocks, &uf->bufblocks);
496 struct compat_floppy_fdc_state __user *uf;
497 struct floppy_fdc_state *f = karg;
499 uf = compat_ptr(arg);
500 err = __put_user(f->spec1, &uf->spec1);
501 err |= __put_user(f->spec2, &uf->spec2);
502 err |= __put_user(f->dtr, &uf->dtr);
503 err |= __put_user(f->version, &uf->version);
504 err |= __put_user(f->dor, &uf->dor);
505 err |= __put_user(f->address, &uf->address);
506 err |= __copy_to_user((char __user *)&uf->address + sizeof(uf->address),
507 (char *)&f->address + sizeof(f->address), sizeof(int));
508 err |= __put_user(f->driver_version, &uf->driver_version);
509 err |= __copy_to_user(uf->track, f->track, sizeof(f->track));
514 struct compat_floppy_write_errors __user *uf;
515 struct floppy_write_errors *f = karg;
517 uf = compat_ptr(arg);
518 err = __put_user(f->write_errors, &uf->write_errors);
519 err |= __put_user(f->first_error_sector, &uf->first_error_sector);
520 err |= __put_user(f->first_error_generation, &uf->first_error_generation);
521 err |= __put_user(f->last_error_sector, &uf->last_error_sector);
522 err |= __put_user(f->last_error_generation, &uf->last_error_generation);
523 err |= __put_user(f->badness, &uf->badness);
537 static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
538 unsigned cmd, unsigned long arg)
541 case HDIO_GET_UNMASKINTR:
542 case HDIO_GET_MULTCOUNT:
543 case HDIO_GET_KEEPSETTINGS:
545 case HDIO_GET_NOWERR:
548 case HDIO_GET_WCACHE:
549 case HDIO_GET_ACOUSTIC:
550 case HDIO_GET_ADDRESS:
551 case HDIO_GET_BUSSTATE:
552 return compat_hdio_ioctl(bdev, mode, cmd, arg);
559 case FDPOLLDRVSTAT32:
562 return compat_fd_ioctl(bdev, mode, cmd, arg);
564 return compat_cdrom_read_audio(bdev, mode, cmd, arg);
565 case CDROM_SEND_PACKET:
566 return compat_cdrom_generic_command(bdev, mode, cmd, arg);
569 * No handler required for the ones below, we just need to
570 * convert arg to a 64 bit pointer.
574 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
575 * Some need translations, these do not.
577 case HDIO_GET_IDENTITY:
578 case HDIO_DRIVE_TASK:
580 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
582 /* 0x02 -- Floppy ioctls */
603 case CDROMPLAYTRKIND:
604 case CDROMREADTOCHDR:
605 case CDROMREADTOCENTRY:
611 case CDROMMULTISESSION:
618 case CDROM_DISC_STATUS:
619 case CDROM_CHANGER_NSLOTS:
620 case CDROM_GET_CAPABILITY:
621 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
622 * not take a struct cdrom_read, instead they take a struct cdrom_msf
623 * which is compatible.
628 case CDROMREADCOOKED:
631 case DVD_READ_STRUCT:
632 case DVD_WRITE_STRUCT:
634 arg = (unsigned long)compat_ptr(arg);
635 /* These intepret arg as an unsigned long, not as a pointer,
636 * so we must not do compat_ptr() conversion. */
637 case HDIO_SET_MULTCOUNT:
638 case HDIO_SET_UNMASKINTR:
639 case HDIO_SET_KEEPSETTINGS:
641 case HDIO_SET_NOWERR:
643 case HDIO_SET_PIO_MODE:
645 case HDIO_SET_WCACHE:
646 case HDIO_SET_ACOUSTIC:
647 case HDIO_SET_BUSSTATE:
648 case HDIO_SET_ADDRESS:
650 case CDROM_SET_OPTIONS:
651 case CDROM_CLEAR_OPTIONS:
652 case CDROM_SELECT_SPEED:
653 case CDROM_SELECT_DISC:
654 case CDROM_MEDIA_CHANGED:
655 case CDROM_DRIVE_STATUS:
660 /* unknown ioctl number */
664 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
667 /* Most of the generic ioctls are handled in the normal fallback path.
668 This assumes the blkdev's low level compat_ioctl always returns
669 ENOIOCTLCMD for unknown ioctls. */
670 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
672 int ret = -ENOIOCTLCMD;
673 struct inode *inode = file->f_mapping->host;
674 struct block_device *bdev = inode->i_bdev;
675 struct gendisk *disk = bdev->bd_disk;
676 fmode_t mode = file->f_mode;
677 struct backing_dev_info *bdi;
681 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
682 * to updated it before every ioctl.
684 if (file->f_flags & O_NDELAY)
685 mode |= FMODE_NDELAY;
687 mode &= ~FMODE_NDELAY;
691 return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
693 return compat_put_uint(arg, bdev_physical_block_size(bdev));
695 return compat_put_uint(arg, bdev_io_min(bdev));
697 return compat_put_uint(arg, bdev_io_opt(bdev));
699 return compat_put_int(arg, bdev_alignment_offset(bdev));
700 case BLKDISCARDZEROES:
701 return compat_put_uint(arg, bdev_discard_zeroes_data(bdev));
707 * the ones below are implemented in blkdev_locked_ioctl,
708 * but we call blkdev_ioctl, which gets the lock for us
711 return blkdev_ioctl(bdev, mode, cmd,
712 (unsigned long)compat_ptr(arg));
714 return blkdev_ioctl(bdev, mode, BLKBSZSET,
715 (unsigned long)compat_ptr(arg));
717 return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
722 bdi = blk_get_backing_dev_info(bdev);
725 return compat_put_long(arg,
726 (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
727 case BLKROGET: /* compatible */
728 return compat_put_int(arg, bdev_read_only(bdev) != 0);
729 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
730 return compat_put_int(arg, block_size(bdev));
731 case BLKSSZGET: /* get block device hardware sector size */
732 return compat_put_int(arg, bdev_logical_block_size(bdev));
734 return compat_put_ushort(arg,
735 queue_max_sectors(bdev_get_queue(bdev)));
736 case BLKRASET: /* compatible, but no compat_ptr (!) */
738 if (!capable(CAP_SYS_ADMIN))
740 bdi = blk_get_backing_dev_info(bdev);
743 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
746 size = i_size_read(bdev->bd_inode);
747 if ((size >> 9) > ~0UL)
749 return compat_put_ulong(arg, size >> 9);
751 case BLKGETSIZE64_32:
752 return compat_put_u64(arg, i_size_read(bdev->bd_inode));
754 case BLKTRACESETUP32:
755 case BLKTRACESTART: /* compatible */
756 case BLKTRACESTOP: /* compatible */
757 case BLKTRACETEARDOWN: /* compatible */
758 ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
761 if (disk->fops->compat_ioctl)
762 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
763 if (ret == -ENOIOCTLCMD)
764 ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg);