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 struct bio *next_bio(struct bio *bio, unsigned int nr_pages, 13 gfp_t gfp) 14 { 15 struct bio *new = bio_alloc(gfp, nr_pages); 16 17 if (bio) { 18 bio_chain(bio, new); 19 submit_bio(bio); 20 } 21 22 return new; 23 } 24 25 int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, 26 sector_t nr_sects, gfp_t gfp_mask, int flags, 27 struct bio **biop) 28 { 29 struct request_queue *q = bdev_get_queue(bdev); 30 struct bio *bio = *biop; 31 unsigned int granularity; 32 enum req_op op; 33 int alignment; 34 35 if (!q) 36 return -ENXIO; 37 38 if (flags & BLKDEV_DISCARD_SECURE) { 39 if (!blk_queue_secure_erase(q)) 40 return -EOPNOTSUPP; 41 op = REQ_OP_SECURE_ERASE; 42 } else { 43 if (!blk_queue_discard(q)) 44 return -EOPNOTSUPP; 45 op = REQ_OP_DISCARD; 46 } 47 48 /* Zero-sector (unknown) and one-sector granularities are the same. */ 49 granularity = max(q->limits.discard_granularity >> 9, 1U); 50 alignment = (bdev_discard_alignment(bdev) >> 9) % granularity; 51 52 while (nr_sects) { 53 unsigned int req_sects; 54 sector_t end_sect, tmp; 55 56 /* Make sure bi_size doesn't overflow */ 57 req_sects = min_t(sector_t, nr_sects, UINT_MAX >> 9); 58 59 /** 60 * If splitting a request, and the next starting sector would be 61 * misaligned, stop the discard at the previous aligned sector. 62 */ 63 end_sect = sector + req_sects; 64 tmp = end_sect; 65 if (req_sects < nr_sects && 66 sector_div(tmp, granularity) != alignment) { 67 end_sect = end_sect - alignment; 68 sector_div(end_sect, granularity); 69 end_sect = end_sect * granularity + alignment; 70 req_sects = end_sect - sector; 71 } 72 73 bio = next_bio(bio, 1, gfp_mask); 74 bio->bi_iter.bi_sector = sector; 75 bio->bi_bdev = bdev; 76 bio_set_op_attrs(bio, op, 0); 77 78 bio->bi_iter.bi_size = req_sects << 9; 79 nr_sects -= req_sects; 80 sector = end_sect; 81 82 /* 83 * We can loop for a long time in here, if someone does 84 * full device discards (like mkfs). Be nice and allow 85 * us to schedule out to avoid softlocking if preempt 86 * is disabled. 87 */ 88 cond_resched(); 89 } 90 91 *biop = bio; 92 return 0; 93 } 94 EXPORT_SYMBOL(__blkdev_issue_discard); 95 96 /** 97 * blkdev_issue_discard - queue a discard 98 * @bdev: blockdev to issue discard for 99 * @sector: start sector 100 * @nr_sects: number of sectors to discard 101 * @gfp_mask: memory allocation flags (for bio_alloc) 102 * @flags: BLKDEV_IFL_* flags to control behaviour 103 * 104 * Description: 105 * Issue a discard request for the sectors in question. 106 */ 107 int blkdev_issue_discard(struct block_device *bdev, sector_t sector, 108 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags) 109 { 110 struct bio *bio = NULL; 111 struct blk_plug plug; 112 int ret; 113 114 blk_start_plug(&plug); 115 ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, flags, 116 &bio); 117 if (!ret && bio) { 118 ret = submit_bio_wait(bio); 119 if (ret == -EOPNOTSUPP) 120 ret = 0; 121 } 122 blk_finish_plug(&plug); 123 124 return ret; 125 } 126 EXPORT_SYMBOL(blkdev_issue_discard); 127 128 /** 129 * blkdev_issue_write_same - queue a write same operation 130 * @bdev: target blockdev 131 * @sector: start sector 132 * @nr_sects: number of sectors to write 133 * @gfp_mask: memory allocation flags (for bio_alloc) 134 * @page: page containing data to write 135 * 136 * Description: 137 * Issue a write same request for the sectors in question. 138 */ 139 int blkdev_issue_write_same(struct block_device *bdev, sector_t sector, 140 sector_t nr_sects, gfp_t gfp_mask, 141 struct page *page) 142 { 143 struct request_queue *q = bdev_get_queue(bdev); 144 unsigned int max_write_same_sectors; 145 struct bio *bio = NULL; 146 int ret = 0; 147 148 if (!q) 149 return -ENXIO; 150 151 /* Ensure that max_write_same_sectors doesn't overflow bi_size */ 152 max_write_same_sectors = UINT_MAX >> 9; 153 154 while (nr_sects) { 155 bio = next_bio(bio, 1, gfp_mask); 156 bio->bi_iter.bi_sector = sector; 157 bio->bi_bdev = bdev; 158 bio->bi_vcnt = 1; 159 bio->bi_io_vec->bv_page = page; 160 bio->bi_io_vec->bv_offset = 0; 161 bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev); 162 bio_set_op_attrs(bio, REQ_OP_WRITE_SAME, 0); 163 164 if (nr_sects > max_write_same_sectors) { 165 bio->bi_iter.bi_size = max_write_same_sectors << 9; 166 nr_sects -= max_write_same_sectors; 167 sector += max_write_same_sectors; 168 } else { 169 bio->bi_iter.bi_size = nr_sects << 9; 170 nr_sects = 0; 171 } 172 } 173 174 if (bio) 175 ret = submit_bio_wait(bio); 176 return ret != -EOPNOTSUPP ? ret : 0; 177 } 178 EXPORT_SYMBOL(blkdev_issue_write_same); 179 180 /** 181 * blkdev_issue_zeroout - generate number of zero filed write bios 182 * @bdev: blockdev to issue 183 * @sector: start sector 184 * @nr_sects: number of sectors to write 185 * @gfp_mask: memory allocation flags (for bio_alloc) 186 * 187 * Description: 188 * Generate and issue number of bios with zerofiled pages. 189 */ 190 191 static int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, 192 sector_t nr_sects, gfp_t gfp_mask) 193 { 194 int ret; 195 struct bio *bio = NULL; 196 unsigned int sz; 197 198 while (nr_sects != 0) { 199 bio = next_bio(bio, min(nr_sects, (sector_t)BIO_MAX_PAGES), 200 gfp_mask); 201 bio->bi_iter.bi_sector = sector; 202 bio->bi_bdev = bdev; 203 bio_set_op_attrs(bio, REQ_OP_WRITE, 0); 204 205 while (nr_sects != 0) { 206 sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects); 207 ret = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0); 208 nr_sects -= ret >> 9; 209 sector += ret >> 9; 210 if (ret < (sz << 9)) 211 break; 212 } 213 } 214 215 if (bio) 216 return submit_bio_wait(bio); 217 return 0; 218 } 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) 226 * @discard: whether to discard the block range 227 * 228 * Description: 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. 239 */ 240 241 int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, 242 sector_t nr_sects, gfp_t gfp_mask, bool discard) 243 { 244 struct request_queue *q = bdev_get_queue(bdev); 245 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; 249 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; 254 255 return __blkdev_issue_zeroout(bdev, sector, nr_sects, gfp_mask); 256 } 257 EXPORT_SYMBOL(blkdev_issue_zeroout); 258