Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1542037 > unrolled thread

[RFC] block: check partition alignment

Started byStefan Haberland <sth@linux.vnet.ibm.com>
First post2016-12-14 17:50 +0100
Last post2016-12-15 10:10 +0100
Articles 3 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [RFC] block: check partition alignment Stefan Haberland <sth@linux.vnet.ibm.com> - 2016-12-14 17:50 +0100
    Re: [RFC] block: check partition alignment Christoph Hellwig <hch@infradead.org> - 2016-12-14 18:10 +0100
      Re: [RFC] block: check partition alignment Christoph Hellwig <hch@infradead.org> - 2016-12-15 10:10 +0100

#1542037 — [RFC] block: check partition alignment

FromStefan Haberland <sth@linux.vnet.ibm.com>
Date2016-12-14 17:50 +0100
Subject[RFC] block: check partition alignment
Message-ID<sOkUG-ju-9@gated-at.bofh.it>
Partitions that are not aligned to the blocksize of a device may cause
invalid I/O requests because the blocklayer cares only about alignment
within the partition when building requests on partitions.

device
|--------4096--------|--------4096--------|--------4096--------|
partition offset 512byte
|-512-|--------4096--------|--------4096--------|--------4096--------|

When reading/writing one 4k block of the partition this maps to
reading/writing with an offset of 512 byte of the device leading to
unaligned requests for the device which in turn may cause unexpected
behavior of the device driver.

For DASD devices we have to translate the block number into a cylinder,
head, record format. The unaligned requests lead to wrong calculation
and therefore to misdirected I/O. In a "good" case this leads to I/O
errors because the underlying hardware detects the wrong addressing.
In a worst case scenario this might destroy data on the device.

To prevent partitions that are not aligned to the physical blocksize
of a device check for the alignment in the blkpg_ioctl.

Signed-off-by: Stefan Haberland <sth@linux.vnet.ibm.com>
---
 block/ioctl.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/block/ioctl.c b/block/ioctl.c
index 755119c..8b83afee 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -45,6 +45,9 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user
 				    || pstart < 0 || plength < 0 || partno > 65535)
 					return -EINVAL;
 			}
+			/* check if partition is aligned to blocksize */
+			if (p.start % bdev_physical_block_size(bdev) != 0)
+				return -EINVAL;
 
 			mutex_lock(&bdev->bd_mutex);
 
-- 
2.8.4

[toc] | [next] | [standalone]


#1542058

FromChristoph Hellwig <hch@infradead.org>
Date2016-12-14 18:10 +0100
Message-ID<sOle2-F7-21@gated-at.bofh.it>
In reply to#1542037
> To prevent partitions that are not aligned to the physical blocksize
> of a device check for the alignment in the blkpg_ioctl.

We'd also need to reject this when reading partitions from disk, right?

> +			/* check if partition is aligned to blocksize */
> +			if (p.start % bdev_physical_block_size(bdev) != 0)

And this should be bdev_logical_block_size, as the logical block size
is the only thing that matters for the OS - exposing the physical block
size is just an optional hint to prevent users from doing stupid
things (like creating unaligned partitions :))

[toc] | [prev] | [next] | [standalone]


#1542569

FromChristoph Hellwig <hch@infradead.org>
Date2016-12-15 10:10 +0100
Message-ID<sOAd4-3Wj-23@gated-at.bofh.it>
In reply to#1542058
On Thu, Dec 15, 2016 at 10:33:47AM +0900, Damien Le Moal wrote:
> For a regular block device, I agree. But in Stephan case, I think that
> the check really needs to be against the physical block size, with the
> added condition that the bdev is a DASD device (similarly to the zone
> alignment check for zoned block devices).

Then they need to expose a chunk_size.  physical block size is defined
as not having a meaning for the kernel.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web