1 // SPDX-License-Identifier: GPL-2.0
3 * Functions related to generic helpers functions
5 #include <linux/kernel.h>
6 #include <linux/module.h>
8 #include <linux/blkdev.h>
9 #include <linux/scatterlist.h>
13 static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector)
15 unsigned int discard_granularity = bdev_discard_granularity(bdev);
16 sector_t granularity_aligned_sector;
18 if (bdev_is_partition(bdev))
19 sector += bdev->bd_start_sect;
21 granularity_aligned_sector =
22 round_up(sector, discard_granularity >> SECTOR_SHIFT);
25 * Make sure subsequent bios start aligned to the discard granularity if
26 * it needs to be split.
28 if (granularity_aligned_sector != sector)
29 return granularity_aligned_sector - sector;
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.
35 return round_down(UINT_MAX, discard_granularity) >> SECTOR_SHIFT;
38 static void await_bio_endio(struct bio *bio)
40 complete(bio->bi_private);
45 * await_bio_chain - ends @bio and waits for every chained bio to complete
47 static void await_bio_chain(struct bio *bio)
49 DECLARE_COMPLETION_ONSTACK_MAP(done,
50 bio->bi_bdev->bd_disk->lockdep_map);
52 bio->bi_private = &done;
53 bio->bi_end_io = await_bio_endio;
58 int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
59 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop)
61 struct bio *bio = *biop;
64 if (bdev_read_only(bdev))
66 if (!bdev_max_discard_sectors(bdev))
69 /* In case the discard granularity isn't set by buggy device driver */
70 if (WARN_ON_ONCE(!bdev_discard_granularity(bdev))) {
71 pr_err_ratelimited("%pg: Error: discard_granularity is 0.\n",
76 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
77 if ((sector | nr_sects) & bs_mask)
85 min(nr_sects, bio_discard_limit(bdev, sector));
87 bio = blk_next_bio(bio, bdev, 0, REQ_OP_DISCARD, gfp_mask);
88 bio->bi_iter.bi_sector = sector;
89 bio->bi_iter.bi_size = req_sects << 9;
91 nr_sects -= req_sects;
94 * We can loop for a long time in here, if someone does
95 * full device discards (like mkfs). Be nice and allow
96 * us to schedule out to avoid softlocking if preempt
100 if (fatal_signal_pending(current)) {
101 await_bio_chain(bio);
109 EXPORT_SYMBOL(__blkdev_issue_discard);
112 * blkdev_issue_discard - queue a discard
113 * @bdev: blockdev to issue discard for
114 * @sector: start sector
115 * @nr_sects: number of sectors to discard
116 * @gfp_mask: memory allocation flags (for bio_alloc)
119 * Issue a discard request for the sectors in question.
121 int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
122 sector_t nr_sects, gfp_t gfp_mask)
124 struct bio *bio = NULL;
125 struct blk_plug plug;
128 blk_start_plug(&plug);
129 ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, &bio);
131 ret = submit_bio_wait(bio);
132 if (ret == -EOPNOTSUPP)
136 blk_finish_plug(&plug);
140 EXPORT_SYMBOL(blkdev_issue_discard);
142 static int __blkdev_issue_write_zeroes(struct block_device *bdev,
143 sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
144 struct bio **biop, unsigned flags)
146 struct bio *bio = *biop;
147 unsigned int max_sectors;
149 if (bdev_read_only(bdev))
152 /* Ensure that max_sectors doesn't overflow bi_size */
153 max_sectors = bdev_write_zeroes_sectors(bdev);
155 if (max_sectors == 0)
159 unsigned int len = min_t(sector_t, nr_sects, max_sectors);
161 bio = blk_next_bio(bio, bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask);
162 bio->bi_iter.bi_sector = sector;
163 if (flags & BLKDEV_ZERO_NOUNMAP)
164 bio->bi_opf |= REQ_NOUNMAP;
166 bio->bi_iter.bi_size = len << SECTOR_SHIFT;
170 if (fatal_signal_pending(current)) {
171 await_bio_chain(bio);
181 * Convert a number of 512B sectors to a number of pages.
182 * The result is limited to a number of pages that can fit into a BIO.
183 * Also make sure that the result is always at least 1 (page) for the cases
184 * where nr_sects is lower than the number of sectors in a page.
186 static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects)
188 sector_t pages = DIV_ROUND_UP_SECTOR_T(nr_sects, PAGE_SIZE / 512);
190 return min(pages, (sector_t)BIO_MAX_VECS);
193 static int __blkdev_issue_zero_pages(struct block_device *bdev,
194 sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
197 struct bio *bio = *biop;
201 if (bdev_read_only(bdev))
204 while (nr_sects != 0) {
205 bio = blk_next_bio(bio, bdev, __blkdev_sectors_to_bio_pages(nr_sects),
206 REQ_OP_WRITE, gfp_mask);
207 bio->bi_iter.bi_sector = sector;
209 while (nr_sects != 0) {
210 sz = min((sector_t) PAGE_SIZE, nr_sects << 9);
211 bi_size = bio_add_page(bio, ZERO_PAGE(0), sz, 0);
212 nr_sects -= bi_size >> 9;
213 sector += bi_size >> 9;
218 if (fatal_signal_pending(current)) {
219 await_bio_chain(bio);
229 * __blkdev_issue_zeroout - generate number of zero filed write bios
230 * @bdev: blockdev to issue
231 * @sector: start sector
232 * @nr_sects: number of sectors to write
233 * @gfp_mask: memory allocation flags (for bio_alloc)
234 * @biop: pointer to anchor bio
235 * @flags: controls detailed behavior
238 * Zero-fill a block range, either using hardware offload or by explicitly
239 * writing zeroes to the device.
241 * If a device is using logical block provisioning, the underlying space will
242 * not be released if %flags contains BLKDEV_ZERO_NOUNMAP.
244 * If %flags contains BLKDEV_ZERO_NOFALLBACK, the function will return
245 * -EOPNOTSUPP if no explicit hardware offload for zeroing is provided.
247 int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
248 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
254 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
255 if ((sector | nr_sects) & bs_mask)
258 ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask,
260 if (ret != -EOPNOTSUPP || (flags & BLKDEV_ZERO_NOFALLBACK))
263 return __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask,
266 EXPORT_SYMBOL(__blkdev_issue_zeroout);
269 * blkdev_issue_zeroout - zero-fill a block range
270 * @bdev: blockdev to write
271 * @sector: start sector
272 * @nr_sects: number of sectors to write
273 * @gfp_mask: memory allocation flags (for bio_alloc)
274 * @flags: controls detailed behavior
277 * Zero-fill a block range, either using hardware offload or by explicitly
278 * writing zeroes to the device. See __blkdev_issue_zeroout() for the
279 * valid values for %flags.
281 int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
282 sector_t nr_sects, gfp_t gfp_mask, unsigned flags)
287 struct blk_plug plug;
288 bool try_write_zeroes = !!bdev_write_zeroes_sectors(bdev);
290 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
291 if ((sector | nr_sects) & bs_mask)
296 blk_start_plug(&plug);
297 if (try_write_zeroes) {
298 ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects,
299 gfp_mask, &bio, flags);
300 } else if (!(flags & BLKDEV_ZERO_NOFALLBACK)) {
301 ret = __blkdev_issue_zero_pages(bdev, sector, nr_sects,
304 /* No zeroing offload support */
307 if (ret == 0 && bio) {
308 ret = submit_bio_wait(bio);
311 blk_finish_plug(&plug);
312 if (ret && ret != -EINTR && try_write_zeroes) {
313 if (!(flags & BLKDEV_ZERO_NOFALLBACK)) {
314 try_write_zeroes = false;
317 if (!bdev_write_zeroes_sectors(bdev)) {
319 * Zeroing offload support was indicated, but the
320 * device reported ILLEGAL REQUEST (for some devices
321 * there is no non-destructive way to verify whether
322 * WRITE ZEROES is actually supported).
330 EXPORT_SYMBOL(blkdev_issue_zeroout);
332 int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
333 sector_t nr_sects, gfp_t gfp)
335 sector_t bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
336 unsigned int max_sectors = bdev_max_secure_erase_sectors(bdev);
337 struct bio *bio = NULL;
338 struct blk_plug plug;
341 /* make sure that "len << SECTOR_SHIFT" doesn't overflow */
342 if (max_sectors > UINT_MAX >> SECTOR_SHIFT)
343 max_sectors = UINT_MAX >> SECTOR_SHIFT;
344 max_sectors &= ~bs_mask;
346 if (max_sectors == 0)
348 if ((sector | nr_sects) & bs_mask)
350 if (bdev_read_only(bdev))
353 blk_start_plug(&plug);
355 unsigned int len = min_t(sector_t, nr_sects, max_sectors);
357 bio = blk_next_bio(bio, bdev, 0, REQ_OP_SECURE_ERASE, gfp);
358 bio->bi_iter.bi_sector = sector;
359 bio->bi_iter.bi_size = len << SECTOR_SHIFT;
364 if (fatal_signal_pending(current)) {
365 await_bio_chain(bio);
372 ret = submit_bio_wait(bio);
375 blk_finish_plug(&plug);
379 EXPORT_SYMBOL(blkdev_issue_secure_erase);