Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bio-integrity: fold bio_integrity_enabled to bio_integrity_prep

Currently all integrity prep hooks are open-coded, and if prepare fails
we ignore it's code and fail bio with EIO. Let's return real error to
upper layer, so later caller may react accordingly.

In fact no one want to use bio_integrity_prep() w/o bio_integrity_enabled,
so it is reasonable to fold it in to one function.

Signed-off-by: Dmitry Monakhov <dmonakhov@openvz.org>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
[hch: merged with the latest block tree,
return bool from bio_integrity_prep]
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Dmitry Monakhov and committed by
Jens Axboe
e23947bd fbd08e76

+50 -91
+2 -4
Documentation/block/data-integrity.txt
··· 192 192 supported by the block device. 193 193 194 194 195 - int bio_integrity_prep(bio); 195 + bool bio_integrity_prep(bio); 196 196 197 197 To generate IMD for WRITE and to set up buffers for READ, the 198 198 filesystem must call bio_integrity_prep(bio). ··· 201 201 sector must be set, and the bio should have all data pages 202 202 added. It is up to the caller to ensure that the bio does not 203 203 change while I/O is in progress. 204 - 205 - bio_integrity_prep() should only be called if 206 - bio_integrity_enabled() returned 1. 204 + Complete bio with error if prepare failed for some reson. 207 205 208 206 209 207 5.3 PASSING EXISTING INTEGRITY METADATA
+39 -49
block/bio-integrity.c
··· 160 160 EXPORT_SYMBOL(bio_integrity_add_page); 161 161 162 162 /** 163 - * bio_integrity_enabled - Check whether integrity can be passed 164 - * @bio: bio to check 165 - * 166 - * Description: Determines whether bio_integrity_prep() can be called 167 - * on this bio or not. bio data direction and target device must be 168 - * set prior to calling. The functions honors the write_generate and 169 - * read_verify flags in sysfs. 170 - */ 171 - bool bio_integrity_enabled(struct bio *bio) 172 - { 173 - struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev); 174 - 175 - if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE) 176 - return false; 177 - 178 - if (!bio_sectors(bio)) 179 - return false; 180 - 181 - /* Already protected? */ 182 - if (bio_integrity(bio)) 183 - return false; 184 - 185 - if (bi == NULL) 186 - return false; 187 - 188 - if (bio_data_dir(bio) == READ && bi->profile->verify_fn != NULL && 189 - (bi->flags & BLK_INTEGRITY_VERIFY)) 190 - return true; 191 - 192 - if (bio_data_dir(bio) == WRITE && bi->profile->generate_fn != NULL && 193 - (bi->flags & BLK_INTEGRITY_GENERATE)) 194 - return true; 195 - 196 - return false; 197 - } 198 - EXPORT_SYMBOL(bio_integrity_enabled); 199 - 200 - /** 201 163 * bio_integrity_intervals - Return number of integrity intervals for a bio 202 164 * @bi: blk_integrity profile for device 203 165 * @sectors: Size of the bio in 512-byte sectors ··· 224 262 * bio_integrity_prep - Prepare bio for integrity I/O 225 263 * @bio: bio to prepare 226 264 * 227 - * Description: Allocates a buffer for integrity metadata, maps the 228 - * pages and attaches them to a bio. The bio must have data 229 - * direction, target device and start sector set priot to calling. In 230 - * the WRITE case, integrity metadata will be generated using the 231 - * block device's integrity function. In the READ case, the buffer 265 + * Description: Checks if the bio already has an integrity payload attached. 266 + * If it does, the payload has been generated by another kernel subsystem, 267 + * and we just pass it through. Otherwise allocates integrity payload. 268 + * The bio must have data direction, target device and start sector set priot 269 + * to calling. In the WRITE case, integrity metadata will be generated using 270 + * the block device's integrity function. In the READ case, the buffer 232 271 * will be prepared for DMA and a suitable end_io handler set up. 233 272 */ 234 - int bio_integrity_prep(struct bio *bio) 273 + bool bio_integrity_prep(struct bio *bio) 235 274 { 236 275 struct bio_integrity_payload *bip; 237 276 struct blk_integrity *bi; ··· 242 279 unsigned int len, nr_pages; 243 280 unsigned int bytes, offset, i; 244 281 unsigned int intervals; 282 + blk_status_t status; 245 283 246 284 bi = bdev_get_integrity(bio->bi_bdev); 247 285 q = bdev_get_queue(bio->bi_bdev); 248 - BUG_ON(bi == NULL); 249 - BUG_ON(bio_integrity(bio)); 286 + if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE) 287 + return true; 250 288 289 + if (!bio_sectors(bio)) 290 + return true; 291 + 292 + /* Already protected? */ 293 + if (bio_integrity(bio)) 294 + return true; 295 + 296 + if (bi == NULL) 297 + return true; 298 + 299 + if (bio_data_dir(bio) == READ) { 300 + if (!bi->profile->verify_fn || 301 + !(bi->flags & BLK_INTEGRITY_VERIFY)) 302 + return true; 303 + } else { 304 + if (!bi->profile->generate_fn || 305 + !(bi->flags & BLK_INTEGRITY_GENERATE)) 306 + return true; 307 + } 251 308 intervals = bio_integrity_intervals(bi, bio_sectors(bio)); 252 309 253 310 /* Allocate kernel buffer for protection data */ 254 311 len = intervals * bi->tuple_size; 255 312 buf = kmalloc(len, GFP_NOIO | q->bounce_gfp); 313 + status = BLK_STS_RESOURCE; 256 314 if (unlikely(buf == NULL)) { 257 315 printk(KERN_ERR "could not allocate integrity buffer\n"); 258 - return -ENOMEM; 316 + goto err_end_io; 259 317 } 260 318 261 319 end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT; ··· 288 304 if (IS_ERR(bip)) { 289 305 printk(KERN_ERR "could not allocate data integrity bioset\n"); 290 306 kfree(buf); 291 - return PTR_ERR(bip); 307 + status = BLK_STS_RESOURCE; 308 + goto err_end_io; 292 309 } 293 310 294 311 bip->bip_flags |= BIP_BLOCK_INTEGRITY; ··· 334 349 /* Auto-generate integrity metadata if this is a write */ 335 350 if (bio_data_dir(bio) == WRITE) 336 351 bio_integrity_process(bio, bi->profile->generate_fn); 352 + return true; 337 353 338 - return 0; 354 + err_end_io: 355 + bio->bi_status = status; 356 + bio_endio(bio); 357 + return false; 358 + 339 359 } 340 360 EXPORT_SYMBOL(bio_integrity_prep); 341 361
+1 -4
block/blk-core.c
··· 1787 1787 1788 1788 blk_queue_split(q, &bio); 1789 1789 1790 - if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { 1791 - bio->bi_status = BLK_STS_IOERR; 1792 - bio_endio(bio); 1790 + if (!bio_integrity_prep(bio)) 1793 1791 return BLK_QC_T_NONE; 1794 - } 1795 1792 1796 1793 if (op_is_flush(bio->bi_opf)) { 1797 1794 spin_lock_irq(q->queue_lock);
+1 -3
block/blk-mq.c
··· 1550 1550 1551 1551 blk_queue_split(q, &bio); 1552 1552 1553 - if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { 1554 - bio_io_error(bio); 1553 + if (!bio_integrity_prep(bio)) 1555 1554 return BLK_QC_T_NONE; 1556 - } 1557 1555 1558 1556 if (!is_flush_fua && !blk_queue_nomerges(q) && 1559 1557 blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
+2 -11
drivers/nvdimm/blk.c
··· 179 179 int err = 0, rw; 180 180 bool do_acct; 181 181 182 - /* 183 - * bio_integrity_enabled also checks if the bio already has an 184 - * integrity payload attached. If it does, we *don't* do a 185 - * bio_integrity_prep here - the payload has been generated by 186 - * another kernel subsystem, and we just pass it through. 187 - */ 188 - if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { 189 - bio->bi_status = BLK_STS_IOERR; 190 - goto out; 191 - } 182 + if (!bio_integrity_prep(bio)) 183 + return BLK_QC_T_NONE; 192 184 193 185 bip = bio_integrity(bio); 194 186 nsblk = q->queuedata; ··· 204 212 if (do_acct) 205 213 nd_iostat_end(bio, start); 206 214 207 - out: 208 215 bio_endio(bio); 209 216 return BLK_QC_T_NONE; 210 217 }
+2 -11
drivers/nvdimm/btt.c
··· 1203 1203 int err = 0; 1204 1204 bool do_acct; 1205 1205 1206 - /* 1207 - * bio_integrity_enabled also checks if the bio already has an 1208 - * integrity payload attached. If it does, we *don't* do a 1209 - * bio_integrity_prep here - the payload has been generated by 1210 - * another kernel subsystem, and we just pass it through. 1211 - */ 1212 - if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) { 1213 - bio->bi_status = BLK_STS_IOERR; 1214 - goto out; 1215 - } 1206 + if (!bio_integrity_prep(bio)) 1207 + return BLK_QC_T_NONE; 1216 1208 1217 1209 do_acct = nd_iostat_start(bio, &start); 1218 1210 bio_for_each_segment(bvec, bio, iter) { ··· 1231 1239 if (do_acct) 1232 1240 nd_iostat_end(bio, start); 1233 1241 1234 - out: 1235 1242 bio_endio(bio); 1236 1243 return BLK_QC_T_NONE; 1237 1244 }
+3 -9
include/linux/bio.h
··· 724 724 extern struct bio_integrity_payload *bio_integrity_alloc(struct bio *, gfp_t, unsigned int); 725 725 extern void bio_integrity_free(struct bio *); 726 726 extern int bio_integrity_add_page(struct bio *, struct page *, unsigned int, unsigned int); 727 - extern bool bio_integrity_enabled(struct bio *bio); 728 - extern int bio_integrity_prep(struct bio *); 727 + extern bool bio_integrity_prep(struct bio *); 729 728 extern void bio_integrity_endio(struct bio *); 730 729 extern void bio_integrity_advance(struct bio *, unsigned int); 731 730 extern void bio_integrity_trim(struct bio *); ··· 740 741 return NULL; 741 742 } 742 743 743 - static inline bool bio_integrity_enabled(struct bio *bio) 744 - { 745 - return false; 746 - } 747 - 748 744 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size) 749 745 { 750 746 return 0; ··· 750 756 return; 751 757 } 752 758 753 - static inline int bio_integrity_prep(struct bio *bio) 759 + static inline bool bio_integrity_prep(struct bio *bio) 754 760 { 755 - return 0; 761 + return true; 756 762 } 757 763 758 764 static inline void bio_integrity_free(struct bio *bio)