Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965870AbcCOTnb (ORCPT ); Tue, 15 Mar 2016 15:43:31 -0400 Received: from userp1040.oracle.com ([156.151.31.81]:48096 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965823AbcCOTnX (ORCPT ); Tue, 15 Mar 2016 15:43:23 -0400 Subject: [PATCH 2/3] block: require write_same and discard requests align to logical block size From: "Darrick J. Wong" To: axboe@kernel.dk, torvalds@linux-foundation.org, darrick.wong@oracle.com Cc: bfields@fieldses.org, tytso@mit.edu, akpm@linux-foundation.org, martin.petersen@oracle.com, linux-api@vger.kernel.org, david@fromorbit.com, linux-kernel@vger.kernel.org, shane.seymour@hpe.com, hch@infradead.org, linux-fsdevel@vger.kernel.org, jlayton@poochiereds.net, Christoph Hellwig Date: Tue, 15 Mar 2016 12:42:36 -0700 Message-ID: <20160315194236.30093.87681.stgit@birch.djwong.org> In-Reply-To: <20160315194221.30093.70506.stgit@birch.djwong.org> References: <20160315194221.30093.70506.stgit@birch.djwong.org> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Source-IP: userv0021.oracle.com [156.151.31.71] Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2303 Lines: 70 Make sure that the offset and length arguments that we're using to construct WRITE SAME and DISCARD requests are actually aligned to the logical block size. Failure to do this causes other errors in other parts of the block layer or the SCSI layer because disks don't support partial logical block writes. Signed-off-by: Darrick J. Wong Reviewed-by: Christoph Hellwig --- block/blk-lib.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/block/blk-lib.c b/block/blk-lib.c index 9ebf653..9dca6bb 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -49,6 +49,7 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, struct bio *bio; int ret = 0; struct blk_plug plug; + sector_t bs_mask; if (!q) return -ENXIO; @@ -56,6 +57,10 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, if (!blk_queue_discard(q)) return -EOPNOTSUPP; + bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; + if ((sector | nr_sects) & bs_mask) + return -EINVAL; + /* Zero-sector (unknown) and one-sector granularities are the same. */ granularity = max(q->limits.discard_granularity >> 9, 1U); alignment = (bdev_discard_alignment(bdev) >> 9) % granularity; @@ -148,6 +153,7 @@ int blkdev_issue_write_same(struct block_device *bdev, sector_t sector, DECLARE_COMPLETION_ONSTACK(wait); struct request_queue *q = bdev_get_queue(bdev); unsigned int max_write_same_sectors; + sector_t bs_mask; struct bio_batch bb; struct bio *bio; int ret = 0; @@ -155,6 +161,10 @@ int blkdev_issue_write_same(struct block_device *bdev, sector_t sector, if (!q) return -ENXIO; + bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; + if ((sector | nr_sects) & bs_mask) + return -EINVAL; + /* Ensure that max_write_same_sectors doesn't overflow bi_size */ max_write_same_sectors = UINT_MAX >> 9; @@ -218,9 +228,14 @@ static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, int ret; struct bio *bio; struct bio_batch bb; + sector_t bs_mask; unsigned int sz; DECLARE_COMPLETION_ONSTACK(wait); + bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; + if ((sector | nr_sects) & bs_mask) + return -EINVAL; + atomic_set(&bb.done, 1); bb.error = 0; bb.wait = &wait;