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/syscalls.h>
10 #include <linux/smp_lock.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_long(unsigned long arg, long val)
26 return put_user(val, (compat_long_t __user *)compat_ptr(arg));
29 static int compat_put_ulong(unsigned long arg, compat_ulong_t val)
31 return put_user(val, (compat_ulong_t __user *)compat_ptr(arg));
34 static int compat_put_u64(unsigned long arg, u64 val)
36 return put_user(val, (compat_u64 __user *)compat_ptr(arg));
39 #define BLKBSZGET_32 _IOR(0x12, 112, int)
40 #define BLKBSZSET_32 _IOW(0x12, 113, int)
41 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
43 struct compat_blk_user_trace_setup {
52 #define BLKTRACESETUP32 _IOWR(0x12, 115, struct compat_blk_user_trace_setup)
54 static int compat_blk_trace_setup(struct block_device *bdev, char __user *arg)
56 struct blk_user_trace_setup buts;
57 struct compat_blk_user_trace_setup cbuts;
58 struct request_queue *q;
61 q = bdev_get_queue(bdev);
65 if (copy_from_user(&cbuts, arg, sizeof(cbuts)))
68 buts = (struct blk_user_trace_setup) {
69 .act_mask = cbuts.act_mask,
70 .buf_size = cbuts.buf_size,
71 .buf_nr = cbuts.buf_nr,
72 .start_lba = cbuts.start_lba,
73 .end_lba = cbuts.end_lba,
76 memcpy(&buts.name, &cbuts.name, 32);
78 mutex_lock(&bdev->bd_mutex);
79 ret = do_blk_trace_setup(q, bdev, &buts);
80 mutex_unlock(&bdev->bd_mutex);
84 if (copy_to_user(arg, &buts.name, 32))
90 static int compat_blkdev_driver_ioctl(struct inode *inode, struct file *file,
91 struct gendisk *disk, unsigned cmd, unsigned long arg)
97 * No handler required for the ones below, we just need to
98 * convert arg to a 64 bit pointer.
102 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
103 * Some need translations, these do not.
105 case HDIO_GET_IDENTITY:
106 case HDIO_DRIVE_TASK:
109 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
111 /* 0x02 -- Floppy ioctls */
132 case CDROMPLAYTRKIND:
133 case CDROMREADTOCHDR:
134 case CDROMREADTOCENTRY:
140 case CDROMMULTISESSION:
147 case CDROM_DISC_STATUS:
148 case CDROM_CHANGER_NSLOTS:
149 case CDROM_GET_CAPABILITY:
150 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
151 * not take a struct cdrom_read, instead they take a struct cdrom_msf
152 * which is compatible.
157 case CDROMREADCOOKED:
160 case DVD_READ_STRUCT:
161 case DVD_WRITE_STRUCT:
163 arg = (unsigned long)compat_ptr(arg);
164 /* These intepret arg as an unsigned long, not as a pointer,
165 * so we must not do compat_ptr() conversion. */
166 case HDIO_SET_MULTCOUNT:
167 case HDIO_SET_UNMASKINTR:
168 case HDIO_SET_KEEPSETTINGS:
170 case HDIO_SET_NOWERR:
172 case HDIO_SET_PIO_MODE:
174 case HDIO_SET_WCACHE:
175 case HDIO_SET_ACOUSTIC:
176 case HDIO_SET_BUSSTATE:
177 case HDIO_SET_ADDRESS:
179 case CDROM_SET_OPTIONS:
180 case CDROM_CLEAR_OPTIONS:
181 case CDROM_SELECT_SPEED:
182 case CDROM_SELECT_DISC:
183 case CDROM_MEDIA_CHANGED:
184 case CDROM_DRIVE_STATUS:
189 /* unknown ioctl number */
193 if (disk->fops->unlocked_ioctl)
194 return disk->fops->unlocked_ioctl(file, cmd, arg);
196 if (disk->fops->ioctl) {
198 ret = disk->fops->ioctl(inode, file, cmd, arg);
206 static int compat_blkdev_locked_ioctl(struct inode *inode, struct file *file,
207 struct block_device *bdev,
208 unsigned cmd, unsigned long arg)
210 struct backing_dev_info *bdi;
217 bdi = blk_get_backing_dev_info(bdev);
220 return compat_put_long(arg,
221 (bdi->ra_pages * PAGE_CACHE_SIZE) / 512);
222 case BLKROGET: /* compatible */
223 return compat_put_int(arg, bdev_read_only(bdev) != 0);
224 case BLKBSZGET_32: /* get the logical block size (cf. BLKSSZGET) */
225 return compat_put_int(arg, block_size(bdev));
226 case BLKSSZGET: /* get block device hardware sector size */
227 return compat_put_int(arg, bdev_hardsect_size(bdev));
229 return compat_put_ushort(arg,
230 bdev_get_queue(bdev)->max_sectors);
231 case BLKRASET: /* compatible, but no compat_ptr (!) */
233 if (!capable(CAP_SYS_ADMIN))
235 bdi = blk_get_backing_dev_info(bdev);
238 bdi->ra_pages = (arg * 512) / PAGE_CACHE_SIZE;
241 if ((bdev->bd_inode->i_size >> 9) > ~0UL)
243 return compat_put_ulong(arg, bdev->bd_inode->i_size >> 9);
245 case BLKGETSIZE64_32:
246 return compat_put_u64(arg, bdev->bd_inode->i_size);
248 case BLKTRACESETUP32:
249 return compat_blk_trace_setup(bdev, compat_ptr(arg));
250 case BLKTRACESTART: /* compatible */
251 case BLKTRACESTOP: /* compatible */
252 case BLKTRACETEARDOWN: /* compatible */
253 return blk_trace_ioctl(bdev, cmd, compat_ptr(arg));
258 /* Most of the generic ioctls are handled in the normal fallback path.
259 This assumes the blkdev's low level compat_ioctl always returns
260 ENOIOCTLCMD for unknown ioctls. */
261 long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
263 int ret = -ENOIOCTLCMD;
264 struct inode *inode = file->f_mapping->host;
265 struct block_device *bdev = inode->i_bdev;
266 struct gendisk *disk = bdev->bd_disk;
272 * the ones below are implemented in blkdev_locked_ioctl,
273 * but we call blkdev_ioctl, which gets the lock for us
276 return blkdev_ioctl(inode, file, cmd,
277 (unsigned long)compat_ptr(arg));
279 return blkdev_ioctl(inode, file, BLKBSZSET,
280 (unsigned long)compat_ptr(arg));
284 ret = compat_blkdev_locked_ioctl(inode, file, bdev, cmd, arg);
285 /* FIXME: why do we assume -> compat_ioctl needs the BKL? */
286 if (ret == -ENOIOCTLCMD && disk->fops->compat_ioctl)
287 ret = disk->fops->compat_ioctl(file, cmd, arg);
290 if (ret != -ENOIOCTLCMD)
293 return compat_blkdev_driver_ioctl(inode, file, disk, cmd, arg);