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)
62 memset(&geo, 0, sizeof(geo));
64 * We need to set the startsect first, the driver may
65 * want to override it.
67 geo.start = get_start_sect(bdev);
68 ret = disk->fops->getgeo(bdev, &geo);
72 ret = copy_to_user(ugeo, &geo, 4);
73 ret |= __put_user(geo.start, &ugeo->start);
80 static int compat_hdio_ioctl(struct block_device *bdev, fmode_t mode,
81 unsigned int cmd, unsigned long arg)
83 mm_segment_t old_fs = get_fs();
85 unsigned int __user *uvp;
89 error = __blkdev_driver_ioctl(bdev, mode,
90 cmd, (unsigned long)(&kval));
94 uvp = compat_ptr(arg);
95 if (put_user(kval, uvp))
101 struct compat_cdrom_read_audio {
102 union cdrom_addr addr;
104 compat_int_t nframes;
108 struct compat_cdrom_generic_command {
109 unsigned char cmd[CDROM_PACKET_SIZE];
110 compat_caddr_t buffer;
111 compat_uint_t buflen;
113 compat_caddr_t sense;
114 unsigned char data_direction;
116 compat_int_t timeout;
117 compat_caddr_t reserved[1];
120 static int compat_cdrom_read_audio(struct block_device *bdev, fmode_t mode,
121 unsigned int cmd, unsigned long arg)
123 struct cdrom_read_audio __user *cdread_audio;
124 struct compat_cdrom_read_audio __user *cdread_audio32;
128 cdread_audio = compat_alloc_user_space(sizeof(*cdread_audio));
129 cdread_audio32 = compat_ptr(arg);
131 if (copy_in_user(&cdread_audio->addr,
132 &cdread_audio32->addr,
133 (sizeof(*cdread_audio32) -
134 sizeof(compat_caddr_t))))
137 if (get_user(data, &cdread_audio32->buf))
139 datap = compat_ptr(data);
140 if (put_user(datap, &cdread_audio->buf))
143 return __blkdev_driver_ioctl(bdev, mode, cmd,
144 (unsigned long)cdread_audio);
147 static int compat_cdrom_generic_command(struct block_device *bdev, fmode_t mode,
148 unsigned int cmd, unsigned long arg)
150 struct cdrom_generic_command __user *cgc;
151 struct compat_cdrom_generic_command __user *cgc32;
156 cgc = compat_alloc_user_space(sizeof(*cgc));
157 cgc32 = compat_ptr(arg);
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]))
176 return __blkdev_driver_ioctl(bdev, mode, cmd, (unsigned long)cgc);
179 struct compat_blkpg_ioctl_arg {
182 compat_int_t datalen;
186 static int compat_blkpg_ioctl(struct block_device *bdev, fmode_t mode,
187 unsigned int cmd, struct compat_blkpg_ioctl_arg __user *ua32)
189 struct blkpg_ioctl_arg __user *a = compat_alloc_user_space(sizeof(*a));
190 compat_caddr_t udata;
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);
205 return blkdev_ioctl(bdev, mode, cmd, (unsigned long)a);
208 #define BLKBSZGET_32 _IOR(0x12, 112, int)
209 #define BLKBSZSET_32 _IOW(0x12, 113, int)
210 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
212 struct compat_floppy_drive_params {
214 compat_ulong_t max_dtr;
218 compat_ulong_t spinup;
219 compat_ulong_t spindown;
220 unsigned char spindown_offset;
221 unsigned char select_delay;
223 unsigned char tracks;
224 compat_ulong_t timeout;
225 unsigned char interleave_sect;
226 struct floppy_max_errors max_errors;
230 compat_int_t checkfreq;
231 compat_int_t native_format;
234 struct compat_floppy_drive_struct {
236 compat_ulong_t spinup_date;
237 compat_ulong_t select_date;
238 compat_ulong_t first_read_date;
243 compat_int_t generation;
244 compat_int_t keep_data;
246 compat_int_t fd_device;
247 compat_int_t last_checked;
248 compat_caddr_t dmabuf;
249 compat_int_t bufblocks;
252 struct compat_floppy_fdc_state {
256 unsigned char version;
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];
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;
277 #define FDSETPRM32 _IOW(2, 0x42, struct compat_floppy_struct)
278 #define FDDEFPRM32 _IOW(2, 0x43, struct compat_floppy_struct)
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)
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 }
301 #define NR_FD_IOCTL_TRANS ARRAY_SIZE(fd_ioctl_trans_table)
303 static int compat_fd_ioctl(struct block_device *bdev, fmode_t mode,
304 unsigned int cmd, unsigned long arg)
306 mm_segment_t old_fs = get_fs();
308 unsigned int kcmd = 0;
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;
325 struct compat_floppy_struct __user *uf;
326 struct floppy_struct *f;
328 uf = compat_ptr(arg);
329 f = karg = kmalloc(sizeof(struct floppy_struct), GFP_KERNEL);
332 if (cmd == FDGETPRM32)
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);
354 struct compat_floppy_drive_params __user *uf;
355 struct floppy_drive_params *f;
357 uf = compat_ptr(arg);
358 f = karg = kmalloc(sizeof(struct floppy_drive_params), GFP_KERNEL);
361 if (cmd == FDGETDRVPRM32)
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);
389 case FDPOLLDRVSTAT32:
390 karg = kmalloc(sizeof(struct floppy_drive_struct), GFP_KERNEL);
395 karg = kmalloc(sizeof(struct floppy_fdc_state), GFP_KERNEL);
400 karg = kmalloc(sizeof(struct floppy_write_errors), GFP_KERNEL);
408 err = __blkdev_driver_ioctl(bdev, mode, kcmd, (unsigned long)karg);
415 struct floppy_struct *f = karg;
416 struct compat_floppy_struct __user *uf = compat_ptr(arg);
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);
432 struct compat_floppy_drive_params __user *uf;
433 struct floppy_drive_params *f = karg;
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);
458 case FDPOLLDRVSTAT32:
460 struct compat_floppy_drive_struct __user *uf;
461 struct floppy_drive_struct *f = karg;
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);
483 struct compat_floppy_fdc_state __user *uf;
484 struct floppy_fdc_state *f = karg;
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));
501 struct compat_floppy_write_errors __user *uf;
502 struct floppy_write_errors *f = karg;
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);
524 static int compat_blkdev_driver_ioctl(struct block_device *bdev, fmode_t mode,
525 unsigned cmd, unsigned long arg)
528 case HDIO_GET_UNMASKINTR:
529 case HDIO_GET_MULTCOUNT:
530 case HDIO_GET_KEEPSETTINGS:
532 case HDIO_GET_NOWERR:
535 case HDIO_GET_WCACHE:
536 case HDIO_GET_ACOUSTIC:
537 case HDIO_GET_ADDRESS:
538 case HDIO_GET_BUSSTATE:
539 return compat_hdio_ioctl(bdev, mode, cmd, arg);
546 case FDPOLLDRVSTAT32:
549 return compat_fd_ioctl(bdev, mode, cmd, arg);
551 return compat_cdrom_read_audio(bdev, mode, cmd, arg);
552 case CDROM_SEND_PACKET:
553 return compat_cdrom_generic_command(bdev, mode, cmd, arg);
556 * No handler required for the ones below, we just need to
557 * convert arg to a 64 bit pointer.
561 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
562 * Some need translations, these do not.
564 case HDIO_GET_IDENTITY:
565 case HDIO_DRIVE_TASK:
567 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
569 /* 0x02 -- Floppy ioctls */
590 case CDROMPLAYTRKIND:
591 case CDROMREADTOCHDR:
592 case CDROMREADTOCENTRY:
598 case CDROMMULTISESSION:
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.
615 case CDROMREADCOOKED:
618 case DVD_READ_STRUCT:
619 case DVD_WRITE_STRUCT:
621 arg = (unsigned long)compat_ptr(arg);
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:
628 case HDIO_SET_NOWERR:
630 case HDIO_SET_PIO_MODE:
632 case HDIO_SET_WCACHE:
633 case HDIO_SET_ACOUSTIC:
634 case HDIO_SET_BUSSTATE:
635 case HDIO_SET_ADDRESS:
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:
647 /* unknown ioctl number */
651 return __blkdev_driver_ioctl(bdev, mode, cmd, arg);
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)
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;
664 struct backing_dev_info *bdi;
668 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
669 * to updated it before every ioctl.
671 if (file->f_flags & O_NDELAY)
672 mode |= FMODE_NDELAY;
674 mode &= ~FMODE_NDELAY;
678 return compat_hdio_getgeo(disk, bdev, compat_ptr(arg));
680 return compat_put_uint(arg, bdev_physical_block_size(bdev));
682 return compat_put_uint(arg, bdev_io_min(bdev));
684 return compat_put_uint(arg, bdev_io_opt(bdev));
686 return compat_put_int(arg, bdev_alignment_offset(bdev));
687 case BLKDISCARDZEROES:
688 return compat_put_uint(arg, bdev_discard_zeroes_data(bdev));
694 * the ones below are implemented in blkdev_locked_ioctl,
695 * but we call blkdev_ioctl, which gets the lock for us
698 return blkdev_ioctl(bdev, mode, cmd,
699 (unsigned long)compat_ptr(arg));
701 return blkdev_ioctl(bdev, mode, BLKBSZSET,
702 (unsigned long)compat_ptr(arg));
704 return compat_blkpg_ioctl(bdev, mode, cmd, compat_ptr(arg));
709 bdi = blk_get_backing_dev_info(bdev);
712 return compat_put_long(arg,
713 (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
714 case BLKROGET: /* compatible */
715 return compat_put_int(arg, bdev_read_only(bdev) != 0);
716 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
717 return compat_put_int(arg, block_size(bdev));
718 case BLKSSZGET: /* get block device hardware sector size */
719 return compat_put_int(arg, bdev_logical_block_size(bdev));
721 return compat_put_ushort(arg,
722 queue_max_sectors(bdev_get_queue(bdev)));
724 return compat_put_ushort(arg,
725 !blk_queue_nonrot(bdev_get_queue(bdev)));
726 case BLKRASET: /* compatible, but no compat_ptr (!) */
728 if (!capable(CAP_SYS_ADMIN))
730 bdi = blk_get_backing_dev_info(bdev);
733 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
736 size = i_size_read(bdev->bd_inode);
737 if ((size >> 9) > ~0UL)
739 return compat_put_ulong(arg, size >> 9);
741 case BLKGETSIZE64_32:
742 return compat_put_u64(arg, i_size_read(bdev->bd_inode));
744 case BLKTRACESETUP32:
745 case BLKTRACESTART: /* compatible */
746 case BLKTRACESTOP: /* compatible */
747 case BLKTRACETEARDOWN: /* compatible */
748 ret = blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
751 if (disk->fops->compat_ioctl)
752 ret = disk->fops->compat_ioctl(bdev, mode, cmd, arg);
753 if (ret == -ENOIOCTLCMD)
754 ret = compat_blkdev_driver_ioctl(bdev, mode, cmd, arg);