blob: 25de73e4759b82d624c2a89b545baf6ea859f624 [file] [log] [blame]
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +04001/*
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 void blkdev_discard_end_io(struct bio *bio, int err)
13{
14 if (err) {
15 if (err == -EOPNOTSUPP)
16 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
17 clear_bit(BIO_UPTODATE, &bio->bi_flags);
18 }
19
20 if (bio->bi_private)
21 complete(bio->bi_private);
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040022
23 bio_put(bio);
24}
25
26/**
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
33 *
34 * Description:
35 * Issue a discard request for the sectors in question.
36 */
37int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
38 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
39{
40 DECLARE_COMPLETION_ONSTACK(wait);
41 struct request_queue *q = bdev_get_queue(bdev);
Christoph Hellwig8c555362010-08-18 05:29:22 -040042 int type = REQ_WRITE | REQ_DISCARD;
Jens Axboe10d1f9e2010-07-15 10:49:31 -060043 unsigned int max_discard_sectors;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040044 struct bio *bio;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040045 int ret = 0;
46
47 if (!q)
48 return -ENXIO;
49
50 if (!blk_queue_discard(q))
51 return -EOPNOTSUPP;
52
Jens Axboe10d1f9e2010-07-15 10:49:31 -060053 /*
54 * Ensure that max_discard_sectors is of the proper
55 * granularity
56 */
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;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040060
Jens Axboe10d1f9e2010-07-15 10:49:31 -060061 max_discard_sectors &= ~(disc_sects - 1);
62 }
63
Christoph Hellwigdd3932e2010-09-16 20:51:46 +020064 if (flags & BLKDEV_DISCARD_SECURE) {
Adrian Hunter8d57a982010-08-11 14:17:49 -070065 if (!blk_queue_secdiscard(q))
66 return -EOPNOTSUPP;
Christoph Hellwig8c555362010-08-18 05:29:22 -040067 type |= REQ_SECURE;
Adrian Hunter8d57a982010-08-11 14:17:49 -070068 }
69
Jens Axboe10d1f9e2010-07-15 10:49:31 -060070 while (nr_sects && !ret) {
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040071 bio = bio_alloc(gfp_mask, 1);
Christoph Hellwig66ac0282010-06-18 16:59:42 +020072 if (!bio) {
73 ret = -ENOMEM;
74 break;
75 }
76
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040077 bio->bi_sector = sector;
78 bio->bi_end_io = blkdev_discard_end_io;
79 bio->bi_bdev = bdev;
Christoph Hellwigdd3932e2010-09-16 20:51:46 +020080 bio->bi_private = &wait;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040081
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040082 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;
86 } else {
87 bio->bi_size = nr_sects << 9;
88 nr_sects = 0;
89 }
90
91 bio_get(bio);
92 submit_bio(type, bio);
93
Christoph Hellwigdd3932e2010-09-16 20:51:46 +020094 wait_for_completion(&wait);
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040095
96 if (bio_flagged(bio, BIO_EOPNOTSUPP))
97 ret = -EOPNOTSUPP;
98 else if (!bio_flagged(bio, BIO_UPTODATE))
99 ret = -EIO;
100 bio_put(bio);
101 }
Christoph Hellwig66ac0282010-06-18 16:59:42 +0200102
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +0400103 return ret;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +0400104}
105EXPORT_SYMBOL(blkdev_issue_discard);
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400106
107struct bio_batch
108{
109 atomic_t done;
110 unsigned long flags;
111 struct completion *wait;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400112};
113
114static void bio_batch_end_io(struct bio *bio, int err)
115{
116 struct bio_batch *bb = bio->bi_private;
Jens Axboe0341aaf2010-04-29 09:28:21 +0200117
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400118 if (err) {
119 if (err == -EOPNOTSUPP)
120 set_bit(BIO_EOPNOTSUPP, &bb->flags);
121 else
122 clear_bit(BIO_UPTODATE, &bb->flags);
123 }
Lukas Czerner0aeea182011-03-11 10:23:53 +0100124 if (bb)
125 if (atomic_dec_and_test(&bb->done))
126 complete(bb->wait);
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400127 bio_put(bio);
128}
129
130/**
Ben Hutchings291d24f2011-03-01 13:45:24 -0500131 * blkdev_issue_zeroout - generate number of zero filed write bios
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400132 * @bdev: blockdev to issue
133 * @sector: start sector
134 * @nr_sects: number of sectors to write
135 * @gfp_mask: memory allocation flags (for bio_alloc)
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400136 *
137 * Description:
138 * Generate and issue number of bios with zerofiled pages.
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400139 */
140
141int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200142 sector_t nr_sects, gfp_t gfp_mask)
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400143{
Dmitry Monakhov18edc8e2010-08-06 13:23:25 +0200144 int ret;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400145 struct bio *bio;
146 struct bio_batch bb;
Lukas Czerner0aeea182011-03-11 10:23:53 +0100147 unsigned int sz;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400148 DECLARE_COMPLETION_ONSTACK(wait);
149
Lukas Czerner0aeea182011-03-11 10:23:53 +0100150 atomic_set(&bb.done, 1);
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400151 bb.flags = 1 << BIO_UPTODATE;
152 bb.wait = &wait;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400153
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400154submit:
Dmitry Monakhov18edc8e2010-08-06 13:23:25 +0200155 ret = 0;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400156 while (nr_sects != 0) {
157 bio = bio_alloc(gfp_mask,
158 min(nr_sects, (sector_t)BIO_MAX_PAGES));
Dmitry Monakhov18edc8e2010-08-06 13:23:25 +0200159 if (!bio) {
160 ret = -ENOMEM;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400161 break;
Dmitry Monakhov18edc8e2010-08-06 13:23:25 +0200162 }
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400163
164 bio->bi_sector = sector;
165 bio->bi_bdev = bdev;
166 bio->bi_end_io = bio_batch_end_io;
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200167 bio->bi_private = &bb;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400168
Jens Axboe0341aaf2010-04-29 09:28:21 +0200169 while (nr_sects != 0) {
170 sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400171 if (sz == 0)
172 /* bio has maximum size possible */
173 break;
174 ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
175 nr_sects -= ret >> 9;
176 sector += ret >> 9;
177 if (ret < (sz << 9))
178 break;
179 }
Dmitry Monakhov18edc8e2010-08-06 13:23:25 +0200180 ret = 0;
Lukas Czerner0aeea182011-03-11 10:23:53 +0100181 atomic_inc(&bb.done);
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400182 submit_bio(WRITE, bio);
183 }
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400184
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200185 /* Wait for bios in-flight */
Lukas Czerner0aeea182011-03-11 10:23:53 +0100186 if (!atomic_dec_and_test(&bb.done))
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200187 wait_for_completion(&wait);
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400188
189 if (!test_bit(BIO_UPTODATE, &bb.flags))
190 /* One of bios in the batch was completed with error.*/
191 ret = -EIO;
192
193 if (ret)
194 goto out;
195
196 if (test_bit(BIO_EOPNOTSUPP, &bb.flags)) {
197 ret = -EOPNOTSUPP;
198 goto out;
199 }
200 if (nr_sects != 0)
201 goto submit;
202out:
203 return ret;
204}
205EXPORT_SYMBOL(blkdev_issue_zeroout);