Commit | Line | Data |
---|---|---|
f31e7e40 DM |
1 | /* |
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 | ||
12 | static 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); | |
f31e7e40 DM |
22 | |
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 | */ | |
37 | int 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); | |
8c555367 | 42 | int type = REQ_WRITE | REQ_DISCARD; |
10d1f9e2 | 43 | unsigned int max_discard_sectors; |
f31e7e40 | 44 | struct bio *bio; |
f31e7e40 DM |
45 | int ret = 0; |
46 | ||
47 | if (!q) | |
48 | return -ENXIO; | |
49 | ||
50 | if (!blk_queue_discard(q)) | |
51 | return -EOPNOTSUPP; | |
52 | ||
10d1f9e2 JA |
53 | /* |
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; | |
60 | ||
61 | max_discard_sectors &= ~(disc_sects - 1); | |
62 | } | |
f31e7e40 | 63 | |
dd3932ed | 64 | if (flags & BLKDEV_DISCARD_SECURE) { |
8d57a98c AH |
65 | if (!blk_queue_secdiscard(q)) |
66 | return -EOPNOTSUPP; | |
8c555367 | 67 | type |= REQ_SECURE; |
8d57a98c AH |
68 | } |
69 | ||
10d1f9e2 | 70 | while (nr_sects && !ret) { |
f31e7e40 | 71 | bio = bio_alloc(gfp_mask, 1); |
66ac0280 CH |
72 | if (!bio) { |
73 | ret = -ENOMEM; | |
74 | break; | |
75 | } | |
76 | ||
f31e7e40 DM |
77 | bio->bi_sector = sector; |
78 | bio->bi_end_io = blkdev_discard_end_io; | |
79 | bio->bi_bdev = bdev; | |
dd3932ed | 80 | bio->bi_private = &wait; |
f31e7e40 | 81 | |
f31e7e40 DM |
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; | |
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 | ||
dd3932ed | 94 | wait_for_completion(&wait); |
f31e7e40 DM |
95 | |
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 | } | |
66ac0280 | 102 | |
f31e7e40 | 103 | return ret; |
f31e7e40 DM |
104 | } |
105 | EXPORT_SYMBOL(blkdev_issue_discard); | |
3f14d792 DM |
106 | |
107 | struct bio_batch | |
108 | { | |
109 | atomic_t done; | |
110 | unsigned long flags; | |
111 | struct completion *wait; | |
3f14d792 DM |
112 | }; |
113 | ||
114 | static void bio_batch_end_io(struct bio *bio, int err) | |
115 | { | |
116 | struct bio_batch *bb = bio->bi_private; | |
0341aafb | 117 | |
3f14d792 DM |
118 | if (err) { |
119 | if (err == -EOPNOTSUPP) | |
120 | set_bit(BIO_EOPNOTSUPP, &bb->flags); | |
121 | else | |
122 | clear_bit(BIO_UPTODATE, &bb->flags); | |
123 | } | |
0aeea189 LC |
124 | if (bb) |
125 | if (atomic_dec_and_test(&bb->done)) | |
126 | complete(bb->wait); | |
3f14d792 DM |
127 | bio_put(bio); |
128 | } | |
129 | ||
130 | /** | |
291d24f6 | 131 | * blkdev_issue_zeroout - generate number of zero filed write bios |
3f14d792 DM |
132 | * @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) | |
3f14d792 DM |
136 | * |
137 | * Description: | |
138 | * Generate and issue number of bios with zerofiled pages. | |
3f14d792 DM |
139 | */ |
140 | ||
141 | int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, | |
dd3932ed | 142 | sector_t nr_sects, gfp_t gfp_mask) |
3f14d792 | 143 | { |
18edc8ea | 144 | int ret; |
3f14d792 DM |
145 | struct bio *bio; |
146 | struct bio_batch bb; | |
0aeea189 | 147 | unsigned int sz; |
3f14d792 DM |
148 | DECLARE_COMPLETION_ONSTACK(wait); |
149 | ||
0aeea189 | 150 | atomic_set(&bb.done, 1); |
3f14d792 DM |
151 | bb.flags = 1 << BIO_UPTODATE; |
152 | bb.wait = &wait; | |
3f14d792 | 153 | |
3f14d792 | 154 | submit: |
18edc8ea | 155 | ret = 0; |
3f14d792 DM |
156 | while (nr_sects != 0) { |
157 | bio = bio_alloc(gfp_mask, | |
158 | min(nr_sects, (sector_t)BIO_MAX_PAGES)); | |
18edc8ea DM |
159 | if (!bio) { |
160 | ret = -ENOMEM; | |
3f14d792 | 161 | break; |
18edc8ea | 162 | } |
3f14d792 DM |
163 | |
164 | bio->bi_sector = sector; | |
165 | bio->bi_bdev = bdev; | |
166 | bio->bi_end_io = bio_batch_end_io; | |
dd3932ed | 167 | bio->bi_private = &bb; |
3f14d792 | 168 | |
0341aafb JA |
169 | while (nr_sects != 0) { |
170 | sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects); | |
3f14d792 DM |
171 | 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 | } | |
18edc8ea | 180 | ret = 0; |
0aeea189 | 181 | atomic_inc(&bb.done); |
3f14d792 DM |
182 | submit_bio(WRITE, bio); |
183 | } | |
3f14d792 | 184 | |
dd3932ed | 185 | /* Wait for bios in-flight */ |
0aeea189 | 186 | if (!atomic_dec_and_test(&bb.done)) |
dd3932ed | 187 | wait_for_completion(&wait); |
3f14d792 DM |
188 | |
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; | |
202 | out: | |
203 | return ret; | |
204 | } | |
205 | EXPORT_SYMBOL(blkdev_issue_zeroout); |