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 gendisk
*disk
= bdev
->bd_disk
;
20 struct blkpg_partition p
;
21 sector_t start
, length
, capacity
, end
;
23 if (!capable(CAP_SYS_ADMIN
))
25 if (copy_from_user(&p
, upart
, sizeof(struct blkpg_partition
)))
27 if (bdev_is_partition(bdev
))
33 if (op
== BLKPG_DEL_PARTITION
)
34 return bdev_del_partition(disk
, p
.pno
);
36 if (p
.start
< 0 || p
.length
<= 0 || LLONG_MAX
- p
.length
< p
.start
)
38 /* Check that the partition is aligned to the block size */
39 if (!IS_ALIGNED(p
.start
| p
.length
, bdev_logical_block_size(bdev
)))
42 start
= p
.start
>> SECTOR_SHIFT
;
43 length
= p
.length
>> SECTOR_SHIFT
;
44 capacity
= get_capacity(disk
);
46 if (check_add_overflow(start
, length
, &end
))
49 if (start
>= capacity
|| end
> capacity
)
53 case BLKPG_ADD_PARTITION
:
54 return bdev_add_partition(disk
, p
.pno
, start
, length
);
55 case BLKPG_RESIZE_PARTITION
:
56 return bdev_resize_partition(disk
, p
.pno
, start
, length
);
62 static int blkpg_ioctl(struct block_device
*bdev
,
63 struct blkpg_ioctl_arg __user
*arg
)
65 struct blkpg_partition __user
*udata
;
68 if (get_user(op
, &arg
->op
) || get_user(udata
, &arg
->data
))
71 return blkpg_do_ioctl(bdev
, udata
, op
);
75 struct compat_blkpg_ioctl_arg
{
82 static int compat_blkpg_ioctl(struct block_device
*bdev
,
83 struct compat_blkpg_ioctl_arg __user
*arg
)
88 if (get_user(op
, &arg
->op
) || get_user(udata
, &arg
->data
))
91 return blkpg_do_ioctl(bdev
, compat_ptr(udata
), op
);
95 static int blk_ioctl_discard(struct block_device
*bdev
, blk_mode_t mode
,
98 unsigned int bs_mask
= bdev_logical_block_size(bdev
) - 1;
99 uint64_t range
[2], start
, len
, end
;
100 struct bio
*prev
= NULL
, *bio
;
101 sector_t sector
, nr_sects
;
102 struct blk_plug plug
;
105 if (!(mode
& BLK_OPEN_WRITE
))
108 if (!bdev_max_discard_sectors(bdev
))
110 if (bdev_read_only(bdev
))
113 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
121 if ((start
| len
) & bs_mask
)
124 if (check_add_overflow(start
, len
, &end
) ||
125 end
> bdev_nr_bytes(bdev
))
128 filemap_invalidate_lock(bdev
->bd_mapping
);
129 err
= truncate_bdev_range(bdev
, mode
, start
, start
+ len
- 1);
133 sector
= start
>> SECTOR_SHIFT
;
134 nr_sects
= len
>> SECTOR_SHIFT
;
136 blk_start_plug(&plug
);
138 if (fatal_signal_pending(current
)) {
140 bio_await_chain(prev
);
144 bio
= blk_alloc_discard_bio(bdev
, §or
, &nr_sects
,
148 prev
= bio_chain_and_submit(prev
, bio
);
151 err
= submit_bio_wait(prev
);
152 if (err
== -EOPNOTSUPP
)
157 blk_finish_plug(&plug
);
159 filemap_invalidate_unlock(bdev
->bd_mapping
);
163 static int blk_ioctl_secure_erase(struct block_device
*bdev
, blk_mode_t mode
,
170 if (!(mode
& BLK_OPEN_WRITE
))
172 if (!bdev_max_secure_erase_sectors(bdev
))
174 if (copy_from_user(range
, argp
, sizeof(range
)))
179 if ((start
& 511) || (len
& 511))
181 if (start
+ len
> bdev_nr_bytes(bdev
))
184 filemap_invalidate_lock(bdev
->bd_mapping
);
185 err
= truncate_bdev_range(bdev
, mode
, start
, start
+ len
- 1);
187 err
= blkdev_issue_secure_erase(bdev
, start
>> 9, len
>> 9,
189 filemap_invalidate_unlock(bdev
->bd_mapping
);
194 static int blk_ioctl_zeroout(struct block_device
*bdev
, blk_mode_t mode
,
198 uint64_t start
, end
, len
;
201 if (!(mode
& BLK_OPEN_WRITE
))
204 if (copy_from_user(range
, (void __user
*)arg
, sizeof(range
)))
209 end
= start
+ len
- 1;
215 if (end
>= (uint64_t)bdev_nr_bytes(bdev
))
220 /* Invalidate the page cache, including dirty pages */
221 filemap_invalidate_lock(bdev
->bd_mapping
);
222 err
= truncate_bdev_range(bdev
, mode
, start
, end
);
226 err
= blkdev_issue_zeroout(bdev
, start
>> 9, len
>> 9, GFP_KERNEL
,
227 BLKDEV_ZERO_NOUNMAP
| BLKDEV_ZERO_KILLABLE
);
230 filemap_invalidate_unlock(bdev
->bd_mapping
);
234 static int put_ushort(unsigned short __user
*argp
, unsigned short val
)
236 return put_user(val
, argp
);
239 static int put_int(int __user
*argp
, int val
)
241 return put_user(val
, argp
);
244 static int put_uint(unsigned int __user
*argp
, unsigned int val
)
246 return put_user(val
, argp
);
249 static int put_long(long __user
*argp
, long val
)
251 return put_user(val
, argp
);
254 static int put_ulong(unsigned long __user
*argp
, unsigned long val
)
256 return put_user(val
, argp
);
259 static int put_u64(u64 __user
*argp
, u64 val
)
261 return put_user(val
, argp
);
265 static int compat_put_long(compat_long_t __user
*argp
, long val
)
267 return put_user(val
, argp
);
270 static int compat_put_ulong(compat_ulong_t __user
*argp
, compat_ulong_t val
)
272 return put_user(val
, argp
);
278 * This is the equivalent of compat_ptr_ioctl(), to be used by block
279 * drivers that implement only commands that are completely compatible
280 * between 32-bit and 64-bit user space
282 int blkdev_compat_ptr_ioctl(struct block_device
*bdev
, blk_mode_t mode
,
283 unsigned cmd
, unsigned long arg
)
285 struct gendisk
*disk
= bdev
->bd_disk
;
287 if (disk
->fops
->ioctl
)
288 return disk
->fops
->ioctl(bdev
, mode
, cmd
,
289 (unsigned long)compat_ptr(arg
));
293 EXPORT_SYMBOL(blkdev_compat_ptr_ioctl
);
296 static bool blkdev_pr_allowed(struct block_device
*bdev
, blk_mode_t mode
)
298 /* no sense to make reservations for partitions */
299 if (bdev_is_partition(bdev
))
302 if (capable(CAP_SYS_ADMIN
))
305 * Only allow unprivileged reservations if the file descriptor is open
308 return mode
& BLK_OPEN_WRITE
;
311 static int blkdev_pr_register(struct block_device
*bdev
, blk_mode_t mode
,
312 struct pr_registration __user
*arg
)
314 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
315 struct pr_registration reg
;
317 if (!blkdev_pr_allowed(bdev
, mode
))
319 if (!ops
|| !ops
->pr_register
)
321 if (copy_from_user(®
, arg
, sizeof(reg
)))
324 if (reg
.flags
& ~PR_FL_IGNORE_KEY
)
326 return ops
->pr_register(bdev
, reg
.old_key
, reg
.new_key
, reg
.flags
);
329 static int blkdev_pr_reserve(struct block_device
*bdev
, blk_mode_t mode
,
330 struct pr_reservation __user
*arg
)
332 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
333 struct pr_reservation rsv
;
335 if (!blkdev_pr_allowed(bdev
, mode
))
337 if (!ops
|| !ops
->pr_reserve
)
339 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
342 if (rsv
.flags
& ~PR_FL_IGNORE_KEY
)
344 return ops
->pr_reserve(bdev
, rsv
.key
, rsv
.type
, rsv
.flags
);
347 static int blkdev_pr_release(struct block_device
*bdev
, blk_mode_t mode
,
348 struct pr_reservation __user
*arg
)
350 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
351 struct pr_reservation rsv
;
353 if (!blkdev_pr_allowed(bdev
, mode
))
355 if (!ops
|| !ops
->pr_release
)
357 if (copy_from_user(&rsv
, arg
, sizeof(rsv
)))
362 return ops
->pr_release(bdev
, rsv
.key
, rsv
.type
);
365 static int blkdev_pr_preempt(struct block_device
*bdev
, blk_mode_t mode
,
366 struct pr_preempt __user
*arg
, bool abort
)
368 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
371 if (!blkdev_pr_allowed(bdev
, mode
))
373 if (!ops
|| !ops
->pr_preempt
)
375 if (copy_from_user(&p
, arg
, sizeof(p
)))
380 return ops
->pr_preempt(bdev
, p
.old_key
, p
.new_key
, p
.type
, abort
);
383 static int blkdev_pr_clear(struct block_device
*bdev
, blk_mode_t mode
,
384 struct pr_clear __user
*arg
)
386 const struct pr_ops
*ops
= bdev
->bd_disk
->fops
->pr_ops
;
389 if (!blkdev_pr_allowed(bdev
, mode
))
391 if (!ops
|| !ops
->pr_clear
)
393 if (copy_from_user(&c
, arg
, sizeof(c
)))
398 return ops
->pr_clear(bdev
, c
.key
);
401 static int blkdev_flushbuf(struct block_device
*bdev
, unsigned cmd
,
404 if (!capable(CAP_SYS_ADMIN
))
407 mutex_lock(&bdev
->bd_holder_lock
);
408 if (bdev
->bd_holder_ops
&& bdev
->bd_holder_ops
->sync
)
409 bdev
->bd_holder_ops
->sync(bdev
);
411 mutex_unlock(&bdev
->bd_holder_lock
);
415 invalidate_bdev(bdev
);
419 static int blkdev_roset(struct block_device
*bdev
, unsigned cmd
,
424 if (!capable(CAP_SYS_ADMIN
))
427 if (get_user(n
, (int __user
*)arg
))
429 if (bdev
->bd_disk
->fops
->set_read_only
) {
430 ret
= bdev
->bd_disk
->fops
->set_read_only(bdev
, n
);
435 bdev_set_flag(bdev
, BD_READ_ONLY
);
437 bdev_clear_flag(bdev
, BD_READ_ONLY
);
441 static int blkdev_getgeo(struct block_device
*bdev
,
442 struct hd_geometry __user
*argp
)
444 struct gendisk
*disk
= bdev
->bd_disk
;
445 struct hd_geometry geo
;
450 if (!disk
->fops
->getgeo
)
454 * We need to set the startsect first, the driver may
455 * want to override it.
457 memset(&geo
, 0, sizeof(geo
));
458 geo
.start
= get_start_sect(bdev
);
459 ret
= disk
->fops
->getgeo(bdev
, &geo
);
462 if (copy_to_user(argp
, &geo
, sizeof(geo
)))
468 struct compat_hd_geometry
{
470 unsigned char sectors
;
471 unsigned short cylinders
;
475 static int compat_hdio_getgeo(struct block_device
*bdev
,
476 struct compat_hd_geometry __user
*ugeo
)
478 struct gendisk
*disk
= bdev
->bd_disk
;
479 struct hd_geometry geo
;
484 if (!disk
->fops
->getgeo
)
487 memset(&geo
, 0, sizeof(geo
));
489 * We need to set the startsect first, the driver may
490 * want to override it.
492 geo
.start
= get_start_sect(bdev
);
493 ret
= disk
->fops
->getgeo(bdev
, &geo
);
497 ret
= copy_to_user(ugeo
, &geo
, 4);
498 ret
|= put_user(geo
.start
, &ugeo
->start
);
506 /* set the logical block size */
507 static int blkdev_bszset(struct file
*file
, blk_mode_t mode
,
510 // this one might be file_inode(file)->i_rdev - a rare valid
511 // use of file_inode() for those.
512 dev_t dev
= I_BDEV(file
->f_mapping
->host
)->bd_dev
;
513 struct file
*excl_file
;
516 if (!capable(CAP_SYS_ADMIN
))
520 if (get_user(n
, argp
))
523 if (mode
& BLK_OPEN_EXCL
)
524 return set_blocksize(file
, n
);
526 excl_file
= bdev_file_open_by_dev(dev
, mode
, &dev
, NULL
);
527 if (IS_ERR(excl_file
))
529 ret
= set_blocksize(excl_file
, n
);
535 * Common commands that are handled the same way on native and compat
536 * user space. Note the separate arg/argp parameters that are needed
537 * to deal with the compat_ptr() conversion.
539 static int blkdev_common_ioctl(struct block_device
*bdev
, blk_mode_t mode
,
540 unsigned int cmd
, unsigned long arg
,
543 unsigned int max_sectors
;
547 return blkdev_flushbuf(bdev
, cmd
, arg
);
549 return blkdev_roset(bdev
, cmd
, arg
);
551 return blk_ioctl_discard(bdev
, mode
, arg
);
553 return blk_ioctl_secure_erase(bdev
, mode
, argp
);
555 return blk_ioctl_zeroout(bdev
, mode
, arg
);
557 return put_u64(argp
, bdev
->bd_disk
->diskseq
);
559 return blkdev_report_zones_ioctl(bdev
, cmd
, arg
);
564 return blkdev_zone_mgmt_ioctl(bdev
, mode
, cmd
, arg
);
566 return put_uint(argp
, bdev_zone_sectors(bdev
));
568 return put_uint(argp
, bdev_nr_zones(bdev
));
570 return put_int(argp
, bdev_read_only(bdev
) != 0);
571 case BLKSSZGET
: /* get block device logical block size */
572 return put_int(argp
, bdev_logical_block_size(bdev
));
573 case BLKPBSZGET
: /* get block device physical block size */
574 return put_uint(argp
, bdev_physical_block_size(bdev
));
576 return put_uint(argp
, bdev_io_min(bdev
));
578 return put_uint(argp
, bdev_io_opt(bdev
));
580 return put_int(argp
, bdev_alignment_offset(bdev
));
581 case BLKDISCARDZEROES
:
582 return put_uint(argp
, 0);
584 max_sectors
= min_t(unsigned int, USHRT_MAX
,
585 queue_max_sectors(bdev_get_queue(bdev
)));
586 return put_ushort(argp
, max_sectors
);
588 return put_ushort(argp
, !bdev_nonrot(bdev
));
591 if(!capable(CAP_SYS_ADMIN
))
593 bdev
->bd_disk
->bdi
->ra_pages
= (arg
* 512) / PAGE_SIZE
;
596 if (!capable(CAP_SYS_ADMIN
))
598 if (bdev_is_partition(bdev
))
600 return disk_scan_partitions(bdev
->bd_disk
,
601 mode
| BLK_OPEN_STRICT_SCAN
);
604 case BLKTRACETEARDOWN
:
605 return blk_trace_ioctl(bdev
, cmd
, argp
);
606 case IOC_PR_REGISTER
:
607 return blkdev_pr_register(bdev
, mode
, argp
);
609 return blkdev_pr_reserve(bdev
, mode
, argp
);
611 return blkdev_pr_release(bdev
, mode
, argp
);
613 return blkdev_pr_preempt(bdev
, mode
, argp
, false);
614 case IOC_PR_PREEMPT_ABORT
:
615 return blkdev_pr_preempt(bdev
, mode
, argp
, true);
617 return blkdev_pr_clear(bdev
, mode
, argp
);
624 * Always keep this in sync with compat_blkdev_ioctl()
625 * to handle all incompatible commands in both functions.
627 * New commands must be compatible and go into blkdev_common_ioctl
629 long blkdev_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
631 struct block_device
*bdev
= I_BDEV(file
->f_mapping
->host
);
632 void __user
*argp
= (void __user
*)arg
;
633 blk_mode_t mode
= file_to_blk_mode(file
);
637 /* These need separate implementations for the data structure */
639 return blkdev_getgeo(bdev
, argp
);
641 return blkpg_ioctl(bdev
, argp
);
643 /* Compat mode returns 32-bit data instead of 'long' */
648 return put_long(argp
,
649 (bdev
->bd_disk
->bdi
->ra_pages
* PAGE_SIZE
) / 512);
651 if (bdev_nr_sectors(bdev
) > ~0UL)
653 return put_ulong(argp
, bdev_nr_sectors(bdev
));
655 /* The data is compatible, but the command number is different */
656 case BLKBSZGET
: /* get block device soft block size (cf. BLKSSZGET) */
657 return put_int(argp
, block_size(bdev
));
659 return blkdev_bszset(file
, mode
, argp
);
661 return put_u64(argp
, bdev_nr_bytes(bdev
));
663 /* Incompatible alignment on i386 */
665 return blk_trace_ioctl(bdev
, cmd
, argp
);
670 ret
= blkdev_common_ioctl(bdev
, mode
, cmd
, arg
, argp
);
671 if (ret
!= -ENOIOCTLCMD
)
674 if (!bdev
->bd_disk
->fops
->ioctl
)
676 return bdev
->bd_disk
->fops
->ioctl(bdev
, mode
, cmd
, arg
);
681 #define BLKBSZGET_32 _IOR(0x12, 112, int)
682 #define BLKBSZSET_32 _IOW(0x12, 113, int)
683 #define BLKGETSIZE64_32 _IOR(0x12, 114, int)
685 /* Most of the generic ioctls are handled in the normal fallback path.
686 This assumes the blkdev's low level compat_ioctl always returns
687 ENOIOCTLCMD for unknown ioctls. */
688 long compat_blkdev_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
691 void __user
*argp
= compat_ptr(arg
);
692 struct block_device
*bdev
= I_BDEV(file
->f_mapping
->host
);
693 struct gendisk
*disk
= bdev
->bd_disk
;
694 blk_mode_t mode
= file_to_blk_mode(file
);
697 /* These need separate implementations for the data structure */
699 return compat_hdio_getgeo(bdev
, argp
);
701 return compat_blkpg_ioctl(bdev
, argp
);
703 /* Compat mode returns 32-bit data instead of 'long' */
708 return compat_put_long(argp
,
709 (bdev
->bd_disk
->bdi
->ra_pages
* PAGE_SIZE
) / 512);
711 if (bdev_nr_sectors(bdev
) > ~(compat_ulong_t
)0)
713 return compat_put_ulong(argp
, bdev_nr_sectors(bdev
));
715 /* The data is compatible, but the command number is different */
716 case BLKBSZGET_32
: /* get the logical block size (cf. BLKSSZGET) */
717 return put_int(argp
, bdev_logical_block_size(bdev
));
719 return blkdev_bszset(file
, mode
, argp
);
720 case BLKGETSIZE64_32
:
721 return put_u64(argp
, bdev_nr_bytes(bdev
));
723 /* Incompatible alignment on i386 */
724 case BLKTRACESETUP32
:
725 return blk_trace_ioctl(bdev
, cmd
, argp
);
730 ret
= blkdev_common_ioctl(bdev
, mode
, cmd
, arg
, argp
);
731 if (ret
== -ENOIOCTLCMD
&& disk
->fops
->compat_ioctl
)
732 ret
= disk
->fops
->compat_ioctl(bdev
, mode
, cmd
, arg
);