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>
15 struct completion
*wait
;
18 static void bio_batch_end_io(struct bio
*bio
)
20 struct bio_batch
*bb
= bio
->bi_private
;
22 if (bio
->bi_error
&& bio
->bi_error
!= -EOPNOTSUPP
)
23 bb
->error
= bio
->bi_error
;
24 if (atomic_dec_and_test(&bb
->done
))
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
34 #define MAX_BIO_SECTORS ((1U << 31) >> 9)
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
45 * Issue a discard request for the sectors in question.
47 int blkdev_issue_discard(struct block_device
*bdev
, sector_t sector
,
48 sector_t nr_sects
, gfp_t gfp_mask
, unsigned long flags
)
50 DECLARE_COMPLETION_ONSTACK(wait
);
51 struct request_queue
*q
= bdev_get_queue(bdev
);
52 int type
= REQ_WRITE
| REQ_DISCARD
;
61 if (!blk_queue_discard(q
))
64 if (flags
& BLKDEV_DISCARD_SECURE
) {
65 if (!blk_queue_secdiscard(q
))
70 atomic_set(&bb
.done
, 1);
74 blk_start_plug(&plug
);
76 unsigned int req_sects
;
79 bio
= bio_alloc(gfp_mask
, 1);
85 req_sects
= min_t(sector_t
, nr_sects
, MAX_BIO_SECTORS
);
86 end_sect
= sector
+ req_sects
;
88 bio
->bi_iter
.bi_sector
= sector
;
89 bio
->bi_end_io
= bio_batch_end_io
;
91 bio
->bi_private
= &bb
;
93 bio
->bi_iter
.bi_size
= req_sects
<< 9;
94 nr_sects
-= req_sects
;
98 submit_bio(type
, bio
);
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
108 blk_finish_plug(&plug
);
110 /* Wait for bios in-flight */
111 if (!atomic_dec_and_test(&bb
.done
))
112 wait_for_completion_io(&wait
);
118 EXPORT_SYMBOL(blkdev_issue_discard
);
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
129 * Issue a write same request for the sectors in question.
131 int blkdev_issue_write_same(struct block_device
*bdev
, sector_t sector
,
132 sector_t nr_sects
, gfp_t gfp_mask
,
135 DECLARE_COMPLETION_ONSTACK(wait
);
136 struct request_queue
*q
= bdev_get_queue(bdev
);
137 unsigned int max_write_same_sectors
;
145 /* Ensure that max_write_same_sectors doesn't overflow bi_size */
146 max_write_same_sectors
= UINT_MAX
>> 9;
148 atomic_set(&bb
.done
, 1);
153 bio
= bio_alloc(gfp_mask
, 1);
159 bio
->bi_iter
.bi_sector
= sector
;
160 bio
->bi_end_io
= bio_batch_end_io
;
162 bio
->bi_private
= &bb
;
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
);
168 if (nr_sects
> max_write_same_sectors
) {
169 bio
->bi_iter
.bi_size
= max_write_same_sectors
<< 9;
170 nr_sects
-= max_write_same_sectors
;
171 sector
+= max_write_same_sectors
;
173 bio
->bi_iter
.bi_size
= nr_sects
<< 9;
177 atomic_inc(&bb
.done
);
178 submit_bio(REQ_WRITE
| REQ_WRITE_SAME
, bio
);
181 /* Wait for bios in-flight */
182 if (!atomic_dec_and_test(&bb
.done
))
183 wait_for_completion_io(&wait
);
189 EXPORT_SYMBOL(blkdev_issue_write_same
);
192 * blkdev_issue_zeroout - generate number of zero filed write bios
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)
199 * Generate and issue number of bios with zerofiled pages.
202 static int __blkdev_issue_zeroout(struct block_device
*bdev
, sector_t sector
,
203 sector_t nr_sects
, gfp_t gfp_mask
)
209 DECLARE_COMPLETION_ONSTACK(wait
);
211 atomic_set(&bb
.done
, 1);
216 while (nr_sects
!= 0) {
217 bio
= bio_alloc(gfp_mask
,
218 min(nr_sects
, (sector_t
)BIO_MAX_PAGES
));
224 bio
->bi_iter
.bi_sector
= sector
;
226 bio
->bi_end_io
= bio_batch_end_io
;
227 bio
->bi_private
= &bb
;
229 while (nr_sects
!= 0) {
230 sz
= min((sector_t
) PAGE_SIZE
>> 9 , nr_sects
);
231 ret
= bio_add_page(bio
, ZERO_PAGE(0), sz
<< 9, 0);
232 nr_sects
-= ret
>> 9;
238 atomic_inc(&bb
.done
);
239 submit_bio(WRITE
, bio
);
242 /* Wait for bios in-flight */
243 if (!atomic_dec_and_test(&bb
.done
))
244 wait_for_completion_io(&wait
);
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)
257 * @discard: whether to discard the block range
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.
272 int blkdev_issue_zeroout(struct block_device
*bdev
, sector_t sector
,
273 sector_t nr_sects
, gfp_t gfp_mask
, bool discard
)
275 struct request_queue
*q
= bdev_get_queue(bdev
);
277 if (discard
&& blk_queue_discard(q
) && q
->limits
.discard_zeroes_data
&&
278 blkdev_issue_discard(bdev
, sector
, nr_sects
, gfp_mask
, 0) == 0)
281 if (bdev_write_same(bdev
) &&
282 blkdev_issue_write_same(bdev
, sector
, nr_sects
, gfp_mask
,
286 return __blkdev_issue_zeroout(bdev
, sector
, nr_sects
, gfp_mask
);
288 EXPORT_SYMBOL(blkdev_issue_zeroout
);