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.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/block/blk-lib.c b/block/blk-lib.c
index 2b461b496a78..16b06f62e68c 100644
--- a/block/blk-lib.c
+++ b/block/blk-lib.c
@@ -44,6 +44,7 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
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 unsigned int max_discard_sectors;
47 unsigned int granularity;
47 struct bio_batch bb; 48 struct bio_batch bb;
48 struct bio *bio; 49 struct bio *bio;
49 int ret = 0; 50 int ret = 0;
@@ -54,18 +55,18 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
54 if (!blk_queue_discard(q)) 55 if (!blk_queue_discard(q))
55 return -EOPNOTSUPP; 56 return -EOPNOTSUPP;
56 57
58 /* Zero-sector (unknown) and one-sector granularities are the same. */
59 granularity = max(q->limits.discard_granularity >> 9, 1U);
60
57 /* 61 /*
58 * Ensure that max_discard_sectors is of the proper 62 * Ensure that max_discard_sectors is of the proper
59 * granularity 63 * granularity
60 */ 64 */
61 max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9); 65 max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
66 max_discard_sectors = round_down(max_discard_sectors, granularity);
62 if (unlikely(!max_discard_sectors)) { 67 if (unlikely(!max_discard_sectors)) {
63 /* Avoid infinite loop below. Being cautious never hurts. */ 68 /* Avoid infinite loop below. Being cautious never hurts. */
64 return -EOPNOTSUPP; 69 return -EOPNOTSUPP;
65 } else if (q->limits.discard_granularity) {
66 unsigned int disc_sects = q->limits.discard_granularity >> 9;
67
68 max_discard_sectors &= ~(disc_sects - 1);
69 } 70 }
70 71
71 if (flags & BLKDEV_DISCARD_SECURE) { 72 if (flags & BLKDEV_DISCARD_SECURE) {