Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 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 | |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 12 | static struct bio *next_bio(struct bio *bio, int rw, unsigned int nr_pages, |
| 13 | gfp_t gfp) |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 14 | { |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 15 | struct bio *new = bio_alloc(gfp, nr_pages); |
Lukas Czerner | 5dba308 | 2011-05-06 19:26:27 -0600 | [diff] [blame] | 16 | |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 17 | if (bio) { |
| 18 | bio_chain(bio, new); |
| 19 | submit_bio(rw, bio); |
| 20 | } |
| 21 | |
| 22 | return new; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 23 | } |
| 24 | |
Christoph Hellwig | 38f2525 | 2016-04-16 14:55:28 -0400 | [diff] [blame] | 25 | int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, |
| 26 | sector_t nr_sects, gfp_t gfp_mask, int type, struct bio **biop) |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 27 | { |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 28 | struct request_queue *q = bdev_get_queue(bdev); |
Christoph Hellwig | 38f2525 | 2016-04-16 14:55:28 -0400 | [diff] [blame] | 29 | struct bio *bio = *biop; |
Ming Lin | a22c4d7 | 2015-10-22 09:59:42 -0700 | [diff] [blame] | 30 | unsigned int granularity; |
| 31 | int alignment; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 32 | |
| 33 | if (!q) |
| 34 | return -ENXIO; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 35 | if (!blk_queue_discard(q)) |
| 36 | return -EOPNOTSUPP; |
Christoph Hellwig | 38f2525 | 2016-04-16 14:55:28 -0400 | [diff] [blame] | 37 | if ((type & REQ_SECURE) && !blk_queue_secdiscard(q)) |
| 38 | return -EOPNOTSUPP; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 39 | |
Ming Lin | a22c4d7 | 2015-10-22 09:59:42 -0700 | [diff] [blame] | 40 | /* Zero-sector (unknown) and one-sector granularities are the same. */ |
| 41 | granularity = max(q->limits.discard_granularity >> 9, 1U); |
| 42 | alignment = (bdev_discard_alignment(bdev) >> 9) % granularity; |
| 43 | |
Lukas Czerner | 5dba308 | 2011-05-06 19:26:27 -0600 | [diff] [blame] | 44 | while (nr_sects) { |
Paolo Bonzini | c6e6663 | 2012-08-02 09:48:50 +0200 | [diff] [blame] | 45 | unsigned int req_sects; |
Ming Lin | a22c4d7 | 2015-10-22 09:59:42 -0700 | [diff] [blame] | 46 | sector_t end_sect, tmp; |
Paolo Bonzini | c6e6663 | 2012-08-02 09:48:50 +0200 | [diff] [blame] | 47 | |
Ming Lin | a22c4d7 | 2015-10-22 09:59:42 -0700 | [diff] [blame] | 48 | /* Make sure bi_size doesn't overflow */ |
| 49 | req_sects = min_t(sector_t, nr_sects, UINT_MAX >> 9); |
| 50 | |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 51 | /** |
Ming Lin | a22c4d7 | 2015-10-22 09:59:42 -0700 | [diff] [blame] | 52 | * If splitting a request, and the next starting sector would be |
| 53 | * misaligned, stop the discard at the previous aligned sector. |
| 54 | */ |
Paolo Bonzini | c6e6663 | 2012-08-02 09:48:50 +0200 | [diff] [blame] | 55 | end_sect = sector + req_sects; |
Ming Lin | a22c4d7 | 2015-10-22 09:59:42 -0700 | [diff] [blame] | 56 | tmp = end_sect; |
| 57 | if (req_sects < nr_sects && |
| 58 | sector_div(tmp, granularity) != alignment) { |
| 59 | end_sect = end_sect - alignment; |
| 60 | sector_div(end_sect, granularity); |
| 61 | end_sect = end_sect * granularity + alignment; |
| 62 | req_sects = end_sect - sector; |
| 63 | } |
Paolo Bonzini | c6e6663 | 2012-08-02 09:48:50 +0200 | [diff] [blame] | 64 | |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 65 | bio = next_bio(bio, type, 1, gfp_mask); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 66 | bio->bi_iter.bi_sector = sector; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 67 | bio->bi_bdev = bdev; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 68 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 69 | bio->bi_iter.bi_size = req_sects << 9; |
Paolo Bonzini | c6e6663 | 2012-08-02 09:48:50 +0200 | [diff] [blame] | 70 | nr_sects -= req_sects; |
| 71 | sector = end_sect; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 72 | |
Jens Axboe | c8123f8 | 2014-02-12 09:34:01 -0700 | [diff] [blame] | 73 | /* |
| 74 | * We can loop for a long time in here, if someone does |
| 75 | * full device discards (like mkfs). Be nice and allow |
| 76 | * us to schedule out to avoid softlocking if preempt |
| 77 | * is disabled. |
| 78 | */ |
| 79 | cond_resched(); |
Lukas Czerner | 5dba308 | 2011-05-06 19:26:27 -0600 | [diff] [blame] | 80 | } |
Christoph Hellwig | 38f2525 | 2016-04-16 14:55:28 -0400 | [diff] [blame] | 81 | |
| 82 | *biop = bio; |
| 83 | return 0; |
| 84 | } |
| 85 | EXPORT_SYMBOL(__blkdev_issue_discard); |
| 86 | |
| 87 | /** |
| 88 | * blkdev_issue_discard - queue a discard |
| 89 | * @bdev: blockdev to issue discard for |
| 90 | * @sector: start sector |
| 91 | * @nr_sects: number of sectors to discard |
| 92 | * @gfp_mask: memory allocation flags (for bio_alloc) |
| 93 | * @flags: BLKDEV_IFL_* flags to control behaviour |
| 94 | * |
| 95 | * Description: |
| 96 | * Issue a discard request for the sectors in question. |
| 97 | */ |
| 98 | int blkdev_issue_discard(struct block_device *bdev, sector_t sector, |
| 99 | sector_t nr_sects, gfp_t gfp_mask, unsigned long flags) |
| 100 | { |
| 101 | int type = REQ_WRITE | REQ_DISCARD; |
| 102 | struct bio *bio = NULL; |
| 103 | struct blk_plug plug; |
| 104 | int ret; |
| 105 | |
| 106 | if (flags & BLKDEV_DISCARD_SECURE) |
| 107 | type |= REQ_SECURE; |
| 108 | |
| 109 | blk_start_plug(&plug); |
| 110 | ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, type, |
| 111 | &bio); |
Mike Snitzer | bbd848e0f | 2016-05-05 11:54:21 -0400 | [diff] [blame] | 112 | if (!ret && bio) { |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 113 | ret = submit_bio_wait(type, bio); |
Mike Snitzer | bbd848e0f | 2016-05-05 11:54:21 -0400 | [diff] [blame] | 114 | if (ret == -EOPNOTSUPP) |
| 115 | ret = 0; |
Shaun Tancheff | 05bd92d | 2016-06-07 11:32:13 -0500 | [diff] [blame] | 116 | bio_put(bio); |
Mike Snitzer | bbd848e0f | 2016-05-05 11:54:21 -0400 | [diff] [blame] | 117 | } |
Shaohua Li | 0cfbcaf | 2012-12-14 11:15:51 +0800 | [diff] [blame] | 118 | blk_finish_plug(&plug); |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 119 | |
Mike Snitzer | bbd848e0f | 2016-05-05 11:54:21 -0400 | [diff] [blame] | 120 | return ret; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 121 | } |
| 122 | EXPORT_SYMBOL(blkdev_issue_discard); |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 123 | |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 124 | /** |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 125 | * blkdev_issue_write_same - queue a write same operation |
| 126 | * @bdev: target blockdev |
| 127 | * @sector: start sector |
| 128 | * @nr_sects: number of sectors to write |
| 129 | * @gfp_mask: memory allocation flags (for bio_alloc) |
| 130 | * @page: page containing data to write |
| 131 | * |
| 132 | * Description: |
| 133 | * Issue a write same request for the sectors in question. |
| 134 | */ |
| 135 | int blkdev_issue_write_same(struct block_device *bdev, sector_t sector, |
| 136 | sector_t nr_sects, gfp_t gfp_mask, |
| 137 | struct page *page) |
| 138 | { |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 139 | struct request_queue *q = bdev_get_queue(bdev); |
| 140 | unsigned int max_write_same_sectors; |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 141 | struct bio *bio = NULL; |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 142 | int ret = 0; |
| 143 | |
| 144 | if (!q) |
| 145 | return -ENXIO; |
| 146 | |
Ming Lin | b49a087 | 2015-05-22 00:46:56 -0700 | [diff] [blame] | 147 | /* Ensure that max_write_same_sectors doesn't overflow bi_size */ |
| 148 | max_write_same_sectors = UINT_MAX >> 9; |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 149 | |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 150 | while (nr_sects) { |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 151 | bio = next_bio(bio, REQ_WRITE | REQ_WRITE_SAME, 1, gfp_mask); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 152 | bio->bi_iter.bi_sector = sector; |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 153 | bio->bi_bdev = bdev; |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 154 | bio->bi_vcnt = 1; |
| 155 | bio->bi_io_vec->bv_page = page; |
| 156 | bio->bi_io_vec->bv_offset = 0; |
| 157 | bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev); |
| 158 | |
| 159 | if (nr_sects > max_write_same_sectors) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 160 | bio->bi_iter.bi_size = max_write_same_sectors << 9; |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 161 | nr_sects -= max_write_same_sectors; |
| 162 | sector += max_write_same_sectors; |
| 163 | } else { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 164 | bio->bi_iter.bi_size = nr_sects << 9; |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 165 | nr_sects = 0; |
| 166 | } |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Shaun Tancheff | 05bd92d | 2016-06-07 11:32:13 -0500 | [diff] [blame] | 169 | if (bio) { |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 170 | ret = submit_bio_wait(REQ_WRITE | REQ_WRITE_SAME, bio); |
Shaun Tancheff | 05bd92d | 2016-06-07 11:32:13 -0500 | [diff] [blame] | 171 | bio_put(bio); |
| 172 | } |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 173 | return ret != -EOPNOTSUPP ? ret : 0; |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 174 | } |
| 175 | EXPORT_SYMBOL(blkdev_issue_write_same); |
| 176 | |
| 177 | /** |
Ben Hutchings | 291d24f | 2011-03-01 13:45:24 -0500 | [diff] [blame] | 178 | * blkdev_issue_zeroout - generate number of zero filed write bios |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 179 | * @bdev: blockdev to issue |
| 180 | * @sector: start sector |
| 181 | * @nr_sects: number of sectors to write |
| 182 | * @gfp_mask: memory allocation flags (for bio_alloc) |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 183 | * |
| 184 | * Description: |
| 185 | * Generate and issue number of bios with zerofiled pages. |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 186 | */ |
| 187 | |
Fabian Frederick | 3508678 | 2014-05-26 22:19:14 +0200 | [diff] [blame] | 188 | static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, |
| 189 | sector_t nr_sects, gfp_t gfp_mask) |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 190 | { |
Dmitry Monakhov | 18edc8e | 2010-08-06 13:23:25 +0200 | [diff] [blame] | 191 | int ret; |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 192 | struct bio *bio = NULL; |
Lukas Czerner | 0aeea18 | 2011-03-11 10:23:53 +0100 | [diff] [blame] | 193 | unsigned int sz; |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 194 | |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 195 | while (nr_sects != 0) { |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 196 | bio = next_bio(bio, WRITE, |
| 197 | min(nr_sects, (sector_t)BIO_MAX_PAGES), |
| 198 | gfp_mask); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 199 | bio->bi_iter.bi_sector = sector; |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 200 | bio->bi_bdev = bdev; |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 201 | |
Jens Axboe | 0341aaf | 2010-04-29 09:28:21 +0200 | [diff] [blame] | 202 | while (nr_sects != 0) { |
| 203 | sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects); |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 204 | ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0); |
| 205 | nr_sects -= ret >> 9; |
| 206 | sector += ret >> 9; |
| 207 | if (ret < (sz << 9)) |
| 208 | break; |
| 209 | } |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 210 | } |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 211 | |
Shaun Tancheff | 05bd92d | 2016-06-07 11:32:13 -0500 | [diff] [blame] | 212 | if (bio) { |
| 213 | ret = submit_bio_wait(WRITE, bio); |
| 214 | bio_put(bio); |
| 215 | return ret; |
| 216 | } |
Christoph Hellwig | 9082e87 | 2016-04-16 14:55:27 -0400 | [diff] [blame] | 217 | return 0; |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 218 | } |
Martin K. Petersen | 579e8f3 | 2012-09-18 12:19:28 -0400 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * blkdev_issue_zeroout - zero-fill a block range |
| 222 | * @bdev: blockdev to write |
| 223 | * @sector: start sector |
| 224 | * @nr_sects: number of sectors to write |
| 225 | * @gfp_mask: memory allocation flags (for bio_alloc) |
Martin K. Petersen | d93ba7a | 2015-01-20 20:06:30 -0500 | [diff] [blame] | 226 | * @discard: whether to discard the block range |
Martin K. Petersen | 579e8f3 | 2012-09-18 12:19:28 -0400 | [diff] [blame] | 227 | * |
| 228 | * Description: |
Martin K. Petersen | d93ba7a | 2015-01-20 20:06:30 -0500 | [diff] [blame] | 229 | * Zero-fill a block range. If the discard flag is set and the block |
| 230 | * device guarantees that subsequent READ operations to the block range |
| 231 | * in question will return zeroes, the blocks will be discarded. Should |
| 232 | * the discard request fail, if the discard flag is not set, or if |
| 233 | * discard_zeroes_data is not supported, this function will resort to |
| 234 | * zeroing the blocks manually, thus provisioning (allocating, |
| 235 | * anchoring) them. If the block device supports the WRITE SAME command |
| 236 | * blkdev_issue_zeroout() will use it to optimize the process of |
| 237 | * clearing the block range. Otherwise the zeroing will be performed |
| 238 | * using regular WRITE calls. |
Martin K. Petersen | 579e8f3 | 2012-09-18 12:19:28 -0400 | [diff] [blame] | 239 | */ |
| 240 | |
| 241 | int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, |
Martin K. Petersen | d93ba7a | 2015-01-20 20:06:30 -0500 | [diff] [blame] | 242 | sector_t nr_sects, gfp_t gfp_mask, bool discard) |
Martin K. Petersen | 579e8f3 | 2012-09-18 12:19:28 -0400 | [diff] [blame] | 243 | { |
Martin K. Petersen | d93ba7a | 2015-01-20 20:06:30 -0500 | [diff] [blame] | 244 | struct request_queue *q = bdev_get_queue(bdev); |
Martin K. Petersen | d93ba7a | 2015-01-20 20:06:30 -0500 | [diff] [blame] | 245 | |
Martin K. Petersen | 9f9ee1f | 2015-02-05 10:14:54 -0700 | [diff] [blame] | 246 | if (discard && blk_queue_discard(q) && q->limits.discard_zeroes_data && |
| 247 | blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, 0) == 0) |
| 248 | return 0; |
Martin K. Petersen | d93ba7a | 2015-01-20 20:06:30 -0500 | [diff] [blame] | 249 | |
Martin K. Petersen | 9f9ee1f | 2015-02-05 10:14:54 -0700 | [diff] [blame] | 250 | if (bdev_write_same(bdev) && |
| 251 | blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask, |
| 252 | ZERO_PAGE(0)) == 0) |
| 253 | return 0; |
Martin K. Petersen | 579e8f3 | 2012-09-18 12:19:28 -0400 | [diff] [blame] | 254 | |
| 255 | return __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask); |
| 256 | } |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 257 | EXPORT_SYMBOL(blkdev_issue_zeroout); |