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 sector_t bio_discard_limit(struct block_device *bdev, sector_t sector) 14 { 14 { 15 unsigned int discard_granularity = bde 15 unsigned int discard_granularity = bdev_discard_granularity(bdev); 16 sector_t granularity_aligned_sector; 16 sector_t granularity_aligned_sector; 17 17 18 if (bdev_is_partition(bdev)) 18 if (bdev_is_partition(bdev)) 19 sector += bdev->bd_start_sect; 19 sector += bdev->bd_start_sect; 20 20 21 granularity_aligned_sector = 21 granularity_aligned_sector = 22 round_up(sector, discard_granu 22 round_up(sector, discard_granularity >> SECTOR_SHIFT); 23 23 24 /* 24 /* 25 * Make sure subsequent bios start ali 25 * Make sure subsequent bios start aligned to the discard granularity if 26 * it needs to be split. 26 * it needs to be split. 27 */ 27 */ 28 if (granularity_aligned_sector != sect 28 if (granularity_aligned_sector != sector) 29 return granularity_aligned_sec 29 return granularity_aligned_sector - sector; 30 30 31 /* 31 /* 32 * Align the bio size to the discard g 32 * Align the bio size to the discard granularity to make splitting the bio 33 * at discard granularity boundaries e 33 * at discard granularity boundaries easier in the driver if needed. 34 */ 34 */ 35 return round_down(UINT_MAX, discard_gr 35 return round_down(UINT_MAX, discard_granularity) >> SECTOR_SHIFT; 36 } 36 } 37 37 38 struct bio *blk_alloc_discard_bio(struct block !! 38 int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, 39 sector_t *sector, sector_t *nr !! 39 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop) 40 { 40 { 41 sector_t bio_sects = min(*nr_sects, bi !! 41 struct bio *bio = *biop; 42 struct bio *bio; !! 42 sector_t bs_mask; 43 43 44 if (!bio_sects) !! 44 if (bdev_read_only(bdev)) 45 return NULL; !! 45 return -EPERM; >> 46 if (!bdev_max_discard_sectors(bdev)) >> 47 return -EOPNOTSUPP; 46 48 47 bio = bio_alloc(bdev, 0, REQ_OP_DISCAR !! 49 /* In case the discard granularity isn't set by buggy device driver */ 48 if (!bio) !! 50 if (WARN_ON_ONCE(!bdev_discard_granularity(bdev))) { 49 return NULL; !! 51 pr_err_ratelimited("%pg: Error: discard_granularity is 0.\n", 50 bio->bi_iter.bi_sector = *sector; !! 52 bdev); 51 bio->bi_iter.bi_size = bio_sects << SE !! 53 return -EOPNOTSUPP; 52 *sector += bio_sects; !! 54 } 53 *nr_sects -= bio_sects; << 54 /* << 55 * We can loop for a long time in here << 56 * discards (like mkfs). Be nice and << 57 * softlocking if preempt is disabled. << 58 */ << 59 cond_resched(); << 60 return bio; << 61 } << 62 55 63 int __blkdev_issue_discard(struct block_device !! 56 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; 64 sector_t nr_sects, gfp_t gfp_m !! 57 if ((sector | nr_sects) & bs_mask) 65 { !! 58 return -EINVAL; 66 struct bio *bio; !! 59 >> 60 if (!nr_sects) >> 61 return -EINVAL; >> 62 >> 63 while (nr_sects) { >> 64 sector_t req_sects = >> 65 min(nr_sects, bio_discard_limit(bdev, sector)); >> 66 >> 67 bio = blk_next_bio(bio, bdev, 0, REQ_OP_DISCARD, gfp_mask); >> 68 bio->bi_iter.bi_sector = sector; >> 69 bio->bi_iter.bi_size = req_sects << 9; >> 70 sector += req_sects; >> 71 nr_sects -= req_sects; >> 72 >> 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(); >> 80 } 67 81 68 while ((bio = blk_alloc_discard_bio(bd !! 82 *biop = bio; 69 gfp_mask))) << 70 *biop = bio_chain_and_submit(* << 71 return 0; 83 return 0; 72 } 84 } 73 EXPORT_SYMBOL(__blkdev_issue_discard); 85 EXPORT_SYMBOL(__blkdev_issue_discard); 74 86 75 /** 87 /** 76 * blkdev_issue_discard - queue a discard 88 * blkdev_issue_discard - queue a discard 77 * @bdev: blockdev to issue discard for 89 * @bdev: blockdev to issue discard for 78 * @sector: start sector 90 * @sector: start sector 79 * @nr_sects: number of sectors to discard 91 * @nr_sects: number of sectors to discard 80 * @gfp_mask: memory allocation flags (for b 92 * @gfp_mask: memory allocation flags (for bio_alloc) 81 * 93 * 82 * Description: 94 * Description: 83 * Issue a discard request for the sectors 95 * Issue a discard request for the sectors in question. 84 */ 96 */ 85 int blkdev_issue_discard(struct block_device * 97 int blkdev_issue_discard(struct block_device *bdev, sector_t sector, 86 sector_t nr_sects, gfp_t gfp_m 98 sector_t nr_sects, gfp_t gfp_mask) 87 { 99 { 88 struct bio *bio = NULL; 100 struct bio *bio = NULL; 89 struct blk_plug plug; 101 struct blk_plug plug; 90 int ret; 102 int ret; 91 103 92 blk_start_plug(&plug); 104 blk_start_plug(&plug); 93 ret = __blkdev_issue_discard(bdev, sec 105 ret = __blkdev_issue_discard(bdev, sector, nr_sects, gfp_mask, &bio); 94 if (!ret && bio) { 106 if (!ret && bio) { 95 ret = submit_bio_wait(bio); 107 ret = submit_bio_wait(bio); 96 if (ret == -EOPNOTSUPP) 108 if (ret == -EOPNOTSUPP) 97 ret = 0; 109 ret = 0; 98 bio_put(bio); 110 bio_put(bio); 99 } 111 } 100 blk_finish_plug(&plug); 112 blk_finish_plug(&plug); 101 113 102 return ret; 114 return ret; 103 } 115 } 104 EXPORT_SYMBOL(blkdev_issue_discard); 116 EXPORT_SYMBOL(blkdev_issue_discard); 105 117 106 static sector_t bio_write_zeroes_limit(struct !! 118 static int __blkdev_issue_write_zeroes(struct block_device *bdev, >> 119 sector_t sector, sector_t nr_sects, gfp_t gfp_mask, >> 120 struct bio **biop, unsigned flags) 107 { 121 { 108 sector_t bs_mask = (bdev_logical_block !! 122 struct bio *bio = *biop; >> 123 unsigned int max_sectors; 109 124 110 return min(bdev_write_zeroes_sectors(b !! 125 if (bdev_read_only(bdev)) 111 (UINT_MAX >> SECTOR_SHIFT) & ~ !! 126 return -EPERM; 112 } << 113 127 114 /* !! 128 /* Ensure that max_sectors doesn't overflow bi_size */ 115 * There is no reliable way for the SCSI subsy !! 129 max_sectors = bdev_write_zeroes_sectors(bdev); 116 * device supports a WRITE SAME operation with << 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 */ << 121 static void __blkdev_issue_write_zeroes(struct << 122 sector_t sector, sector_t nr_s << 123 struct bio **biop, unsigned fl << 124 { << 125 130 126 while (nr_sects) { !! 131 if (max_sectors == 0) 127 unsigned int len = min(nr_sect !! 132 return -EOPNOTSUPP; 128 struct bio *bio; << 129 133 130 if ((flags & BLKDEV_ZERO_KILLA !! 134 while (nr_sects) { 131 fatal_signal_pending(curre !! 135 unsigned int len = min_t(sector_t, nr_sects, max_sectors); 132 break; << 133 136 134 bio = bio_alloc(bdev, 0, REQ_O !! 137 bio = blk_next_bio(bio, bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask); 135 bio->bi_iter.bi_sector = secto 138 bio->bi_iter.bi_sector = sector; 136 if (flags & BLKDEV_ZERO_NOUNMA 139 if (flags & BLKDEV_ZERO_NOUNMAP) 137 bio->bi_opf |= REQ_NOU 140 bio->bi_opf |= REQ_NOUNMAP; 138 141 139 bio->bi_iter.bi_size = len << 142 bio->bi_iter.bi_size = len << SECTOR_SHIFT; 140 *biop = bio_chain_and_submit(* << 141 << 142 nr_sects -= len; 143 nr_sects -= len; 143 sector += len; 144 sector += len; 144 cond_resched(); 145 cond_resched(); 145 } 146 } 146 } << 147 << 148 static int blkdev_issue_write_zeroes(struct bl << 149 sector_t nr_sects, gfp_t gfp, << 150 { << 151 sector_t limit = bio_write_zeroes_limi << 152 struct bio *bio = NULL; << 153 struct blk_plug plug; << 154 int ret = 0; << 155 << 156 blk_start_plug(&plug); << 157 __blkdev_issue_write_zeroes(bdev, sect << 158 flags, limit); << 159 if (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); << 167 bio_put(bio); << 168 } << 169 blk_finish_plug(&plug); << 170 147 171 /* !! 148 *biop = bio; 172 * For some devices there is no non-de !! 149 return 0; 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; << 179 return ret; << 180 } 150 } 181 151 182 /* 152 /* 183 * Convert a number of 512B sectors to a numbe 153 * Convert a number of 512B sectors to a number of pages. 184 * The result is limited to a number of pages 154 * 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 155 * 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 156 * where nr_sects is lower than the number of sectors in a page. 187 */ 157 */ 188 static unsigned int __blkdev_sectors_to_bio_pa 158 static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects) 189 { 159 { 190 sector_t pages = DIV_ROUND_UP_SECTOR_T 160 sector_t pages = DIV_ROUND_UP_SECTOR_T(nr_sects, PAGE_SIZE / 512); 191 161 192 return min(pages, (sector_t)BIO_MAX_VE 162 return min(pages, (sector_t)BIO_MAX_VECS); 193 } 163 } 194 164 195 static void __blkdev_issue_zero_pages(struct b !! 165 static int __blkdev_issue_zero_pages(struct block_device *bdev, 196 sector_t sector, sector_t nr_s 166 sector_t sector, sector_t nr_sects, gfp_t gfp_mask, 197 struct bio **biop, unsigned in !! 167 struct bio **biop) 198 { 168 { 199 while (nr_sects) { !! 169 struct bio *bio = *biop; 200 unsigned int nr_vecs = __blkde !! 170 int bi_size = 0; 201 struct bio *bio; !! 171 unsigned int sz; >> 172 >> 173 if (bdev_read_only(bdev)) >> 174 return -EPERM; 202 175 203 bio = bio_alloc(bdev, nr_vecs, !! 176 while (nr_sects != 0) { >> 177 bio = blk_next_bio(bio, bdev, __blkdev_sectors_to_bio_pages(nr_sects), >> 178 REQ_OP_WRITE, gfp_mask); 204 bio->bi_iter.bi_sector = secto 179 bio->bi_iter.bi_sector = sector; 205 180 206 if ((flags & BLKDEV_ZERO_KILLA !! 181 while (nr_sects != 0) { 207 fatal_signal_pending(curre !! 182 sz = min((sector_t) PAGE_SIZE, nr_sects << 9); 208 break; !! 183 bi_size = bio_add_page(bio, ZERO_PAGE(0), sz, 0); 209 !! 184 nr_sects -= bi_size >> 9; 210 do { !! 185 sector += bi_size >> 9; 211 unsigned int len, adde !! 186 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; 187 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 } 188 } 246 ret = submit_bio_wait(bio); !! 189 cond_resched(); 247 bio_put(bio); << 248 } 190 } 249 blk_finish_plug(&plug); << 250 191 251 return ret; !! 192 *biop = bio; >> 193 return 0; 252 } 194 } 253 195 254 /** 196 /** 255 * __blkdev_issue_zeroout - generate number of 197 * __blkdev_issue_zeroout - generate number of zero filed write bios 256 * @bdev: blockdev to issue 198 * @bdev: blockdev to issue 257 * @sector: start sector 199 * @sector: start sector 258 * @nr_sects: number of sectors to write 200 * @nr_sects: number of sectors to write 259 * @gfp_mask: memory allocation flags (for b 201 * @gfp_mask: memory allocation flags (for bio_alloc) 260 * @biop: pointer to anchor bio 202 * @biop: pointer to anchor bio 261 * @flags: controls detailed behavior 203 * @flags: controls detailed behavior 262 * 204 * 263 * Description: 205 * Description: 264 * Zero-fill a block range, either using hard 206 * Zero-fill a block range, either using hardware offload or by explicitly 265 * writing zeroes to the device. 207 * writing zeroes to the device. 266 * 208 * 267 * If a device is using logical block provisi 209 * If a device is using logical block provisioning, the underlying space will 268 * not be released if %flags contains BLKDEV_ 210 * not be released if %flags contains BLKDEV_ZERO_NOUNMAP. 269 * 211 * 270 * If %flags contains BLKDEV_ZERO_NOFALLBACK, 212 * If %flags contains BLKDEV_ZERO_NOFALLBACK, the function will return 271 * -EOPNOTSUPP if no explicit hardware offloa 213 * -EOPNOTSUPP if no explicit hardware offload for zeroing is provided. 272 */ 214 */ 273 int __blkdev_issue_zeroout(struct block_device 215 int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, 274 sector_t nr_sects, gfp_t gfp_m 216 sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, 275 unsigned flags) 217 unsigned flags) 276 { 218 { 277 sector_t limit = bio_write_zeroes_limi !! 219 int ret; >> 220 sector_t bs_mask; 278 221 279 if (bdev_read_only(bdev)) !! 222 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; 280 return -EPERM; !! 223 if ((sector | nr_sects) & bs_mask) >> 224 return -EINVAL; 281 225 282 if (limit) { !! 226 ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask, 283 __blkdev_issue_write_zeroes(bd !! 227 biop, flags); 284 gfp_mask, biop !! 228 if (ret != -EOPNOTSUPP || (flags & BLKDEV_ZERO_NOFALLBACK)) 285 } else { !! 229 return ret; 286 if (flags & BLKDEV_ZERO_NOFALL !! 230 287 return -EOPNOTSUPP; !! 231 return __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask, 288 __blkdev_issue_zero_pages(bdev !! 232 biop); 289 biop, flags); << 290 } << 291 return 0; << 292 } 233 } 293 EXPORT_SYMBOL(__blkdev_issue_zeroout); 234 EXPORT_SYMBOL(__blkdev_issue_zeroout); 294 235 295 /** 236 /** 296 * blkdev_issue_zeroout - zero-fill a block ra 237 * blkdev_issue_zeroout - zero-fill a block range 297 * @bdev: blockdev to write 238 * @bdev: blockdev to write 298 * @sector: start sector 239 * @sector: start sector 299 * @nr_sects: number of sectors to write 240 * @nr_sects: number of sectors to write 300 * @gfp_mask: memory allocation flags (for b 241 * @gfp_mask: memory allocation flags (for bio_alloc) 301 * @flags: controls detailed behavior 242 * @flags: controls detailed behavior 302 * 243 * 303 * Description: 244 * Description: 304 * Zero-fill a block range, either using hard 245 * Zero-fill a block range, either using hardware offload or by explicitly 305 * writing zeroes to the device. See __blkde 246 * writing zeroes to the device. See __blkdev_issue_zeroout() for the 306 * valid values for %flags. 247 * valid values for %flags. 307 */ 248 */ 308 int blkdev_issue_zeroout(struct block_device * 249 int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, 309 sector_t nr_sects, gfp_t gfp_m 250 sector_t nr_sects, gfp_t gfp_mask, unsigned flags) 310 { 251 { 311 int ret; !! 252 int ret = 0; >> 253 sector_t bs_mask; >> 254 struct bio *bio; >> 255 struct blk_plug plug; >> 256 bool try_write_zeroes = !!bdev_write_zeroes_sectors(bdev); 312 257 313 if ((sector | nr_sects) & ((bdev_logic !! 258 bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; >> 259 if ((sector | nr_sects) & bs_mask) 314 return -EINVAL; 260 return -EINVAL; 315 if (bdev_read_only(bdev)) << 316 return -EPERM; << 317 261 318 if (bdev_write_zeroes_sectors(bdev)) { !! 262 retry: 319 ret = blkdev_issue_write_zeroe !! 263 bio = NULL; 320 gfp_mask, flag !! 264 blk_start_plug(&plug); 321 if (ret != -EOPNOTSUPP) !! 265 if (try_write_zeroes) { 322 return ret; !! 266 ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, >> 267 gfp_mask, &bio, flags); >> 268 } else if (!(flags & BLKDEV_ZERO_NOFALLBACK)) { >> 269 ret = __blkdev_issue_zero_pages(bdev, sector, nr_sects, >> 270 gfp_mask, &bio); >> 271 } else { >> 272 /* No zeroing offload support */ >> 273 ret = -EOPNOTSUPP; >> 274 } >> 275 if (ret == 0 && bio) { >> 276 ret = submit_bio_wait(bio); >> 277 bio_put(bio); >> 278 } >> 279 blk_finish_plug(&plug); >> 280 if (ret && try_write_zeroes) { >> 281 if (!(flags & BLKDEV_ZERO_NOFALLBACK)) { >> 282 try_write_zeroes = false; >> 283 goto retry; >> 284 } >> 285 if (!bdev_write_zeroes_sectors(bdev)) { >> 286 /* >> 287 * Zeroing offload support was indicated, but the >> 288 * device reported ILLEGAL REQUEST (for some devices >> 289 * there is no non-destructive way to verify whether >> 290 * WRITE ZEROES is actually supported). >> 291 */ >> 292 ret = -EOPNOTSUPP; >> 293 } 323 } 294 } 324 295 325 return blkdev_issue_zero_pages(bdev, s !! 296 return ret; 326 } 297 } 327 EXPORT_SYMBOL(blkdev_issue_zeroout); 298 EXPORT_SYMBOL(blkdev_issue_zeroout); 328 299 329 int blkdev_issue_secure_erase(struct block_dev 300 int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector, 330 sector_t nr_sects, gfp_t gfp) 301 sector_t nr_sects, gfp_t gfp) 331 { 302 { 332 sector_t bs_mask = (bdev_logical_block 303 sector_t bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; 333 unsigned int max_sectors = bdev_max_se 304 unsigned int max_sectors = bdev_max_secure_erase_sectors(bdev); 334 struct bio *bio = NULL; 305 struct bio *bio = NULL; 335 struct blk_plug plug; 306 struct blk_plug plug; 336 int ret = 0; 307 int ret = 0; 337 308 338 /* make sure that "len << SECTOR_SHIFT 309 /* make sure that "len << SECTOR_SHIFT" doesn't overflow */ 339 if (max_sectors > UINT_MAX >> SECTOR_S 310 if (max_sectors > UINT_MAX >> SECTOR_SHIFT) 340 max_sectors = UINT_MAX >> SECT 311 max_sectors = UINT_MAX >> SECTOR_SHIFT; 341 max_sectors &= ~bs_mask; 312 max_sectors &= ~bs_mask; 342 313 343 if (max_sectors == 0) 314 if (max_sectors == 0) 344 return -EOPNOTSUPP; 315 return -EOPNOTSUPP; 345 if ((sector | nr_sects) & bs_mask) 316 if ((sector | nr_sects) & bs_mask) 346 return -EINVAL; 317 return -EINVAL; 347 if (bdev_read_only(bdev)) 318 if (bdev_read_only(bdev)) 348 return -EPERM; 319 return -EPERM; 349 320 350 blk_start_plug(&plug); 321 blk_start_plug(&plug); 351 while (nr_sects) { 322 while (nr_sects) { 352 unsigned int len = min_t(secto 323 unsigned int len = min_t(sector_t, nr_sects, max_sectors); 353 324 354 bio = blk_next_bio(bio, bdev, 325 bio = blk_next_bio(bio, bdev, 0, REQ_OP_SECURE_ERASE, gfp); 355 bio->bi_iter.bi_sector = secto 326 bio->bi_iter.bi_sector = sector; 356 bio->bi_iter.bi_size = len << 327 bio->bi_iter.bi_size = len << SECTOR_SHIFT; 357 328 358 sector += len; 329 sector += len; 359 nr_sects -= len; 330 nr_sects -= len; 360 cond_resched(); 331 cond_resched(); 361 } 332 } 362 if (bio) { 333 if (bio) { 363 ret = submit_bio_wait(bio); 334 ret = submit_bio_wait(bio); 364 bio_put(bio); 335 bio_put(bio); 365 } 336 } 366 blk_finish_plug(&plug); 337 blk_finish_plug(&plug); 367 338 368 return ret; 339 return ret; 369 } 340 } 370 EXPORT_SYMBOL(blkdev_issue_secure_erase); 341 EXPORT_SYMBOL(blkdev_issue_secure_erase); 371 342
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.