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 void blkdev_discard_end_io(struct bio *bio, int err)
15 if (err == -EOPNOTSUPP)
16 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
17 clear_bit(BIO_UPTODATE, &bio->bi_flags);
21 complete(bio->bi_private);
27 * blkdev_issue_discard - queue a discard
28 * @bdev: blockdev to issue discard for
29 * @sector: start sector
30 * @nr_sects: number of sectors to discard
31 * @gfp_mask: memory allocation flags (for bio_alloc)
32 * @flags: BLKDEV_IFL_* flags to control behaviour
35 * Issue a discard request for the sectors in question.
37 int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
38 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
40 DECLARE_COMPLETION_ONSTACK(wait);
41 struct request_queue *q = bdev_get_queue(bdev);
42 int type = REQ_WRITE | REQ_DISCARD;
43 unsigned int max_discard_sectors;
50 if (!blk_queue_discard(q))
54 * Ensure that max_discard_sectors is of the proper
57 max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
58 if (q->limits.discard_granularity) {
59 unsigned int disc_sects = q->limits.discard_granularity >> 9;
61 max_discard_sectors &= ~(disc_sects - 1);
64 if (flags & BLKDEV_DISCARD_SECURE) {
65 if (!blk_queue_secdiscard(q))
70 while (nr_sects && !ret) {
71 bio = bio_alloc(gfp_mask, 1);
77 bio->bi_sector = sector;
78 bio->bi_end_io = blkdev_discard_end_io;
80 bio->bi_private = &wait;
82 if (nr_sects > max_discard_sectors) {
83 bio->bi_size = max_discard_sectors << 9;
84 nr_sects -= max_discard_sectors;
85 sector += max_discard_sectors;
87 bio->bi_size = nr_sects << 9;
92 submit_bio(type, bio);
94 wait_for_completion(&wait);
96 if (bio_flagged(bio, BIO_EOPNOTSUPP))
98 else if (!bio_flagged(bio, BIO_UPTODATE))
105 EXPORT_SYMBOL(blkdev_issue_discard);
111 struct completion *wait;
112 bio_end_io_t *end_io;
115 static void bio_batch_end_io(struct bio *bio, int err)
117 struct bio_batch *bb = bio->bi_private;
120 if (err == -EOPNOTSUPP)
121 set_bit(BIO_EOPNOTSUPP, &bb->flags);
123 clear_bit(BIO_UPTODATE, &bb->flags);
127 bb->end_io(bio, err);
128 atomic_inc(&bb->done);
135 * blkdev_issue_zeroout generate number of zero filed write bios
136 * @bdev: blockdev to issue
137 * @sector: start sector
138 * @nr_sects: number of sectors to write
139 * @gfp_mask: memory allocation flags (for bio_alloc)
142 * Generate and issue number of bios with zerofiled pages.
143 * Send barrier at the beginning and at the end if requested. This guarantie
144 * correct request ordering. Empty barrier allow us to avoid post queue flush.
147 int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
148 sector_t nr_sects, gfp_t gfp_mask)
153 unsigned int sz, issued = 0;
154 DECLARE_COMPLETION_ONSTACK(wait);
156 atomic_set(&bb.done, 0);
157 bb.flags = 1 << BIO_UPTODATE;
163 while (nr_sects != 0) {
164 bio = bio_alloc(gfp_mask,
165 min(nr_sects, (sector_t)BIO_MAX_PAGES));
171 bio->bi_sector = sector;
173 bio->bi_end_io = bio_batch_end_io;
174 bio->bi_private = &bb;
176 while (nr_sects != 0) {
177 sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
179 /* bio has maximum size possible */
181 ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
182 nr_sects -= ret >> 9;
189 submit_bio(WRITE, bio);
192 /* Wait for bios in-flight */
193 while (issued != atomic_read(&bb.done))
194 wait_for_completion(&wait);
196 if (!test_bit(BIO_UPTODATE, &bb.flags))
197 /* One of bios in the batch was completed with error.*/
203 if (test_bit(BIO_EOPNOTSUPP, &bb.flags)) {
212 EXPORT_SYMBOL(blkdev_issue_zeroout);