blk-lib.c (8e8c668927b029f6ccc350eb1aa936864cc4eb6f) blk-lib.c (288dab8a35a0bde426a09870943c8d3ee3a50dab)
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
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
12static struct bio *next_bio(struct bio *bio, int rw, unsigned int nr_pages,
12static struct bio *next_bio(struct bio *bio, unsigned int nr_pages,
13 gfp_t gfp)
14{
15 struct bio *new = bio_alloc(gfp, nr_pages);
16
17 if (bio) {
18 bio_chain(bio, new);
13 gfp_t gfp)
14{
15 struct bio *new = bio_alloc(gfp, nr_pages);
16
17 if (bio) {
18 bio_chain(bio, new);
19 submit_bio(rw, bio);
19 submit_bio(bio);
20 }
21
22 return new;
23}
24
25int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
20 }
21
22 return new;
23}
24
25int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
26 sector_t nr_sects, gfp_t gfp_mask, int type, struct bio **biop)
26 sector_t nr_sects, gfp_t gfp_mask, int flags,
27 struct bio **biop)
27{
28 struct request_queue *q = bdev_get_queue(bdev);
29 struct bio *bio = *biop;
30 unsigned int granularity;
28{
29 struct request_queue *q = bdev_get_queue(bdev);
30 struct bio *bio = *biop;
31 unsigned int granularity;
32 enum req_op op;
31 int alignment;
32
33 if (!q)
34 return -ENXIO;
33 int alignment;
34
35 if (!q)
36 return -ENXIO;
35 if (!blk_queue_discard(q))
36 return -EOPNOTSUPP;
37 if ((type & REQ_SECURE) && !blk_queue_secdiscard(q))
38 return -EOPNOTSUPP;
39
37
38 if (flags & BLKDEV_DISCARD_SECURE) {
39 if (!blk_queue_secure_erase(q))
40 return -EOPNOTSUPP;
41 op = REQ_OP_SECURE_ERASE;
42 } else {
43 if (!blk_queue_discard(q))
44 return -EOPNOTSUPP;
45 op = REQ_OP_DISCARD;
46 }
47
40 /* Zero-sector (unknown) and one-sector granularities are the same. */
41 granularity = max(q->limits.discard_granularity >> 9, 1U);
42 alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
43
44 while (nr_sects) {
45 unsigned int req_sects;
46 sector_t end_sect, tmp;
47

--- 9 unchanged lines hidden (view full) ---

57 if (req_sects < nr_sects &&
58 sector_div(tmp, granularity) != alignment) {
59 end_sect = end_sect - alignment;
60 sector_div(end_sect, granularity);
61 end_sect = end_sect * granularity + alignment;
62 req_sects = end_sect - sector;
63 }
64
48 /* Zero-sector (unknown) and one-sector granularities are the same. */
49 granularity = max(q->limits.discard_granularity >> 9, 1U);
50 alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
51
52 while (nr_sects) {
53 unsigned int req_sects;
54 sector_t end_sect, tmp;
55

--- 9 unchanged lines hidden (view full) ---

65 if (req_sects < nr_sects &&
66 sector_div(tmp, granularity) != alignment) {
67 end_sect = end_sect - alignment;
68 sector_div(end_sect, granularity);
69 end_sect = end_sect * granularity + alignment;
70 req_sects = end_sect - sector;
71 }
72
65 bio = next_bio(bio, type, 1, gfp_mask);
73 bio = next_bio(bio, 1, gfp_mask);
66 bio->bi_iter.bi_sector = sector;
67 bio->bi_bdev = bdev;
74 bio->bi_iter.bi_sector = sector;
75 bio->bi_bdev = bdev;
76 bio_set_op_attrs(bio, op, 0);
68
69 bio->bi_iter.bi_size = req_sects << 9;
70 nr_sects -= req_sects;
71 sector = end_sect;
72
73 /*
74 * We can loop for a long time in here, if someone does
75 * full device discards (like mkfs). Be nice and allow

--- 17 unchanged lines hidden (view full) ---

93 * @flags: BLKDEV_IFL_* flags to control behaviour
94 *
95 * Description:
96 * Issue a discard request for the sectors in question.
97 */
98int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
99 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
100{
77
78 bio->bi_iter.bi_size = req_sects << 9;
79 nr_sects -= req_sects;
80 sector = end_sect;
81
82 /*
83 * We can loop for a long time in here, if someone does
84 * full device discards (like mkfs). Be nice and allow

--- 17 unchanged lines hidden (view full) ---

102 * @flags: BLKDEV_IFL_* flags to control behaviour
103 *
104 * Description:
105 * Issue a discard request for the sectors in question.
106 */
107int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
108 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
109{
101 int type = REQ_WRITE | REQ_DISCARD;
102 struct bio *bio = NULL;
103 struct blk_plug plug;
104 int ret;
105
110 struct bio *bio = NULL;
111 struct blk_plug plug;
112 int ret;
113
106 if (flags & BLKDEV_DISCARD_SECURE)
107 type |= REQ_SECURE;
108
109 blk_start_plug(&plug);
114 blk_start_plug(&plug);
110 ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, type,
115 ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, flags,
111 &bio);
112 if (!ret && bio) {
116 &bio);
117 if (!ret && bio) {
113 ret = submit_bio_wait(type, bio);
118 ret = submit_bio_wait(bio);
114 if (ret == -EOPNOTSUPP)
115 ret = 0;
116 }
117 blk_finish_plug(&plug);
118
119 return ret;
120}
121EXPORT_SYMBOL(blkdev_issue_discard);

--- 20 unchanged lines hidden (view full) ---

142
143 if (!q)
144 return -ENXIO;
145
146 /* Ensure that max_write_same_sectors doesn't overflow bi_size */
147 max_write_same_sectors = UINT_MAX >> 9;
148
149 while (nr_sects) {
119 if (ret == -EOPNOTSUPP)
120 ret = 0;
121 }
122 blk_finish_plug(&plug);
123
124 return ret;
125}
126EXPORT_SYMBOL(blkdev_issue_discard);

--- 20 unchanged lines hidden (view full) ---

147
148 if (!q)
149 return -ENXIO;
150
151 /* Ensure that max_write_same_sectors doesn't overflow bi_size */
152 max_write_same_sectors = UINT_MAX >> 9;
153
154 while (nr_sects) {
150 bio = next_bio(bio, REQ_WRITE | REQ_WRITE_SAME, 1, gfp_mask);
155 bio = next_bio(bio, 1, gfp_mask);
151 bio->bi_iter.bi_sector = sector;
152 bio->bi_bdev = bdev;
153 bio->bi_vcnt = 1;
154 bio->bi_io_vec->bv_page = page;
155 bio->bi_io_vec->bv_offset = 0;
156 bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
156 bio->bi_iter.bi_sector = sector;
157 bio->bi_bdev = bdev;
158 bio->bi_vcnt = 1;
159 bio->bi_io_vec->bv_page = page;
160 bio->bi_io_vec->bv_offset = 0;
161 bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
162 bio_set_op_attrs(bio, REQ_OP_WRITE_SAME, 0);
157
158 if (nr_sects > max_write_same_sectors) {
159 bio->bi_iter.bi_size = max_write_same_sectors << 9;
160 nr_sects -= max_write_same_sectors;
161 sector += max_write_same_sectors;
162 } else {
163 bio->bi_iter.bi_size = nr_sects << 9;
164 nr_sects = 0;
165 }
166 }
167
168 if (bio)
163
164 if (nr_sects > max_write_same_sectors) {
165 bio->bi_iter.bi_size = max_write_same_sectors << 9;
166 nr_sects -= max_write_same_sectors;
167 sector += max_write_same_sectors;
168 } else {
169 bio->bi_iter.bi_size = nr_sects << 9;
170 nr_sects = 0;
171 }
172 }
173
174 if (bio)
169 ret = submit_bio_wait(REQ_WRITE | REQ_WRITE_SAME, bio);
175 ret = submit_bio_wait(bio);
170 return ret != -EOPNOTSUPP ? ret : 0;
171}
172EXPORT_SYMBOL(blkdev_issue_write_same);
173
174/**
175 * blkdev_issue_zeroout - generate number of zero filed write bios
176 * @bdev: blockdev to issue
177 * @sector: start sector

--- 7 unchanged lines hidden (view full) ---

185static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
186 sector_t nr_sects, gfp_t gfp_mask)
187{
188 int ret;
189 struct bio *bio = NULL;
190 unsigned int sz;
191
192 while (nr_sects != 0) {
176 return ret != -EOPNOTSUPP ? ret : 0;
177}
178EXPORT_SYMBOL(blkdev_issue_write_same);
179
180/**
181 * blkdev_issue_zeroout - generate number of zero filed write bios
182 * @bdev: blockdev to issue
183 * @sector: start sector

--- 7 unchanged lines hidden (view full) ---

191static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
192 sector_t nr_sects, gfp_t gfp_mask)
193{
194 int ret;
195 struct bio *bio = NULL;
196 unsigned int sz;
197
198 while (nr_sects != 0) {
193 bio = next_bio(bio, WRITE,
194 min(nr_sects, (sector_t)BIO_MAX_PAGES),
199 bio = next_bio(bio, min(nr_sects, (sector_t)BIO_MAX_PAGES),
195 gfp_mask);
196 bio->bi_iter.bi_sector = sector;
197 bio->bi_bdev = bdev;
200 gfp_mask);
201 bio->bi_iter.bi_sector = sector;
202 bio->bi_bdev = bdev;
203 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
198
199 while (nr_sects != 0) {
200 sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
201 ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
202 nr_sects -= ret >> 9;
203 sector += ret >> 9;
204 if (ret < (sz << 9))
205 break;
206 }
207 }
208
209 if (bio)
204
205 while (nr_sects != 0) {
206 sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
207 ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
208 nr_sects -= ret >> 9;
209 sector += ret >> 9;
210 if (ret < (sz << 9))
211 break;
212 }
213 }
214
215 if (bio)
210 return submit_bio_wait(WRITE, bio);
216 return submit_bio_wait(bio);
211 return 0;
212}
213
214/**
215 * blkdev_issue_zeroout - zero-fill a block range
216 * @bdev: blockdev to write
217 * @sector: start sector
218 * @nr_sects: number of sectors to write

--- 33 unchanged lines hidden ---
217 return 0;
218}
219
220/**
221 * blkdev_issue_zeroout - zero-fill a block range
222 * @bdev: blockdev to write
223 * @sector: start sector
224 * @nr_sects: number of sectors to write

--- 33 unchanged lines hidden ---