4 #include <linux/kernel.h>
5 #include <linux/module.h>
18 static void bio_batch_end_io(
struct bio *bio,
int err)
46 unsigned int max_discard_sectors;
55 if (!blk_queue_discard(q))
59 granularity =
max(q->limits.discard_granularity >> 9, 1
U);
60 mask = granularity - 1;
61 alignment = (bdev_discard_alignment(bdev) >> 9) & mask;
67 max_discard_sectors =
min(q->limits.max_discard_sectors,
UINT_MAX >> 9);
68 max_discard_sectors =
round_down(max_discard_sectors, granularity);
69 if (
unlikely(!max_discard_sectors)) {
74 if (flags & BLKDEV_DISCARD_SECURE) {
75 if (!blk_queue_secdiscard(q))
81 bb.
flags = 1 << BIO_UPTODATE;
85 unsigned int req_sects;
88 bio = bio_alloc(gfp_mask, 1);
100 end_sect = sector + req_sects;
101 if (req_sects < nr_sects && (end_sect & mask) != alignment) {
105 req_sects = end_sect -
sector;
109 bio->bi_end_io = bio_batch_end_io;
111 bio->bi_private = &
bb;
113 bio->bi_size = req_sects << 9;
114 nr_sects -= req_sects;
149 unsigned int max_write_same_sectors;
157 max_write_same_sectors = q->limits.max_write_same_sectors;
159 if (max_write_same_sectors == 0)
163 bb.
flags = 1 << BIO_UPTODATE;
167 bio = bio_alloc(gfp_mask, 1);
174 bio->bi_end_io = bio_batch_end_io;
176 bio->bi_private = &
bb;
178 bio->bi_io_vec->bv_page =
page;
179 bio->bi_io_vec->bv_offset = 0;
180 bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev);
182 if (nr_sects > max_write_same_sectors) {
183 bio->bi_size = max_write_same_sectors << 9;
184 nr_sects -= max_write_same_sectors;
185 sector += max_write_same_sectors;
187 bio->bi_size = nr_sects << 9;
227 bb.
flags = 1 << BIO_UPTODATE;
231 while (nr_sects != 0) {
232 bio = bio_alloc(gfp_mask,
241 bio->bi_end_io = bio_batch_end_io;
242 bio->bi_private = &
bb;
244 while (nr_sects != 0) {
247 nr_sects -= ret >> 9;
282 if (bdev_write_same(bdev)) {
290 pr_err(
"%s: WRITE SAME failed. Manually zeroing.\n", bdn);