blk-lib: check for kill signal
[linux-2.6-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
8a08c5fd
KB
38static void await_bio_endio(struct bio *bio)
39{
40 complete(bio->bi_private);
41 bio_put(bio);
42}
43
44/*
45 * await_bio_chain - ends @bio and waits for every chained bio to complete
46 */
47static void await_bio_chain(struct bio *bio)
48{
49 DECLARE_COMPLETION_ONSTACK_MAP(done,
50 bio->bi_bdev->bd_disk->lockdep_map);
51
52 bio->bi_private = &done;
53 bio->bi_end_io = await_bio_endio;
54 bio_endio(bio);
55 blk_wait_io(&done);
56}
57
38f25255 58int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
44abff2c 59 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop)
f31e7e40 60{
38f25255 61 struct bio *bio = *biop;
e3cc28ea 62 sector_t bs_mask;
f31e7e40 63
a13553c7
ID
64 if (bdev_read_only(bdev))
65 return -EPERM;
44abff2c
CH
66 if (!bdev_max_discard_sectors(bdev))
67 return -EOPNOTSUPP;
f31e7e40 68
b35fd742 69 /* In case the discard granularity isn't set by buggy device driver */
7b47ef52 70 if (WARN_ON_ONCE(!bdev_discard_granularity(bdev))) {
02ff3dd2
CH
71 pr_err_ratelimited("%pg: Error: discard_granularity is 0.\n",
72 bdev);
b35fd742
CL
73 return -EOPNOTSUPP;
74 }
75
28b2be20
DW
76 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
77 if ((sector | nr_sects) & bs_mask)
78 return -EINVAL;
79
ba5d7385
ML
80 if (!nr_sects)
81 return -EINVAL;
a22c4d7e 82
ba5d7385 83 while (nr_sects) {
e3cc28ea
CH
84 sector_t req_sects =
85 min(nr_sects, bio_discard_limit(bdev, sector));
4800bf7b 86
44abff2c 87 bio = blk_next_bio(bio, bdev, 0, REQ_OP_DISCARD, gfp_mask);
4f024f37 88 bio->bi_iter.bi_sector = sector;
4f024f37 89 bio->bi_iter.bi_size = req_sects << 9;
ba5d7385 90 sector += req_sects;
c6e66634 91 nr_sects -= req_sects;
f31e7e40 92
c8123f8c
JA
93 /*
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
97 * is disabled.
98 */
99 cond_resched();
8a08c5fd
KB
100 if (fatal_signal_pending(current)) {
101 await_bio_chain(bio);
102 return -EINTR;
103 }
5dba3089 104 }
38f25255
CH
105
106 *biop = bio;
107 return 0;
108}
109EXPORT_SYMBOL(__blkdev_issue_discard);
110
111/**
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)
38f25255
CH
117 *
118 * Description:
119 * Issue a discard request for the sectors in question.
120 */
121int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
44abff2c 122 sector_t nr_sects, gfp_t gfp_mask)
38f25255 123{
38f25255
CH
124 struct bio *bio = NULL;
125 struct blk_plug plug;
126 int ret;
127
38f25255 128 blk_start_plug(&plug);
44abff2c 129 ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, &bio);
bbd848e0 130 if (!ret && bio) {
4e49ea4a 131 ret = submit_bio_wait(bio);
48920ff2 132 if (ret == -EOPNOTSUPP)
bbd848e0 133 ret = 0;
05bd92dd 134 bio_put(bio);
bbd848e0 135 }
0cfbcafc 136 blk_finish_plug(&plug);
f31e7e40 137
bbd848e0 138 return ret;
f31e7e40
DM
139}
140EXPORT_SYMBOL(blkdev_issue_discard);
3f14d792 141
a6f0788e
CK
142static int __blkdev_issue_write_zeroes(struct block_device *bdev,
143 sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
d928be9f 144 struct bio **biop, unsigned flags)
a6f0788e
CK
145{
146 struct bio *bio = *biop;
76a27e1b 147 unsigned int max_sectors;
a6f0788e 148
a13553c7
ID
149 if (bdev_read_only(bdev))
150 return -EPERM;
151
76a27e1b
KB
152 /* Ensure that max_sectors doesn't overflow bi_size */
153 max_sectors = bdev_write_zeroes_sectors(bdev);
a6f0788e 154
76a27e1b 155 if (max_sectors == 0)
a6f0788e
CK
156 return -EOPNOTSUPP;
157
158 while (nr_sects) {
76a27e1b
KB
159 unsigned int len = min_t(sector_t, nr_sects, max_sectors);
160
0a3140ea 161 bio = blk_next_bio(bio, bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask);
a6f0788e 162 bio->bi_iter.bi_sector = sector;
d928be9f
CH
163 if (flags & BLKDEV_ZERO_NOUNMAP)
164 bio->bi_opf |= REQ_NOUNMAP;
a6f0788e 165
76a27e1b
KB
166 bio->bi_iter.bi_size = len << SECTOR_SHIFT;
167 nr_sects -= len;
168 sector += len;
a6f0788e 169 cond_resched();
8a08c5fd
KB
170 if (fatal_signal_pending(current)) {
171 await_bio_chain(bio);
172 return -EINTR;
173 }
a6f0788e
CK
174 }
175
176 *biop = bio;
177 return 0;
178}
179
615d22a5
DLM
180/*
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.
185 */
186static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects)
187{
09c2c359 188 sector_t pages = DIV_ROUND_UP_SECTOR_T(nr_sects, PAGE_SIZE / 512);
615d22a5 189
a8affc03 190 return min(pages, (sector_t)BIO_MAX_VECS);
615d22a5
DLM
191}
192
425a4dba
ID
193static int __blkdev_issue_zero_pages(struct block_device *bdev,
194 sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
195 struct bio **biop)
196{
425a4dba
ID
197 struct bio *bio = *biop;
198 int bi_size = 0;
199 unsigned int sz;
200
a13553c7
ID
201 if (bdev_read_only(bdev))
202 return -EPERM;
203
425a4dba 204 while (nr_sects != 0) {
0a3140ea
CK
205 bio = blk_next_bio(bio, bdev, __blkdev_sectors_to_bio_pages(nr_sects),
206 REQ_OP_WRITE, gfp_mask);
425a4dba 207 bio->bi_iter.bi_sector = sector;
425a4dba
ID
208
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;
214 if (bi_size < sz)
215 break;
216 }
217 cond_resched();
8a08c5fd
KB
218 if (fatal_signal_pending(current)) {
219 await_bio_chain(bio);
220 return -EINTR;
221 }
425a4dba
ID
222 }
223
224 *biop = bio;
225 return 0;
226}
227
3f14d792 228/**
e73c23ff 229 * __blkdev_issue_zeroout - generate number of zero filed write bios
3f14d792
DM
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)
e73c23ff 234 * @biop: pointer to anchor bio
ee472d83 235 * @flags: controls detailed behavior
3f14d792
DM
236 *
237 * Description:
ee472d83
CH
238 * Zero-fill a block range, either using hardware offload or by explicitly
239 * writing zeroes to the device.
240 *
241 * If a device is using logical block provisioning, the underlying space will
242 * not be released if %flags contains BLKDEV_ZERO_NOUNMAP.
cb365b96
CH
243 *
244 * If %flags contains BLKDEV_ZERO_NOFALLBACK, the function will return
245 * -EOPNOTSUPP if no explicit hardware offload for zeroing is provided.
3f14d792 246 */
e73c23ff
CK
247int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
248 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
ee472d83 249 unsigned flags)
3f14d792 250{
18edc8ea 251 int ret;
28b2be20
DW
252 sector_t bs_mask;
253
254 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
255 if ((sector | nr_sects) & bs_mask)
256 return -EINVAL;
3f14d792 257
a6f0788e 258 ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask,
d928be9f 259 biop, flags);
cb365b96 260 if (ret != -EOPNOTSUPP || (flags & BLKDEV_ZERO_NOFALLBACK))
425a4dba 261 return ret;
3f14d792 262
425a4dba
ID
263 return __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask,
264 biop);
3f14d792 265}
e73c23ff 266EXPORT_SYMBOL(__blkdev_issue_zeroout);
579e8f3c
MP
267
268/**
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)
ee472d83 274 * @flags: controls detailed behavior
579e8f3c
MP
275 *
276 * Description:
ee472d83
CH
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.
579e8f3c 280 */
579e8f3c 281int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
ee472d83 282 sector_t nr_sects, gfp_t gfp_mask, unsigned flags)
579e8f3c 283{
d5ce4c31
ID
284 int ret = 0;
285 sector_t bs_mask;
286 struct bio *bio;
e73c23ff 287 struct blk_plug plug;
d5ce4c31 288 bool try_write_zeroes = !!bdev_write_zeroes_sectors(bdev);
d93ba7a5 289
d5ce4c31
ID
290 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
291 if ((sector | nr_sects) & bs_mask)
292 return -EINVAL;
293
294retry:
295 bio = NULL;
e73c23ff 296 blk_start_plug(&plug);
d5ce4c31
ID
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,
302 gfp_mask, &bio);
303 } else {
304 /* No zeroing offload support */
305 ret = -EOPNOTSUPP;
306 }
e73c23ff
CK
307 if (ret == 0 && bio) {
308 ret = submit_bio_wait(bio);
309 bio_put(bio);
310 }
311 blk_finish_plug(&plug);
8a08c5fd 312 if (ret && ret != -EINTR && try_write_zeroes) {
d5ce4c31
ID
313 if (!(flags & BLKDEV_ZERO_NOFALLBACK)) {
314 try_write_zeroes = false;
315 goto retry;
316 }
317 if (!bdev_write_zeroes_sectors(bdev)) {
318 /*
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).
323 */
324 ret = -EOPNOTSUPP;
325 }
326 }
579e8f3c 327
e73c23ff 328 return ret;
579e8f3c 329}
3f14d792 330EXPORT_SYMBOL(blkdev_issue_zeroout);
44abff2c
CH
331
332int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
333 sector_t nr_sects, gfp_t gfp)
334{
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;
339 int ret = 0;
340
c4fa3684
MP
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;
345
44abff2c
CH
346 if (max_sectors == 0)
347 return -EOPNOTSUPP;
348 if ((sector | nr_sects) & bs_mask)
349 return -EINVAL;
350 if (bdev_read_only(bdev))
351 return -EPERM;
352
353 blk_start_plug(&plug);
5affe497 354 while (nr_sects) {
44abff2c
CH
355 unsigned int len = min_t(sector_t, nr_sects, max_sectors);
356
357 bio = blk_next_bio(bio, bdev, 0, REQ_OP_SECURE_ERASE, gfp);
358 bio->bi_iter.bi_sector = sector;
c4fa3684 359 bio->bi_iter.bi_size = len << SECTOR_SHIFT;
44abff2c 360
c4fa3684
MP
361 sector += len;
362 nr_sects -= len;
44abff2c 363 cond_resched();
8a08c5fd
KB
364 if (fatal_signal_pending(current)) {
365 await_bio_chain(bio);
366 ret = -EINTR;
367 bio = NULL;
368 break;
369 }
44abff2c 370 }
5affe497
KB
371 if (bio) {
372 ret = submit_bio_wait(bio);
373 bio_put(bio);
374 }
44abff2c
CH
375 blk_finish_plug(&plug);
376
377 return ret;
378}
379EXPORT_SYMBOL(blkdev_issue_secure_erase);