block: make sure writesame bio is aligned with logical block size
[sfrench/cifs-2.6.git] / block / blk-lib.c
index d1b9dd03da256f36d0f12f3b6dbc839656de7529..e8b3bb9bf37595acea4ec434b3664f8ad6a8c2b8 100644 (file)
@@ -10,8 +10,7 @@
 
 #include "blk.h"
 
-static struct bio *next_bio(struct bio *bio, unsigned int nr_pages,
-               gfp_t gfp)
+struct bio *blk_next_bio(struct bio *bio, unsigned int nr_pages, gfp_t gfp)
 {
        struct bio *new = bio_alloc(gfp, nr_pages);
 
@@ -29,9 +28,7 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 {
        struct request_queue *q = bdev_get_queue(bdev);
        struct bio *bio = *biop;
-       unsigned int granularity;
        unsigned int op;
-       int alignment;
        sector_t bs_mask;
 
        if (!q)
@@ -54,47 +51,21 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
        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;
+       if (!nr_sects)
+               return -EINVAL;
 
        while (nr_sects) {
-               unsigned int req_sects;
-               sector_t end_sect, tmp;
-
-               /*
-                * Issue in chunks of the user defined max discard setting,
-                * ensuring that bi_size doesn't overflow
-                */
-               req_sects = min_t(sector_t, nr_sects,
-                                       q->limits.max_discard_sectors);
-               if (!req_sects)
-                       goto fail;
-               if (req_sects > UINT_MAX >> 9)
-                       req_sects = UINT_MAX >> 9;
-
-               /*
-                * If splitting a request, and the next starting sector would be
-                * 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;
-                       req_sects = end_sect - sector;
-               }
+               unsigned int req_sects = min_t(unsigned int, nr_sects,
+                               bio_allowed_max_sectors(q));
 
-               bio = next_bio(bio, 0, gfp_mask);
+               bio = blk_next_bio(bio, 0, gfp_mask);
                bio->bi_iter.bi_sector = sector;
                bio_set_dev(bio, bdev);
                bio_set_op_attrs(bio, op, 0);
 
                bio->bi_iter.bi_size = req_sects << 9;
+               sector += req_sects;
                nr_sects -= req_sects;
-               sector = end_sect;
 
                /*
                 * We can loop for a long time in here, if someone does
@@ -107,14 +78,6 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 
        *biop = bio;
        return 0;
-
-fail:
-       if (bio) {
-               submit_bio_wait(bio);
-               bio_put(bio);
-       }
-       *biop = NULL;
-       return -EOPNOTSUPP;
 }
 EXPORT_SYMBOL(__blkdev_issue_discard);
 
@@ -186,10 +149,10 @@ static int __blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
                return -EOPNOTSUPP;
 
        /* Ensure that max_write_same_sectors doesn't overflow bi_size */
-       max_write_same_sectors = UINT_MAX >> 9;
+       max_write_same_sectors = bio_allowed_max_sectors(q);
 
        while (nr_sects) {
-               bio = next_bio(bio, 1, gfp_mask);
+               bio = blk_next_bio(bio, 1, gfp_mask);
                bio->bi_iter.bi_sector = sector;
                bio_set_dev(bio, bdev);
                bio->bi_vcnt = 1;
@@ -265,7 +228,7 @@ static int __blkdev_issue_write_zeroes(struct block_device *bdev,
                return -EOPNOTSUPP;
 
        while (nr_sects) {
-               bio = next_bio(bio, 0, gfp_mask);
+               bio = blk_next_bio(bio, 0, gfp_mask);
                bio->bi_iter.bi_sector = sector;
                bio_set_dev(bio, bdev);
                bio->bi_opf = REQ_OP_WRITE_ZEROES;
@@ -316,8 +279,8 @@ static int __blkdev_issue_zero_pages(struct block_device *bdev,
                return -EPERM;
 
        while (nr_sects != 0) {
-               bio = next_bio(bio, __blkdev_sectors_to_bio_pages(nr_sects),
-                              gfp_mask);
+               bio = blk_next_bio(bio, __blkdev_sectors_to_bio_pages(nr_sects),
+                                  gfp_mask);
                bio->bi_iter.bi_sector = sector;
                bio_set_dev(bio, bdev);
                bio_set_op_attrs(bio, REQ_OP_WRITE, 0);