Re: [PATCH v2] block: add check that partition length needs to be aligned with block size
From: Matthew Wilcox
Date: Tue Jun 27 2023 - 13:53:27 EST
On Tue, Jun 27, 2023 at 10:13:39AM +0200, Pankaj Raghav wrote:
> On Tue, Jun 27, 2023 at 01:39:26PM +0900, Damien Le Moal wrote:
> > > diff --git a/block/ioctl.c b/block/ioctl.c
> > > index 3be11941fb2d..c40b382dd58f 100644
> > > --- a/block/ioctl.c
> > > +++ b/block/ioctl.c
> > > @@ -33,14 +33,18 @@ static int blkpg_do_ioctl(struct block_device *bdev,
> > > if (op == BLKPG_DEL_PARTITION)
> > > return bdev_del_partition(disk, p.pno);
> > >
> > > + /* check if partition is aligned to blocksize */
> > > + if (p.start & (bdev_logical_block_size(bdev) - 1))
> > > + return -EINVAL;
> > > + /* check if length is aligned to blocksize */
> > > + if (p.length & (bdev_logical_block_size(bdev) - 1))
> > > + return -EINVAL;
> >
> > long long blksz_mask = bdev_logical_block_size(bdev) - 1;
> >
> > /* Check that the partition is aligned to the block size */
> > if ((p.start & blksz_mask) || (p.length & blksz_mask))
> > return -EINVAL;
>
> A Minor nit on top of your comment:
>
> unsigned int blksz = bdev_logical_block_size(bdev);
>
> /* Check that the partition is aligned to the block size */
> if (!IS_ALIGNED(p.start, blksz) || !IS_ALIGNED(p.length, blksz))
> return -EINVAL;
or you can even do ...
if (!IS_ALIGNED(p.start | p.length), blksz)
Do I win the code golf trophy?