diff mbox

[v3,1/5] block: Introduce blk_round_sect_down()

Message ID 570D5CEB.3030601@sandisk.com (mailing list archive)
State New, archived
Headers show

Commit Message

Bart Van Assche April 12, 2016, 8:39 p.m. UTC
Move the code for rounding down a sector into a new function.
This patch does not change any functionality.

Signed-off-by: Bart Van Assche <bart.vanassche@sandisk.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Cc: Jan Kara <jack@suse.cz>
Cc: Mike Snitzer <snitzer@redhat.com>
Cc: Martin K. Petersen <martin.petersen@oracle.com>
Cc: Dmitry Monakhov <dmonakhov@openvz.org>
Cc: Darrick J. Wong <darrick.wong@oracle.com>
Cc: Sagi Grimberg <sagi@grimberg.me>
---
 block/blk-lib.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

Comments

Hannes Reinecke April 13, 2016, 5:48 a.m. UTC | #1
On 04/12/2016 10:39 PM, Bart Van Assche wrote:
> Move the code for rounding down a sector into a new function.
> This patch does not change any functionality.
> 
> Signed-off-by: Bart Van Assche <bart.vanassche@sandisk.com>
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> Cc: Jan Kara <jack@suse.cz>
> Cc: Mike Snitzer <snitzer@redhat.com>
> Cc: Martin K. Petersen <martin.petersen@oracle.com>
> Cc: Dmitry Monakhov <dmonakhov@openvz.org>
> Cc: Darrick J. Wong <darrick.wong@oracle.com>
> Cc: Sagi Grimberg <sagi@grimberg.me>
> ---
>  block/blk-lib.c | 32 +++++++++++++++++++++++++-------
>  1 file changed, 25 insertions(+), 7 deletions(-)
> 
> diff --git a/block/blk-lib.c b/block/blk-lib.c
> index 9ebf653..2f4c6b2 100644
> --- a/block/blk-lib.c
> +++ b/block/blk-lib.c
> @@ -26,6 +26,26 @@ static void bio_batch_end_io(struct bio *bio)
>  	bio_put(bio);
>  }
>  
> +/*
> + * Return the largest number that is less than or equal to @s and for which
> + * the remainder of the division by @granularity is @alignment.
> + */
> +static sector_t blk_round_sect_down(sector_t s, u32 granularity, u32 alignment)
> +{
> +	sector_t tmp = s, res = s;
> +	u32 remainder;
> +
> +	WARN_ON_ONCE(alignment >= granularity);
> +
> +	remainder = sector_div(tmp, granularity);
> +	if (remainder == alignment)
> +		return res;
> +	res -= remainder - alignment;
> +	if (remainder < alignment)
> +		res -= granularity;
> +	return min(res, s);
> +}
> +
>  /**
>   * blkdev_issue_discard - queue a discard
>   * @bdev:	blockdev to issue discard for
> @@ -73,7 +93,7 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
>  	blk_start_plug(&plug);
>  	while (nr_sects) {
>  		unsigned int req_sects;
> -		sector_t end_sect, tmp;
> +		sector_t end_sect;
>  
>  		bio = bio_alloc(gfp_mask, 1);
>  		if (!bio) {
> @@ -89,12 +109,10 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
>  		 * misaligned, stop the discard at the previous aligned sector.
>  		 */
>  		end_sect = sector + req_sects;
> -		tmp = end_sect;
> -		if (req_sects < nr_sects &&
> -		    sector_div(tmp, granularity) != alignment) {
> -			end_sect = end_sect - alignment;
> -			sector_div(end_sect, granularity);
> -			end_sect = end_sect * granularity + alignment;
> +		if (req_sects < nr_sects) {
> +			end_sect = blk_round_sect_down(end_sect, granularity,
> +						       alignment);
> +			WARN_ON_ONCE(end_sect < sector);
>  			req_sects = end_sect - sector;
>  		}
>  
> 
Reviewed-by: Hannes Reinecke <hare@suse.com>

Cheers,

Hannes
diff mbox

Patch

diff --git a/block/blk-lib.c b/block/blk-lib.c
index 9ebf653..2f4c6b2 100644
--- a/block/blk-lib.c
+++ b/block/blk-lib.c
@@ -26,6 +26,26 @@  static void bio_batch_end_io(struct bio *bio)
 	bio_put(bio);
 }
 
+/*
+ * Return the largest number that is less than or equal to @s and for which
+ * the remainder of the division by @granularity is @alignment.
+ */
+static sector_t blk_round_sect_down(sector_t s, u32 granularity, u32 alignment)
+{
+	sector_t tmp = s, res = s;
+	u32 remainder;
+
+	WARN_ON_ONCE(alignment >= granularity);
+
+	remainder = sector_div(tmp, granularity);
+	if (remainder == alignment)
+		return res;
+	res -= remainder - alignment;
+	if (remainder < alignment)
+		res -= granularity;
+	return min(res, s);
+}
+
 /**
  * blkdev_issue_discard - queue a discard
  * @bdev:	blockdev to issue discard for
@@ -73,7 +93,7 @@  int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 	blk_start_plug(&plug);
 	while (nr_sects) {
 		unsigned int req_sects;
-		sector_t end_sect, tmp;
+		sector_t end_sect;
 
 		bio = bio_alloc(gfp_mask, 1);
 		if (!bio) {
@@ -89,12 +109,10 @@  int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 		 * misaligned, stop the discard at the previous aligned sector.
 		 */
 		end_sect = sector + req_sects;
-		tmp = end_sect;
-		if (req_sects < nr_sects &&
-		    sector_div(tmp, granularity) != alignment) {
-			end_sect = end_sect - alignment;
-			sector_div(end_sect, granularity);
-			end_sect = end_sect * granularity + alignment;
+		if (req_sects < nr_sects) {
+			end_sect = blk_round_sect_down(end_sect, granularity,
+						       alignment);
+			WARN_ON_ONCE(end_sect < sector);
 			req_sects = end_sect - sector;
 		}