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 struct bio *blk_alloc_discard_bio(struct block_device *bdev,
39 sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask)
41 sector_t bio_sects = min(*nr_sects, bio_discard_limit(bdev, *sector));
47 bio = bio_alloc(bdev, 0, REQ_OP_DISCARD, gfp_mask);
50 bio->bi_iter.bi_sector = *sector;
51 bio->bi_iter.bi_size = bio_sects << SECTOR_SHIFT;
53 *nr_sects -= bio_sects;
55 * We can loop for a long time in here if someone does full device
56 * discards (like mkfs). Be nice and allow us to schedule out to avoid
57 * softlocking if preempt is disabled.
63 int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
64 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop)
68 while ((bio = blk_alloc_discard_bio(bdev, §or, &nr_sects,
70 *biop = bio_chain_and_submit(*biop, bio);
73 EXPORT_SYMBOL(__blkdev_issue_discard);
76 * blkdev_issue_discard - queue a discard
77 * @bdev: blockdev to issue discard for
78 * @sector: start sector
79 * @nr_sects: number of sectors to discard
80 * @gfp_mask: memory allocation flags (for bio_alloc)
83 * Issue a discard request for the sectors in question.
85 int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
86 sector_t nr_sects, gfp_t gfp_mask)
88 struct bio *bio = NULL;
92 blk_start_plug(&plug);
93 ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, &bio);
95 ret = submit_bio_wait(bio);
96 if (ret == -EOPNOTSUPP)
100 blk_finish_plug(&plug);
104 EXPORT_SYMBOL(blkdev_issue_discard);
106 static int __blkdev_issue_write_zeroes(struct block_device *bdev,
107 sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
108 struct bio **biop, unsigned flags)
110 struct bio *bio = *biop;
111 unsigned int max_sectors;
113 if (bdev_read_only(bdev))
116 /* Ensure that max_sectors doesn't overflow bi_size */
117 max_sectors = bdev_write_zeroes_sectors(bdev);
119 if (max_sectors == 0)
123 unsigned int len = min_t(sector_t, nr_sects, max_sectors);
125 bio = blk_next_bio(bio, bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask);
126 bio->bi_iter.bi_sector = sector;
127 if (flags & BLKDEV_ZERO_NOUNMAP)
128 bio->bi_opf |= REQ_NOUNMAP;
130 bio->bi_iter.bi_size = len << SECTOR_SHIFT;
141 * Convert a number of 512B sectors to a number of pages.
142 * The result is limited to a number of pages that can fit into a BIO.
143 * Also make sure that the result is always at least 1 (page) for the cases
144 * where nr_sects is lower than the number of sectors in a page.
146 static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects)
148 sector_t pages = DIV_ROUND_UP_SECTOR_T(nr_sects, PAGE_SIZE / 512);
150 return min(pages, (sector_t)BIO_MAX_VECS);
153 static int __blkdev_issue_zero_pages(struct block_device *bdev,
154 sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
157 struct bio *bio = *biop;
161 if (bdev_read_only(bdev))
164 while (nr_sects != 0) {
165 bio = blk_next_bio(bio, bdev, __blkdev_sectors_to_bio_pages(nr_sects),
166 REQ_OP_WRITE, gfp_mask);
167 bio->bi_iter.bi_sector = sector;
169 while (nr_sects != 0) {
170 sz = min((sector_t) PAGE_SIZE, nr_sects << 9);
171 bi_size = bio_add_page(bio, ZERO_PAGE(0), sz, 0);
172 nr_sects -= bi_size >> 9;
173 sector += bi_size >> 9;
185 * __blkdev_issue_zeroout - generate number of zero filed write bios
186 * @bdev: blockdev to issue
187 * @sector: start sector
188 * @nr_sects: number of sectors to write
189 * @gfp_mask: memory allocation flags (for bio_alloc)
190 * @biop: pointer to anchor bio
191 * @flags: controls detailed behavior
194 * Zero-fill a block range, either using hardware offload or by explicitly
195 * writing zeroes to the device.
197 * If a device is using logical block provisioning, the underlying space will
198 * not be released if %flags contains BLKDEV_ZERO_NOUNMAP.
200 * If %flags contains BLKDEV_ZERO_NOFALLBACK, the function will return
201 * -EOPNOTSUPP if no explicit hardware offload for zeroing is provided.
203 int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
204 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
210 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
211 if ((sector | nr_sects) & bs_mask)
214 ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask,
216 if (ret != -EOPNOTSUPP || (flags & BLKDEV_ZERO_NOFALLBACK))
219 return __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask,
222 EXPORT_SYMBOL(__blkdev_issue_zeroout);
225 * blkdev_issue_zeroout - zero-fill a block range
226 * @bdev: blockdev to write
227 * @sector: start sector
228 * @nr_sects: number of sectors to write
229 * @gfp_mask: memory allocation flags (for bio_alloc)
230 * @flags: controls detailed behavior
233 * Zero-fill a block range, either using hardware offload or by explicitly
234 * writing zeroes to the device. See __blkdev_issue_zeroout() for the
235 * valid values for %flags.
237 int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
238 sector_t nr_sects, gfp_t gfp_mask, unsigned flags)
243 struct blk_plug plug;
244 bool try_write_zeroes = !!bdev_write_zeroes_sectors(bdev);
246 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
247 if ((sector | nr_sects) & bs_mask)
252 blk_start_plug(&plug);
253 if (try_write_zeroes) {
254 ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects,
255 gfp_mask, &bio, flags);
256 } else if (!(flags & BLKDEV_ZERO_NOFALLBACK)) {
257 ret = __blkdev_issue_zero_pages(bdev, sector, nr_sects,
260 /* No zeroing offload support */
263 if (ret == 0 && bio) {
264 ret = submit_bio_wait(bio);
267 blk_finish_plug(&plug);
268 if (ret && try_write_zeroes) {
269 if (!(flags & BLKDEV_ZERO_NOFALLBACK)) {
270 try_write_zeroes = false;
273 if (!bdev_write_zeroes_sectors(bdev)) {
275 * Zeroing offload support was indicated, but the
276 * device reported ILLEGAL REQUEST (for some devices
277 * there is no non-destructive way to verify whether
278 * WRITE ZEROES is actually supported).
286 EXPORT_SYMBOL(blkdev_issue_zeroout);
288 int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
289 sector_t nr_sects, gfp_t gfp)
291 sector_t bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
292 unsigned int max_sectors = bdev_max_secure_erase_sectors(bdev);
293 struct bio *bio = NULL;
294 struct blk_plug plug;
297 /* make sure that "len << SECTOR_SHIFT" doesn't overflow */
298 if (max_sectors > UINT_MAX >> SECTOR_SHIFT)
299 max_sectors = UINT_MAX >> SECTOR_SHIFT;
300 max_sectors &= ~bs_mask;
302 if (max_sectors == 0)
304 if ((sector | nr_sects) & bs_mask)
306 if (bdev_read_only(bdev))
309 blk_start_plug(&plug);
311 unsigned int len = min_t(sector_t, nr_sects, max_sectors);
313 bio = blk_next_bio(bio, bdev, 0, REQ_OP_SECURE_ERASE, gfp);
314 bio->bi_iter.bi_sector = sector;
315 bio->bi_iter.bi_size = len << SECTOR_SHIFT;
322 ret = submit_bio_wait(bio);
325 blk_finish_plug(&plug);
329 EXPORT_SYMBOL(blkdev_issue_secure_erase);