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>
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 static int compat_blkdev_driver_ioctl(struct block_device
*bdev
, fmode_t mode
,
213 unsigned cmd
, unsigned long arg
)
216 case HDIO_GET_UNMASKINTR
:
217 case HDIO_GET_MULTCOUNT
:
218 case HDIO_GET_KEEPSETTINGS
:
220 case HDIO_GET_NOWERR
:
223 case HDIO_GET_WCACHE
:
224 case HDIO_GET_ACOUSTIC
:
225 case HDIO_GET_ADDRESS
:
226 case HDIO_GET_BUSSTATE
:
227 return compat_hdio_ioctl(bdev
, mode
, cmd
, arg
);
229 return compat_cdrom_read_audio(bdev
, mode
, cmd
, arg
);
230 case CDROM_SEND_PACKET
:
231 return compat_cdrom_generic_command(bdev
, mode
, cmd
, arg
);
234 * No handler required for the ones below, we just need to
235 * convert arg to a 64 bit pointer.
239 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
240 * Some need translations, these do not.
242 case HDIO_GET_IDENTITY
:
243 case HDIO_DRIVE_TASK
:
245 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
251 case CDROMPLAYTRKIND
:
252 case CDROMREADTOCHDR
:
253 case CDROMREADTOCENTRY
:
259 case CDROMMULTISESSION
:
266 case CDROM_DISC_STATUS
:
267 case CDROM_CHANGER_NSLOTS
:
268 case CDROM_GET_CAPABILITY
:
269 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
270 * not take a struct cdrom_read, instead they take a struct cdrom_msf
271 * which is compatible.
276 case CDROMREADCOOKED
:
279 case DVD_READ_STRUCT
:
280 case DVD_WRITE_STRUCT
:
282 arg
= (unsigned long)compat_ptr(arg
);
283 /* These intepret arg as an unsigned long, not as a pointer,
284 * so we must not do compat_ptr() conversion. */
285 case HDIO_SET_MULTCOUNT
:
286 case HDIO_SET_UNMASKINTR
:
287 case HDIO_SET_KEEPSETTINGS
:
289 case HDIO_SET_NOWERR
:
291 case HDIO_SET_PIO_MODE
:
293 case HDIO_SET_WCACHE
:
294 case HDIO_SET_ACOUSTIC
:
295 case HDIO_SET_BUSSTATE
:
296 case HDIO_SET_ADDRESS
:
298 case CDROM_SET_OPTIONS
:
299 case CDROM_CLEAR_OPTIONS
:
300 case CDROM_SELECT_SPEED
:
301 case CDROM_SELECT_DISC
:
302 case CDROM_MEDIA_CHANGED
:
303 case CDROM_DRIVE_STATUS
:
308 /* unknown ioctl number */
312 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
315 /* Most of the generic ioctls are handled in the normal fallback path.
316 This assumes the blkdev's low level compat_ioctl always returns
317 ENOIOCTLCMD for unknown ioctls. */
318 long compat_blkdev_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
320 int ret
= -ENOIOCTLCMD
;
321 struct inode
*inode
= file
->f_mapping
->host
;
322 struct block_device
*bdev
= inode
->i_bdev
;
323 struct gendisk
*disk
= bdev
->bd_disk
;
324 fmode_t mode
= file
->f_mode
;
325 struct backing_dev_info
*bdi
;
327 unsigned int max_sectors
;
330 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
331 * to updated it before every ioctl.
333 if (file
->f_flags
& O_NDELAY
)
334 mode
|= FMODE_NDELAY
;
336 mode
&= ~FMODE_NDELAY
;
340 return compat_hdio_getgeo(disk
, bdev
, compat_ptr(arg
));
342 return compat_put_uint(arg
, bdev_physical_block_size(bdev
));
344 return compat_put_uint(arg
, bdev_io_min(bdev
));
346 return compat_put_uint(arg
, bdev_io_opt(bdev
));
348 return compat_put_int(arg
, bdev_alignment_offset(bdev
));
349 case BLKDISCARDZEROES
:
350 return compat_put_uint(arg
, bdev_discard_zeroes_data(bdev
));
357 * the ones below are implemented in blkdev_locked_ioctl,
358 * but we call blkdev_ioctl, which gets the lock for us
361 return blkdev_ioctl(bdev
, mode
, cmd
,
362 (unsigned long)compat_ptr(arg
));
364 return blkdev_ioctl(bdev
, mode
, BLKBSZSET
,
365 (unsigned long)compat_ptr(arg
));
367 return compat_blkpg_ioctl(bdev
, mode
, cmd
, compat_ptr(arg
));
372 bdi
= blk_get_backing_dev_info(bdev
);
373 return compat_put_long(arg
,
374 (bdi
->ra_pages
* PAGE_CACHE_SIZE
) / 512);
375 case BLKROGET
: /* compatible */
376 return compat_put_int(arg
, bdev_read_only(bdev
) != 0);
377 case BLKBSZGET_32
: /* get the logical block size (cf. BLKSSZGET) */
378 return compat_put_int(arg
, block_size(bdev
));
379 case BLKSSZGET
: /* get block device hardware sector size */
380 return compat_put_int(arg
, bdev_logical_block_size(bdev
));
382 max_sectors
= min_t(unsigned int, USHRT_MAX
,
383 queue_max_sectors(bdev_get_queue(bdev
)));
384 return compat_put_ushort(arg
, max_sectors
);
386 return compat_put_ushort(arg
,
387 !blk_queue_nonrot(bdev_get_queue(bdev
)));
388 case BLKRASET
: /* compatible, but no compat_ptr (!) */
390 if (!capable(CAP_SYS_ADMIN
))
392 bdi
= blk_get_backing_dev_info(bdev
);
393 bdi
->ra_pages
= (arg
* 512) / PAGE_CACHE_SIZE
;
396 size
= i_size_read(bdev
->bd_inode
);
397 if ((size
>> 9) > ~0UL)
399 return compat_put_ulong(arg
, size
>> 9);
401 case BLKGETSIZE64_32
:
402 return compat_put_u64(arg
, i_size_read(bdev
->bd_inode
));
404 case BLKTRACESETUP32
:
405 case BLKTRACESTART
: /* compatible */
406 case BLKTRACESTOP
: /* compatible */
407 case BLKTRACETEARDOWN
: /* compatible */
408 ret
= blk_trace_ioctl(bdev
, cmd
, compat_ptr(arg
));
410 case IOC_PR_REGISTER
:
414 case IOC_PR_PREEMPT_ABORT
:
416 return blkdev_ioctl(bdev
, mode
, cmd
,
417 (unsigned long)compat_ptr(arg
));
419 if (disk
->fops
->compat_ioctl
)
420 ret
= disk
->fops
->compat_ioctl(bdev
, mode
, cmd
, arg
);
421 if (ret
== -ENOIOCTLCMD
)
422 ret
= compat_blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);