1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/blkdev.h>
3 #include <linux/blkpg.h>
4 #include <linux/blktrace_api.h>
5 #include <linux/cdrom.h>
6 #include <linux/compat.h>
7 #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 unsigned long __user
*p
;
86 p
= compat_alloc_user_space(sizeof(unsigned long));
87 error
= __blkdev_driver_ioctl(bdev
, mode
,
88 cmd
, (unsigned long)p
);
90 unsigned int __user
*uvp
= compat_ptr(arg
);
92 if (get_user(v
, p
) || put_user(v
, uvp
))
98 struct compat_cdrom_read_audio
{
99 union cdrom_addr addr
;
101 compat_int_t nframes
;
105 struct compat_cdrom_generic_command
{
106 unsigned char cmd
[CDROM_PACKET_SIZE
];
107 compat_caddr_t buffer
;
108 compat_uint_t buflen
;
110 compat_caddr_t sense
;
111 unsigned char data_direction
;
113 compat_int_t timeout
;
114 compat_caddr_t reserved
[1];
117 static int compat_cdrom_read_audio(struct block_device
*bdev
, fmode_t mode
,
118 unsigned int cmd
, unsigned long arg
)
120 struct cdrom_read_audio __user
*cdread_audio
;
121 struct compat_cdrom_read_audio __user
*cdread_audio32
;
125 cdread_audio
= compat_alloc_user_space(sizeof(*cdread_audio
));
126 cdread_audio32
= compat_ptr(arg
);
128 if (copy_in_user(&cdread_audio
->addr
,
129 &cdread_audio32
->addr
,
130 (sizeof(*cdread_audio32
) -
131 sizeof(compat_caddr_t
))))
134 if (get_user(data
, &cdread_audio32
->buf
))
136 datap
= compat_ptr(data
);
137 if (put_user(datap
, &cdread_audio
->buf
))
140 return __blkdev_driver_ioctl(bdev
, mode
, cmd
,
141 (unsigned long)cdread_audio
);
144 static int compat_cdrom_generic_command(struct block_device
*bdev
, fmode_t mode
,
145 unsigned int cmd
, unsigned long arg
)
147 struct cdrom_generic_command __user
*cgc
;
148 struct compat_cdrom_generic_command __user
*cgc32
;
153 cgc
= compat_alloc_user_space(sizeof(*cgc
));
154 cgc32
= compat_ptr(arg
);
156 if (copy_in_user(&cgc
->cmd
, &cgc32
->cmd
, sizeof(cgc
->cmd
)) ||
157 get_user(data
, &cgc32
->buffer
) ||
158 put_user(compat_ptr(data
), &cgc
->buffer
) ||
159 copy_in_user(&cgc
->buflen
, &cgc32
->buflen
,
160 (sizeof(unsigned int) + sizeof(int))) ||
161 get_user(data
, &cgc32
->sense
) ||
162 put_user(compat_ptr(data
), &cgc
->sense
) ||
163 get_user(dir
, &cgc32
->data_direction
) ||
164 put_user(dir
, &cgc
->data_direction
) ||
165 get_user(itmp
, &cgc32
->quiet
) ||
166 put_user(itmp
, &cgc
->quiet
) ||
167 get_user(itmp
, &cgc32
->timeout
) ||
168 put_user(itmp
, &cgc
->timeout
) ||
169 get_user(data
, &cgc32
->reserved
[0]) ||
170 put_user(compat_ptr(data
), &cgc
->reserved
[0]))
173 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, (unsigned long)cgc
);
176 struct compat_blkpg_ioctl_arg
{
179 compat_int_t datalen
;
183 static int compat_blkpg_ioctl(struct block_device
*bdev
, fmode_t mode
,
184 unsigned int cmd
, struct compat_blkpg_ioctl_arg __user
*ua32
)
186 struct blkpg_ioctl_arg __user
*a
= compat_alloc_user_space(sizeof(*a
));
187 compat_caddr_t udata
;
191 err
= get_user(n
, &ua32
->op
);
192 err
|= put_user(n
, &a
->op
);
193 err
|= get_user(n
, &ua32
->flags
);
194 err
|= put_user(n
, &a
->flags
);
195 err
|= get_user(n
, &ua32
->datalen
);
196 err
|= put_user(n
, &a
->datalen
);
197 err
|= get_user(udata
, &ua32
->data
);
198 err
|= put_user(compat_ptr(udata
), &a
->data
);
202 return blkdev_ioctl(bdev
, mode
, cmd
, (unsigned long)a
);
205 #define BLKBSZGET_32 _IOR(0x12, 112, int)
206 #define BLKBSZSET_32 _IOW(0x12, 113, int)
207 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
209 static int compat_blkdev_driver_ioctl(struct block_device
*bdev
, fmode_t mode
,
210 unsigned cmd
, unsigned long arg
)
213 case HDIO_GET_UNMASKINTR
:
214 case HDIO_GET_MULTCOUNT
:
215 case HDIO_GET_KEEPSETTINGS
:
217 case HDIO_GET_NOWERR
:
220 case HDIO_GET_WCACHE
:
221 case HDIO_GET_ACOUSTIC
:
222 case HDIO_GET_ADDRESS
:
223 case HDIO_GET_BUSSTATE
:
224 return compat_hdio_ioctl(bdev
, mode
, cmd
, arg
);
226 return compat_cdrom_read_audio(bdev
, mode
, cmd
, arg
);
227 case CDROM_SEND_PACKET
:
228 return compat_cdrom_generic_command(bdev
, mode
, cmd
, arg
);
231 * No handler required for the ones below, we just need to
232 * convert arg to a 64 bit pointer.
236 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
237 * Some need translations, these do not.
239 case HDIO_GET_IDENTITY
:
240 case HDIO_DRIVE_TASK
:
242 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
248 case CDROMPLAYTRKIND
:
249 case CDROMREADTOCHDR
:
250 case CDROMREADTOCENTRY
:
256 case CDROMMULTISESSION
:
263 case CDROM_DISC_STATUS
:
264 case CDROM_CHANGER_NSLOTS
:
265 case CDROM_GET_CAPABILITY
:
266 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
267 * not take a struct cdrom_read, instead they take a struct cdrom_msf
268 * which is compatible.
273 case CDROMREADCOOKED
:
276 case DVD_READ_STRUCT
:
277 case DVD_WRITE_STRUCT
:
279 arg
= (unsigned long)compat_ptr(arg
);
280 /* These intepret arg as an unsigned long, not as a pointer,
281 * so we must not do compat_ptr() conversion. */
282 case HDIO_SET_MULTCOUNT
:
283 case HDIO_SET_UNMASKINTR
:
284 case HDIO_SET_KEEPSETTINGS
:
286 case HDIO_SET_NOWERR
:
288 case HDIO_SET_PIO_MODE
:
290 case HDIO_SET_WCACHE
:
291 case HDIO_SET_ACOUSTIC
:
292 case HDIO_SET_BUSSTATE
:
293 case HDIO_SET_ADDRESS
:
295 case CDROM_SET_OPTIONS
:
296 case CDROM_CLEAR_OPTIONS
:
297 case CDROM_SELECT_SPEED
:
298 case CDROM_SELECT_DISC
:
299 case CDROM_MEDIA_CHANGED
:
300 case CDROM_DRIVE_STATUS
:
305 /* unknown ioctl number */
309 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
312 /* Most of the generic ioctls are handled in the normal fallback path.
313 This assumes the blkdev's low level compat_ioctl always returns
314 ENOIOCTLCMD for unknown ioctls. */
315 long compat_blkdev_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
317 int ret
= -ENOIOCTLCMD
;
318 struct inode
*inode
= file
->f_mapping
->host
;
319 struct block_device
*bdev
= inode
->i_bdev
;
320 struct gendisk
*disk
= bdev
->bd_disk
;
321 fmode_t mode
= file
->f_mode
;
323 unsigned int max_sectors
;
326 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
327 * to updated it before every ioctl.
329 if (file
->f_flags
& O_NDELAY
)
330 mode
|= FMODE_NDELAY
;
332 mode
&= ~FMODE_NDELAY
;
336 return compat_hdio_getgeo(disk
, bdev
, compat_ptr(arg
));
338 return compat_put_uint(arg
, bdev_physical_block_size(bdev
));
340 return compat_put_uint(arg
, bdev_io_min(bdev
));
342 return compat_put_uint(arg
, bdev_io_opt(bdev
));
344 return compat_put_int(arg
, bdev_alignment_offset(bdev
));
345 case BLKDISCARDZEROES
:
346 return compat_put_uint(arg
, 0);
353 * the ones below are implemented in blkdev_locked_ioctl,
354 * but we call blkdev_ioctl, which gets the lock for us
357 return blkdev_ioctl(bdev
, mode
, cmd
,
358 (unsigned long)compat_ptr(arg
));
360 return blkdev_ioctl(bdev
, mode
, BLKBSZSET
,
361 (unsigned long)compat_ptr(arg
));
363 return compat_blkpg_ioctl(bdev
, mode
, cmd
, compat_ptr(arg
));
368 return compat_put_long(arg
,
369 (bdev
->bd_bdi
->ra_pages
* PAGE_SIZE
) / 512);
370 case BLKROGET
: /* compatible */
371 return compat_put_int(arg
, bdev_read_only(bdev
) != 0);
372 case BLKBSZGET_32
: /* get the logical block size (cf. BLKSSZGET) */
373 return compat_put_int(arg
, block_size(bdev
));
374 case BLKSSZGET
: /* get block device hardware sector size */
375 return compat_put_int(arg
, bdev_logical_block_size(bdev
));
377 max_sectors
= min_t(unsigned int, USHRT_MAX
,
378 queue_max_sectors(bdev_get_queue(bdev
)));
379 return compat_put_ushort(arg
, max_sectors
);
381 return compat_put_ushort(arg
,
382 !blk_queue_nonrot(bdev_get_queue(bdev
)));
383 case BLKRASET
: /* compatible, but no compat_ptr (!) */
385 if (!capable(CAP_SYS_ADMIN
))
387 bdev
->bd_bdi
->ra_pages
= (arg
* 512) / PAGE_SIZE
;
390 size
= i_size_read(bdev
->bd_inode
);
391 if ((size
>> 9) > ~0UL)
393 return compat_put_ulong(arg
, size
>> 9);
395 case BLKGETSIZE64_32
:
396 return compat_put_u64(arg
, i_size_read(bdev
->bd_inode
));
398 case BLKTRACESETUP32
:
399 case BLKTRACESTART
: /* compatible */
400 case BLKTRACESTOP
: /* compatible */
401 case BLKTRACETEARDOWN
: /* compatible */
402 ret
= blk_trace_ioctl(bdev
, cmd
, compat_ptr(arg
));
405 if (disk
->fops
->compat_ioctl
)
406 ret
= disk
->fops
->compat_ioctl(bdev
, mode
, cmd
, arg
);
407 if (ret
== -ENOIOCTLCMD
)
408 ret
= compat_blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);