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>
10 #include <linux/slab.h>
11 #include <linux/syscalls.h>
12 #include <linux/types.h>
13 #include <linux/uaccess.h>
15 static int compat_put_ushort(unsigned long arg
, unsigned short val
)
17 return put_user(val
, (unsigned short __user
*)compat_ptr(arg
));
20 static int compat_put_int(unsigned long arg
, int val
)
22 return put_user(val
, (compat_int_t __user
*)compat_ptr(arg
));
25 static int compat_put_uint(unsigned long arg
, unsigned int val
)
27 return put_user(val
, (compat_uint_t __user
*)compat_ptr(arg
));
30 static int compat_put_long(unsigned long arg
, long val
)
32 return put_user(val
, (compat_long_t __user
*)compat_ptr(arg
));
35 static int compat_put_ulong(unsigned long arg
, compat_ulong_t val
)
37 return put_user(val
, (compat_ulong_t __user
*)compat_ptr(arg
));
40 static int compat_put_u64(unsigned long arg
, u64 val
)
42 return put_user(val
, (compat_u64 __user
*)compat_ptr(arg
));
45 struct compat_hd_geometry
{
47 unsigned char sectors
;
48 unsigned short cylinders
;
52 static int compat_hdio_getgeo(struct gendisk
*disk
, struct block_device
*bdev
,
53 struct compat_hd_geometry __user
*ugeo
)
55 struct hd_geometry geo
;
60 if (!disk
->fops
->getgeo
)
63 memset(&geo
, 0, sizeof(geo
));
65 * We need to set the startsect first, the driver may
66 * want to override it.
68 geo
.start
= get_start_sect(bdev
);
69 ret
= disk
->fops
->getgeo(bdev
, &geo
);
73 ret
= copy_to_user(ugeo
, &geo
, 4);
74 ret
|= put_user(geo
.start
, &ugeo
->start
);
81 static int compat_hdio_ioctl(struct block_device
*bdev
, fmode_t mode
,
82 unsigned int cmd
, unsigned long arg
)
84 unsigned long __user
*p
;
87 p
= compat_alloc_user_space(sizeof(unsigned long));
88 error
= __blkdev_driver_ioctl(bdev
, mode
,
89 cmd
, (unsigned long)p
);
91 unsigned int __user
*uvp
= compat_ptr(arg
);
93 if (get_user(v
, p
) || put_user(v
, uvp
))
99 struct compat_cdrom_read_audio
{
100 union cdrom_addr addr
;
102 compat_int_t nframes
;
106 struct compat_cdrom_generic_command
{
107 unsigned char cmd
[CDROM_PACKET_SIZE
];
108 compat_caddr_t buffer
;
109 compat_uint_t buflen
;
111 compat_caddr_t sense
;
112 unsigned char data_direction
;
114 compat_int_t timeout
;
115 compat_caddr_t reserved
[1];
118 static int compat_cdrom_read_audio(struct block_device
*bdev
, fmode_t mode
,
119 unsigned int cmd
, unsigned long arg
)
121 struct cdrom_read_audio __user
*cdread_audio
;
122 struct compat_cdrom_read_audio __user
*cdread_audio32
;
126 cdread_audio
= compat_alloc_user_space(sizeof(*cdread_audio
));
127 cdread_audio32
= compat_ptr(arg
);
129 if (copy_in_user(&cdread_audio
->addr
,
130 &cdread_audio32
->addr
,
131 (sizeof(*cdread_audio32
) -
132 sizeof(compat_caddr_t
))))
135 if (get_user(data
, &cdread_audio32
->buf
))
137 datap
= compat_ptr(data
);
138 if (put_user(datap
, &cdread_audio
->buf
))
141 return __blkdev_driver_ioctl(bdev
, mode
, cmd
,
142 (unsigned long)cdread_audio
);
145 static int compat_cdrom_generic_command(struct block_device
*bdev
, fmode_t mode
,
146 unsigned int cmd
, unsigned long arg
)
148 struct cdrom_generic_command __user
*cgc
;
149 struct compat_cdrom_generic_command __user
*cgc32
;
154 cgc
= compat_alloc_user_space(sizeof(*cgc
));
155 cgc32
= compat_ptr(arg
);
157 if (copy_in_user(&cgc
->cmd
, &cgc32
->cmd
, sizeof(cgc
->cmd
)) ||
158 get_user(data
, &cgc32
->buffer
) ||
159 put_user(compat_ptr(data
), &cgc
->buffer
) ||
160 copy_in_user(&cgc
->buflen
, &cgc32
->buflen
,
161 (sizeof(unsigned int) + sizeof(int))) ||
162 get_user(data
, &cgc32
->sense
) ||
163 put_user(compat_ptr(data
), &cgc
->sense
) ||
164 get_user(dir
, &cgc32
->data_direction
) ||
165 put_user(dir
, &cgc
->data_direction
) ||
166 get_user(itmp
, &cgc32
->quiet
) ||
167 put_user(itmp
, &cgc
->quiet
) ||
168 get_user(itmp
, &cgc32
->timeout
) ||
169 put_user(itmp
, &cgc
->timeout
) ||
170 get_user(data
, &cgc32
->reserved
[0]) ||
171 put_user(compat_ptr(data
), &cgc
->reserved
[0]))
174 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, (unsigned long)cgc
);
177 struct compat_blkpg_ioctl_arg
{
180 compat_int_t datalen
;
184 static int compat_blkpg_ioctl(struct block_device
*bdev
, fmode_t mode
,
185 unsigned int cmd
, struct compat_blkpg_ioctl_arg __user
*ua32
)
187 struct blkpg_ioctl_arg __user
*a
= compat_alloc_user_space(sizeof(*a
));
188 compat_caddr_t udata
;
192 err
= get_user(n
, &ua32
->op
);
193 err
|= put_user(n
, &a
->op
);
194 err
|= get_user(n
, &ua32
->flags
);
195 err
|= put_user(n
, &a
->flags
);
196 err
|= get_user(n
, &ua32
->datalen
);
197 err
|= put_user(n
, &a
->datalen
);
198 err
|= get_user(udata
, &ua32
->data
);
199 err
|= put_user(compat_ptr(udata
), &a
->data
);
203 return blkdev_ioctl(bdev
, mode
, cmd
, (unsigned long)a
);
206 #define BLKBSZGET_32 _IOR(0x12, 112, int)
207 #define BLKBSZSET_32 _IOW(0x12, 113, int)
208 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
210 static int compat_blkdev_driver_ioctl(struct block_device
*bdev
, fmode_t mode
,
211 unsigned cmd
, unsigned long arg
)
214 case HDIO_GET_UNMASKINTR
:
215 case HDIO_GET_MULTCOUNT
:
216 case HDIO_GET_KEEPSETTINGS
:
218 case HDIO_GET_NOWERR
:
221 case HDIO_GET_WCACHE
:
222 case HDIO_GET_ACOUSTIC
:
223 case HDIO_GET_ADDRESS
:
224 case HDIO_GET_BUSSTATE
:
225 return compat_hdio_ioctl(bdev
, mode
, cmd
, arg
);
227 return compat_cdrom_read_audio(bdev
, mode
, cmd
, arg
);
228 case CDROM_SEND_PACKET
:
229 return compat_cdrom_generic_command(bdev
, mode
, cmd
, arg
);
232 * No handler required for the ones below, we just need to
233 * convert arg to a 64 bit pointer.
237 * 0x03 -- HD/IDE ioctl's used by hdparm and friends.
238 * Some need translations, these do not.
240 case HDIO_GET_IDENTITY
:
241 case HDIO_DRIVE_TASK
:
243 /* 0x330 is reserved -- it used to be HDIO_GETGEO_BIG */
249 case CDROMPLAYTRKIND
:
250 case CDROMREADTOCHDR
:
251 case CDROMREADTOCENTRY
:
257 case CDROMMULTISESSION
:
264 case CDROM_DISC_STATUS
:
265 case CDROM_CHANGER_NSLOTS
:
266 case CDROM_GET_CAPABILITY
:
267 /* Ignore cdrom.h about these next 5 ioctls, they absolutely do
268 * not take a struct cdrom_read, instead they take a struct cdrom_msf
269 * which is compatible.
274 case CDROMREADCOOKED
:
277 case DVD_READ_STRUCT
:
278 case DVD_WRITE_STRUCT
:
280 arg
= (unsigned long)compat_ptr(arg
);
281 /* These intepret arg as an unsigned long, not as a pointer,
282 * so we must not do compat_ptr() conversion. */
283 case HDIO_SET_MULTCOUNT
:
284 case HDIO_SET_UNMASKINTR
:
285 case HDIO_SET_KEEPSETTINGS
:
287 case HDIO_SET_NOWERR
:
289 case HDIO_SET_PIO_MODE
:
291 case HDIO_SET_WCACHE
:
292 case HDIO_SET_ACOUSTIC
:
293 case HDIO_SET_BUSSTATE
:
294 case HDIO_SET_ADDRESS
:
296 case CDROM_SET_OPTIONS
:
297 case CDROM_CLEAR_OPTIONS
:
298 case CDROM_SELECT_SPEED
:
299 case CDROM_SELECT_DISC
:
300 case CDROM_MEDIA_CHANGED
:
301 case CDROM_DRIVE_STATUS
:
306 /* unknown ioctl number */
310 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
313 /* Most of the generic ioctls are handled in the normal fallback path.
314 This assumes the blkdev's low level compat_ioctl always returns
315 ENOIOCTLCMD for unknown ioctls. */
316 long compat_blkdev_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
318 int ret
= -ENOIOCTLCMD
;
319 struct inode
*inode
= file
->f_mapping
->host
;
320 struct block_device
*bdev
= inode
->i_bdev
;
321 struct gendisk
*disk
= bdev
->bd_disk
;
322 fmode_t mode
= file
->f_mode
;
324 unsigned int max_sectors
;
327 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
328 * to updated it before every ioctl.
330 if (file
->f_flags
& O_NDELAY
)
331 mode
|= FMODE_NDELAY
;
333 mode
&= ~FMODE_NDELAY
;
337 return compat_hdio_getgeo(disk
, bdev
, compat_ptr(arg
));
339 return compat_put_uint(arg
, bdev_physical_block_size(bdev
));
341 return compat_put_uint(arg
, bdev_io_min(bdev
));
343 return compat_put_uint(arg
, bdev_io_opt(bdev
));
345 return compat_put_int(arg
, bdev_alignment_offset(bdev
));
346 case BLKDISCARDZEROES
:
347 return compat_put_uint(arg
, 0);
354 * the ones below are implemented in blkdev_locked_ioctl,
355 * 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 return compat_put_long(arg
,
372 (bdev
->bd_bdi
->ra_pages
* PAGE_SIZE
) / 512);
373 case BLKROGET
: /* compatible */
374 return compat_put_int(arg
, bdev_read_only(bdev
) != 0);
375 case BLKBSZGET_32
: /* get the logical block size (cf. BLKSSZGET) */
376 return compat_put_int(arg
, block_size(bdev
));
377 case BLKSSZGET
: /* get block device hardware sector size */
378 return compat_put_int(arg
, bdev_logical_block_size(bdev
));
380 max_sectors
= min_t(unsigned int, USHRT_MAX
,
381 queue_max_sectors(bdev_get_queue(bdev
)));
382 return compat_put_ushort(arg
, max_sectors
);
384 return compat_put_ushort(arg
,
385 !blk_queue_nonrot(bdev_get_queue(bdev
)));
386 case BLKRASET
: /* compatible, but no compat_ptr (!) */
388 if (!capable(CAP_SYS_ADMIN
))
390 bdev
->bd_bdi
->ra_pages
= (arg
* 512) / PAGE_SIZE
;
393 size
= i_size_read(bdev
->bd_inode
);
394 if ((size
>> 9) > ~0UL)
396 return compat_put_ulong(arg
, size
>> 9);
398 case BLKGETSIZE64_32
:
399 return compat_put_u64(arg
, i_size_read(bdev
->bd_inode
));
401 case BLKTRACESETUP32
:
402 case BLKTRACESTART
: /* compatible */
403 case BLKTRACESTOP
: /* compatible */
404 case BLKTRACETEARDOWN
: /* compatible */
405 ret
= blk_trace_ioctl(bdev
, cmd
, compat_ptr(arg
));
407 case IOC_PR_REGISTER
:
411 case IOC_PR_PREEMPT_ABORT
:
413 return blkdev_ioctl(bdev
, mode
, cmd
,
414 (unsigned long)compat_ptr(arg
));
416 if (disk
->fops
->compat_ioctl
)
417 ret
= disk
->fops
->compat_ioctl(bdev
, mode
, cmd
, arg
);
418 if (ret
== -ENOIOCTLCMD
)
419 ret
= compat_blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);