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/hdreg.h>
8 #include <linux/slab.h>
9 #include <linux/syscalls.h>
10 #include <linux/types.h>
11 #include <linux/uaccess.h>
13 static int compat_put_ushort(unsigned long arg
, unsigned short val
)
15 return put_user(val
, (unsigned short __user
*)compat_ptr(arg
));
18 static int compat_put_int(unsigned long arg
, int val
)
20 return put_user(val
, (compat_int_t __user
*)compat_ptr(arg
));
23 static int compat_put_uint(unsigned long arg
, unsigned int val
)
25 return put_user(val
, (compat_uint_t __user
*)compat_ptr(arg
));
28 static int compat_put_long(unsigned long arg
, long val
)
30 return put_user(val
, (compat_long_t __user
*)compat_ptr(arg
));
33 static int compat_put_ulong(unsigned long arg
, compat_ulong_t val
)
35 return put_user(val
, (compat_ulong_t __user
*)compat_ptr(arg
));
38 static int compat_put_u64(unsigned long arg
, u64 val
)
40 return put_user(val
, (compat_u64 __user
*)compat_ptr(arg
));
43 struct compat_hd_geometry
{
45 unsigned char sectors
;
46 unsigned short cylinders
;
50 static int compat_hdio_getgeo(struct gendisk
*disk
, struct block_device
*bdev
,
51 struct compat_hd_geometry __user
*ugeo
)
53 struct hd_geometry geo
;
58 if (!disk
->fops
->getgeo
)
61 memset(&geo
, 0, sizeof(geo
));
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 static int compat_blkdev_driver_ioctl(struct block_device
*bdev
, fmode_t mode
,
212 unsigned cmd
, unsigned long arg
)
215 case HDIO_GET_UNMASKINTR
:
216 case HDIO_GET_MULTCOUNT
:
217 case HDIO_GET_KEEPSETTINGS
:
219 case HDIO_GET_NOWERR
:
222 case HDIO_GET_WCACHE
:
223 case HDIO_GET_ACOUSTIC
:
224 case HDIO_GET_ADDRESS
:
225 case HDIO_GET_BUSSTATE
:
226 return compat_hdio_ioctl(bdev
, mode
, cmd
, arg
);
228 return compat_cdrom_read_audio(bdev
, mode
, cmd
, arg
);
229 case CDROM_SEND_PACKET
:
230 return compat_cdrom_generic_command(bdev
, mode
, cmd
, arg
);
233 * No handler required for the ones below, we just need to
234 * convert arg to a 64 bit pointer.
238 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
239 * Some need translations, these do not.
241 case HDIO_GET_IDENTITY
:
242 case HDIO_DRIVE_TASK
:
244 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
250 case CDROMPLAYTRKIND
:
251 case CDROMREADTOCHDR
:
252 case CDROMREADTOCENTRY
:
258 case CDROMMULTISESSION
:
265 case CDROM_DISC_STATUS
:
266 case CDROM_CHANGER_NSLOTS
:
267 case CDROM_GET_CAPABILITY
:
268 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
269 * not take a struct cdrom_read, instead they take a struct cdrom_msf
270 * which is compatible.
275 case CDROMREADCOOKED
:
278 case DVD_READ_STRUCT
:
279 case DVD_WRITE_STRUCT
:
281 arg
= (unsigned long)compat_ptr(arg
);
282 /* These intepret arg as an unsigned long, not as a pointer,
283 * so we must not do compat_ptr() conversion. */
284 case HDIO_SET_MULTCOUNT
:
285 case HDIO_SET_UNMASKINTR
:
286 case HDIO_SET_KEEPSETTINGS
:
288 case HDIO_SET_NOWERR
:
290 case HDIO_SET_PIO_MODE
:
292 case HDIO_SET_WCACHE
:
293 case HDIO_SET_ACOUSTIC
:
294 case HDIO_SET_BUSSTATE
:
295 case HDIO_SET_ADDRESS
:
297 case CDROM_SET_OPTIONS
:
298 case CDROM_CLEAR_OPTIONS
:
299 case CDROM_SELECT_SPEED
:
300 case CDROM_SELECT_DISC
:
301 case CDROM_MEDIA_CHANGED
:
302 case CDROM_DRIVE_STATUS
:
307 /* unknown ioctl number */
311 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
314 /* Most of the generic ioctls are handled in the normal fallback path.
315 This assumes the blkdev's low level compat_ioctl always returns
316 ENOIOCTLCMD for unknown ioctls. */
317 long compat_blkdev_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
319 int ret
= -ENOIOCTLCMD
;
320 struct inode
*inode
= file
->f_mapping
->host
;
321 struct block_device
*bdev
= inode
->i_bdev
;
322 struct gendisk
*disk
= bdev
->bd_disk
;
323 fmode_t mode
= file
->f_mode
;
324 struct backing_dev_info
*bdi
;
326 unsigned int max_sectors
;
329 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
330 * to updated it before every ioctl.
332 if (file
->f_flags
& O_NDELAY
)
333 mode
|= FMODE_NDELAY
;
335 mode
&= ~FMODE_NDELAY
;
339 return compat_hdio_getgeo(disk
, bdev
, compat_ptr(arg
));
341 return compat_put_uint(arg
, bdev_physical_block_size(bdev
));
343 return compat_put_uint(arg
, bdev_io_min(bdev
));
345 return compat_put_uint(arg
, bdev_io_opt(bdev
));
347 return compat_put_int(arg
, bdev_alignment_offset(bdev
));
348 case BLKDISCARDZEROES
:
349 return compat_put_uint(arg
, bdev_discard_zeroes_data(bdev
));
356 * the ones below are implemented in blkdev_locked_ioctl,
357 * but we call blkdev_ioctl, which gets the lock for us
360 return blkdev_ioctl(bdev
, mode
, cmd
,
361 (unsigned long)compat_ptr(arg
));
363 return blkdev_ioctl(bdev
, mode
, BLKBSZSET
,
364 (unsigned long)compat_ptr(arg
));
366 return compat_blkpg_ioctl(bdev
, mode
, cmd
, compat_ptr(arg
));
371 bdi
= blk_get_backing_dev_info(bdev
);
372 return compat_put_long(arg
,
373 (bdi
->ra_pages
* PAGE_SIZE
) / 512);
374 case BLKROGET
: /* compatible */
375 return compat_put_int(arg
, bdev_read_only(bdev
) != 0);
376 case BLKBSZGET_32
: /* get the logical block size (cf. BLKSSZGET) */
377 return compat_put_int(arg
, block_size(bdev
));
378 case BLKSSZGET
: /* get block device hardware sector size */
379 return compat_put_int(arg
, bdev_logical_block_size(bdev
));
381 max_sectors
= min_t(unsigned int, USHRT_MAX
,
382 queue_max_sectors(bdev_get_queue(bdev
)));
383 return compat_put_ushort(arg
, max_sectors
);
385 return compat_put_ushort(arg
,
386 !blk_queue_nonrot(bdev_get_queue(bdev
)));
387 case BLKRASET
: /* compatible, but no compat_ptr (!) */
389 if (!capable(CAP_SYS_ADMIN
))
391 bdi
= blk_get_backing_dev_info(bdev
);
392 bdi
->ra_pages
= (arg
* 512) / PAGE_SIZE
;
395 size
= i_size_read(bdev
->bd_inode
);
396 if ((size
>> 9) > ~0UL)
398 return compat_put_ulong(arg
, size
>> 9);
400 case BLKGETSIZE64_32
:
401 return compat_put_u64(arg
, i_size_read(bdev
->bd_inode
));
403 case BLKTRACESETUP32
:
404 case BLKTRACESTART
: /* compatible */
405 case BLKTRACESTOP
: /* compatible */
406 case BLKTRACETEARDOWN
: /* compatible */
407 ret
= blk_trace_ioctl(bdev
, cmd
, compat_ptr(arg
));
410 if (disk
->fops
->compat_ioctl
)
411 ret
= disk
->fops
->compat_ioctl(bdev
, mode
, cmd
, arg
);
412 if (ret
== -ENOIOCTLCMD
)
413 ret
= compat_blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);