blob: bd40292e5009675ef17d6914b67010834cf1beb6 [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
Lukas Czerner5dba3082011-05-06 19:26:27 -060012struct bio_batch {
13 atomic_t done;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020014 int error;
Lukas Czerner5dba3082011-05-06 19:26:27 -060015 struct completion *wait;
16};
17
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020018static void bio_batch_end_io(struct bio *bio)
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040019{
Lukas Czerner5dba3082011-05-06 19:26:27 -060020 struct bio_batch *bb = bio->bi_private;
21
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020022 if (bio->bi_error && bio->bi_error != -EOPNOTSUPP)
23 bb->error = bio->bi_error;
Lukas Czerner5dba3082011-05-06 19:26:27 -060024 if (atomic_dec_and_test(&bb->done))
25 complete(bb->wait);
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040026 bio_put(bio);
27}
28
Ming Linb49a0872015-05-22 00:46:56 -070029/*
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
32 * of two.
33 */
34#define MAX_BIO_SECTORS ((1U << 31) >> 9)
35
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040036/**
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
43 *
44 * Description:
45 * Issue a discard request for the sectors in question.
46 */
47int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
48 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags)
49{
50 DECLARE_COMPLETION_ONSTACK(wait);
51 struct request_queue *q = bdev_get_queue(bdev);
Christoph Hellwig8c555362010-08-18 05:29:22 -040052 int type = REQ_WRITE | REQ_DISCARD;
Lukas Czerner5dba3082011-05-06 19:26:27 -060053 struct bio_batch bb;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040054 struct bio *bio;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040055 int ret = 0;
Shaohua Li0cfbcaf2012-12-14 11:15:51 +080056 struct blk_plug plug;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040057
58 if (!q)
59 return -ENXIO;
60
61 if (!blk_queue_discard(q))
62 return -EOPNOTSUPP;
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
Lukas Czerner5dba3082011-05-06 19:26:27 -060070 atomic_set(&bb.done, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020071 bb.error = 0;
Lukas Czerner5dba3082011-05-06 19:26:27 -060072 bb.wait = &wait;
73
Shaohua Li0cfbcaf2012-12-14 11:15:51 +080074 blk_start_plug(&plug);
Lukas Czerner5dba3082011-05-06 19:26:27 -060075 while (nr_sects) {
Paolo Bonzinic6e66632012-08-02 09:48:50 +020076 unsigned int req_sects;
Ming Linb49a0872015-05-22 00:46:56 -070077 sector_t end_sect;
Paolo Bonzinic6e66632012-08-02 09:48:50 +020078
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040079 bio = bio_alloc(gfp_mask, 1);
Christoph Hellwig66ac0282010-06-18 16:59:42 +020080 if (!bio) {
81 ret = -ENOMEM;
82 break;
83 }
84
Ming Linb49a0872015-05-22 00:46:56 -070085 req_sects = min_t(sector_t, nr_sects, MAX_BIO_SECTORS);
Paolo Bonzinic6e66632012-08-02 09:48:50 +020086 end_sect = sector + req_sects;
Paolo Bonzinic6e66632012-08-02 09:48:50 +020087
Kent Overstreet4f024f32013-10-11 15:44:27 -070088 bio->bi_iter.bi_sector = sector;
Lukas Czerner5dba3082011-05-06 19:26:27 -060089 bio->bi_end_io = bio_batch_end_io;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040090 bio->bi_bdev = bdev;
Lukas Czerner5dba3082011-05-06 19:26:27 -060091 bio->bi_private = &bb;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040092
Kent Overstreet4f024f32013-10-11 15:44:27 -070093 bio->bi_iter.bi_size = req_sects << 9;
Paolo Bonzinic6e66632012-08-02 09:48:50 +020094 nr_sects -= req_sects;
95 sector = end_sect;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040096
Lukas Czerner5dba3082011-05-06 19:26:27 -060097 atomic_inc(&bb.done);
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +040098 submit_bio(type, bio);
Jens Axboec8123f82014-02-12 09:34:01 -070099
100 /*
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
104 * is disabled.
105 */
106 cond_resched();
Lukas Czerner5dba3082011-05-06 19:26:27 -0600107 }
Shaohua Li0cfbcaf2012-12-14 11:15:51 +0800108 blk_finish_plug(&plug);
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +0400109
Lukas Czerner5dba3082011-05-06 19:26:27 -0600110 /* Wait for bios in-flight */
111 if (!atomic_dec_and_test(&bb.done))
Vladimir Davydov55770222013-02-14 18:19:59 +0400112 wait_for_completion_io(&wait);
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +0400113
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200114 if (bb.error)
115 return bb.error;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +0400116 return ret;
Dmitry Monakhovf31e7e42010-04-28 17:55:08 +0400117}
118EXPORT_SYMBOL(blkdev_issue_discard);
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400119
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400120/**
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400121 * 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
127 *
128 * Description:
129 * Issue a write same request for the sectors in question.
130 */
131int blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
132 sector_t nr_sects, gfp_t gfp_mask,
133 struct page *page)
134{
135 DECLARE_COMPLETION_ONSTACK(wait);
136 struct request_queue *q = bdev_get_queue(bdev);
137 unsigned int max_write_same_sectors;
138 struct bio_batch bb;
139 struct bio *bio;
140 int ret = 0;
141
142 if (!q)
143 return -ENXIO;
144
Ming Linb49a0872015-05-22 00:46:56 -0700145 /* Ensure that max_write_same_sectors doesn't overflow bi_size */
146 max_write_same_sectors = UINT_MAX >> 9;
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400147
148 atomic_set(&bb.done, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200149 bb.error = 0;
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400150 bb.wait = &wait;
151
152 while (nr_sects) {
153 bio = bio_alloc(gfp_mask, 1);
154 if (!bio) {
155 ret = -ENOMEM;
156 break;
157 }
158
Kent Overstreet4f024f32013-10-11 15:44:27 -0700159 bio->bi_iter.bi_sector = sector;
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400160 bio->bi_end_io = bio_batch_end_io;
161 bio->bi_bdev = bdev;
162 bio->bi_private = &bb;
163 bio->bi_vcnt = 1;
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);
167
168 if (nr_sects > max_write_same_sectors) {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700169 bio->bi_iter.bi_size = max_write_same_sectors << 9;
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400170 nr_sects -= max_write_same_sectors;
171 sector += max_write_same_sectors;
172 } else {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700173 bio->bi_iter.bi_size = nr_sects << 9;
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400174 nr_sects = 0;
175 }
176
177 atomic_inc(&bb.done);
178 submit_bio(REQ_WRITE | REQ_WRITE_SAME, bio);
179 }
180
181 /* Wait for bios in-flight */
182 if (!atomic_dec_and_test(&bb.done))
Vladimir Davydov55770222013-02-14 18:19:59 +0400183 wait_for_completion_io(&wait);
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400184
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200185 if (bb.error)
186 return bb.error;
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400187 return ret;
188}
189EXPORT_SYMBOL(blkdev_issue_write_same);
190
191/**
Ben Hutchings291d24f2011-03-01 13:45:24 -0500192 * blkdev_issue_zeroout - generate number of zero filed write bios
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400193 * @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)
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400197 *
198 * Description:
199 * Generate and issue number of bios with zerofiled pages.
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400200 */
201
Fabian Frederick35086782014-05-26 22:19:14 +0200202static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
203 sector_t nr_sects, gfp_t gfp_mask)
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400204{
Dmitry Monakhov18edc8e2010-08-06 13:23:25 +0200205 int ret;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400206 struct bio *bio;
207 struct bio_batch bb;
Lukas Czerner0aeea182011-03-11 10:23:53 +0100208 unsigned int sz;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400209 DECLARE_COMPLETION_ONSTACK(wait);
210
Lukas Czerner0aeea182011-03-11 10:23:53 +0100211 atomic_set(&bb.done, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200212 bb.error = 0;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400213 bb.wait = &wait;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400214
Dmitry Monakhov18edc8e2010-08-06 13:23:25 +0200215 ret = 0;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400216 while (nr_sects != 0) {
217 bio = bio_alloc(gfp_mask,
218 min(nr_sects, (sector_t)BIO_MAX_PAGES));
Dmitry Monakhov18edc8e2010-08-06 13:23:25 +0200219 if (!bio) {
220 ret = -ENOMEM;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400221 break;
Dmitry Monakhov18edc8e2010-08-06 13:23:25 +0200222 }
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400223
Kent Overstreet4f024f32013-10-11 15:44:27 -0700224 bio->bi_iter.bi_sector = sector;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400225 bio->bi_bdev = bdev;
226 bio->bi_end_io = bio_batch_end_io;
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200227 bio->bi_private = &bb;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400228
Jens Axboe0341aaf2010-04-29 09:28:21 +0200229 while (nr_sects != 0) {
230 sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects);
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400231 ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0);
232 nr_sects -= ret >> 9;
233 sector += ret >> 9;
234 if (ret < (sz << 9))
235 break;
236 }
Dmitry Monakhov18edc8e2010-08-06 13:23:25 +0200237 ret = 0;
Lukas Czerner0aeea182011-03-11 10:23:53 +0100238 atomic_inc(&bb.done);
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400239 submit_bio(WRITE, bio);
240 }
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400241
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200242 /* Wait for bios in-flight */
Lukas Czerner0aeea182011-03-11 10:23:53 +0100243 if (!atomic_dec_and_test(&bb.done))
Vladimir Davydov55770222013-02-14 18:19:59 +0400244 wait_for_completion_io(&wait);
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400245
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200246 if (bb.error)
247 return bb.error;
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400248 return ret;
249}
Martin K. Petersen579e8f32012-09-18 12:19:28 -0400250
251/**
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)
Martin K. Petersend93ba7a2015-01-20 20:06:30 -0500257 * @discard: whether to discard the block range
Martin K. Petersen579e8f32012-09-18 12:19:28 -0400258 *
259 * Description:
Martin K. Petersend93ba7a2015-01-20 20:06:30 -0500260 * 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.
Martin K. Petersen579e8f32012-09-18 12:19:28 -0400270 */
271
272int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
Martin K. Petersend93ba7a2015-01-20 20:06:30 -0500273 sector_t nr_sects, gfp_t gfp_mask, bool discard)
Martin K. Petersen579e8f32012-09-18 12:19:28 -0400274{
Martin K. Petersend93ba7a2015-01-20 20:06:30 -0500275 struct request_queue *q = bdev_get_queue(bdev);
Martin K. Petersend93ba7a2015-01-20 20:06:30 -0500276
Martin K. Petersen9f9ee1f2015-02-05 10:14:54 -0700277 if (discard && blk_queue_discard(q) && q->limits.discard_zeroes_data &&
278 blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, 0) == 0)
279 return 0;
Martin K. Petersend93ba7a2015-01-20 20:06:30 -0500280
Martin K. Petersen9f9ee1f2015-02-05 10:14:54 -0700281 if (bdev_write_same(bdev) &&
282 blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask,
283 ZERO_PAGE(0)) == 0)
284 return 0;
Martin K. Petersen579e8f32012-09-18 12:19:28 -0400285
286 return __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask);
287}
Dmitry Monakhov3f14d792010-04-28 17:55:09 +0400288EXPORT_SYMBOL(blkdev_issue_zeroout);