mptcp: disable incompatible-pointer-types warnings
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / block / blk-lib.c
CommitLineData
f31e7e40
DM
1/*
2 * Functions related to generic helpers functions
3 */
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/bio.h>
7#include <linux/blkdev.h>
8#include <linux/scatterlist.h>
9
10#include "blk.h"
11
5dba3089
LC
12struct bio_batch {
13 atomic_t done;
4246a0b6 14 int error;
5dba3089
LC
15 struct completion *wait;
16};
17
4246a0b6 18static void bio_batch_end_io(struct bio *bio)
f31e7e40 19{
5dba3089
LC
20 struct bio_batch *bb = bio->bi_private;
21
4246a0b6
CH
22 if (bio->bi_error && bio->bi_error != -EOPNOTSUPP)
23 bb->error = bio->bi_error;
5dba3089
LC
24 if (atomic_dec_and_test(&bb->done))
25 complete(bb->wait);
f31e7e40
DM
26 bio_put(bio);
27}
28
1cac41cb
MB
29static struct bio *next_bio(struct bio *bio, unsigned int nr_pages,
30 int type, gfp_t gfp)
31{
32 struct bio *new = bio_alloc(gfp, nr_pages);
33
34 if (bio) {
35 bio_chain(bio, new);
36 submit_bio(type, bio);
37 }
38
39 return new;
40}
41
42int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
43 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags,
44 struct bio **biop)
f31e7e40 45{
f31e7e40 46 struct request_queue *q = bdev_get_queue(bdev);
1cac41cb
MB
47 struct bio *bio = *biop;
48 int type = REQ_WRITE | REQ_DISCARD | REQ_PRIO;
a22c4d7e
ML
49 unsigned int granularity;
50 int alignment;
1cac41cb 51 sector_t bs_mask;
f31e7e40
DM
52
53 if (!q)
54 return -ENXIO;
55
56 if (!blk_queue_discard(q))
57 return -EOPNOTSUPP;
58
1cac41cb
MB
59 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
60 if ((sector | nr_sects) & bs_mask)
61 return -EINVAL;
62
63 /* Zero-sector (unknown) and one-sector granularities are the same. */
a22c4d7e
ML
64 granularity = max(q->limits.discard_granularity >> 9, 1U);
65 alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
66
dd3932ed 67 if (flags & BLKDEV_DISCARD_SECURE) {
8d57a98c
AH
68 if (!blk_queue_secdiscard(q))
69 return -EOPNOTSUPP;
8c555367 70 type |= REQ_SECURE;
8d57a98c
AH
71 }
72
1cac41cb
MB
73 if (flags & BLKDEV_DISCARD_SYNC)
74 type |= REQ_SYNC;
5dba3089
LC
75
76 while (nr_sects) {
c6e66634 77 unsigned int req_sects;
a22c4d7e 78 sector_t end_sect, tmp;
c6e66634 79
a22c4d7e
ML
80 /* Make sure bi_size doesn't overflow */
81 req_sects = min_t(sector_t, nr_sects, UINT_MAX >> 9);
82
1cac41cb 83 /**
a22c4d7e
ML
84 * If splitting a request, and the next starting sector would be
85 * misaligned, stop the discard at the previous aligned sector.
86 */
c6e66634 87 end_sect = sector + req_sects;
a22c4d7e
ML
88 tmp = end_sect;
89 if (req_sects < nr_sects &&
90 sector_div(tmp, granularity) != alignment) {
91 end_sect = end_sect - alignment;
92 sector_div(end_sect, granularity);
93 end_sect = end_sect * granularity + alignment;
94 req_sects = end_sect - sector;
95 }
c6e66634 96
1cac41cb 97 bio = next_bio(bio, 1, type, gfp_mask);
4f024f37 98 bio->bi_iter.bi_sector = sector;
f31e7e40 99 bio->bi_bdev = bdev;
f31e7e40 100
4f024f37 101 bio->bi_iter.bi_size = req_sects << 9;
c6e66634
PB
102 nr_sects -= req_sects;
103 sector = end_sect;
f31e7e40 104
c8123f8c
JA
105 /*
106 * We can loop for a long time in here, if someone does
107 * full device discards (like mkfs). Be nice and allow
108 * us to schedule out to avoid softlocking if preempt
109 * is disabled.
110 */
111 cond_resched();
5dba3089 112 }
f31e7e40 113
1cac41cb
MB
114 *biop = bio;
115 return 0;
116}
117EXPORT_SYMBOL(__blkdev_issue_discard);
118
119/**
120 * blkdev_issue_discard - queue a discard
121 * @bdev: blockdev to issue discard for
122 * @sector: start sector
123 * @nr_sects: number of sectors to discard
124 * @gfp_mask: memory allocation flags (for bio_alloc)
125 * @flags: BLKDEV_IFL_* flags to control behaviour
126 *
127 * Description:
128 * Issue a discard request for the sectors in question.
129 */
130int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
131 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
132{
133 struct bio *bio = NULL;
134 struct blk_plug plug;
135 int type = REQ_WRITE | REQ_DISCARD | REQ_PRIO;
136 int ret;
137
138 blk_start_plug(&plug);
139 ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, flags,
140 &bio);
141 if (!ret && bio) {
142 ret = submit_bio_wait(type, bio);
143 if (ret == -EOPNOTSUPP)
144 ret = 0;
145 bio_put(bio);
146 }
147 blk_finish_plug(&plug);
f31e7e40 148
f31e7e40 149 return ret;
f31e7e40
DM
150}
151EXPORT_SYMBOL(blkdev_issue_discard);
3f14d792 152
4363ac7c
MP
153/**
154 * blkdev_issue_write_same - queue a write same operation
155 * @bdev: target blockdev
156 * @sector: start sector
157 * @nr_sects: number of sectors to write
158 * @gfp_mask: memory allocation flags (for bio_alloc)
159 * @page: page containing data to write
160 *
161 * Description:
162 * Issue a write same request for the sectors in question.
163 */
164int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
165 sector_t nr_sects, gfp_t gfp_mask,
166 struct page *page)
167{
168 DECLARE_COMPLETION_ONSTACK(wait);
169 struct request_queue *q = bdev_get_queue(bdev);
170 unsigned int max_write_same_sectors;
171 struct bio_batch bb;
172 struct bio *bio;
173 int ret = 0;
174
175 if (!q)
176 return -ENXIO;
177
b49a0871
ML
178 /* Ensure that max_write_same_sectors doesn't overflow bi_size */
179 max_write_same_sectors = UINT_MAX >> 9;
4363ac7c
MP
180
181 atomic_set(&bb.done, 1);
4246a0b6 182 bb.error = 0;
4363ac7c
MP
183 bb.wait = &wait;
184
185 while (nr_sects) {
186 bio = bio_alloc(gfp_mask, 1);
187 if (!bio) {
188 ret = -ENOMEM;
189 break;
190 }
191
4f024f37 192 bio->bi_iter.bi_sector = sector;
4363ac7c
MP
193 bio->bi_end_io = bio_batch_end_io;
194 bio->bi_bdev = bdev;
195 bio->bi_private = &bb;
196 bio->bi_vcnt = 1;
197 bio->bi_io_vec->bv_page = page;
198 bio->bi_io_vec->bv_offset = 0;
199 bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
200
201 if (nr_sects > max_write_same_sectors) {
4f024f37 202 bio->bi_iter.bi_size = max_write_same_sectors << 9;
4363ac7c
MP
203 nr_sects -= max_write_same_sectors;
204 sector += max_write_same_sectors;
205 } else {
4f024f37 206 bio->bi_iter.bi_size = nr_sects << 9;
4363ac7c
MP
207 nr_sects = 0;
208 }
209
210 atomic_inc(&bb.done);
211 submit_bio(REQ_WRITE | REQ_WRITE_SAME, bio);
212 }
213
214 /* Wait for bios in-flight */
215 if (!atomic_dec_and_test(&bb.done))
5577022f 216 wait_for_completion_io(&wait);
4363ac7c 217
4246a0b6
CH
218 if (bb.error)
219 return bb.error;
4363ac7c
MP
220 return ret;
221}
222EXPORT_SYMBOL(blkdev_issue_write_same);
223
3f14d792 224/**
291d24f6 225 * blkdev_issue_zeroout - generate number of zero filed write bios
3f14d792
DM
226 * @bdev: blockdev to issue
227 * @sector: start sector
228 * @nr_sects: number of sectors to write
229 * @gfp_mask: memory allocation flags (for bio_alloc)
3f14d792
DM
230 *
231 * Description:
232 * Generate and issue number of bios with zerofiled pages.
3f14d792
DM
233 */
234
35086784
FF
235static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
236 sector_t nr_sects, gfp_t gfp_mask)
3f14d792 237{
18edc8ea 238 int ret;
3f14d792
DM
239 struct bio *bio;
240 struct bio_batch bb;
0aeea189 241 unsigned int sz;
3f14d792
DM
242 DECLARE_COMPLETION_ONSTACK(wait);
243
0aeea189 244 atomic_set(&bb.done, 1);
4246a0b6 245 bb.error = 0;
3f14d792 246 bb.wait = &wait;
3f14d792 247
18edc8ea 248 ret = 0;
3f14d792
DM
249 while (nr_sects != 0) {
250 bio = bio_alloc(gfp_mask,
251 min(nr_sects, (sector_t)BIO_MAX_PAGES));
18edc8ea
DM
252 if (!bio) {
253 ret = -ENOMEM;
3f14d792 254 break;
18edc8ea 255 }
3f14d792 256
4f024f37 257 bio->bi_iter.bi_sector = sector;
3f14d792
DM
258 bio->bi_bdev = bdev;
259 bio->bi_end_io = bio_batch_end_io;
dd3932ed 260 bio->bi_private = &bb;
3f14d792 261
0341aafb
JA
262 while (nr_sects != 0) {
263 sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
3f14d792
DM
264 ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
265 nr_sects -= ret >> 9;
266 sector += ret >> 9;
267 if (ret < (sz << 9))
268 break;
269 }
18edc8ea 270 ret = 0;
0aeea189 271 atomic_inc(&bb.done);
3f14d792
DM
272 submit_bio(WRITE, bio);
273 }
3f14d792 274
dd3932ed 275 /* Wait for bios in-flight */
0aeea189 276 if (!atomic_dec_and_test(&bb.done))
5577022f 277 wait_for_completion_io(&wait);
3f14d792 278
4246a0b6
CH
279 if (bb.error)
280 return bb.error;
3f14d792
DM
281 return ret;
282}
579e8f3c
MP
283
284/**
285 * blkdev_issue_zeroout - zero-fill a block range
286 * @bdev: blockdev to write
287 * @sector: start sector
288 * @nr_sects: number of sectors to write
289 * @gfp_mask: memory allocation flags (for bio_alloc)
d93ba7a5 290 * @discard: whether to discard the block range
579e8f3c
MP
291 *
292 * Description:
d93ba7a5
MP
293 * Zero-fill a block range. If the discard flag is set and the block
294 * device guarantees that subsequent READ operations to the block range
295 * in question will return zeroes, the blocks will be discarded. Should
296 * the discard request fail, if the discard flag is not set, or if
297 * discard_zeroes_data is not supported, this function will resort to
298 * zeroing the blocks manually, thus provisioning (allocating,
299 * anchoring) them. If the block device supports the WRITE SAME command
300 * blkdev_issue_zeroout() will use it to optimize the process of
301 * clearing the block range. Otherwise the zeroing will be performed
302 * using regular WRITE calls.
579e8f3c
MP
303 */
304
305int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
d93ba7a5 306 sector_t nr_sects, gfp_t gfp_mask, bool discard)
579e8f3c 307{
d93ba7a5 308 struct request_queue *q = bdev_get_queue(bdev);
d93ba7a5 309
9f9ee1f2
MP
310 if (discard && blk_queue_discard(q) && q->limits.discard_zeroes_data &&
311 blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, 0) == 0)
312 return 0;
d93ba7a5 313
9f9ee1f2
MP
314 if (bdev_write_same(bdev) &&
315 blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask,
316 ZERO_PAGE(0)) == 0)
317 return 0;
579e8f3c
MP
318
319 return __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask);
320}
3f14d792 321EXPORT_SYMBOL(blkdev_issue_zeroout);