]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - block/blk-lib.c
block: remove struct bio_batch
[mirror_ubuntu-zesty-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
9082e87b
CH
12static struct bio *next_bio(struct bio *bio, int rw, unsigned int nr_pages,
13 gfp_t gfp)
f31e7e40 14{
9082e87b
CH
15 struct bio *new = bio_alloc(gfp, nr_pages);
16
17 if (bio) {
18 bio_chain(bio, new);
19 submit_bio(rw, bio);
20 }
5dba3089 21
9082e87b 22 return new;
f31e7e40
DM
23}
24
25/**
26 * blkdev_issue_discard - queue a discard
27 * @bdev: blockdev to issue discard for
28 * @sector: start sector
29 * @nr_sects: number of sectors to discard
30 * @gfp_mask: memory allocation flags (for bio_alloc)
31 * @flags: BLKDEV_IFL_* flags to control behaviour
32 *
33 * Description:
34 * Issue a discard request for the sectors in question.
35 */
36int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
37 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
38{
f31e7e40 39 struct request_queue *q = bdev_get_queue(bdev);
8c555367 40 int type = REQ_WRITE | REQ_DISCARD;
a22c4d7e
ML
41 unsigned int granularity;
42 int alignment;
9082e87b 43 struct bio *bio = NULL;
f31e7e40 44 int ret = 0;
0cfbcafc 45 struct blk_plug plug;
f31e7e40
DM
46
47 if (!q)
48 return -ENXIO;
49
50 if (!blk_queue_discard(q))
51 return -EOPNOTSUPP;
52
a22c4d7e
ML
53 /* Zero-sector (unknown) and one-sector granularities are the same. */
54 granularity = max(q->limits.discard_granularity >> 9, 1U);
55 alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
56
dd3932ed 57 if (flags & BLKDEV_DISCARD_SECURE) {
8d57a98c
AH
58 if (!blk_queue_secdiscard(q))
59 return -EOPNOTSUPP;
8c555367 60 type |= REQ_SECURE;
8d57a98c
AH
61 }
62
0cfbcafc 63 blk_start_plug(&plug);
5dba3089 64 while (nr_sects) {
c6e66634 65 unsigned int req_sects;
a22c4d7e 66 sector_t end_sect, tmp;
c6e66634 67
a22c4d7e
ML
68 /* Make sure bi_size doesn't overflow */
69 req_sects = min_t(sector_t, nr_sects, UINT_MAX >> 9);
70
9082e87b 71 /**
a22c4d7e
ML
72 * If splitting a request, and the next starting sector would be
73 * misaligned, stop the discard at the previous aligned sector.
74 */
c6e66634 75 end_sect = sector + req_sects;
a22c4d7e
ML
76 tmp = end_sect;
77 if (req_sects < nr_sects &&
78 sector_div(tmp, granularity) != alignment) {
79 end_sect = end_sect - alignment;
80 sector_div(end_sect, granularity);
81 end_sect = end_sect * granularity + alignment;
82 req_sects = end_sect - sector;
83 }
c6e66634 84
9082e87b 85 bio = next_bio(bio, type, 1, gfp_mask);
4f024f37 86 bio->bi_iter.bi_sector = sector;
f31e7e40 87 bio->bi_bdev = bdev;
f31e7e40 88
4f024f37 89 bio->bi_iter.bi_size = req_sects << 9;
c6e66634
PB
90 nr_sects -= req_sects;
91 sector = end_sect;
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();
5dba3089 100 }
9082e87b
CH
101 if (bio)
102 ret = submit_bio_wait(type, bio);
0cfbcafc 103 blk_finish_plug(&plug);
f31e7e40 104
9082e87b 105 return ret != -EOPNOTSUPP ? ret : 0;
f31e7e40
DM
106}
107EXPORT_SYMBOL(blkdev_issue_discard);
3f14d792 108
4363ac7c
MP
109/**
110 * blkdev_issue_write_same - queue a write same operation
111 * @bdev: target blockdev
112 * @sector: start sector
113 * @nr_sects: number of sectors to write
114 * @gfp_mask: memory allocation flags (for bio_alloc)
115 * @page: page containing data to write
116 *
117 * Description:
118 * Issue a write same request for the sectors in question.
119 */
120int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
121 sector_t nr_sects, gfp_t gfp_mask,
122 struct page *page)
123{
4363ac7c
MP
124 struct request_queue *q = bdev_get_queue(bdev);
125 unsigned int max_write_same_sectors;
9082e87b 126 struct bio *bio = NULL;
4363ac7c
MP
127 int ret = 0;
128
129 if (!q)
130 return -ENXIO;
131
b49a0871
ML
132 /* Ensure that max_write_same_sectors doesn't overflow bi_size */
133 max_write_same_sectors = UINT_MAX >> 9;
4363ac7c 134
4363ac7c 135 while (nr_sects) {
9082e87b 136 bio = next_bio(bio, REQ_WRITE | REQ_WRITE_SAME, 1, gfp_mask);
4f024f37 137 bio->bi_iter.bi_sector = sector;
4363ac7c 138 bio->bi_bdev = bdev;
4363ac7c
MP
139 bio->bi_vcnt = 1;
140 bio->bi_io_vec->bv_page = page;
141 bio->bi_io_vec->bv_offset = 0;
142 bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
143
144 if (nr_sects > max_write_same_sectors) {
4f024f37 145 bio->bi_iter.bi_size = max_write_same_sectors << 9;
4363ac7c
MP
146 nr_sects -= max_write_same_sectors;
147 sector += max_write_same_sectors;
148 } else {
4f024f37 149 bio->bi_iter.bi_size = nr_sects << 9;
4363ac7c
MP
150 nr_sects = 0;
151 }
4363ac7c
MP
152 }
153
9082e87b
CH
154 if (bio)
155 ret = submit_bio_wait(REQ_WRITE | REQ_WRITE_SAME, bio);
156 return ret != -EOPNOTSUPP ? ret : 0;
4363ac7c
MP
157}
158EXPORT_SYMBOL(blkdev_issue_write_same);
159
3f14d792 160/**
291d24f6 161 * blkdev_issue_zeroout - generate number of zero filed write bios
3f14d792
DM
162 * @bdev: blockdev to issue
163 * @sector: start sector
164 * @nr_sects: number of sectors to write
165 * @gfp_mask: memory allocation flags (for bio_alloc)
3f14d792
DM
166 *
167 * Description:
168 * Generate and issue number of bios with zerofiled pages.
3f14d792
DM
169 */
170
35086784
FF
171static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
172 sector_t nr_sects, gfp_t gfp_mask)
3f14d792 173{
18edc8ea 174 int ret;
9082e87b 175 struct bio *bio = NULL;
0aeea189 176 unsigned int sz;
3f14d792 177
3f14d792 178 while (nr_sects != 0) {
9082e87b
CH
179 bio = next_bio(bio, WRITE,
180 min(nr_sects, (sector_t)BIO_MAX_PAGES),
181 gfp_mask);
4f024f37 182 bio->bi_iter.bi_sector = sector;
3f14d792 183 bio->bi_bdev = bdev;
3f14d792 184
0341aafb
JA
185 while (nr_sects != 0) {
186 sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
3f14d792
DM
187 ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
188 nr_sects -= ret >> 9;
189 sector += ret >> 9;
190 if (ret < (sz << 9))
191 break;
192 }
3f14d792 193 }
3f14d792 194
9082e87b
CH
195 if (bio)
196 return submit_bio_wait(WRITE, bio);
197 return 0;
3f14d792 198}
579e8f3c
MP
199
200/**
201 * blkdev_issue_zeroout - zero-fill a block range
202 * @bdev: blockdev to write
203 * @sector: start sector
204 * @nr_sects: number of sectors to write
205 * @gfp_mask: memory allocation flags (for bio_alloc)
d93ba7a5 206 * @discard: whether to discard the block range
579e8f3c
MP
207 *
208 * Description:
d93ba7a5
MP
209 * Zero-fill a block range. If the discard flag is set and the block
210 * device guarantees that subsequent READ operations to the block range
211 * in question will return zeroes, the blocks will be discarded. Should
212 * the discard request fail, if the discard flag is not set, or if
213 * discard_zeroes_data is not supported, this function will resort to
214 * zeroing the blocks manually, thus provisioning (allocating,
215 * anchoring) them. If the block device supports the WRITE SAME command
216 * blkdev_issue_zeroout() will use it to optimize the process of
217 * clearing the block range. Otherwise the zeroing will be performed
218 * using regular WRITE calls.
579e8f3c
MP
219 */
220
221int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
d93ba7a5 222 sector_t nr_sects, gfp_t gfp_mask, bool discard)
579e8f3c 223{
d93ba7a5 224 struct request_queue *q = bdev_get_queue(bdev);
d93ba7a5 225
9f9ee1f2
MP
226 if (discard && blk_queue_discard(q) && q->limits.discard_zeroes_data &&
227 blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, 0) == 0)
228 return 0;
d93ba7a5 229
9f9ee1f2
MP
230 if (bdev_write_same(bdev) &&
231 blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask,
232 ZERO_PAGE(0)) == 0)
233 return 0;
579e8f3c
MP
234
235 return __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask);
236}
3f14d792 237EXPORT_SYMBOL(blkdev_issue_zeroout);