]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/blk-lib.c
Merge branch 'fixes' of git://ftp.arm.linux.org.uk/~rmk/linux-arm
[mirror_ubuntu-artful-kernel.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
b49a0871
ML
29/*
30 * Ensure that max discard sectors doesn't overflow bi_size and hopefully
31 * it is of the proper granularity as long as the granularity is a power
32 * of two.
33 */
34#define MAX_BIO_SECTORS ((1U << 31) >> 9)
35
f31e7e40
DM
36/**
37 * blkdev_issue_discard - queue a discard
38 * @bdev: blockdev to issue discard for
39 * @sector: start sector
40 * @nr_sects: number of sectors to discard
41 * @gfp_mask: memory allocation flags (for bio_alloc)
42 * @flags: BLKDEV_IFL_* flags to control behaviour
43 *
44 * Description:
45 * Issue a discard request for the sectors in question.
46 */
47int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
48 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
49{
50 DECLARE_COMPLETION_ONSTACK(wait);
51 struct request_queue *q = bdev_get_queue(bdev);
8c555367 52 int type = REQ_WRITE | REQ_DISCARD;
5dba3089 53 struct bio_batch bb;
f31e7e40 54 struct bio *bio;
f31e7e40 55 int ret = 0;
0cfbcafc 56 struct blk_plug plug;
f31e7e40
DM
57
58 if (!q)
59 return -ENXIO;
60
61 if (!blk_queue_discard(q))
62 return -EOPNOTSUPP;
63
dd3932ed 64 if (flags & BLKDEV_DISCARD_SECURE) {
8d57a98c
AH
65 if (!blk_queue_secdiscard(q))
66 return -EOPNOTSUPP;
8c555367 67 type |= REQ_SECURE;
8d57a98c
AH
68 }
69
5dba3089 70 atomic_set(&bb.done, 1);
4246a0b6 71 bb.error = 0;
5dba3089
LC
72 bb.wait = &wait;
73
0cfbcafc 74 blk_start_plug(&plug);
5dba3089 75 while (nr_sects) {
c6e66634 76 unsigned int req_sects;
b49a0871 77 sector_t end_sect;
c6e66634 78
f31e7e40 79 bio = bio_alloc(gfp_mask, 1);
66ac0280
CH
80 if (!bio) {
81 ret = -ENOMEM;
82 break;
83 }
84
b49a0871 85 req_sects = min_t(sector_t, nr_sects, MAX_BIO_SECTORS);
c6e66634 86 end_sect = sector + req_sects;
c6e66634 87
4f024f37 88 bio->bi_iter.bi_sector = sector;
5dba3089 89 bio->bi_end_io = bio_batch_end_io;
f31e7e40 90 bio->bi_bdev = bdev;
5dba3089 91 bio->bi_private = &bb;
f31e7e40 92
4f024f37 93 bio->bi_iter.bi_size = req_sects << 9;
c6e66634
PB
94 nr_sects -= req_sects;
95 sector = end_sect;
f31e7e40 96
5dba3089 97 atomic_inc(&bb.done);
f31e7e40 98 submit_bio(type, bio);
c8123f8c
JA
99
100 /*
101 * We can loop for a long time in here, if someone does
102 * full device discards (like mkfs). Be nice and allow
103 * us to schedule out to avoid softlocking if preempt
104 * is disabled.
105 */
106 cond_resched();
5dba3089 107 }
0cfbcafc 108 blk_finish_plug(&plug);
f31e7e40 109
5dba3089
LC
110 /* Wait for bios in-flight */
111 if (!atomic_dec_and_test(&bb.done))
5577022f 112 wait_for_completion_io(&wait);
f31e7e40 113
4246a0b6
CH
114 if (bb.error)
115 return bb.error;
f31e7e40 116 return ret;
f31e7e40
DM
117}
118EXPORT_SYMBOL(blkdev_issue_discard);
3f14d792 119
4363ac7c
MP
120/**
121 * blkdev_issue_write_same - queue a write same operation
122 * @bdev: target blockdev
123 * @sector: start sector
124 * @nr_sects: number of sectors to write
125 * @gfp_mask: memory allocation flags (for bio_alloc)
126 * @page: page containing data to write
127 *
128 * Description:
129 * Issue a write same request for the sectors in question.
130 */
131int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
132 sector_t nr_sects, gfp_t gfp_mask,
133 struct page *page)
134{
135 DECLARE_COMPLETION_ONSTACK(wait);
136 struct request_queue *q = bdev_get_queue(bdev);
137 unsigned int max_write_same_sectors;
138 struct bio_batch bb;
139 struct bio *bio;
140 int ret = 0;
141
142 if (!q)
143 return -ENXIO;
144
b49a0871
ML
145 /* Ensure that max_write_same_sectors doesn't overflow bi_size */
146 max_write_same_sectors = UINT_MAX >> 9;
4363ac7c
MP
147
148 atomic_set(&bb.done, 1);
4246a0b6 149 bb.error = 0;
4363ac7c
MP
150 bb.wait = &wait;
151
152 while (nr_sects) {
153 bio = bio_alloc(gfp_mask, 1);
154 if (!bio) {
155 ret = -ENOMEM;
156 break;
157 }
158
4f024f37 159 bio->bi_iter.bi_sector = sector;
4363ac7c
MP
160 bio->bi_end_io = bio_batch_end_io;
161 bio->bi_bdev = bdev;
162 bio->bi_private = &bb;
163 bio->bi_vcnt = 1;
164 bio->bi_io_vec->bv_page = page;
165 bio->bi_io_vec->bv_offset = 0;
166 bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
167
168 if (nr_sects > max_write_same_sectors) {
4f024f37 169 bio->bi_iter.bi_size = max_write_same_sectors << 9;
4363ac7c
MP
170 nr_sects -= max_write_same_sectors;
171 sector += max_write_same_sectors;
172 } else {
4f024f37 173 bio->bi_iter.bi_size = nr_sects << 9;
4363ac7c
MP
174 nr_sects = 0;
175 }
176
177 atomic_inc(&bb.done);
178 submit_bio(REQ_WRITE | REQ_WRITE_SAME, bio);
179 }
180
181 /* Wait for bios in-flight */
182 if (!atomic_dec_and_test(&bb.done))
5577022f 183 wait_for_completion_io(&wait);
4363ac7c 184
4246a0b6
CH
185 if (bb.error)
186 return bb.error;
4363ac7c
MP
187 return ret;
188}
189EXPORT_SYMBOL(blkdev_issue_write_same);
190
3f14d792 191/**
291d24f6 192 * blkdev_issue_zeroout - generate number of zero filed write bios
3f14d792
DM
193 * @bdev: blockdev to issue
194 * @sector: start sector
195 * @nr_sects: number of sectors to write
196 * @gfp_mask: memory allocation flags (for bio_alloc)
3f14d792
DM
197 *
198 * Description:
199 * Generate and issue number of bios with zerofiled pages.
3f14d792
DM
200 */
201
35086784
FF
202static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
203 sector_t nr_sects, gfp_t gfp_mask)
3f14d792 204{
18edc8ea 205 int ret;
3f14d792
DM
206 struct bio *bio;
207 struct bio_batch bb;
0aeea189 208 unsigned int sz;
3f14d792
DM
209 DECLARE_COMPLETION_ONSTACK(wait);
210
0aeea189 211 atomic_set(&bb.done, 1);
4246a0b6 212 bb.error = 0;
3f14d792 213 bb.wait = &wait;
3f14d792 214
18edc8ea 215 ret = 0;
3f14d792
DM
216 while (nr_sects != 0) {
217 bio = bio_alloc(gfp_mask,
218 min(nr_sects, (sector_t)BIO_MAX_PAGES));
18edc8ea
DM
219 if (!bio) {
220 ret = -ENOMEM;
3f14d792 221 break;
18edc8ea 222 }
3f14d792 223
4f024f37 224 bio->bi_iter.bi_sector = sector;
3f14d792
DM
225 bio->bi_bdev = bdev;
226 bio->bi_end_io = bio_batch_end_io;
dd3932ed 227 bio->bi_private = &bb;
3f14d792 228
0341aafb
JA
229 while (nr_sects != 0) {
230 sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
3f14d792
DM
231 ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
232 nr_sects -= ret >> 9;
233 sector += ret >> 9;
234 if (ret < (sz << 9))
235 break;
236 }
18edc8ea 237 ret = 0;
0aeea189 238 atomic_inc(&bb.done);
3f14d792
DM
239 submit_bio(WRITE, bio);
240 }
3f14d792 241
dd3932ed 242 /* Wait for bios in-flight */
0aeea189 243 if (!atomic_dec_and_test(&bb.done))
5577022f 244 wait_for_completion_io(&wait);
3f14d792 245
4246a0b6
CH
246 if (bb.error)
247 return bb.error;
3f14d792
DM
248 return ret;
249}
579e8f3c
MP
250
251/**
252 * blkdev_issue_zeroout - zero-fill a block range
253 * @bdev: blockdev to write
254 * @sector: start sector
255 * @nr_sects: number of sectors to write
256 * @gfp_mask: memory allocation flags (for bio_alloc)
d93ba7a5 257 * @discard: whether to discard the block range
579e8f3c
MP
258 *
259 * Description:
d93ba7a5
MP
260 * Zero-fill a block range. If the discard flag is set and the block
261 * device guarantees that subsequent READ operations to the block range
262 * in question will return zeroes, the blocks will be discarded. Should
263 * the discard request fail, if the discard flag is not set, or if
264 * discard_zeroes_data is not supported, this function will resort to
265 * zeroing the blocks manually, thus provisioning (allocating,
266 * anchoring) them. If the block device supports the WRITE SAME command
267 * blkdev_issue_zeroout() will use it to optimize the process of
268 * clearing the block range. Otherwise the zeroing will be performed
269 * using regular WRITE calls.
579e8f3c
MP
270 */
271
272int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
d93ba7a5 273 sector_t nr_sects, gfp_t gfp_mask, bool discard)
579e8f3c 274{
d93ba7a5 275 struct request_queue *q = bdev_get_queue(bdev);
d93ba7a5 276
9f9ee1f2
MP
277 if (discard && blk_queue_discard(q) && q->limits.discard_zeroes_data &&
278 blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, 0) == 0)
279 return 0;
d93ba7a5 280
9f9ee1f2
MP
281 if (bdev_write_same(bdev) &&
282 blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask,
283 ZERO_PAGE(0)) == 0)
284 return 0;
579e8f3c
MP
285
286 return __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask);
287}
3f14d792 288EXPORT_SYMBOL(blkdev_issue_zeroout);