1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/capability.h>
3 #include <linux/compat.h>
4 #include <linux/blkdev.h>
5 #include <linux/export.h>
7 #include <linux/blkpg.h>
8 #include <linux/hdreg.h>
9 #include <linux/backing-dev.h>
11 #include <linux/blktrace_api.h>
13 #include <linux/uaccess.h>
16 static int blkpg_do_ioctl(struct block_device
*bdev
,
17 struct blkpg_partition __user
*upart
, int op
)
19 struct blkpg_partition p
;
20 long long start
, length
;
22 if (!capable(CAP_SYS_ADMIN
))
24 if (copy_from_user(&p
, upart
, sizeof(struct blkpg_partition
)))
26 if (bdev_is_partition(bdev
))
32 if (op
== BLKPG_DEL_PARTITION
)
33 return bdev_del_partition(bdev
, p
.pno
);
35 start
= p
.start
>> SECTOR_SHIFT
;
36 length
= p
.length
>> SECTOR_SHIFT
;
39 case BLKPG_ADD_PARTITION
:
40 /* check if partition is aligned to blocksize */
41 if (p
.start
& (bdev_logical_block_size(bdev
) - 1))
43 return bdev_add_partition(bdev
, p
.pno
, start
, length
);
44 case BLKPG_RESIZE_PARTITION
:
45 return bdev_resize_partition(bdev
, p
.pno
, start
, length
);
51 static int blkpg_ioctl(struct block_device
*bdev
,
52 struct blkpg_ioctl_arg __user
*arg
)
54 struct blkpg_partition __user
*udata
;
57 if (get_user(op
, &arg
->op
) || get_user(udata
, &arg
->data
))
60 return blkpg_do_ioctl(bdev
, udata
, op
);
64 struct compat_blkpg_ioctl_arg
{
71 static int compat_blkpg_ioctl(struct block_device
*bdev
,
72 struct compat_blkpg_ioctl_arg __user
*arg
)
77 if (get_user(op
, &arg
->op
) || get_user(udata
, &arg
->data
))
80 return blkpg_do_ioctl(bdev
, compat_ptr(udata
), op
);
84 static int blkdev_reread_part(struct block_device
*bdev
)
88 if (!disk_part_scan_enabled(bdev
->bd_disk
) || bdev_is_partition(bdev
))
90 if (!capable(CAP_SYS_ADMIN
))
93 mutex_lock(&bdev
->bd_mutex
);
94 ret
= bdev_disk_changed(bdev
, false);
95 mutex_unlock(&bdev
->bd_mutex
);
100 static int blk_ioctl_discard(struct block_device
*bdev
, fmode_t mode
,
101 unsigned long arg
, unsigned long flags
)
105 struct request_queue
*q
= bdev_get_queue(bdev
);
108 if (!(mode
& FMODE_WRITE
))
111 if (!blk_queue_discard(q
))
114 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
125 if (start
+ len
> i_size_read(bdev
->bd_inode
))
128 err
= truncate_bdev_range(bdev
, mode
, start
, start
+ len
- 1);
132 return blkdev_issue_discard(bdev
, start
>> 9, len
>> 9,
136 static int blk_ioctl_zeroout(struct block_device
*bdev
, fmode_t mode
,
140 uint64_t start
, end
, len
;
143 if (!(mode
& FMODE_WRITE
))
146 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
151 end
= start
+ len
- 1;
157 if (end
>= (uint64_t)i_size_read(bdev
->bd_inode
))
162 /* Invalidate the page cache, including dirty pages */
163 err
= truncate_bdev_range(bdev
, mode
, start
, end
);
167 return blkdev_issue_zeroout(bdev
, start
>> 9, len
>> 9, GFP_KERNEL
,
168 BLKDEV_ZERO_NOUNMAP
);
171 static int put_ushort(unsigned short __user
*argp
, unsigned short val
)
173 return put_user(val
, argp
);
176 static int put_int(int __user
*argp
, int val
)
178 return put_user(val
, argp
);
181 static int put_uint(unsigned int __user
*argp
, unsigned int val
)
183 return put_user(val
, argp
);
186 static int put_long(long __user
*argp
, long val
)
188 return put_user(val
, argp
);
191 static int put_ulong(unsigned long __user
*argp
, unsigned long val
)
193 return put_user(val
, argp
);
196 static int put_u64(u64 __user
*argp
, u64 val
)
198 return put_user(val
, argp
);
202 static int compat_put_long(compat_long_t __user
*argp
, long val
)
204 return put_user(val
, argp
);
207 static int compat_put_ulong(compat_ulong_t __user
*argp
, compat_ulong_t val
)
209 return put_user(val
, argp
);
215 * This is the equivalent of compat_ptr_ioctl(), to be used by block
216 * drivers that implement only commands that are completely compatible
217 * between 32-bit and 64-bit user space
219 int blkdev_compat_ptr_ioctl(struct block_device
*bdev
, fmode_t mode
,
220 unsigned cmd
, unsigned long arg
)
222 struct gendisk
*disk
= bdev
->bd_disk
;
224 if (disk
->fops
->ioctl
)
225 return disk
->fops
->ioctl(bdev
, mode
, cmd
,
226 (unsigned long)compat_ptr(arg
));
230 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl
);
233 static int blkdev_pr_register(struct block_device
*bdev
,
234 struct pr_registration __user
*arg
)
236 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
237 struct pr_registration reg
;
239 if (!capable(CAP_SYS_ADMIN
))
241 if (!ops
|| !ops
->pr_register
)
243 if (copy_from_user(®
, arg
, sizeof(reg
)))
246 if (reg
.flags
& ~PR_FL_IGNORE_KEY
)
248 return ops
->pr_register(bdev
, reg
.old_key
, reg
.new_key
, reg
.flags
);
251 static int blkdev_pr_reserve(struct block_device
*bdev
,
252 struct pr_reservation __user
*arg
)
254 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
255 struct pr_reservation rsv
;
257 if (!capable(CAP_SYS_ADMIN
))
259 if (!ops
|| !ops
->pr_reserve
)
261 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
264 if (rsv
.flags
& ~PR_FL_IGNORE_KEY
)
266 return ops
->pr_reserve(bdev
, rsv
.key
, rsv
.type
, rsv
.flags
);
269 static int blkdev_pr_release(struct block_device
*bdev
,
270 struct pr_reservation __user
*arg
)
272 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
273 struct pr_reservation rsv
;
275 if (!capable(CAP_SYS_ADMIN
))
277 if (!ops
|| !ops
->pr_release
)
279 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
284 return ops
->pr_release(bdev
, rsv
.key
, rsv
.type
);
287 static int blkdev_pr_preempt(struct block_device
*bdev
,
288 struct pr_preempt __user
*arg
, bool abort
)
290 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
293 if (!capable(CAP_SYS_ADMIN
))
295 if (!ops
|| !ops
->pr_preempt
)
297 if (copy_from_user(&p
, arg
, sizeof(p
)))
302 return ops
->pr_preempt(bdev
, p
.old_key
, p
.new_key
, p
.type
, abort
);
305 static int blkdev_pr_clear(struct block_device
*bdev
,
306 struct pr_clear __user
*arg
)
308 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
311 if (!capable(CAP_SYS_ADMIN
))
313 if (!ops
|| !ops
->pr_clear
)
315 if (copy_from_user(&c
, arg
, sizeof(c
)))
320 return ops
->pr_clear(bdev
, c
.key
);
323 static int blkdev_flushbuf(struct block_device
*bdev
, fmode_t mode
,
324 unsigned cmd
, unsigned long arg
)
326 if (!capable(CAP_SYS_ADMIN
))
329 invalidate_bdev(bdev
);
333 static int blkdev_roset(struct block_device
*bdev
, fmode_t mode
,
334 unsigned cmd
, unsigned long arg
)
338 if (!capable(CAP_SYS_ADMIN
))
341 if (get_user(n
, (int __user
*)arg
))
343 if (bdev
->bd_disk
->fops
->set_read_only
) {
344 ret
= bdev
->bd_disk
->fops
->set_read_only(bdev
, n
);
348 bdev
->bd_read_only
= n
;
352 static int blkdev_getgeo(struct block_device
*bdev
,
353 struct hd_geometry __user
*argp
)
355 struct gendisk
*disk
= bdev
->bd_disk
;
356 struct hd_geometry geo
;
361 if (!disk
->fops
->getgeo
)
365 * We need to set the startsect first, the driver may
366 * want to override it.
368 memset(&geo
, 0, sizeof(geo
));
369 geo
.start
= get_start_sect(bdev
);
370 ret
= disk
->fops
->getgeo(bdev
, &geo
);
373 if (copy_to_user(argp
, &geo
, sizeof(geo
)))
379 struct compat_hd_geometry
{
381 unsigned char sectors
;
382 unsigned short cylinders
;
386 static int compat_hdio_getgeo(struct block_device
*bdev
,
387 struct compat_hd_geometry __user
*ugeo
)
389 struct gendisk
*disk
= bdev
->bd_disk
;
390 struct hd_geometry geo
;
395 if (!disk
->fops
->getgeo
)
398 memset(&geo
, 0, sizeof(geo
));
400 * We need to set the startsect first, the driver may
401 * want to override it.
403 geo
.start
= get_start_sect(bdev
);
404 ret
= disk
->fops
->getgeo(bdev
, &geo
);
408 ret
= copy_to_user(ugeo
, &geo
, 4);
409 ret
|= put_user(geo
.start
, &ugeo
->start
);
417 /* set the logical block size */
418 static int blkdev_bszset(struct block_device
*bdev
, fmode_t mode
,
423 if (!capable(CAP_SYS_ADMIN
))
427 if (get_user(n
, argp
))
430 if (mode
& FMODE_EXCL
)
431 return set_blocksize(bdev
, n
);
433 if (IS_ERR(blkdev_get_by_dev(bdev
->bd_dev
, mode
| FMODE_EXCL
, &bdev
)))
435 ret
= set_blocksize(bdev
, n
);
436 blkdev_put(bdev
, mode
| FMODE_EXCL
);
442 * Common commands that are handled the same way on native and compat
443 * user space. Note the separate arg/argp parameters that are needed
444 * to deal with the compat_ptr() conversion.
446 static int blkdev_common_ioctl(struct block_device
*bdev
, fmode_t mode
,
447 unsigned cmd
, unsigned long arg
, void __user
*argp
)
449 unsigned int max_sectors
;
453 return blkdev_flushbuf(bdev
, mode
, cmd
, arg
);
455 return blkdev_roset(bdev
, mode
, cmd
, arg
);
457 return blk_ioctl_discard(bdev
, mode
, arg
, 0);
459 return blk_ioctl_discard(bdev
, mode
, arg
,
460 BLKDEV_DISCARD_SECURE
);
462 return blk_ioctl_zeroout(bdev
, mode
, arg
);
464 return blkdev_report_zones_ioctl(bdev
, mode
, cmd
, arg
);
469 return blkdev_zone_mgmt_ioctl(bdev
, mode
, cmd
, arg
);
471 return put_uint(argp
, bdev_zone_sectors(bdev
));
473 return put_uint(argp
, blkdev_nr_zones(bdev
->bd_disk
));
475 return put_int(argp
, bdev_read_only(bdev
) != 0);
476 case BLKSSZGET
: /* get block device logical block size */
477 return put_int(argp
, bdev_logical_block_size(bdev
));
478 case BLKPBSZGET
: /* get block device physical block size */
479 return put_uint(argp
, bdev_physical_block_size(bdev
));
481 return put_uint(argp
, bdev_io_min(bdev
));
483 return put_uint(argp
, bdev_io_opt(bdev
));
485 return put_int(argp
, bdev_alignment_offset(bdev
));
486 case BLKDISCARDZEROES
:
487 return put_uint(argp
, 0);
489 max_sectors
= min_t(unsigned int, USHRT_MAX
,
490 queue_max_sectors(bdev_get_queue(bdev
)));
491 return put_ushort(argp
, max_sectors
);
493 return put_ushort(argp
, !blk_queue_nonrot(bdev_get_queue(bdev
)));
496 if(!capable(CAP_SYS_ADMIN
))
498 bdev
->bd_bdi
->ra_pages
= (arg
* 512) / PAGE_SIZE
;
501 return blkdev_reread_part(bdev
);
504 case BLKTRACETEARDOWN
:
505 return blk_trace_ioctl(bdev
, cmd
, argp
);
506 case IOC_PR_REGISTER
:
507 return blkdev_pr_register(bdev
, argp
);
509 return blkdev_pr_reserve(bdev
, argp
);
511 return blkdev_pr_release(bdev
, argp
);
513 return blkdev_pr_preempt(bdev
, argp
, false);
514 case IOC_PR_PREEMPT_ABORT
:
515 return blkdev_pr_preempt(bdev
, argp
, true);
517 return blkdev_pr_clear(bdev
, argp
);
524 * Always keep this in sync with compat_blkdev_ioctl()
525 * to handle all incompatible commands in both functions.
527 * New commands must be compatible and go into blkdev_common_ioctl
529 int blkdev_ioctl(struct block_device
*bdev
, fmode_t mode
, unsigned cmd
,
534 void __user
*argp
= (void __user
*)arg
;
537 /* These need separate implementations for the data structure */
539 return blkdev_getgeo(bdev
, argp
);
541 return blkpg_ioctl(bdev
, argp
);
543 /* Compat mode returns 32-bit data instead of 'long' */
548 return put_long(argp
, (bdev
->bd_bdi
->ra_pages
*PAGE_SIZE
) / 512);
550 size
= i_size_read(bdev
->bd_inode
);
551 if ((size
>> 9) > ~0UL)
553 return put_ulong(argp
, size
>> 9);
555 /* The data is compatible, but the command number is different */
556 case BLKBSZGET
: /* get block device soft block size (cf. BLKSSZGET) */
557 return put_int(argp
, block_size(bdev
));
559 return blkdev_bszset(bdev
, mode
, argp
);
561 return put_u64(argp
, i_size_read(bdev
->bd_inode
));
563 /* Incompatible alignment on i386 */
565 return blk_trace_ioctl(bdev
, cmd
, argp
);
570 ret
= blkdev_common_ioctl(bdev
, mode
, cmd
, arg
, argp
);
571 if (ret
!= -ENOIOCTLCMD
)
574 if (!bdev
->bd_disk
->fops
->ioctl
)
576 return bdev
->bd_disk
->fops
->ioctl(bdev
, mode
, cmd
, arg
);
578 EXPORT_SYMBOL_GPL(blkdev_ioctl
); /* for /dev/raw */
582 #define BLKBSZGET_32 _IOR(0x12, 112, int)
583 #define BLKBSZSET_32 _IOW(0x12, 113, int)
584 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
586 /* Most of the generic ioctls are handled in the normal fallback path.
587 This assumes the blkdev's low level compat_ioctl always returns
588 ENOIOCTLCMD for unknown ioctls. */
589 long compat_blkdev_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
592 void __user
*argp
= compat_ptr(arg
);
593 struct block_device
*bdev
= I_BDEV(file
->f_mapping
->host
);
594 struct gendisk
*disk
= bdev
->bd_disk
;
595 fmode_t mode
= file
->f_mode
;
599 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
600 * to updated it before every ioctl.
602 if (file
->f_flags
& O_NDELAY
)
603 mode
|= FMODE_NDELAY
;
605 mode
&= ~FMODE_NDELAY
;
608 /* These need separate implementations for the data structure */
610 return compat_hdio_getgeo(bdev
, argp
);
612 return compat_blkpg_ioctl(bdev
, argp
);
614 /* Compat mode returns 32-bit data instead of 'long' */
619 return compat_put_long(argp
,
620 (bdev
->bd_bdi
->ra_pages
* PAGE_SIZE
) / 512);
622 size
= i_size_read(bdev
->bd_inode
);
623 if ((size
>> 9) > ~0UL)
625 return compat_put_ulong(argp
, size
>> 9);
627 /* The data is compatible, but the command number is different */
628 case BLKBSZGET_32
: /* get the logical block size (cf. BLKSSZGET) */
629 return put_int(argp
, bdev_logical_block_size(bdev
));
631 return blkdev_bszset(bdev
, mode
, argp
);
632 case BLKGETSIZE64_32
:
633 return put_u64(argp
, i_size_read(bdev
->bd_inode
));
635 /* Incompatible alignment on i386 */
636 case BLKTRACESETUP32
:
637 return blk_trace_ioctl(bdev
, cmd
, argp
);
642 ret
= blkdev_common_ioctl(bdev
, mode
, cmd
, arg
, argp
);
643 if (ret
== -ENOIOCTLCMD
&& disk
->fops
->compat_ioctl
)
644 ret
= disk
->fops
->compat_ioctl(bdev
, mode
, cmd
, arg
);