block: remove the discard_granularity check in __blkdev_issue_discard
[linux-block.git] / block / blk-lib.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
f31e7e40
DM
2/*
3 * Functions related to generic helpers functions
4 */
5#include <linux/kernel.h>
6#include <linux/module.h>
7#include <linux/bio.h>
8#include <linux/blkdev.h>
9#include <linux/scatterlist.h>
10
11#include "blk.h"
12
e3cc28ea
CH
13static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector)
14{
7b47ef52 15 unsigned int discard_granularity = bdev_discard_granularity(bdev);
e3cc28ea
CH
16 sector_t granularity_aligned_sector;
17
18 if (bdev_is_partition(bdev))
19 sector += bdev->bd_start_sect;
20
21 granularity_aligned_sector =
22 round_up(sector, discard_granularity >> SECTOR_SHIFT);
23
24 /*
25 * Make sure subsequent bios start aligned to the discard granularity if
26 * it needs to be split.
27 */
28 if (granularity_aligned_sector != sector)
29 return granularity_aligned_sector - sector;
30
31 /*
32 * Align the bio size to the discard granularity to make splitting the bio
33 * at discard granularity boundaries easier in the driver if needed.
34 */
35 return round_down(UINT_MAX, discard_granularity) >> SECTOR_SHIFT;
36}
37
38f25255 38int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
44abff2c 39 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop)
f31e7e40 40{
38f25255 41 struct bio *bio = *biop;
e3cc28ea 42 sector_t bs_mask;
f31e7e40 43
a13553c7
ID
44 if (bdev_read_only(bdev))
45 return -EPERM;
44abff2c
CH
46 if (!bdev_max_discard_sectors(bdev))
47 return -EOPNOTSUPP;
f31e7e40 48
28b2be20
DW
49 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
50 if ((sector | nr_sects) & bs_mask)
51 return -EINVAL;
52
ba5d7385
ML
53 if (!nr_sects)
54 return -EINVAL;
a22c4d7e 55
ba5d7385 56 while (nr_sects) {
e3cc28ea
CH
57 sector_t req_sects =
58 min(nr_sects, bio_discard_limit(bdev, sector));
4800bf7b 59
44abff2c 60 bio = blk_next_bio(bio, bdev, 0, REQ_OP_DISCARD, gfp_mask);
4f024f37 61 bio->bi_iter.bi_sector = sector;
4f024f37 62 bio->bi_iter.bi_size = req_sects << 9;
ba5d7385 63 sector += req_sects;
c6e66634 64 nr_sects -= req_sects;
f31e7e40 65
c8123f8c
JA
66 /*
67 * We can loop for a long time in here, if someone does
68 * full device discards (like mkfs). Be nice and allow
69 * us to schedule out to avoid softlocking if preempt
70 * is disabled.
71 */
72 cond_resched();
5dba3089 73 }
38f25255
CH
74
75 *biop = bio;
76 return 0;
77}
78EXPORT_SYMBOL(__blkdev_issue_discard);
79
80/**
81 * blkdev_issue_discard - queue a discard
82 * @bdev: blockdev to issue discard for
83 * @sector: start sector
84 * @nr_sects: number of sectors to discard
85 * @gfp_mask: memory allocation flags (for bio_alloc)
38f25255
CH
86 *
87 * Description:
88 * Issue a discard request for the sectors in question.
89 */
90int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
44abff2c 91 sector_t nr_sects, gfp_t gfp_mask)
38f25255 92{
38f25255
CH
93 struct bio *bio = NULL;
94 struct blk_plug plug;
95 int ret;
96
38f25255 97 blk_start_plug(&plug);
44abff2c 98 ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, &bio);
bbd848e0 99 if (!ret && bio) {
4e49ea4a 100 ret = submit_bio_wait(bio);
48920ff2 101 if (ret == -EOPNOTSUPP)
bbd848e0 102 ret = 0;
05bd92dd 103 bio_put(bio);
bbd848e0 104 }
0cfbcafc 105 blk_finish_plug(&plug);
f31e7e40 106
bbd848e0 107 return ret;
f31e7e40
DM
108}
109EXPORT_SYMBOL(blkdev_issue_discard);
3f14d792 110
a6f0788e
CK
111static int __blkdev_issue_write_zeroes(struct block_device *bdev,
112 sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
d928be9f 113 struct bio **biop, unsigned flags)
a6f0788e
CK
114{
115 struct bio *bio = *biop;
76a27e1b 116 unsigned int max_sectors;
a6f0788e 117
a13553c7
ID
118 if (bdev_read_only(bdev))
119 return -EPERM;
120
76a27e1b
KB
121 /* Ensure that max_sectors doesn't overflow bi_size */
122 max_sectors = bdev_write_zeroes_sectors(bdev);
a6f0788e 123
76a27e1b 124 if (max_sectors == 0)
a6f0788e
CK
125 return -EOPNOTSUPP;
126
127 while (nr_sects) {
76a27e1b
KB
128 unsigned int len = min_t(sector_t, nr_sects, max_sectors);
129
0a3140ea 130 bio = blk_next_bio(bio, bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask);
a6f0788e 131 bio->bi_iter.bi_sector = sector;
d928be9f
CH
132 if (flags & BLKDEV_ZERO_NOUNMAP)
133 bio->bi_opf |= REQ_NOUNMAP;
a6f0788e 134
76a27e1b
KB
135 bio->bi_iter.bi_size = len << SECTOR_SHIFT;
136 nr_sects -= len;
137 sector += len;
a6f0788e
CK
138 cond_resched();
139 }
140
141 *biop = bio;
142 return 0;
143}
144
615d22a5
DLM
145/*
146 * Convert a number of 512B sectors to a number of pages.
147 * The result is limited to a number of pages that can fit into a BIO.
148 * Also make sure that the result is always at least 1 (page) for the cases
149 * where nr_sects is lower than the number of sectors in a page.
150 */
151static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects)
152{
09c2c359 153 sector_t pages = DIV_ROUND_UP_SECTOR_T(nr_sects, PAGE_SIZE / 512);
615d22a5 154
a8affc03 155 return min(pages, (sector_t)BIO_MAX_VECS);
615d22a5
DLM
156}
157
425a4dba
ID
158static int __blkdev_issue_zero_pages(struct block_device *bdev,
159 sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
160 struct bio **biop)
161{
425a4dba
ID
162 struct bio *bio = *biop;
163 int bi_size = 0;
164 unsigned int sz;
165
a13553c7
ID
166 if (bdev_read_only(bdev))
167 return -EPERM;
168
425a4dba 169 while (nr_sects != 0) {
0a3140ea
CK
170 bio = blk_next_bio(bio, bdev, __blkdev_sectors_to_bio_pages(nr_sects),
171 REQ_OP_WRITE, gfp_mask);
425a4dba 172 bio->bi_iter.bi_sector = sector;
425a4dba
ID
173
174 while (nr_sects != 0) {
175 sz = min((sector_t) PAGE_SIZE, nr_sects << 9);
176 bi_size = bio_add_page(bio, ZERO_PAGE(0), sz, 0);
177 nr_sects -= bi_size >> 9;
178 sector += bi_size >> 9;
179 if (bi_size < sz)
180 break;
181 }
182 cond_resched();
183 }
184
185 *biop = bio;
186 return 0;
187}
188
3f14d792 189/**
e73c23ff 190 * __blkdev_issue_zeroout - generate number of zero filed write bios
3f14d792
DM
191 * @bdev: blockdev to issue
192 * @sector: start sector
193 * @nr_sects: number of sectors to write
194 * @gfp_mask: memory allocation flags (for bio_alloc)
e73c23ff 195 * @biop: pointer to anchor bio
ee472d83 196 * @flags: controls detailed behavior
3f14d792
DM
197 *
198 * Description:
ee472d83
CH
199 * Zero-fill a block range, either using hardware offload or by explicitly
200 * writing zeroes to the device.
201 *
202 * If a device is using logical block provisioning, the underlying space will
203 * not be released if %flags contains BLKDEV_ZERO_NOUNMAP.
cb365b96
CH
204 *
205 * If %flags contains BLKDEV_ZERO_NOFALLBACK, the function will return
206 * -EOPNOTSUPP if no explicit hardware offload for zeroing is provided.
3f14d792 207 */
e73c23ff
CK
208int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
209 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
ee472d83 210 unsigned flags)
3f14d792 211{
18edc8ea 212 int ret;
28b2be20
DW
213 sector_t bs_mask;
214
215 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
216 if ((sector | nr_sects) & bs_mask)
217 return -EINVAL;
3f14d792 218
a6f0788e 219 ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask,
d928be9f 220 biop, flags);
cb365b96 221 if (ret != -EOPNOTSUPP || (flags & BLKDEV_ZERO_NOFALLBACK))
425a4dba 222 return ret;
3f14d792 223
425a4dba
ID
224 return __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask,
225 biop);
3f14d792 226}
e73c23ff 227EXPORT_SYMBOL(__blkdev_issue_zeroout);
579e8f3c
MP
228
229/**
230 * blkdev_issue_zeroout - zero-fill a block range
231 * @bdev: blockdev to write
232 * @sector: start sector
233 * @nr_sects: number of sectors to write
234 * @gfp_mask: memory allocation flags (for bio_alloc)
ee472d83 235 * @flags: controls detailed behavior
579e8f3c
MP
236 *
237 * Description:
ee472d83
CH
238 * Zero-fill a block range, either using hardware offload or by explicitly
239 * writing zeroes to the device. See __blkdev_issue_zeroout() for the
240 * valid values for %flags.
579e8f3c 241 */
579e8f3c 242int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
ee472d83 243 sector_t nr_sects, gfp_t gfp_mask, unsigned flags)
579e8f3c 244{
d5ce4c31
ID
245 int ret = 0;
246 sector_t bs_mask;
247 struct bio *bio;
e73c23ff 248 struct blk_plug plug;
d5ce4c31 249 bool try_write_zeroes = !!bdev_write_zeroes_sectors(bdev);
d93ba7a5 250
d5ce4c31
ID
251 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
252 if ((sector | nr_sects) & bs_mask)
253 return -EINVAL;
254
255retry:
256 bio = NULL;
e73c23ff 257 blk_start_plug(&plug);
d5ce4c31
ID
258 if (try_write_zeroes) {
259 ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects,
260 gfp_mask, &bio, flags);
261 } else if (!(flags & BLKDEV_ZERO_NOFALLBACK)) {
262 ret = __blkdev_issue_zero_pages(bdev, sector, nr_sects,
263 gfp_mask, &bio);
264 } else {
265 /* No zeroing offload support */
266 ret = -EOPNOTSUPP;
267 }
e73c23ff
CK
268 if (ret == 0 && bio) {
269 ret = submit_bio_wait(bio);
270 bio_put(bio);
271 }
272 blk_finish_plug(&plug);
bf5e3a30 273 if (ret && try_write_zeroes) {
d5ce4c31
ID
274 if (!(flags & BLKDEV_ZERO_NOFALLBACK)) {
275 try_write_zeroes = false;
276 goto retry;
277 }
278 if (!bdev_write_zeroes_sectors(bdev)) {
279 /*
280 * Zeroing offload support was indicated, but the
281 * device reported ILLEGAL REQUEST (for some devices
282 * there is no non-destructive way to verify whether
283 * WRITE ZEROES is actually supported).
284 */
285 ret = -EOPNOTSUPP;
286 }
287 }
579e8f3c 288
e73c23ff 289 return ret;
579e8f3c 290}
3f14d792 291EXPORT_SYMBOL(blkdev_issue_zeroout);
44abff2c
CH
292
293int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
294 sector_t nr_sects, gfp_t gfp)
295{
296 sector_t bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
297 unsigned int max_sectors = bdev_max_secure_erase_sectors(bdev);
298 struct bio *bio = NULL;
299 struct blk_plug plug;
300 int ret = 0;
301
c4fa3684
MP
302 /* make sure that "len << SECTOR_SHIFT" doesn't overflow */
303 if (max_sectors > UINT_MAX >> SECTOR_SHIFT)
304 max_sectors = UINT_MAX >> SECTOR_SHIFT;
305 max_sectors &= ~bs_mask;
306
44abff2c
CH
307 if (max_sectors == 0)
308 return -EOPNOTSUPP;
309 if ((sector | nr_sects) & bs_mask)
310 return -EINVAL;
311 if (bdev_read_only(bdev))
312 return -EPERM;
313
314 blk_start_plug(&plug);
5affe497 315 while (nr_sects) {
44abff2c
CH
316 unsigned int len = min_t(sector_t, nr_sects, max_sectors);
317
318 bio = blk_next_bio(bio, bdev, 0, REQ_OP_SECURE_ERASE, gfp);
319 bio->bi_iter.bi_sector = sector;
c4fa3684 320 bio->bi_iter.bi_size = len << SECTOR_SHIFT;
44abff2c 321
c4fa3684
MP
322 sector += len;
323 nr_sects -= len;
44abff2c
CH
324 cond_resched();
325 }
5affe497
KB
326 if (bio) {
327 ret = submit_bio_wait(bio);
328 bio_put(bio);
329 }
44abff2c
CH
330 blk_finish_plug(&plug);
331
332 return ret;
333}
334EXPORT_SYMBOL(blkdev_issue_secure_erase);