2 * Functions related to generic helpers functions
4 #include <linux/kernel.h>
5 #include <linux/module.h>
7 #include <linux/blkdev.h>
8 #include <linux/scatterlist.h>
12 static struct bio
*next_bio(struct bio
*bio
, int rw
, unsigned int nr_pages
,
15 struct bio
*new = bio_alloc(gfp
, nr_pages
);
25 int __blkdev_issue_discard(struct block_device
*bdev
, sector_t sector
,
26 sector_t nr_sects
, gfp_t gfp_mask
, int type
, struct bio
**biop
)
28 struct request_queue
*q
= bdev_get_queue(bdev
);
29 struct bio
*bio
= *biop
;
30 unsigned int granularity
;
35 if (!blk_queue_discard(q
))
37 if ((type
& REQ_SECURE
) && !blk_queue_secdiscard(q
))
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
;
45 unsigned int req_sects
;
46 sector_t end_sect
, tmp
;
48 /* Make sure bi_size doesn't overflow */
49 req_sects
= min_t(sector_t
, nr_sects
, UINT_MAX
>> 9);
52 * If splitting a request, and the next starting sector would be
53 * misaligned, stop the discard at the previous aligned sector.
55 end_sect
= sector
+ req_sects
;
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
;
65 bio
= next_bio(bio
, type
, 1, gfp_mask
);
66 bio
->bi_iter
.bi_sector
= sector
;
69 bio
->bi_iter
.bi_size
= req_sects
<< 9;
70 nr_sects
-= req_sects
;
74 * We can loop for a long time in here, if someone does
75 * full device discards (like mkfs). Be nice and allow
76 * us to schedule out to avoid softlocking if preempt
85 EXPORT_SYMBOL(__blkdev_issue_discard
);
88 * blkdev_issue_discard - queue a discard
89 * @bdev: blockdev to issue discard for
90 * @sector: start sector
91 * @nr_sects: number of sectors to discard
92 * @gfp_mask: memory allocation flags (for bio_alloc)
93 * @flags: BLKDEV_IFL_* flags to control behaviour
96 * Issue a discard request for the sectors in question.
98 int blkdev_issue_discard(struct block_device
*bdev
, sector_t sector
,
99 sector_t nr_sects
, gfp_t gfp_mask
, unsigned long flags
)
101 int type
= REQ_WRITE
| REQ_DISCARD
;
102 struct bio
*bio
= NULL
;
103 struct blk_plug plug
;
106 if (flags
& BLKDEV_DISCARD_SECURE
)
109 blk_start_plug(&plug
);
110 ret
= __blkdev_issue_discard(bdev
, sector
, nr_sects
, gfp_mask
, type
,
113 ret
= submit_bio_wait(type
, bio
);
114 if (ret
== -EOPNOTSUPP
)
118 blk_finish_plug(&plug
);
122 EXPORT_SYMBOL(blkdev_issue_discard
);
125 * blkdev_issue_write_same - queue a write same operation
126 * @bdev: target blockdev
127 * @sector: start sector
128 * @nr_sects: number of sectors to write
129 * @gfp_mask: memory allocation flags (for bio_alloc)
130 * @page: page containing data to write
133 * Issue a write same request for the sectors in question.
135 int blkdev_issue_write_same(struct block_device
*bdev
, sector_t sector
,
136 sector_t nr_sects
, gfp_t gfp_mask
,
139 struct request_queue
*q
= bdev_get_queue(bdev
);
140 unsigned int max_write_same_sectors
;
141 struct bio
*bio
= NULL
;
147 /* Ensure that max_write_same_sectors doesn't overflow bi_size */
148 max_write_same_sectors
= UINT_MAX
>> 9;
151 bio
= next_bio(bio
, REQ_WRITE
| REQ_WRITE_SAME
, 1, gfp_mask
);
152 bio
->bi_iter
.bi_sector
= sector
;
155 bio
->bi_io_vec
->bv_page
= page
;
156 bio
->bi_io_vec
->bv_offset
= 0;
157 bio
->bi_io_vec
->bv_len
= bdev_logical_block_size(bdev
);
159 if (nr_sects
> max_write_same_sectors
) {
160 bio
->bi_iter
.bi_size
= max_write_same_sectors
<< 9;
161 nr_sects
-= max_write_same_sectors
;
162 sector
+= max_write_same_sectors
;
164 bio
->bi_iter
.bi_size
= nr_sects
<< 9;
170 ret
= submit_bio_wait(REQ_WRITE
| REQ_WRITE_SAME
, bio
);
173 return ret
!= -EOPNOTSUPP
? ret
: 0;
175 EXPORT_SYMBOL(blkdev_issue_write_same
);
178 * blkdev_issue_zeroout - generate number of zero filed write bios
179 * @bdev: blockdev to issue
180 * @sector: start sector
181 * @nr_sects: number of sectors to write
182 * @gfp_mask: memory allocation flags (for bio_alloc)
185 * Generate and issue number of bios with zerofiled pages.
188 static int __blkdev_issue_zeroout(struct block_device
*bdev
, sector_t sector
,
189 sector_t nr_sects
, gfp_t gfp_mask
)
192 struct bio
*bio
= NULL
;
195 while (nr_sects
!= 0) {
196 bio
= next_bio(bio
, WRITE
,
197 min(nr_sects
, (sector_t
)BIO_MAX_PAGES
),
199 bio
->bi_iter
.bi_sector
= sector
;
202 while (nr_sects
!= 0) {
203 sz
= min((sector_t
) PAGE_SIZE
>> 9 , nr_sects
);
204 ret
= bio_add_page(bio
, ZERO_PAGE(0), sz
<< 9, 0);
205 nr_sects
-= ret
>> 9;
213 ret
= submit_bio_wait(WRITE
, bio
);
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
225 * @gfp_mask: memory allocation flags (for bio_alloc)
226 * @discard: whether to discard the block range
229 * Zero-fill a block range. If the discard flag is set and the block
230 * device guarantees that subsequent READ operations to the block range
231 * in question will return zeroes, the blocks will be discarded. Should
232 * the discard request fail, if the discard flag is not set, or if
233 * discard_zeroes_data is not supported, this function will resort to
234 * zeroing the blocks manually, thus provisioning (allocating,
235 * anchoring) them. If the block device supports the WRITE SAME command
236 * blkdev_issue_zeroout() will use it to optimize the process of
237 * clearing the block range. Otherwise the zeroing will be performed
238 * using regular WRITE calls.
241 int blkdev_issue_zeroout(struct block_device
*bdev
, sector_t sector
,
242 sector_t nr_sects
, gfp_t gfp_mask
, bool discard
)
244 struct request_queue
*q
= bdev_get_queue(bdev
);
246 if (discard
&& blk_queue_discard(q
) && q
->limits
.discard_zeroes_data
&&
247 blkdev_issue_discard(bdev
, sector
, nr_sects
, gfp_mask
, 0) == 0)
250 if (bdev_write_same(bdev
) &&
251 blkdev_issue_write_same(bdev
, sector
, nr_sects
, gfp_mask
,
255 return __blkdev_issue_zeroout(bdev
, sector
, nr_sects
, gfp_mask
);
257 EXPORT_SYMBOL(blkdev_issue_zeroout
);