1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_BIO_INTEGRITY_H
3 #define _LINUX_BIO_INTEGRITY_H
4 
5 #include <linux/bio.h>
6 
7 enum bip_flags {
8 	BIP_BLOCK_INTEGRITY	= 1 << 0, /* block layer owns integrity data */
9 	BIP_MAPPED_INTEGRITY	= 1 << 1, /* ref tag has been remapped */
10 	BIP_CTRL_NOCHECK	= 1 << 2, /* disable HBA integrity checking */
11 	BIP_DISK_NOCHECK	= 1 << 3, /* disable disk integrity checking */
12 	BIP_IP_CHECKSUM		= 1 << 4, /* IP checksum */
13 	BIP_COPY_USER		= 1 << 5, /* Kernel bounce buffer in use */
14 };
15 
16 struct bio_integrity_payload {
17 	struct bio		*bip_bio;	/* parent bio */
18 
19 	struct bvec_iter	bip_iter;
20 
21 	unsigned short		bip_vcnt;	/* # of integrity bio_vecs */
22 	unsigned short		bip_max_vcnt;	/* integrity bio_vec slots */
23 	unsigned short		bip_flags;	/* control flags */
24 
25 	struct bvec_iter	bio_iter;	/* for rewinding parent bio */
26 
27 	struct work_struct	bip_work;	/* I/O completion */
28 
29 	struct bio_vec		*bip_vec;
30 	struct bio_vec		bip_inline_vecs[];/* embedded bvec array */
31 };
32 
33 #ifdef CONFIG_BLK_DEV_INTEGRITY
34 
35 #define bip_for_each_vec(bvl, bip, iter)				\
36 	for_each_bvec(bvl, (bip)->bip_vec, iter, (bip)->bip_iter)
37 
38 #define bio_for_each_integrity_vec(_bvl, _bio, _iter)			\
39 	for_each_bio(_bio)						\
40 		bip_for_each_vec(_bvl, _bio->bi_integrity, _iter)
41 
bio_integrity(struct bio * bio)42 static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
43 {
44 	if (bio->bi_opf & REQ_INTEGRITY)
45 		return bio->bi_integrity;
46 
47 	return NULL;
48 }
49 
bio_integrity_flagged(struct bio * bio,enum bip_flags flag)50 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
51 {
52 	struct bio_integrity_payload *bip = bio_integrity(bio);
53 
54 	if (bip)
55 		return bip->bip_flags & flag;
56 
57 	return false;
58 }
59 
bip_get_seed(struct bio_integrity_payload * bip)60 static inline sector_t bip_get_seed(struct bio_integrity_payload *bip)
61 {
62 	return bip->bip_iter.bi_sector;
63 }
64 
bip_set_seed(struct bio_integrity_payload * bip,sector_t seed)65 static inline void bip_set_seed(struct bio_integrity_payload *bip,
66 				sector_t seed)
67 {
68 	bip->bip_iter.bi_sector = seed;
69 }
70 
71 struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio, gfp_t gfp,
72 		unsigned int nr);
73 int bio_integrity_add_page(struct bio *bio, struct page *page, unsigned int len,
74 		unsigned int offset);
75 int bio_integrity_map_user(struct bio *bio, void __user *ubuf, ssize_t len, u32 seed);
76 void bio_integrity_unmap_user(struct bio *bio);
77 bool bio_integrity_prep(struct bio *bio);
78 void bio_integrity_advance(struct bio *bio, unsigned int bytes_done);
79 void bio_integrity_trim(struct bio *bio);
80 int bio_integrity_clone(struct bio *bio, struct bio *bio_src, gfp_t gfp_mask);
81 int bioset_integrity_create(struct bio_set *bs, int pool_size);
82 void bioset_integrity_free(struct bio_set *bs);
83 void bio_integrity_init(void);
84 
85 #else /* CONFIG_BLK_DEV_INTEGRITY */
86 
bio_integrity(struct bio * bio)87 static inline struct bio_integrity_payload *bio_integrity(struct bio *bio)
88 {
89 	return NULL;
90 }
91 
bioset_integrity_create(struct bio_set * bs,int pool_size)92 static inline int bioset_integrity_create(struct bio_set *bs, int pool_size)
93 {
94 	return 0;
95 }
96 
bioset_integrity_free(struct bio_set * bs)97 static inline void bioset_integrity_free(struct bio_set *bs)
98 {
99 }
100 
bio_integrity_map_user(struct bio * bio,void __user * ubuf,ssize_t len,u32 seed)101 static inline int bio_integrity_map_user(struct bio *bio, void __user *ubuf,
102 					 ssize_t len, u32 seed)
103 {
104 	return -EINVAL;
105 }
106 
bio_integrity_unmap_user(struct bio * bio)107 static inline void bio_integrity_unmap_user(struct bio *bio)
108 {
109 }
110 
bio_integrity_prep(struct bio * bio)111 static inline bool bio_integrity_prep(struct bio *bio)
112 {
113 	return true;
114 }
115 
bio_integrity_clone(struct bio * bio,struct bio * bio_src,gfp_t gfp_mask)116 static inline int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
117 		gfp_t gfp_mask)
118 {
119 	return 0;
120 }
121 
bio_integrity_advance(struct bio * bio,unsigned int bytes_done)122 static inline void bio_integrity_advance(struct bio *bio,
123 		unsigned int bytes_done)
124 {
125 }
126 
bio_integrity_trim(struct bio * bio)127 static inline void bio_integrity_trim(struct bio *bio)
128 {
129 }
130 
bio_integrity_init(void)131 static inline void bio_integrity_init(void)
132 {
133 }
134 
bio_integrity_flagged(struct bio * bio,enum bip_flags flag)135 static inline bool bio_integrity_flagged(struct bio *bio, enum bip_flags flag)
136 {
137 	return false;
138 }
139 
140 static inline struct bio_integrity_payload *
bio_integrity_alloc(struct bio * bio,gfp_t gfp,unsigned int nr)141 bio_integrity_alloc(struct bio *bio, gfp_t gfp, unsigned int nr)
142 {
143 	return ERR_PTR(-EINVAL);
144 }
145 
bio_integrity_add_page(struct bio * bio,struct page * page,unsigned int len,unsigned int offset)146 static inline int bio_integrity_add_page(struct bio *bio, struct page *page,
147 					unsigned int len, unsigned int offset)
148 {
149 	return 0;
150 }
151 #endif /* CONFIG_BLK_DEV_INTEGRITY */
152 #endif /* _LINUX_BIO_INTEGRITY_H */
153