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 block_device
*bdevp
;
21 struct hd_struct
*part
, *lpart
;
22 struct blkpg_partition p
;
23 struct disk_part_iter piter
;
24 long long start
, length
;
27 if (!capable(CAP_SYS_ADMIN
))
29 if (copy_from_user(&p
, upart
, sizeof(struct blkpg_partition
)))
32 if (bdev
!= bdev
->bd_contains
)
38 case BLKPG_ADD_PARTITION
:
40 length
= p
.length
>> 9;
41 /* check for fit in a hd_struct */
42 if (sizeof(sector_t
) == sizeof(long) &&
43 sizeof(long long) > sizeof(long)) {
44 long pstart
= start
, plength
= length
;
45 if (pstart
!= start
|| plength
!= length
46 || pstart
< 0 || plength
< 0 || partno
> 65535)
49 /* check if partition is aligned to blocksize */
50 if (p
.start
& (bdev_logical_block_size(bdev
) - 1))
53 mutex_lock(&bdev
->bd_mutex
);
56 disk_part_iter_init(&piter
, disk
,
57 DISK_PITER_INCL_EMPTY
);
58 while ((part
= disk_part_iter_next(&piter
))) {
59 if (!(start
+ length
<= part
->start_sect
||
60 start
>= part
->start_sect
+ part
->nr_sects
)) {
61 disk_part_iter_exit(&piter
);
62 mutex_unlock(&bdev
->bd_mutex
);
66 disk_part_iter_exit(&piter
);
69 part
= add_partition(disk
, partno
, start
, length
,
70 ADDPART_FLAG_NONE
, NULL
);
71 mutex_unlock(&bdev
->bd_mutex
);
72 return PTR_ERR_OR_ZERO(part
);
73 case BLKPG_DEL_PARTITION
:
74 part
= disk_get_part(disk
, partno
);
78 bdevp
= bdget(part_devt(part
));
83 mutex_lock(&bdevp
->bd_mutex
);
84 if (bdevp
->bd_openers
) {
85 mutex_unlock(&bdevp
->bd_mutex
);
91 invalidate_bdev(bdevp
);
93 mutex_lock_nested(&bdev
->bd_mutex
, 1);
94 delete_partition(disk
, partno
);
95 mutex_unlock(&bdev
->bd_mutex
);
96 mutex_unlock(&bdevp
->bd_mutex
);
100 case BLKPG_RESIZE_PARTITION
:
101 start
= p
.start
>> 9;
102 /* new length of partition in bytes */
103 length
= p
.length
>> 9;
104 /* check for fit in a hd_struct */
105 if (sizeof(sector_t
) == sizeof(long) &&
106 sizeof(long long) > sizeof(long)) {
107 long pstart
= start
, plength
= length
;
108 if (pstart
!= start
|| plength
!= length
109 || pstart
< 0 || plength
< 0)
112 part
= disk_get_part(disk
, partno
);
115 bdevp
= bdget(part_devt(part
));
120 mutex_lock(&bdevp
->bd_mutex
);
121 mutex_lock_nested(&bdev
->bd_mutex
, 1);
122 if (start
!= part
->start_sect
) {
123 mutex_unlock(&bdevp
->bd_mutex
);
124 mutex_unlock(&bdev
->bd_mutex
);
130 disk_part_iter_init(&piter
, disk
,
131 DISK_PITER_INCL_EMPTY
);
132 while ((lpart
= disk_part_iter_next(&piter
))) {
133 if (lpart
->partno
!= partno
&&
134 !(start
+ length
<= lpart
->start_sect
||
135 start
>= lpart
->start_sect
+ lpart
->nr_sects
)
137 disk_part_iter_exit(&piter
);
138 mutex_unlock(&bdevp
->bd_mutex
);
139 mutex_unlock(&bdev
->bd_mutex
);
145 disk_part_iter_exit(&piter
);
146 part_nr_sects_write(part
, (sector_t
)length
);
147 i_size_write(bdevp
->bd_inode
, p
.length
);
148 mutex_unlock(&bdevp
->bd_mutex
);
149 mutex_unlock(&bdev
->bd_mutex
);
158 static int blkpg_ioctl(struct block_device
*bdev
,
159 struct blkpg_ioctl_arg __user
*arg
)
161 struct blkpg_partition __user
*udata
;
164 if (get_user(op
, &arg
->op
) || get_user(udata
, &arg
->data
))
167 return blkpg_do_ioctl(bdev
, udata
, op
);
171 struct compat_blkpg_ioctl_arg
{
174 compat_int_t datalen
;
178 static int compat_blkpg_ioctl(struct block_device
*bdev
,
179 struct compat_blkpg_ioctl_arg __user
*arg
)
181 compat_caddr_t udata
;
184 if (get_user(op
, &arg
->op
) || get_user(udata
, &arg
->data
))
187 return blkpg_do_ioctl(bdev
, compat_ptr(udata
), op
);
191 static int blkdev_reread_part(struct block_device
*bdev
)
195 if (!disk_part_scan_enabled(bdev
->bd_disk
) || bdev
!= bdev
->bd_contains
)
197 if (!capable(CAP_SYS_ADMIN
))
200 mutex_lock(&bdev
->bd_mutex
);
201 ret
= bdev_disk_changed(bdev
, false);
202 mutex_unlock(&bdev
->bd_mutex
);
207 static int blk_ioctl_discard(struct block_device
*bdev
, fmode_t mode
,
208 unsigned long arg
, unsigned long flags
)
212 struct request_queue
*q
= bdev_get_queue(bdev
);
213 struct address_space
*mapping
= bdev
->bd_inode
->i_mapping
;
216 if (!(mode
& FMODE_WRITE
))
219 if (!blk_queue_discard(q
))
222 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
233 if (start
+ len
> i_size_read(bdev
->bd_inode
))
235 truncate_inode_pages_range(mapping
, start
, start
+ len
- 1);
236 return blkdev_issue_discard(bdev
, start
>> 9, len
>> 9,
240 static int blk_ioctl_zeroout(struct block_device
*bdev
, fmode_t mode
,
244 struct address_space
*mapping
;
245 uint64_t start
, end
, len
;
247 if (!(mode
& FMODE_WRITE
))
250 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
255 end
= start
+ len
- 1;
261 if (end
>= (uint64_t)i_size_read(bdev
->bd_inode
))
266 /* Invalidate the page cache, including dirty pages */
267 mapping
= bdev
->bd_inode
->i_mapping
;
268 truncate_inode_pages_range(mapping
, start
, end
);
270 return blkdev_issue_zeroout(bdev
, start
>> 9, len
>> 9, GFP_KERNEL
,
271 BLKDEV_ZERO_NOUNMAP
);
274 static int put_ushort(unsigned short __user
*argp
, unsigned short val
)
276 return put_user(val
, argp
);
279 static int put_int(int __user
*argp
, int val
)
281 return put_user(val
, argp
);
284 static int put_uint(unsigned int __user
*argp
, unsigned int val
)
286 return put_user(val
, argp
);
289 static int put_long(long __user
*argp
, long val
)
291 return put_user(val
, argp
);
294 static int put_ulong(unsigned long __user
*argp
, unsigned long val
)
296 return put_user(val
, argp
);
299 static int put_u64(u64 __user
*argp
, u64 val
)
301 return put_user(val
, argp
);
305 static int compat_put_long(compat_long_t
*argp
, long val
)
307 return put_user(val
, argp
);
310 static int compat_put_ulong(compat_ulong_t
*argp
, compat_ulong_t val
)
312 return put_user(val
, argp
);
316 int __blkdev_driver_ioctl(struct block_device
*bdev
, fmode_t mode
,
317 unsigned cmd
, unsigned long arg
)
319 struct gendisk
*disk
= bdev
->bd_disk
;
321 if (disk
->fops
->ioctl
)
322 return disk
->fops
->ioctl(bdev
, mode
, cmd
, arg
);
327 * For the record: _GPL here is only because somebody decided to slap it
328 * on the previous export. Sheer idiocy, since it wasn't copyrightable
329 * at all and could be open-coded without any exports by anybody who cares.
331 EXPORT_SYMBOL_GPL(__blkdev_driver_ioctl
);
335 * This is the equivalent of compat_ptr_ioctl(), to be used by block
336 * drivers that implement only commands that are completely compatible
337 * between 32-bit and 64-bit user space
339 int blkdev_compat_ptr_ioctl(struct block_device
*bdev
, fmode_t mode
,
340 unsigned cmd
, unsigned long arg
)
342 struct gendisk
*disk
= bdev
->bd_disk
;
344 if (disk
->fops
->ioctl
)
345 return disk
->fops
->ioctl(bdev
, mode
, cmd
,
346 (unsigned long)compat_ptr(arg
));
350 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl
);
353 static int blkdev_pr_register(struct block_device
*bdev
,
354 struct pr_registration __user
*arg
)
356 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
357 struct pr_registration reg
;
359 if (!capable(CAP_SYS_ADMIN
))
361 if (!ops
|| !ops
->pr_register
)
363 if (copy_from_user(®
, arg
, sizeof(reg
)))
366 if (reg
.flags
& ~PR_FL_IGNORE_KEY
)
368 return ops
->pr_register(bdev
, reg
.old_key
, reg
.new_key
, reg
.flags
);
371 static int blkdev_pr_reserve(struct block_device
*bdev
,
372 struct pr_reservation __user
*arg
)
374 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
375 struct pr_reservation rsv
;
377 if (!capable(CAP_SYS_ADMIN
))
379 if (!ops
|| !ops
->pr_reserve
)
381 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
384 if (rsv
.flags
& ~PR_FL_IGNORE_KEY
)
386 return ops
->pr_reserve(bdev
, rsv
.key
, rsv
.type
, rsv
.flags
);
389 static int blkdev_pr_release(struct block_device
*bdev
,
390 struct pr_reservation __user
*arg
)
392 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
393 struct pr_reservation rsv
;
395 if (!capable(CAP_SYS_ADMIN
))
397 if (!ops
|| !ops
->pr_release
)
399 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
404 return ops
->pr_release(bdev
, rsv
.key
, rsv
.type
);
407 static int blkdev_pr_preempt(struct block_device
*bdev
,
408 struct pr_preempt __user
*arg
, bool abort
)
410 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
413 if (!capable(CAP_SYS_ADMIN
))
415 if (!ops
|| !ops
->pr_preempt
)
417 if (copy_from_user(&p
, arg
, sizeof(p
)))
422 return ops
->pr_preempt(bdev
, p
.old_key
, p
.new_key
, p
.type
, abort
);
425 static int blkdev_pr_clear(struct block_device
*bdev
,
426 struct pr_clear __user
*arg
)
428 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
431 if (!capable(CAP_SYS_ADMIN
))
433 if (!ops
|| !ops
->pr_clear
)
435 if (copy_from_user(&c
, arg
, sizeof(c
)))
440 return ops
->pr_clear(bdev
, c
.key
);
444 * Is it an unrecognized ioctl? The correct returns are either
445 * ENOTTY (final) or ENOIOCTLCMD ("I don't know this one, try a
446 * fallback"). ENOIOCTLCMD gets turned into ENOTTY by the ioctl
447 * code before returning.
449 * Confused drivers sometimes return EINVAL, which is wrong. It
450 * means "I understood the ioctl command, but the parameters to
453 * We should aim to just fix the broken drivers, the EINVAL case
456 static inline int is_unrecognized_ioctl(int ret
)
458 return ret
== -EINVAL
||
463 static int blkdev_flushbuf(struct block_device
*bdev
, fmode_t mode
,
464 unsigned cmd
, unsigned long arg
)
468 if (!capable(CAP_SYS_ADMIN
))
471 ret
= __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
472 if (!is_unrecognized_ioctl(ret
))
476 invalidate_bdev(bdev
);
480 static int blkdev_roset(struct block_device
*bdev
, fmode_t mode
,
481 unsigned cmd
, unsigned long arg
)
485 if (!capable(CAP_SYS_ADMIN
))
488 ret
= __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
489 if (!is_unrecognized_ioctl(ret
))
491 if (get_user(n
, (int __user
*)arg
))
493 set_device_ro(bdev
, n
);
497 static int blkdev_getgeo(struct block_device
*bdev
,
498 struct hd_geometry __user
*argp
)
500 struct gendisk
*disk
= bdev
->bd_disk
;
501 struct hd_geometry geo
;
506 if (!disk
->fops
->getgeo
)
510 * We need to set the startsect first, the driver may
511 * want to override it.
513 memset(&geo
, 0, sizeof(geo
));
514 geo
.start
= get_start_sect(bdev
);
515 ret
= disk
->fops
->getgeo(bdev
, &geo
);
518 if (copy_to_user(argp
, &geo
, sizeof(geo
)))
524 struct compat_hd_geometry
{
526 unsigned char sectors
;
527 unsigned short cylinders
;
531 static int compat_hdio_getgeo(struct block_device
*bdev
,
532 struct compat_hd_geometry __user
*ugeo
)
534 struct gendisk
*disk
= bdev
->bd_disk
;
535 struct hd_geometry geo
;
540 if (!disk
->fops
->getgeo
)
543 memset(&geo
, 0, sizeof(geo
));
545 * We need to set the startsect first, the driver may
546 * want to override it.
548 geo
.start
= get_start_sect(bdev
);
549 ret
= disk
->fops
->getgeo(bdev
, &geo
);
553 ret
= copy_to_user(ugeo
, &geo
, 4);
554 ret
|= put_user(geo
.start
, &ugeo
->start
);
562 /* set the logical block size */
563 static int blkdev_bszset(struct block_device
*bdev
, fmode_t mode
,
568 if (!capable(CAP_SYS_ADMIN
))
572 if (get_user(n
, argp
))
575 if (!(mode
& FMODE_EXCL
)) {
577 if (blkdev_get(bdev
, mode
| FMODE_EXCL
, &bdev
) < 0)
581 ret
= set_blocksize(bdev
, n
);
582 if (!(mode
& FMODE_EXCL
))
583 blkdev_put(bdev
, mode
| FMODE_EXCL
);
588 * Common commands that are handled the same way on native and compat
589 * user space. Note the separate arg/argp parameters that are needed
590 * to deal with the compat_ptr() conversion.
592 static int blkdev_common_ioctl(struct block_device
*bdev
, fmode_t mode
,
593 unsigned cmd
, unsigned long arg
, void __user
*argp
)
595 unsigned int max_sectors
;
599 return blkdev_flushbuf(bdev
, mode
, cmd
, arg
);
601 return blkdev_roset(bdev
, mode
, cmd
, arg
);
603 return blk_ioctl_discard(bdev
, mode
, arg
, 0);
605 return blk_ioctl_discard(bdev
, mode
, arg
,
606 BLKDEV_DISCARD_SECURE
);
608 return blk_ioctl_zeroout(bdev
, mode
, arg
);
610 return blkdev_report_zones_ioctl(bdev
, mode
, cmd
, arg
);
615 return blkdev_zone_mgmt_ioctl(bdev
, mode
, cmd
, arg
);
617 return put_uint(argp
, bdev_zone_sectors(bdev
));
619 return put_uint(argp
, blkdev_nr_zones(bdev
->bd_disk
));
621 return put_int(argp
, bdev_read_only(bdev
) != 0);
622 case BLKSSZGET
: /* get block device logical block size */
623 return put_int(argp
, bdev_logical_block_size(bdev
));
624 case BLKPBSZGET
: /* get block device physical block size */
625 return put_uint(argp
, bdev_physical_block_size(bdev
));
627 return put_uint(argp
, bdev_io_min(bdev
));
629 return put_uint(argp
, bdev_io_opt(bdev
));
631 return put_int(argp
, bdev_alignment_offset(bdev
));
632 case BLKDISCARDZEROES
:
633 return put_uint(argp
, 0);
635 max_sectors
= min_t(unsigned int, USHRT_MAX
,
636 queue_max_sectors(bdev_get_queue(bdev
)));
637 return put_ushort(argp
, max_sectors
);
639 return put_ushort(argp
, !blk_queue_nonrot(bdev_get_queue(bdev
)));
642 if(!capable(CAP_SYS_ADMIN
))
644 bdev
->bd_bdi
->ra_pages
= (arg
* 512) / PAGE_SIZE
;
647 return blkdev_reread_part(bdev
);
650 case BLKTRACETEARDOWN
:
651 return blk_trace_ioctl(bdev
, cmd
, argp
);
652 case IOC_PR_REGISTER
:
653 return blkdev_pr_register(bdev
, argp
);
655 return blkdev_pr_reserve(bdev
, argp
);
657 return blkdev_pr_release(bdev
, argp
);
659 return blkdev_pr_preempt(bdev
, argp
, false);
660 case IOC_PR_PREEMPT_ABORT
:
661 return blkdev_pr_preempt(bdev
, argp
, true);
663 return blkdev_pr_clear(bdev
, argp
);
670 * Always keep this in sync with compat_blkdev_ioctl()
671 * to handle all incompatible commands in both functions.
673 * New commands must be compatible and go into blkdev_common_ioctl
675 int blkdev_ioctl(struct block_device
*bdev
, fmode_t mode
, unsigned cmd
,
680 void __user
*argp
= (void __user
*)arg
;
683 /* These need separate implementations for the data structure */
685 return blkdev_getgeo(bdev
, argp
);
687 return blkpg_ioctl(bdev
, argp
);
689 /* Compat mode returns 32-bit data instead of 'long' */
694 return put_long(argp
, (bdev
->bd_bdi
->ra_pages
*PAGE_SIZE
) / 512);
696 size
= i_size_read(bdev
->bd_inode
);
697 if ((size
>> 9) > ~0UL)
699 return put_ulong(argp
, size
>> 9);
701 /* The data is compatible, but the command number is different */
702 case BLKBSZGET
: /* get block device soft block size (cf. BLKSSZGET) */
703 return put_int(argp
, block_size(bdev
));
705 return blkdev_bszset(bdev
, mode
, argp
);
707 return put_u64(argp
, i_size_read(bdev
->bd_inode
));
709 /* Incompatible alignment on i386 */
711 return blk_trace_ioctl(bdev
, cmd
, argp
);
716 ret
= blkdev_common_ioctl(bdev
, mode
, cmd
, arg
, argp
);
717 if (ret
== -ENOIOCTLCMD
)
718 return __blkdev_driver_ioctl(bdev
, mode
, cmd
, arg
);
722 EXPORT_SYMBOL_GPL(blkdev_ioctl
); /* for /dev/raw */
726 #define BLKBSZGET_32 _IOR(0x12, 112, int)
727 #define BLKBSZSET_32 _IOW(0x12, 113, int)
728 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
730 /* Most of the generic ioctls are handled in the normal fallback path.
731 This assumes the blkdev's low level compat_ioctl always returns
732 ENOIOCTLCMD for unknown ioctls. */
733 long compat_blkdev_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
736 void __user
*argp
= compat_ptr(arg
);
737 struct inode
*inode
= file
->f_mapping
->host
;
738 struct block_device
*bdev
= inode
->i_bdev
;
739 struct gendisk
*disk
= bdev
->bd_disk
;
740 fmode_t mode
= file
->f_mode
;
744 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
745 * to updated it before every ioctl.
747 if (file
->f_flags
& O_NDELAY
)
748 mode
|= FMODE_NDELAY
;
750 mode
&= ~FMODE_NDELAY
;
753 /* These need separate implementations for the data structure */
755 return compat_hdio_getgeo(bdev
, argp
);
757 return compat_blkpg_ioctl(bdev
, argp
);
759 /* Compat mode returns 32-bit data instead of 'long' */
764 return compat_put_long(argp
,
765 (bdev
->bd_bdi
->ra_pages
* PAGE_SIZE
) / 512);
767 size
= i_size_read(bdev
->bd_inode
);
768 if ((size
>> 9) > ~0UL)
770 return compat_put_ulong(argp
, size
>> 9);
772 /* The data is compatible, but the command number is different */
773 case BLKBSZGET_32
: /* get the logical block size (cf. BLKSSZGET) */
774 return put_int(argp
, bdev_logical_block_size(bdev
));
776 return blkdev_bszset(bdev
, mode
, argp
);
777 case BLKGETSIZE64_32
:
778 return put_u64(argp
, i_size_read(bdev
->bd_inode
));
780 /* Incompatible alignment on i386 */
781 case BLKTRACESETUP32
:
782 return blk_trace_ioctl(bdev
, cmd
, argp
);
787 ret
= blkdev_common_ioctl(bdev
, mode
, cmd
, arg
, argp
);
788 if (ret
== -ENOIOCTLCMD
&& disk
->fops
->compat_ioctl
)
789 ret
= disk
->fops
->compat_ioctl(bdev
, mode
, cmd
, arg
);