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