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 | |
| 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); |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 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); |
Christoph Hellwig | 8c55536 | 2010-08-18 05:29:22 -0400 | [diff] [blame] | 42 | int type = REQ_WRITE | REQ_DISCARD; |
Jens Axboe | 10d1f9e | 2010-07-15 10:49:31 -0600 | [diff] [blame] | 43 | unsigned int max_discard_sectors; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 44 | struct bio *bio; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 45 | int ret = 0; |
| 46 | |
| 47 | if (!q) |
| 48 | return -ENXIO; |
| 49 | |
| 50 | if (!blk_queue_discard(q)) |
| 51 | return -EOPNOTSUPP; |
| 52 | |
Jens Axboe | 10d1f9e | 2010-07-15 10:49:31 -0600 | [diff] [blame] | 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; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 60 | |
Jens Axboe | 10d1f9e | 2010-07-15 10:49:31 -0600 | [diff] [blame] | 61 | max_discard_sectors &= ~(disc_sects - 1); |
| 62 | } |
| 63 | |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 64 | if (flags & BLKDEV_DISCARD_SECURE) { |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 65 | if (!blk_queue_secdiscard(q)) |
| 66 | return -EOPNOTSUPP; |
Christoph Hellwig | 8c55536 | 2010-08-18 05:29:22 -0400 | [diff] [blame] | 67 | type |= REQ_SECURE; |
Adrian Hunter | 8d57a98 | 2010-08-11 14:17:49 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jens Axboe | 10d1f9e | 2010-07-15 10:49:31 -0600 | [diff] [blame] | 70 | while (nr_sects && !ret) { |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 71 | bio = bio_alloc(gfp_mask, 1); |
Christoph Hellwig | 66ac028 | 2010-06-18 16:59:42 +0200 | [diff] [blame] | 72 | if (!bio) { |
| 73 | ret = -ENOMEM; |
| 74 | break; |
| 75 | } |
| 76 | |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 77 | bio->bi_sector = sector; |
| 78 | bio->bi_end_io = blkdev_discard_end_io; |
| 79 | bio->bi_bdev = bdev; |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 80 | bio->bi_private = &wait; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 81 | |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 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 | |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 94 | wait_for_completion(&wait); |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 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 | } |
Christoph Hellwig | 66ac028 | 2010-06-18 16:59:42 +0200 | [diff] [blame] | 102 | |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 103 | return ret; |
Dmitry Monakhov | f31e7e4 | 2010-04-28 17:55:08 +0400 | [diff] [blame] | 104 | } |
| 105 | EXPORT_SYMBOL(blkdev_issue_discard); |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 106 | |
| 107 | struct bio_batch |
| 108 | { |
| 109 | atomic_t done; |
| 110 | unsigned long flags; |
| 111 | struct completion *wait; |
| 112 | bio_end_io_t *end_io; |
| 113 | }; |
| 114 | |
| 115 | static void bio_batch_end_io(struct bio *bio, int err) |
| 116 | { |
| 117 | struct bio_batch *bb = bio->bi_private; |
Jens Axboe | 0341aaf | 2010-04-29 09:28:21 +0200 | [diff] [blame] | 118 | |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 119 | if (err) { |
| 120 | if (err == -EOPNOTSUPP) |
| 121 | set_bit(BIO_EOPNOTSUPP, &bb->flags); |
| 122 | else |
| 123 | clear_bit(BIO_UPTODATE, &bb->flags); |
| 124 | } |
| 125 | if (bb) { |
| 126 | if (bb->end_io) |
| 127 | bb->end_io(bio, err); |
| 128 | atomic_inc(&bb->done); |
| 129 | complete(bb->wait); |
| 130 | } |
| 131 | bio_put(bio); |
| 132 | } |
| 133 | |
| 134 | /** |
| 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) |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 140 | * |
| 141 | * Description: |
| 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. |
| 145 | */ |
| 146 | |
| 147 | int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 148 | sector_t nr_sects, gfp_t gfp_mask) |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 149 | { |
Dmitry Monakhov | 18edc8e | 2010-08-06 13:23:25 +0200 | [diff] [blame] | 150 | int ret; |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 151 | struct bio *bio; |
| 152 | struct bio_batch bb; |
| 153 | unsigned int sz, issued = 0; |
| 154 | DECLARE_COMPLETION_ONSTACK(wait); |
| 155 | |
| 156 | atomic_set(&bb.done, 0); |
| 157 | bb.flags = 1 << BIO_UPTODATE; |
| 158 | bb.wait = &wait; |
| 159 | bb.end_io = NULL; |
| 160 | |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 161 | submit: |
Dmitry Monakhov | 18edc8e | 2010-08-06 13:23:25 +0200 | [diff] [blame] | 162 | ret = 0; |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 163 | while (nr_sects != 0) { |
| 164 | bio = bio_alloc(gfp_mask, |
| 165 | min(nr_sects, (sector_t)BIO_MAX_PAGES)); |
Dmitry Monakhov | 18edc8e | 2010-08-06 13:23:25 +0200 | [diff] [blame] | 166 | if (!bio) { |
| 167 | ret = -ENOMEM; |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 168 | break; |
Dmitry Monakhov | 18edc8e | 2010-08-06 13:23:25 +0200 | [diff] [blame] | 169 | } |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 170 | |
| 171 | bio->bi_sector = sector; |
| 172 | bio->bi_bdev = bdev; |
| 173 | bio->bi_end_io = bio_batch_end_io; |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 174 | bio->bi_private = &bb; |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 175 | |
Jens Axboe | 0341aaf | 2010-04-29 09:28:21 +0200 | [diff] [blame] | 176 | while (nr_sects != 0) { |
| 177 | sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects); |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 178 | if (sz == 0) |
| 179 | /* bio has maximum size possible */ |
| 180 | break; |
| 181 | ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0); |
| 182 | nr_sects -= ret >> 9; |
| 183 | sector += ret >> 9; |
| 184 | if (ret < (sz << 9)) |
| 185 | break; |
| 186 | } |
Dmitry Monakhov | 18edc8e | 2010-08-06 13:23:25 +0200 | [diff] [blame] | 187 | ret = 0; |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 188 | issued++; |
| 189 | submit_bio(WRITE, bio); |
| 190 | } |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 191 | |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame] | 192 | /* Wait for bios in-flight */ |
| 193 | while (issued != atomic_read(&bb.done)) |
| 194 | wait_for_completion(&wait); |
Dmitry Monakhov | 3f14d79 | 2010-04-28 17:55:09 +0400 | [diff] [blame] | 195 | |
| 196 | if (!test_bit(BIO_UPTODATE, &bb.flags)) |
| 197 | /* One of bios in the batch was completed with error.*/ |
| 198 | ret = -EIO; |
| 199 | |
| 200 | if (ret) |
| 201 | goto out; |
| 202 | |
| 203 | if (test_bit(BIO_EOPNOTSUPP, &bb.flags)) { |
| 204 | ret = -EOPNOTSUPP; |
| 205 | goto out; |
| 206 | } |
| 207 | if (nr_sects != 0) |
| 208 | goto submit; |
| 209 | out: |
| 210 | return ret; |
| 211 | } |
| 212 | EXPORT_SYMBOL(blkdev_issue_zeroout); |