Skip to content

Commit 61fe0e2

Browse files
Linus Walleijstorulf
authored andcommitted
mmc: block: Move duplicate check
mmc_blk_ioctl() calls either mmc_blk_ioctl_cmd() or mmc_blk_ioctl_multi_cmd() and each of these make the same check. Factor it into a new helper function, call it on both branches of the switch() statement and save a chunk of duplicate code. Cc: Shawn Lin <[email protected]> Signed-off-by: Linus Walleij <[email protected]> Signed-off-by: Ulf Hansson <[email protected]>
1 parent 627c3cc commit 61fe0e2

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

drivers/mmc/core/block.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,6 @@ static int mmc_blk_ioctl_cmd(struct block_device *bdev,
564564
int err = 0, ioc_err = 0;
565565
struct request *req;
566566

567-
/*
568-
* The caller must have CAP_SYS_RAWIO, and must be calling this on the
569-
* whole block device, not on a partition. This prevents overspray
570-
* between sibling partitions.
571-
*/
572-
if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
573-
return -EPERM;
574-
575567
idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
576568
if (IS_ERR(idata))
577569
return PTR_ERR(idata);
@@ -624,14 +616,6 @@ static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
624616
__u64 num_of_cmds;
625617
struct request *req;
626618

627-
/*
628-
* The caller must have CAP_SYS_RAWIO, and must be calling this on the
629-
* whole block device, not on a partition. This prevents overspray
630-
* between sibling partitions.
631-
*/
632-
if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
633-
return -EPERM;
634-
635619
if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
636620
sizeof(num_of_cmds)))
637621
return -EFAULT;
@@ -698,14 +682,34 @@ static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
698682
return ioc_err ? ioc_err : err;
699683
}
700684

685+
static int mmc_blk_check_blkdev(struct block_device *bdev)
686+
{
687+
/*
688+
* The caller must have CAP_SYS_RAWIO, and must be calling this on the
689+
* whole block device, not on a partition. This prevents overspray
690+
* between sibling partitions.
691+
*/
692+
if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
693+
return -EPERM;
694+
return 0;
695+
}
696+
701697
static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
702698
unsigned int cmd, unsigned long arg)
703699
{
700+
int ret;
701+
704702
switch (cmd) {
705703
case MMC_IOC_CMD:
704+
ret = mmc_blk_check_blkdev(bdev);
705+
if (ret)
706+
return ret;
706707
return mmc_blk_ioctl_cmd(bdev,
707708
(struct mmc_ioc_cmd __user *)arg);
708709
case MMC_IOC_MULTI_CMD:
710+
ret = mmc_blk_check_blkdev(bdev);
711+
if (ret)
712+
return ret;
709713
return mmc_blk_ioctl_multi_cmd(bdev,
710714
(struct mmc_ioc_multi_cmd __user *)arg);
711715
default:

0 commit comments

Comments
 (0)