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 unsigned long __user
*p
;
85 p
= compat_alloc_user_space(sizeof(unsigned long));
86 error
= __blkdev_driver_ioctl(bdev
, mode
,
87 cmd
, (unsigned long)p
);
89 unsigned int __user
*uvp
= compat_ptr(arg
);
91 if (get_user(v
, p
) || put_user(v
, uvp
))
97 struct compat_cdrom_read_audio
{
98 union cdrom_addr addr
;
100 compat_int_t nframes
;
104 struct compat_cdrom_generic_command
{
105 unsigned char cmd
[CDROM_PACKET_SIZE
];
106 compat_caddr_t buffer
;
107 compat_uint_t buflen
;
109 compat_caddr_t sense
;
110 unsigned char data_direction
;
112 compat_int_t timeout
;
113 compat_caddr_t reserved
[1];
116 static int compat_cdrom_read_audio(struct block_device
*bdev
, fmode_t mode
,
117 unsigned int cmd
, unsigned long arg
)
119 struct cdrom_read_audio __user
*cdread_audio
;
120 struct compat_cdrom_read_audio __user
*cdread_audio32
;
124 cdread_audio
= compat_alloc_user_space(sizeof(*cdread_audio
));
125 cdread_audio32
= compat_ptr(arg
);
127 if (copy_in_user(&cdread_audio
->addr
,
128 &cdread_audio32
->addr
,
129 (sizeof(*cdread_audio32
) -
130 sizeof(compat_caddr_t
))))
133 if (get_user(data
, &cdread_audio32
->buf
))
135 datap
= compat_ptr(data
);
136 if (put_user(datap
, &cdread_audio
->buf
))
139 return __blkdev_driver_ioctl(bdev
, mode
, cmd
,
140 (unsigned long)cdread_audio
);
143 static int compat_cdrom_generic_command(struct block_device
*bdev
, fmode_t mode
,
144 unsigned int cmd
, unsigned long arg
)
146 struct cdrom_generic_command __user
*cgc
;
147 struct compat_cdrom_generic_command __user
*cgc32
;
152 cgc
= compat_alloc_user_space(sizeof(*cgc
));
153 cgc32
= compat_ptr(arg
);
155 if (copy_in_user(&cgc
->cmd
, &cgc32
->cmd
, sizeof(cgc
->cmd
)) ||
156 get_user(data
, &cgc32
->buffer
) ||
157 put_user(compat_ptr(data
), &cgc
->buffer
) ||
158 copy_in_user(&cgc
->buflen
, &cgc32
->buflen
,
159 (sizeof(unsigned int) + sizeof(int))) ||
160 get_user(data
, &cgc32
->sense
) ||
161 put_user(compat_ptr(data
), &cgc
->sense
) ||
162 get_user(dir
, &cgc32
->data_direction
) ||
163 put_user(dir
, &cgc
->data_direction
) ||
164 get_user(itmp
, &cgc32
->quiet
) ||
165 put_user(itmp
, &cgc
->quiet
) ||
166 get_user(itmp
, &cgc32
->timeout
) ||
167 put_user(itmp
, &cgc
->timeout
) ||
168 get_user(data
, &cgc32
->reserved
[0]) ||
169 put_user(compat_ptr(data
), &cgc
->reserved
[0]))
172 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, (unsigned long)cgc
);
175 struct compat_blkpg_ioctl_arg
{
178 compat_int_t datalen
;
182 static int compat_blkpg_ioctl(struct block_device
*bdev
, fmode_t mode
,
183 unsigned int cmd
, struct compat_blkpg_ioctl_arg __user
*ua32
)
185 struct blkpg_ioctl_arg __user
*a
= compat_alloc_user_space(sizeof(*a
));
186 compat_caddr_t udata
;
190 err
= get_user(n
, &ua32
->op
);
191 err
|= put_user(n
, &a
->op
);
192 err
|= get_user(n
, &ua32
->flags
);
193 err
|= put_user(n
, &a
->flags
);
194 err
|= get_user(n
, &ua32
->datalen
);
195 err
|= put_user(n
, &a
->datalen
);
196 err
|= get_user(udata
, &ua32
->data
);
197 err
|= put_user(compat_ptr(udata
), &a
->data
);
201 return blkdev_ioctl(bdev
, mode
, cmd
, (unsigned long)a
);
204 #define BLKBSZGET_32 _IOR(0x12, 112, int)
205 #define BLKBSZSET_32 _IOW(0x12, 113, int)
206 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
208 static int compat_blkdev_driver_ioctl(struct block_device
*bdev
, fmode_t mode
,
209 unsigned cmd
, unsigned long arg
)
212 case HDIO_GET_UNMASKINTR
:
213 case HDIO_GET_MULTCOUNT
:
214 case HDIO_GET_KEEPSETTINGS
:
216 case HDIO_GET_NOWERR
:
219 case HDIO_GET_WCACHE
:
220 case HDIO_GET_ACOUSTIC
:
221 case HDIO_GET_ADDRESS
:
222 case HDIO_GET_BUSSTATE
:
223 return compat_hdio_ioctl(bdev
, mode
, cmd
, arg
);
225 return compat_cdrom_read_audio(bdev
, mode
, cmd
, arg
);
226 case CDROM_SEND_PACKET
:
227 return compat_cdrom_generic_command(bdev
, mode
, cmd
, arg
);
230 * No handler required for the ones below, we just need to
231 * convert arg to a 64 bit pointer.
235 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
236 * Some need translations, these do not.
238 case HDIO_GET_IDENTITY
:
239 case HDIO_DRIVE_TASK
:
241 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
247 case CDROMPLAYTRKIND
:
248 case CDROMREADTOCHDR
:
249 case CDROMREADTOCENTRY
:
255 case CDROMMULTISESSION
:
262 case CDROM_DISC_STATUS
:
263 case CDROM_CHANGER_NSLOTS
:
264 case CDROM_GET_CAPABILITY
:
265 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
266 * not take a struct cdrom_read, instead they take a struct cdrom_msf
267 * which is compatible.
272 case CDROMREADCOOKED
:
275 case DVD_READ_STRUCT
:
276 case DVD_WRITE_STRUCT
:
278 arg
= (unsigned long)compat_ptr(arg
);
279 /* These intepret arg as an unsigned long, not as a pointer,
280 * so we must not do compat_ptr() conversion. */
281 case HDIO_SET_MULTCOUNT
:
282 case HDIO_SET_UNMASKINTR
:
283 case HDIO_SET_KEEPSETTINGS
:
285 case HDIO_SET_NOWERR
:
287 case HDIO_SET_PIO_MODE
:
289 case HDIO_SET_WCACHE
:
290 case HDIO_SET_ACOUSTIC
:
291 case HDIO_SET_BUSSTATE
:
292 case HDIO_SET_ADDRESS
:
294 case CDROM_SET_OPTIONS
:
295 case CDROM_CLEAR_OPTIONS
:
296 case CDROM_SELECT_SPEED
:
297 case CDROM_SELECT_DISC
:
298 case CDROM_MEDIA_CHANGED
:
299 case CDROM_DRIVE_STATUS
:
304 /* unknown ioctl number */
308 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
311 /* Most of the generic ioctls are handled in the normal fallback path.
312 This assumes the blkdev's low level compat_ioctl always returns
313 ENOIOCTLCMD for unknown ioctls. */
314 long compat_blkdev_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
316 int ret
= -ENOIOCTLCMD
;
317 struct inode
*inode
= file
->f_mapping
->host
;
318 struct block_device
*bdev
= inode
->i_bdev
;
319 struct gendisk
*disk
= bdev
->bd_disk
;
320 fmode_t mode
= file
->f_mode
;
322 unsigned int max_sectors
;
325 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
326 * to updated it before every ioctl.
328 if (file
->f_flags
& O_NDELAY
)
329 mode
|= FMODE_NDELAY
;
331 mode
&= ~FMODE_NDELAY
;
335 return compat_hdio_getgeo(disk
, bdev
, compat_ptr(arg
));
337 return compat_put_uint(arg
, bdev_physical_block_size(bdev
));
339 return compat_put_uint(arg
, bdev_io_min(bdev
));
341 return compat_put_uint(arg
, bdev_io_opt(bdev
));
343 return compat_put_int(arg
, bdev_alignment_offset(bdev
));
344 case BLKDISCARDZEROES
:
345 return compat_put_uint(arg
, 0);
352 * the ones below are implemented in blkdev_locked_ioctl,
353 * but we call blkdev_ioctl, which gets the lock for us
356 return blkdev_ioctl(bdev
, mode
, cmd
,
357 (unsigned long)compat_ptr(arg
));
359 return blkdev_ioctl(bdev
, mode
, BLKBSZSET
,
360 (unsigned long)compat_ptr(arg
));
362 return compat_blkpg_ioctl(bdev
, mode
, cmd
, compat_ptr(arg
));
367 return compat_put_long(arg
,
368 (bdev
->bd_bdi
->ra_pages
* PAGE_SIZE
) / 512);
369 case BLKROGET
: /* compatible */
370 return compat_put_int(arg
, bdev_read_only(bdev
) != 0);
371 case BLKBSZGET_32
: /* get the logical block size (cf. BLKSSZGET) */
372 return compat_put_int(arg
, block_size(bdev
));
373 case BLKSSZGET
: /* get block device hardware sector size */
374 return compat_put_int(arg
, bdev_logical_block_size(bdev
));
376 max_sectors
= min_t(unsigned int, USHRT_MAX
,
377 queue_max_sectors(bdev_get_queue(bdev
)));
378 return compat_put_ushort(arg
, max_sectors
);
380 return compat_put_ushort(arg
,
381 !blk_queue_nonrot(bdev_get_queue(bdev
)));
382 case BLKRASET
: /* compatible, but no compat_ptr (!) */
384 if (!capable(CAP_SYS_ADMIN
))
386 bdev
->bd_bdi
->ra_pages
= (arg
* 512) / PAGE_SIZE
;
389 size
= i_size_read(bdev
->bd_inode
);
390 if ((size
>> 9) > ~0UL)
392 return compat_put_ulong(arg
, size
>> 9);
394 case BLKGETSIZE64_32
:
395 return compat_put_u64(arg
, i_size_read(bdev
->bd_inode
));
397 case BLKTRACESETUP32
:
398 case BLKTRACESTART
: /* compatible */
399 case BLKTRACESTOP
: /* compatible */
400 case BLKTRACETEARDOWN
: /* compatible */
401 ret
= blk_trace_ioctl(bdev
, cmd
, compat_ptr(arg
));
404 if (disk
->fops
->compat_ioctl
)
405 ret
= disk
->fops
->compat_ioctl(bdev
, mode
, cmd
, arg
);
406 if (ret
== -ENOIOCTLCMD
)
407 ret
= compat_blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);