1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2025 Christoph Hellwig. 4 */ 5 #include <linux/blk-integrity.h> 6 #include <linux/bio-integrity.h> 7 #include "blk.h" 8 9 struct fs_bio_integrity_buf { 10 struct bio_integrity_payload bip; 11 struct bio_vec bvec; 12 }; 13 14 static struct kmem_cache *fs_bio_integrity_cache; 15 static mempool_t fs_bio_integrity_pool; 16 17 unsigned int fs_bio_integrity_alloc(struct bio *bio) 18 { 19 struct fs_bio_integrity_buf *iib; 20 unsigned int action; 21 22 action = bio_integrity_action(bio); 23 if (!action) 24 return 0; 25 26 iib = mempool_alloc(&fs_bio_integrity_pool, GFP_NOIO); 27 bio_integrity_init(bio, &iib->bip, &iib->bvec, 1); 28 29 bio_integrity_alloc_buf(bio, action & BI_ACT_ZERO); 30 if (action & BI_ACT_CHECK) 31 bio_integrity_setup_default(bio); 32 return action; 33 } 34 35 void fs_bio_integrity_free(struct bio *bio) 36 { 37 struct bio_integrity_payload *bip = bio_integrity(bio); 38 39 bio_integrity_free_buf(bip); 40 mempool_free(container_of(bip, struct fs_bio_integrity_buf, bip), 41 &fs_bio_integrity_pool); 42 43 bio->bi_integrity = NULL; 44 bio->bi_opf &= ~REQ_INTEGRITY; 45 } 46 47 void fs_bio_integrity_generate(struct bio *bio) 48 { 49 if (fs_bio_integrity_alloc(bio)) 50 bio_integrity_generate(bio); 51 } 52 EXPORT_SYMBOL_GPL(fs_bio_integrity_generate); 53 54 int fs_bio_integrity_verify(struct bio *bio, sector_t sector, unsigned int size) 55 { 56 struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk); 57 struct bio_integrity_payload *bip = bio_integrity(bio); 58 59 /* 60 * Reinitialize bip->bip_iter. 61 * 62 * This is for use in the submitter after the driver is done with the 63 * bio. Requires the submitter to remember the sector and the size. 64 */ 65 memset(&bip->bip_iter, 0, sizeof(bip->bip_iter)); 66 bip->bip_iter.bi_sector = sector; 67 bip->bip_iter.bi_size = bio_integrity_bytes(bi, size >> SECTOR_SHIFT); 68 return blk_status_to_errno(bio_integrity_verify(bio, &bip->bip_iter)); 69 } 70 71 static int __init fs_bio_integrity_init(void) 72 { 73 fs_bio_integrity_cache = kmem_cache_create("fs_bio_integrity", 74 sizeof(struct fs_bio_integrity_buf), 0, 75 SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL); 76 if (mempool_init_slab_pool(&fs_bio_integrity_pool, BIO_POOL_SIZE, 77 fs_bio_integrity_cache)) 78 panic("fs_bio_integrity: can't create pool\n"); 79 return 0; 80 } 81 fs_initcall(fs_bio_integrity_init); 82