Skip to content

Commit 0bde8a1

Browse files
Christoph Hellwigaxboe
authored andcommitted
block: add fs_bio_integrity helpers
Add a set of helpers for file system initiated integrity information. These include mempool backed allocations and verifying based on a passed in sector and size which is often available from file system completion routines. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Anuj Gupta <[email protected]> Reviewed-by: Kanchan Joshi <[email protected]> Reviewed-by: Martin K. Petersen <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]> Tested-by: Anuj Gupta <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 8c56ef1 commit 0bde8a1

3 files changed

Lines changed: 88 additions & 1 deletion

File tree

block/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ bfq-y := bfq-iosched.o bfq-wf2q.o bfq-cgroup.o
2626
obj-$(CONFIG_IOSCHED_BFQ) += bfq.o
2727

2828
obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o blk-integrity.o t10-pi.o \
29-
bio-integrity-auto.o
29+
bio-integrity-auto.o bio-integrity-fs.o
3030
obj-$(CONFIG_BLK_DEV_ZONED) += blk-zoned.o
3131
obj-$(CONFIG_BLK_WBT) += blk-wbt.o
3232
obj-$(CONFIG_BLK_DEBUG_FS) += blk-mq-debugfs.o

block/bio-integrity-fs.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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);

include/linux/bio-integrity.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,10 @@ void bio_integrity_alloc_buf(struct bio *bio, bool zero_buffer);
145145
void bio_integrity_free_buf(struct bio_integrity_payload *bip);
146146
void bio_integrity_setup_default(struct bio *bio);
147147

148+
unsigned int fs_bio_integrity_alloc(struct bio *bio);
149+
void fs_bio_integrity_free(struct bio *bio);
150+
void fs_bio_integrity_generate(struct bio *bio);
151+
int fs_bio_integrity_verify(struct bio *bio, sector_t sector,
152+
unsigned int size);
153+
148154
#endif /* _LINUX_BIO_INTEGRITY_H */

0 commit comments

Comments
 (0)