aboutsummaryrefslogtreecommitdiffstats
path: root/block/blk-lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'block/blk-lib.c')
-rw-r--r--block/blk-lib.c23
1 files changed, 13 insertions, 10 deletions
diff --git a/block/blk-lib.c b/block/blk-lib.c
index 9373b58dfab1..5677fd33d7d2 100644
--- a/block/blk-lib.c
+++ b/block/blk-lib.c
@@ -43,8 +43,8 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
43 DECLARE_COMPLETION_ONSTACK(wait); 43 DECLARE_COMPLETION_ONSTACK(wait);
44 struct request_queue *q = bdev_get_queue(bdev); 44 struct request_queue *q = bdev_get_queue(bdev);
45 int type = REQ_WRITE | REQ_DISCARD; 45 int type = REQ_WRITE | REQ_DISCARD;
46 unsigned int max_discard_sectors; 46 sector_t max_discard_sectors;
47 unsigned int granularity, alignment, mask; 47 sector_t granularity, alignment;
48 struct bio_batch bb; 48 struct bio_batch bb;
49 struct bio *bio; 49 struct bio *bio;
50 int ret = 0; 50 int ret = 0;
@@ -57,15 +57,16 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
57 57
58 /* Zero-sector (unknown) and one-sector granularities are the same. */ 58 /* Zero-sector (unknown) and one-sector granularities are the same. */
59 granularity = max(q->limits.discard_granularity >> 9, 1U); 59 granularity = max(q->limits.discard_granularity >> 9, 1U);
60 mask = granularity - 1; 60 alignment = bdev_discard_alignment(bdev) >> 9;
61 alignment = (bdev_discard_alignment(bdev) >> 9) & mask; 61 alignment = sector_div(alignment, granularity);
62 62
63 /* 63 /*
64 * Ensure that max_discard_sectors is of the proper 64 * Ensure that max_discard_sectors is of the proper
65 * granularity, so that requests stay aligned after a split. 65 * granularity, so that requests stay aligned after a split.
66 */ 66 */
67 max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9); 67 max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
68 max_discard_sectors = round_down(max_discard_sectors, granularity); 68 sector_div(max_discard_sectors, granularity);
69 max_discard_sectors *= granularity;
69 if (unlikely(!max_discard_sectors)) { 70 if (unlikely(!max_discard_sectors)) {
70 /* Avoid infinite loop below. Being cautious never hurts. */ 71 /* Avoid infinite loop below. Being cautious never hurts. */
71 return -EOPNOTSUPP; 72 return -EOPNOTSUPP;
@@ -83,7 +84,7 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
83 84
84 while (nr_sects) { 85 while (nr_sects) {
85 unsigned int req_sects; 86 unsigned int req_sects;
86 sector_t end_sect; 87 sector_t end_sect, tmp;
87 88
88 bio = bio_alloc(gfp_mask, 1); 89 bio = bio_alloc(gfp_mask, 1);
89 if (!bio) { 90 if (!bio) {
@@ -98,10 +99,12 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
98 * misaligned, stop the discard at the previous aligned sector. 99 * misaligned, stop the discard at the previous aligned sector.
99 */ 100 */
100 end_sect = sector + req_sects; 101 end_sect = sector + req_sects;
101 if (req_sects < nr_sects && (end_sect & mask) != alignment) { 102 tmp = end_sect;
102 end_sect = 103 if (req_sects < nr_sects &&
103 round_down(end_sect - alignment, granularity) 104 sector_div(tmp, granularity) != alignment) {
104 + alignment; 105 end_sect = end_sect - alignment;
106 sector_div(end_sect, granularity);
107 end_sect = end_sect * granularity + alignment;
105 req_sects = end_sect - sector; 108 req_sects = end_sect - sector;
106 } 109 }
107 110