Commit 81744ee4 authored by Martin K. Petersen's avatar Martin K. Petersen Committed by Jens Axboe

block: Fix incorrect alignment offset reporting and update documentation

queue_sector_alignment_offset returned the wrong value which caused
partitions to report an incorrect alignment_offset.  Since offset
alignment calculation is needed several places it has been split into a
separate helper function.  The topology stacking function has been
updated accordingly.

Furthermore, comments have been added to clarify how the stacking
function works.
Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
Tested-by: default avatarMike Snitzer <snitzer@redhat.com>
Signed-off-by: default avatarJens Axboe <jens.axboe@oracle.com>
parent 2f7a2d89
...@@ -505,20 +505,30 @@ static unsigned int lcm(unsigned int a, unsigned int b) ...@@ -505,20 +505,30 @@ static unsigned int lcm(unsigned int a, unsigned int b)
/** /**
* blk_stack_limits - adjust queue_limits for stacked devices * blk_stack_limits - adjust queue_limits for stacked devices
* @t: the stacking driver limits (top) * @t: the stacking driver limits (top device)
* @b: the underlying queue limits (bottom) * @b: the underlying queue limits (bottom, component device)
* @offset: offset to beginning of data within component device * @offset: offset to beginning of data within component device
* *
* Description: * Description:
* Merges two queue_limit structs. Returns 0 if alignment didn't * This function is used by stacking drivers like MD and DM to ensure
* change. Returns -1 if adding the bottom device caused * that all component devices have compatible block sizes and
* misalignment. * alignments. The stacking driver must provide a queue_limits
* struct (top) and then iteratively call the stacking function for
* all component (bottom) devices. The stacking function will
* attempt to combine the values and ensure proper alignment.
*
* Returns 0 if the top and bottom queue_limits are compatible. The
* top device's block sizes and alignment offsets may be adjusted to
* ensure alignment with the bottom device. If no compatible sizes
* and alignments exist, -1 is returned and the resulting top
* queue_limits will have the misaligned flag set to indicate that
* the alignment_offset is undefined.
*/ */
int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
sector_t offset) sector_t offset)
{ {
sector_t alignment; sector_t alignment;
unsigned int top, bottom, granularity; unsigned int top, bottom;
t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors); t->max_sectors = min_not_zero(t->max_sectors, b->max_sectors);
t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors); t->max_hw_sectors = min_not_zero(t->max_hw_sectors, b->max_hw_sectors);
...@@ -536,15 +546,18 @@ int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, ...@@ -536,15 +546,18 @@ int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
t->max_segment_size = min_not_zero(t->max_segment_size, t->max_segment_size = min_not_zero(t->max_segment_size,
b->max_segment_size); b->max_segment_size);
granularity = max(b->physical_block_size, b->io_min); alignment = queue_limit_alignment_offset(b, offset);
alignment = b->alignment_offset - (offset & (granularity - 1));
/* Bottom device has different alignment. Check that it is
* compatible with the current top alignment.
*/
if (t->alignment_offset != alignment) { if (t->alignment_offset != alignment) {
top = max(t->physical_block_size, t->io_min) top = max(t->physical_block_size, t->io_min)
+ t->alignment_offset; + t->alignment_offset;
bottom = granularity + alignment; bottom = max(b->physical_block_size, b->io_min) + alignment;
/* Verify that top and bottom intervals line up */
if (max(top, bottom) & (min(top, bottom) - 1)) if (max(top, bottom) & (min(top, bottom) - 1))
t->misaligned = 1; t->misaligned = 1;
} }
...@@ -561,32 +574,39 @@ int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, ...@@ -561,32 +574,39 @@ int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
t->no_cluster |= b->no_cluster; t->no_cluster |= b->no_cluster;
t->discard_zeroes_data &= b->discard_zeroes_data; t->discard_zeroes_data &= b->discard_zeroes_data;
/* Physical block size a multiple of the logical block size? */
if (t->physical_block_size & (t->logical_block_size - 1)) { if (t->physical_block_size & (t->logical_block_size - 1)) {
t->physical_block_size = t->logical_block_size; t->physical_block_size = t->logical_block_size;
t->misaligned = 1; t->misaligned = 1;
} }
/* Minimum I/O a multiple of the physical block size? */
if (t->io_min & (t->physical_block_size - 1)) { if (t->io_min & (t->physical_block_size - 1)) {
t->io_min = t->physical_block_size; t->io_min = t->physical_block_size;
t->misaligned = 1; t->misaligned = 1;
} }
/* Optimal I/O a multiple of the physical block size? */
if (t->io_opt & (t->physical_block_size - 1)) { if (t->io_opt & (t->physical_block_size - 1)) {
t->io_opt = 0; t->io_opt = 0;
t->misaligned = 1; t->misaligned = 1;
} }
/* Find lowest common alignment_offset */
t->alignment_offset = lcm(t->alignment_offset, alignment) t->alignment_offset = lcm(t->alignment_offset, alignment)
& (max(t->physical_block_size, t->io_min) - 1); & (max(t->physical_block_size, t->io_min) - 1);
/* Verify that new alignment_offset is on a logical block boundary */
if (t->alignment_offset & (t->logical_block_size - 1)) if (t->alignment_offset & (t->logical_block_size - 1))
t->misaligned = 1; t->misaligned = 1;
/* Discard alignment and granularity */ /* Discard alignment and granularity */
if (b->discard_granularity) { if (b->discard_granularity) {
unsigned int granularity = b->discard_granularity;
offset &= granularity - 1;
alignment = b->discard_alignment - alignment = (granularity + b->discard_alignment - offset)
(offset & (b->discard_granularity - 1)); & (granularity - 1);
if (t->discard_granularity != 0 && if (t->discard_granularity != 0 &&
t->discard_alignment != alignment) { t->discard_alignment != alignment) {
...@@ -598,6 +618,8 @@ int blk_stack_limits(struct queue_limits *t, struct queue_limits *b, ...@@ -598,6 +618,8 @@ int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
t->discard_misaligned = 1; t->discard_misaligned = 1;
} }
t->max_discard_sectors = min_not_zero(t->max_discard_sectors,
b->max_discard_sectors);
t->discard_granularity = max(t->discard_granularity, t->discard_granularity = max(t->discard_granularity,
b->discard_granularity); b->discard_granularity);
t->discard_alignment = lcm(t->discard_alignment, alignment) & t->discard_alignment = lcm(t->discard_alignment, alignment) &
......
...@@ -1116,11 +1116,18 @@ static inline int queue_alignment_offset(struct request_queue *q) ...@@ -1116,11 +1116,18 @@ static inline int queue_alignment_offset(struct request_queue *q)
return q->limits.alignment_offset; return q->limits.alignment_offset;
} }
static inline int queue_limit_alignment_offset(struct queue_limits *lim, sector_t offset)
{
unsigned int granularity = max(lim->physical_block_size, lim->io_min);
offset &= granularity - 1;
return (granularity + lim->alignment_offset - offset) & (granularity - 1);
}
static inline int queue_sector_alignment_offset(struct request_queue *q, static inline int queue_sector_alignment_offset(struct request_queue *q,
sector_t sector) sector_t sector)
{ {
return ((sector << 9) - q->limits.alignment_offset) return queue_limit_alignment_offset(&q->limits, sector << 9);
& (q->limits.io_min - 1);
} }
static inline int bdev_alignment_offset(struct block_device *bdev) static inline int bdev_alignment_offset(struct block_device *bdev)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment