1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * Functions related to generic helpers functi 3 * Functions related to generic helpers functions 4 */ 4 */ 5 #include <linux/kernel.h> 5 #include <linux/kernel.h> 6 #include <linux/module.h> 6 #include <linux/module.h> 7 #include <linux/bio.h> 7 #include <linux/bio.h> 8 #include <linux/blkdev.h> 8 #include <linux/blkdev.h> 9 #include <linux/scatterlist.h> 9 #include <linux/scatterlist.h> 10 10 11 #include "blk.h" 11 #include "blk.h" 12 12 13 static sector_t bio_discard_limit(struct block !! 13 static struct bio *next_bio(struct bio *bio, unsigned int nr_pages, >> 14 gfp_t gfp) 14 { 15 { 15 unsigned int discard_granularity = bde !! 16 struct bio *new = bio_alloc(gfp, nr_pages); 16 sector_t granularity_aligned_sector; << 17 17 18 if (bdev_is_partition(bdev)) !! 18 if (bio) { 19 sector += bdev->bd_start_sect; !! 19 bio_chain(bio, new); 20 !! 20 submit_bio(bio); 21 granularity_aligned_sector = !! 21 } 22 round_up(sector, discard_granu << 23 22 24 /* !! 23 return new; 25 * Make sure subsequent bios start ali << 26 * it needs to be split. << 27 */ << 28 if (granularity_aligned_sector != sect << 29 return granularity_aligned_sec << 30 << 31 /* << 32 * Align the bio size to the discard g << 33 * at discard granularity boundaries e << 34 */ << 35 return round_down(UINT_MAX, discard_gr << 36 } 24 } 37 25 38 struct bio *blk_alloc_discard_bio(struct block !! 26 int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, 39 sector_t *sector, sector_t *nr !! 27 sector_t nr_sects, gfp_t gfp_mask, int flags, >> 28 struct bio **biop) 40 { 29 { 41 sector_t bio_sects = min(*nr_sects, bi !! 30 struct request_queue *q = bdev_get_queue(bdev); 42 struct bio *bio; !! 31 struct bio *bio = *biop; >> 32 unsigned int granularity; >> 33 unsigned int op; >> 34 int alignment; >> 35 sector_t bs_mask; 43 36 44 if (!bio_sects) !! 37 if (!q) 45 return NULL; !! 38 return -ENXIO; 46 39 47 bio = bio_alloc(bdev, 0, REQ_OP_DISCAR !! 40 if (flags & BLKDEV_DISCARD_SECURE) { 48 if (!bio) !! 41 if (!blk_queue_secure_erase(q)) 49 return NULL; !! 42 return -EOPNOTSUPP; 50 bio->bi_iter.bi_sector = *sector; !! 43 op = REQ_OP_SECURE_ERASE; 51 bio->bi_iter.bi_size = bio_sects << SE !! 44 } else { 52 *sector += bio_sects; !! 45 if (!blk_queue_discard(q)) 53 *nr_sects -= bio_sects; !! 46 return -EOPNOTSUPP; 54 /* !! 47 op = REQ_OP_DISCARD; 55 * We can loop for a long time in here !! 48 } 56 * discards (like mkfs). Be nice and << 57 * softlocking if preempt is disabled. << 58 */ << 59 cond_resched(); << 60 return bio; << 61 } << 62 49 63 int __blkdev_issue_discard(struct block_device !! 50 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; 64 sector_t nr_sects, gfp_t gfp_m !! 51 if ((sector | nr_sects) & bs_mask) 65 { !! 52 return -EINVAL; 66 struct bio *bio; !! 53 >> 54 /* Zero-sector (unknown) and one-sector granularities are the same. */ >> 55 granularity = max(q->limits.discard_granularity >> 9, 1U); >> 56 alignment = (bdev_discard_alignment(bdev) >> 9) % granularity; >> 57 >> 58 while (nr_sects) { >> 59 unsigned int req_sects; >> 60 sector_t end_sect, tmp; 67 61 68 while ((bio = blk_alloc_discard_bio(bd !! 62 /* Make sure bi_size doesn't overflow */ 69 gfp_mask))) !! 63 req_sects = min_t(sector_t, nr_sects, UINT_MAX >> 9); 70 *biop = bio_chain_and_submit(* !! 64 >> 65 /** >> 66 * If splitting a request, and the next starting sector would be >> 67 * misaligned, stop the discard at the previous aligned sector. >> 68 */ >> 69 end_sect = sector + req_sects; >> 70 tmp = end_sect; >> 71 if (req_sects < nr_sects && >> 72 sector_div(tmp, granularity) != alignment) { >> 73 end_sect = end_sect - alignment; >> 74 sector_div(end_sect, granularity); >> 75 end_sect = end_sect * granularity + alignment; >> 76 req_sects = end_sect - sector; >> 77 } >> 78 >> 79 bio = next_bio(bio, 0, gfp_mask); >> 80 bio->bi_iter.bi_sector = sector; >> 81 bio_set_dev(bio, bdev); >> 82 bio_set_op_attrs(bio, op, 0); >> 83 >> 84 bio->bi_iter.bi_size = req_sects << 9; >> 85 nr_sects -= req_sects; >> 86 sector = end_sect; >> 87 >> 88 /* >> 89 * We can loop for a long time in here, if someone does >> 90 * full device discards (like mkfs). Be nice and allow >> 91 * us to schedule out to avoid softlocking if preempt >> 92 * is disabled. >> 93 */ >> 94 cond_resched(); >> 95 } >> 96 >> 97 *biop = bio; 71 return 0; 98 return 0; 72 } 99 } 73 EXPORT_SYMBOL(__blkdev_issue_discard); 100 EXPORT_SYMBOL(__blkdev_issue_discard); 74 101 75 /** 102 /** 76 * blkdev_issue_discard - queue a discard 103 * blkdev_issue_discard - queue a discard 77 * @bdev: blockdev to issue discard for 104 * @bdev: blockdev to issue discard for 78 * @sector: start sector 105 * @sector: start sector 79 * @nr_sects: number of sectors to discard 106 * @nr_sects: number of sectors to discard 80 * @gfp_mask: memory allocation flags (for b 107 * @gfp_mask: memory allocation flags (for bio_alloc) >> 108 * @flags: BLKDEV_DISCARD_* flags to control behaviour 81 * 109 * 82 * Description: 110 * Description: 83 * Issue a discard request for the sectors 111 * Issue a discard request for the sectors in question. 84 */ 112 */ 85 int blkdev_issue_discard(struct block_device * 113 int blkdev_issue_discard(struct block_device *bdev, sector_t sector, 86 sector_t nr_sects, gfp_t gfp_m !! 114 sector_t nr_sects, gfp_t gfp_mask, unsigned long flags) 87 { 115 { 88 struct bio *bio = NULL; 116 struct bio *bio = NULL; 89 struct blk_plug plug; 117 struct blk_plug plug; 90 int ret; 118 int ret; 91 119 92 blk_start_plug(&plug); 120 blk_start_plug(&plug); 93 ret = __blkdev_issue_discard(bdev, sec !! 121 ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, flags, >> 122 &bio); 94 if (!ret && bio) { 123 if (!ret && bio) { 95 ret = submit_bio_wait(bio); 124 ret = submit_bio_wait(bio); 96 if (ret == -EOPNOTSUPP) 125 if (ret == -EOPNOTSUPP) 97 ret = 0; 126 ret = 0; 98 bio_put(bio); 127 bio_put(bio); 99 } 128 } 100 blk_finish_plug(&plug); 129 blk_finish_plug(&plug); 101 130 102 return ret; 131 return ret; 103 } 132 } 104 EXPORT_SYMBOL(blkdev_issue_discard); 133 EXPORT_SYMBOL(blkdev_issue_discard); 105 134 106 static sector_t bio_write_zeroes_limit(struct !! 135 /** 107 { !! 136 * __blkdev_issue_write_same - generate number of bios with same page 108 sector_t bs_mask = (bdev_logical_block !! 137 * @bdev: target blockdev 109 !! 138 * @sector: start sector 110 return min(bdev_write_zeroes_sectors(b !! 139 * @nr_sects: number of sectors to write 111 (UINT_MAX >> SECTOR_SHIFT) & ~ !! 140 * @gfp_mask: memory allocation flags (for bio_alloc) 112 } !! 141 * @page: page containing data to write 113 !! 142 * @biop: pointer to anchor bio 114 /* !! 143 * 115 * There is no reliable way for the SCSI subsy !! 144 * Description: 116 * device supports a WRITE SAME operation with !! 145 * Generate and issue number of bios(REQ_OP_WRITE_SAME) with same page. 117 * to media. As a result, write_zeroes is enab << 118 * disabled if a zeroing operation subsequentl << 119 * queue limit is likely to change at runtime. << 120 */ 146 */ 121 static void __blkdev_issue_write_zeroes(struct !! 147 static int __blkdev_issue_write_same(struct block_device *bdev, sector_t sector, 122 sector_t sector, sector_t nr_s !! 148 sector_t nr_sects, gfp_t gfp_mask, struct page *page, 123 struct bio **biop, unsigned fl !! 149 struct bio **biop) 124 { !! 150 { >> 151 struct request_queue *q = bdev_get_queue(bdev); >> 152 unsigned int max_write_same_sectors; >> 153 struct bio *bio = *biop; >> 154 sector_t bs_mask; 125 155 126 while (nr_sects) { !! 156 if (!q) 127 unsigned int len = min(nr_sect !! 157 return -ENXIO; 128 struct bio *bio; << 129 158 130 if ((flags & BLKDEV_ZERO_KILLA !! 159 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; 131 fatal_signal_pending(curre !! 160 if ((sector | nr_sects) & bs_mask) 132 break; !! 161 return -EINVAL; 133 162 134 bio = bio_alloc(bdev, 0, REQ_O !! 163 if (!bdev_write_same(bdev)) 135 bio->bi_iter.bi_sector = secto !! 164 return -EOPNOTSUPP; 136 if (flags & BLKDEV_ZERO_NOUNMA << 137 bio->bi_opf |= REQ_NOU << 138 165 139 bio->bi_iter.bi_size = len << !! 166 /* Ensure that max_write_same_sectors doesn't overflow bi_size */ 140 *biop = bio_chain_and_submit(* !! 167 max_write_same_sectors = UINT_MAX >> 9; 141 168 142 nr_sects -= len; !! 169 while (nr_sects) { 143 sector += len; !! 170 bio = next_bio(bio, 1, gfp_mask); >> 171 bio->bi_iter.bi_sector = sector; >> 172 bio_set_dev(bio, bdev); >> 173 bio->bi_vcnt = 1; >> 174 bio->bi_io_vec->bv_page = page; >> 175 bio->bi_io_vec->bv_offset = 0; >> 176 bio->bi_io_vec->bv_len = bdev_logical_block_size(bdev); >> 177 bio_set_op_attrs(bio, REQ_OP_WRITE_SAME, 0); >> 178 >> 179 if (nr_sects > max_write_same_sectors) { >> 180 bio->bi_iter.bi_size = max_write_same_sectors << 9; >> 181 nr_sects -= max_write_same_sectors; >> 182 sector += max_write_same_sectors; >> 183 } else { >> 184 bio->bi_iter.bi_size = nr_sects << 9; >> 185 nr_sects = 0; >> 186 } 144 cond_resched(); 187 cond_resched(); 145 } 188 } >> 189 >> 190 *biop = bio; >> 191 return 0; 146 } 192 } 147 193 148 static int blkdev_issue_write_zeroes(struct bl !! 194 /** 149 sector_t nr_sects, gfp_t gfp, !! 195 * blkdev_issue_write_same - queue a write same operation >> 196 * @bdev: target blockdev >> 197 * @sector: start sector >> 198 * @nr_sects: number of sectors to write >> 199 * @gfp_mask: memory allocation flags (for bio_alloc) >> 200 * @page: page containing data >> 201 * >> 202 * Description: >> 203 * Issue a write same request for the sectors in question. >> 204 */ >> 205 int blkdev_issue_write_same(struct block_device *bdev, sector_t sector, >> 206 sector_t nr_sects, gfp_t gfp_mask, >> 207 struct page *page) 150 { 208 { 151 sector_t limit = bio_write_zeroes_limi << 152 struct bio *bio = NULL; 209 struct bio *bio = NULL; 153 struct blk_plug plug; 210 struct blk_plug plug; 154 int ret = 0; !! 211 int ret; 155 212 156 blk_start_plug(&plug); 213 blk_start_plug(&plug); 157 __blkdev_issue_write_zeroes(bdev, sect !! 214 ret = __blkdev_issue_write_same(bdev, sector, nr_sects, gfp_mask, page, 158 flags, limit); !! 215 &bio); 159 if (bio) { !! 216 if (ret == 0 && bio) { 160 if ((flags & BLKDEV_ZERO_KILLA << 161 fatal_signal_pending(curre << 162 bio_await_chain(bio); << 163 blk_finish_plug(&plug) << 164 return -EINTR; << 165 } << 166 ret = submit_bio_wait(bio); 217 ret = submit_bio_wait(bio); 167 bio_put(bio); 218 bio_put(bio); 168 } 219 } 169 blk_finish_plug(&plug); 220 blk_finish_plug(&plug); >> 221 return ret; >> 222 } >> 223 EXPORT_SYMBOL(blkdev_issue_write_same); >> 224 >> 225 static int __blkdev_issue_write_zeroes(struct block_device *bdev, >> 226 sector_t sector, sector_t nr_sects, gfp_t gfp_mask, >> 227 struct bio **biop, unsigned flags) >> 228 { >> 229 struct bio *bio = *biop; >> 230 unsigned int max_write_zeroes_sectors; >> 231 struct request_queue *q = bdev_get_queue(bdev); >> 232 >> 233 if (!q) >> 234 return -ENXIO; >> 235 >> 236 /* Ensure that max_write_zeroes_sectors doesn't overflow bi_size */ >> 237 max_write_zeroes_sectors = bdev_write_zeroes_sectors(bdev); 170 238 171 /* !! 239 if (max_write_zeroes_sectors == 0) 172 * For some devices there is no non-de << 173 * WRITE ZEROES is actually supported. << 174 * on an I/O error, in which case we'l << 175 * "not supported" here. << 176 */ << 177 if (ret && !bdev_write_zeroes_sectors( << 178 return -EOPNOTSUPP; 240 return -EOPNOTSUPP; 179 return ret; !! 241 >> 242 while (nr_sects) { >> 243 bio = next_bio(bio, 0, gfp_mask); >> 244 bio->bi_iter.bi_sector = sector; >> 245 bio_set_dev(bio, bdev); >> 246 bio->bi_opf = REQ_OP_WRITE_ZEROES; >> 247 if (flags & BLKDEV_ZERO_NOUNMAP) >> 248 bio->bi_opf |= REQ_NOUNMAP; >> 249 >> 250 if (nr_sects > max_write_zeroes_sectors) { >> 251 bio->bi_iter.bi_size = max_write_zeroes_sectors << 9; >> 252 nr_sects -= max_write_zeroes_sectors; >> 253 sector += max_write_zeroes_sectors; >> 254 } else { >> 255 bio->bi_iter.bi_size = nr_sects << 9; >> 256 nr_sects = 0; >> 257 } >> 258 cond_resched(); >> 259 } >> 260 >> 261 *biop = bio; >> 262 return 0; 180 } 263 } 181 264 182 /* 265 /* 183 * Convert a number of 512B sectors to a numbe 266 * Convert a number of 512B sectors to a number of pages. 184 * The result is limited to a number of pages 267 * The result is limited to a number of pages that can fit into a BIO. 185 * Also make sure that the result is always at 268 * Also make sure that the result is always at least 1 (page) for the cases 186 * where nr_sects is lower than the number of 269 * where nr_sects is lower than the number of sectors in a page. 187 */ 270 */ 188 static unsigned int __blkdev_sectors_to_bio_pa 271 static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects) 189 { 272 { 190 sector_t pages = DIV_ROUND_UP_SECTOR_T 273 sector_t pages = DIV_ROUND_UP_SECTOR_T(nr_sects, PAGE_SIZE / 512); 191 274 192 return min(pages, (sector_t)BIO_MAX_VE !! 275 return min(pages, (sector_t)BIO_MAX_PAGES); 193 } 276 } 194 277 195 static void __blkdev_issue_zero_pages(struct b !! 278 static int __blkdev_issue_zero_pages(struct block_device *bdev, 196 sector_t sector, sector_t nr_s 279 sector_t sector, sector_t nr_sects, gfp_t gfp_mask, 197 struct bio **biop, unsigned in !! 280 struct bio **biop) 198 { 281 { 199 while (nr_sects) { !! 282 struct request_queue *q = bdev_get_queue(bdev); 200 unsigned int nr_vecs = __blkde !! 283 struct bio *bio = *biop; 201 struct bio *bio; !! 284 int bi_size = 0; 202 !! 285 unsigned int sz; 203 bio = bio_alloc(bdev, nr_vecs, !! 286 >> 287 if (!q) >> 288 return -ENXIO; >> 289 >> 290 while (nr_sects != 0) { >> 291 bio = next_bio(bio, __blkdev_sectors_to_bio_pages(nr_sects), >> 292 gfp_mask); 204 bio->bi_iter.bi_sector = secto 293 bio->bi_iter.bi_sector = sector; >> 294 bio_set_dev(bio, bdev); >> 295 bio_set_op_attrs(bio, REQ_OP_WRITE, 0); 205 296 206 if ((flags & BLKDEV_ZERO_KILLA !! 297 while (nr_sects != 0) { 207 fatal_signal_pending(curre !! 298 sz = min((sector_t) PAGE_SIZE, nr_sects << 9); 208 break; !! 299 bi_size = bio_add_page(bio, ZERO_PAGE(0), sz, 0); 209 !! 300 nr_sects -= bi_size >> 9; 210 do { !! 301 sector += bi_size >> 9; 211 unsigned int len, adde !! 302 if (bi_size < sz) 212 << 213 len = min_t(sector_t, << 214 PAGE_SIZE, nr_ << 215 added = bio_add_page(b << 216 if (added < len) << 217 break; 303 break; 218 nr_sects -= added >> S << 219 sector += added >> SEC << 220 } while (nr_sects); << 221 << 222 *biop = bio_chain_and_submit(* << 223 cond_resched(); << 224 } << 225 } << 226 << 227 static int blkdev_issue_zero_pages(struct bloc << 228 sector_t nr_sects, gfp_t gfp, << 229 { << 230 struct bio *bio = NULL; << 231 struct blk_plug plug; << 232 int ret = 0; << 233 << 234 if (flags & BLKDEV_ZERO_NOFALLBACK) << 235 return -EOPNOTSUPP; << 236 << 237 blk_start_plug(&plug); << 238 __blkdev_issue_zero_pages(bdev, sector << 239 if (bio) { << 240 if ((flags & BLKDEV_ZERO_KILLA << 241 fatal_signal_pending(curre << 242 bio_await_chain(bio); << 243 blk_finish_plug(&plug) << 244 return -EINTR; << 245 } 304 } 246 ret = submit_bio_wait(bio); !! 305 cond_resched(); 247 bio_put(bio); << 248 } 306 } 249 blk_finish_plug(&plug); << 250 307 251 return ret; !! 308 *biop = bio; >> 309 return 0; 252 } 310 } 253 311 254 /** 312 /** 255 * __blkdev_issue_zeroout - generate number of 313 * __blkdev_issue_zeroout - generate number of zero filed write bios 256 * @bdev: blockdev to issue 314 * @bdev: blockdev to issue 257 * @sector: start sector 315 * @sector: start sector 258 * @nr_sects: number of sectors to write 316 * @nr_sects: number of sectors to write 259 * @gfp_mask: memory allocation flags (for b 317 * @gfp_mask: memory allocation flags (for bio_alloc) 260 * @biop: pointer to anchor bio 318 * @biop: pointer to anchor bio 261 * @flags: controls detailed behavior 319 * @flags: controls detailed behavior 262 * 320 * 263 * Description: 321 * Description: 264 * Zero-fill a block range, either using hard 322 * Zero-fill a block range, either using hardware offload or by explicitly 265 * writing zeroes to the device. 323 * writing zeroes to the device. 266 * 324 * 267 * If a device is using logical block provisi 325 * If a device is using logical block provisioning, the underlying space will 268 * not be released if %flags contains BLKDEV_ 326 * not be released if %flags contains BLKDEV_ZERO_NOUNMAP. 269 * 327 * 270 * If %flags contains BLKDEV_ZERO_NOFALLBACK, 328 * If %flags contains BLKDEV_ZERO_NOFALLBACK, the function will return 271 * -EOPNOTSUPP if no explicit hardware offloa 329 * -EOPNOTSUPP if no explicit hardware offload for zeroing is provided. 272 */ 330 */ 273 int __blkdev_issue_zeroout(struct block_device 331 int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, 274 sector_t nr_sects, gfp_t gfp_m 332 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, 275 unsigned flags) 333 unsigned flags) 276 { 334 { 277 sector_t limit = bio_write_zeroes_limi !! 335 int ret; >> 336 sector_t bs_mask; 278 337 279 if (bdev_read_only(bdev)) !! 338 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; 280 return -EPERM; !! 339 if ((sector | nr_sects) & bs_mask) >> 340 return -EINVAL; 281 341 282 if (limit) { !! 342 ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask, 283 __blkdev_issue_write_zeroes(bd !! 343 biop, flags); 284 gfp_mask, biop !! 344 if (ret != -EOPNOTSUPP || (flags & BLKDEV_ZERO_NOFALLBACK)) 285 } else { !! 345 return ret; 286 if (flags & BLKDEV_ZERO_NOFALL !! 346 287 return -EOPNOTSUPP; !! 347 return __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask, 288 __blkdev_issue_zero_pages(bdev !! 348 biop); 289 biop, flags); << 290 } << 291 return 0; << 292 } 349 } 293 EXPORT_SYMBOL(__blkdev_issue_zeroout); 350 EXPORT_SYMBOL(__blkdev_issue_zeroout); 294 351 295 /** 352 /** 296 * blkdev_issue_zeroout - zero-fill a block ra 353 * blkdev_issue_zeroout - zero-fill a block range 297 * @bdev: blockdev to write 354 * @bdev: blockdev to write 298 * @sector: start sector 355 * @sector: start sector 299 * @nr_sects: number of sectors to write 356 * @nr_sects: number of sectors to write 300 * @gfp_mask: memory allocation flags (for b 357 * @gfp_mask: memory allocation flags (for bio_alloc) 301 * @flags: controls detailed behavior 358 * @flags: controls detailed behavior 302 * 359 * 303 * Description: 360 * Description: 304 * Zero-fill a block range, either using hard 361 * Zero-fill a block range, either using hardware offload or by explicitly 305 * writing zeroes to the device. See __blkde 362 * writing zeroes to the device. See __blkdev_issue_zeroout() for the 306 * valid values for %flags. 363 * valid values for %flags. 307 */ 364 */ 308 int blkdev_issue_zeroout(struct block_device * 365 int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, 309 sector_t nr_sects, gfp_t gfp_m 366 sector_t nr_sects, gfp_t gfp_mask, unsigned flags) 310 { 367 { 311 int ret; << 312 << 313 if ((sector | nr_sects) & ((bdev_logic << 314 return -EINVAL; << 315 if (bdev_read_only(bdev)) << 316 return -EPERM; << 317 << 318 if (bdev_write_zeroes_sectors(bdev)) { << 319 ret = blkdev_issue_write_zeroe << 320 gfp_mask, flag << 321 if (ret != -EOPNOTSUPP) << 322 return ret; << 323 } << 324 << 325 return blkdev_issue_zero_pages(bdev, s << 326 } << 327 EXPORT_SYMBOL(blkdev_issue_zeroout); << 328 << 329 int blkdev_issue_secure_erase(struct block_dev << 330 sector_t nr_sects, gfp_t gfp) << 331 { << 332 sector_t bs_mask = (bdev_logical_block << 333 unsigned int max_sectors = bdev_max_se << 334 struct bio *bio = NULL; << 335 struct blk_plug plug; << 336 int ret = 0; 368 int ret = 0; >> 369 sector_t bs_mask; >> 370 struct bio *bio; >> 371 struct blk_plug plug; >> 372 bool try_write_zeroes = !!bdev_write_zeroes_sectors(bdev); 337 373 338 /* make sure that "len << SECTOR_SHIFT !! 374 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; 339 if (max_sectors > UINT_MAX >> SECTOR_S << 340 max_sectors = UINT_MAX >> SECT << 341 max_sectors &= ~bs_mask; << 342 << 343 if (max_sectors == 0) << 344 return -EOPNOTSUPP; << 345 if ((sector | nr_sects) & bs_mask) 375 if ((sector | nr_sects) & bs_mask) 346 return -EINVAL; 376 return -EINVAL; 347 if (bdev_read_only(bdev)) << 348 return -EPERM; << 349 377 >> 378 retry: >> 379 bio = NULL; 350 blk_start_plug(&plug); 380 blk_start_plug(&plug); 351 while (nr_sects) { !! 381 if (try_write_zeroes) { 352 unsigned int len = min_t(secto !! 382 ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, 353 !! 383 gfp_mask, &bio, flags); 354 bio = blk_next_bio(bio, bdev, !! 384 } else if (!(flags & BLKDEV_ZERO_NOFALLBACK)) { 355 bio->bi_iter.bi_sector = secto !! 385 ret = __blkdev_issue_zero_pages(bdev, sector, nr_sects, 356 bio->bi_iter.bi_size = len << !! 386 gfp_mask, &bio); 357 !! 387 } else { 358 sector += len; !! 388 /* No zeroing offload support */ 359 nr_sects -= len; !! 389 ret = -EOPNOTSUPP; 360 cond_resched(); << 361 } 390 } 362 if (bio) { !! 391 if (ret == 0 && bio) { 363 ret = submit_bio_wait(bio); 392 ret = submit_bio_wait(bio); 364 bio_put(bio); 393 bio_put(bio); 365 } 394 } 366 blk_finish_plug(&plug); 395 blk_finish_plug(&plug); >> 396 if (ret && try_write_zeroes) { >> 397 if (!(flags & BLKDEV_ZERO_NOFALLBACK)) { >> 398 try_write_zeroes = false; >> 399 goto retry; >> 400 } >> 401 if (!bdev_write_zeroes_sectors(bdev)) { >> 402 /* >> 403 * Zeroing offload support was indicated, but the >> 404 * device reported ILLEGAL REQUEST (for some devices >> 405 * there is no non-destructive way to verify whether >> 406 * WRITE ZEROES is actually supported). >> 407 */ >> 408 ret = -EOPNOTSUPP; >> 409 } >> 410 } 367 411 368 return ret; 412 return ret; 369 } 413 } 370 EXPORT_SYMBOL(blkdev_issue_secure_erase); !! 414 EXPORT_SYMBOL(blkdev_issue_zeroout); 371 415
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.