As Jeff Moyer pointed out we do not honor discard granularity while submitting REQ_DISCARD bios of size smaller than max_discard_sectors. That fact might have unwanted consequences of device ignoring the request, or even worse if device firmware is buggy. This commit changes the behaviour so all discard bios smaller than max_discard_sectors are aligned properly wrt discard_granularity. Signed-off-by: Lukas Czerner <lczerner@xxxxxxxxxx> Reported-by: Jeff Moyer <jmoyer@xxxxxxxxxx> CC: Dmitry Monakhov <dmonakhov@xxxxxxxxxx> CC: Jens Axboe <jaxboe@xxxxxxxxxxxx> --- block/blk-lib.c | 10 ++++------ 1 files changed, 4 insertions(+), 6 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index d7a98d3..4019036 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -46,7 +46,7 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, DECLARE_COMPLETION_ONSTACK(wait); struct request_queue *q = bdev_get_queue(bdev); int type = REQ_WRITE | REQ_DISCARD; - unsigned int max_discard_sectors; + unsigned int max_discard_sectors, disc_sects; struct bio_batch bb; struct bio *bio; int ret = 0; @@ -57,16 +57,14 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, if (!blk_queue_discard(q)) return -EOPNOTSUPP; + disc_sects = q->limits.discard_granularity >> 9; /* * Ensure that max_discard_sectors is of the proper * granularity */ max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9); - if (q->limits.discard_granularity) { - unsigned int disc_sects = q->limits.discard_granularity >> 9; - + if (q->limits.discard_granularity) max_discard_sectors &= ~(disc_sects - 1); - } if (flags & BLKDEV_DISCARD_SECURE) { if (!blk_queue_secdiscard(q)) @@ -95,7 +93,7 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, nr_sects -= max_discard_sectors; sector += max_discard_sectors; } else { - bio->bi_size = nr_sects << 9; + bio->bi_size = ((nr_sects & ~(disc_sects - 1)) << 9); nr_sects = 0; } -- 1.7.4.4 -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html