1 // SPDX-License-Identifier: GPL-2.0
3 * bio-integrity.c - bio data integrity extensions
5 * Copyright (C) 2007, 2008, 2009 Oracle Corporation
6 * Written by: Martin K. Petersen <martin.petersen@oracle.com>
9 #include <linux/blkdev.h>
10 #include <linux/mempool.h>
11 #include <linux/export.h>
12 #include <linux/bio.h>
13 #include <linux/workqueue.h>
14 #include <linux/slab.h>
17 #define BIP_INLINE_VECS 4
19 static struct kmem_cache
*bip_slab
;
20 static struct workqueue_struct
*kintegrityd_wq
;
22 void blk_flush_integrity(void)
24 flush_workqueue(kintegrityd_wq
);
27 void __bio_integrity_free(struct bio_set
*bs
, struct bio_integrity_payload
*bip
)
29 if (bs
&& mempool_initialized(&bs
->bio_integrity_pool
)) {
31 bvec_free(&bs
->bvec_integrity_pool
, bip
->bip_vec
,
33 mempool_free(bip
, &bs
->bio_integrity_pool
);
40 * bio_integrity_alloc - Allocate integrity payload and attach it to bio
41 * @bio: bio to attach integrity metadata to
42 * @gfp_mask: Memory allocation mask
43 * @nr_vecs: Number of integrity metadata scatter-gather elements
45 * Description: This function prepares a bio for attaching integrity
46 * metadata. nr_vecs specifies the maximum number of pages containing
47 * integrity metadata that can be attached.
49 struct bio_integrity_payload
*bio_integrity_alloc(struct bio
*bio
,
53 struct bio_integrity_payload
*bip
;
54 struct bio_set
*bs
= bio
->bi_pool
;
57 if (!bs
|| !mempool_initialized(&bs
->bio_integrity_pool
)) {
58 bip
= kmalloc(struct_size(bip
, bip_inline_vecs
, nr_vecs
), gfp_mask
);
59 inline_vecs
= nr_vecs
;
61 bip
= mempool_alloc(&bs
->bio_integrity_pool
, gfp_mask
);
62 inline_vecs
= BIP_INLINE_VECS
;
66 return ERR_PTR(-ENOMEM
);
68 memset(bip
, 0, sizeof(*bip
));
70 if (nr_vecs
> inline_vecs
) {
71 unsigned long idx
= 0;
73 bip
->bip_vec
= bvec_alloc(gfp_mask
, nr_vecs
, &idx
,
74 &bs
->bvec_integrity_pool
);
77 bip
->bip_max_vcnt
= bvec_nr_vecs(idx
);
80 bip
->bip_vec
= bip
->bip_inline_vecs
;
81 bip
->bip_max_vcnt
= inline_vecs
;
85 bio
->bi_integrity
= bip
;
86 bio
->bi_opf
|= REQ_INTEGRITY
;
90 __bio_integrity_free(bs
, bip
);
91 return ERR_PTR(-ENOMEM
);
93 EXPORT_SYMBOL(bio_integrity_alloc
);
96 * bio_integrity_free - Free bio integrity payload
97 * @bio: bio containing bip to be freed
99 * Description: Used to free the integrity portion of a bio. Usually
100 * called from bio_free().
102 void bio_integrity_free(struct bio
*bio
)
104 struct bio_integrity_payload
*bip
= bio_integrity(bio
);
105 struct bio_set
*bs
= bio
->bi_pool
;
107 if (bip
->bip_flags
& BIP_BLOCK_INTEGRITY
)
108 kfree(page_address(bip
->bip_vec
->bv_page
) +
109 bip
->bip_vec
->bv_offset
);
111 __bio_integrity_free(bs
, bip
);
112 bio
->bi_integrity
= NULL
;
113 bio
->bi_opf
&= ~REQ_INTEGRITY
;
117 * bio_integrity_add_page - Attach integrity metadata
118 * @bio: bio to update
119 * @page: page containing integrity metadata
120 * @len: number of bytes of integrity metadata in page
121 * @offset: start offset within page
123 * Description: Attach a page containing integrity metadata to bio.
125 int bio_integrity_add_page(struct bio
*bio
, struct page
*page
,
126 unsigned int len
, unsigned int offset
)
128 struct bio_integrity_payload
*bip
= bio_integrity(bio
);
131 if (bip
->bip_vcnt
>= bip
->bip_max_vcnt
) {
132 printk(KERN_ERR
"%s: bip_vec full\n", __func__
);
136 iv
= bip
->bip_vec
+ bip
->bip_vcnt
;
139 bvec_gap_to_prev(bio
->bi_disk
->queue
,
140 &bip
->bip_vec
[bip
->bip_vcnt
- 1], offset
))
145 iv
->bv_offset
= offset
;
150 EXPORT_SYMBOL(bio_integrity_add_page
);
153 * bio_integrity_process - Process integrity metadata for a bio
154 * @bio: bio to generate/verify integrity metadata for
155 * @proc_iter: iterator to process
156 * @proc_fn: Pointer to the relevant processing function
158 static blk_status_t
bio_integrity_process(struct bio
*bio
,
159 struct bvec_iter
*proc_iter
, integrity_processing_fn
*proc_fn
)
161 struct blk_integrity
*bi
= blk_get_integrity(bio
->bi_disk
);
162 struct blk_integrity_iter iter
;
163 struct bvec_iter bviter
;
165 struct bio_integrity_payload
*bip
= bio_integrity(bio
);
166 blk_status_t ret
= BLK_STS_OK
;
167 void *prot_buf
= page_address(bip
->bip_vec
->bv_page
) +
168 bip
->bip_vec
->bv_offset
;
170 iter
.disk_name
= bio
->bi_disk
->disk_name
;
171 iter
.interval
= 1 << bi
->interval_exp
;
172 iter
.seed
= proc_iter
->bi_sector
;
173 iter
.prot_buf
= prot_buf
;
175 __bio_for_each_segment(bv
, bio
, bviter
, *proc_iter
) {
176 void *kaddr
= kmap_atomic(bv
.bv_page
);
178 iter
.data_buf
= kaddr
+ bv
.bv_offset
;
179 iter
.data_size
= bv
.bv_len
;
181 ret
= proc_fn(&iter
);
183 kunmap_atomic(kaddr
);
187 kunmap_atomic(kaddr
);
193 * bio_integrity_prep - Prepare bio for integrity I/O
194 * @bio: bio to prepare
196 * Description: Checks if the bio already has an integrity payload attached.
197 * If it does, the payload has been generated by another kernel subsystem,
198 * and we just pass it through. Otherwise allocates integrity payload.
199 * The bio must have data direction, target device and start sector set priot
200 * to calling. In the WRITE case, integrity metadata will be generated using
201 * the block device's integrity function. In the READ case, the buffer
202 * will be prepared for DMA and a suitable end_io handler set up.
204 bool bio_integrity_prep(struct bio
*bio
)
206 struct bio_integrity_payload
*bip
;
207 struct blk_integrity
*bi
= blk_get_integrity(bio
->bi_disk
);
208 struct request_queue
*q
= bio
->bi_disk
->queue
;
210 unsigned long start
, end
;
211 unsigned int len
, nr_pages
;
212 unsigned int bytes
, offset
, i
;
213 unsigned int intervals
;
219 if (bio_op(bio
) != REQ_OP_READ
&& bio_op(bio
) != REQ_OP_WRITE
)
222 if (!bio_sectors(bio
))
225 /* Already protected? */
226 if (bio_integrity(bio
))
229 if (bio_data_dir(bio
) == READ
) {
230 if (!bi
->profile
->verify_fn
||
231 !(bi
->flags
& BLK_INTEGRITY_VERIFY
))
234 if (!bi
->profile
->generate_fn
||
235 !(bi
->flags
& BLK_INTEGRITY_GENERATE
))
238 intervals
= bio_integrity_intervals(bi
, bio_sectors(bio
));
240 /* Allocate kernel buffer for protection data */
241 len
= intervals
* bi
->tuple_size
;
242 buf
= kmalloc(len
, GFP_NOIO
| q
->bounce_gfp
);
243 status
= BLK_STS_RESOURCE
;
244 if (unlikely(buf
== NULL
)) {
245 printk(KERN_ERR
"could not allocate integrity buffer\n");
249 end
= (((unsigned long) buf
) + len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
250 start
= ((unsigned long) buf
) >> PAGE_SHIFT
;
251 nr_pages
= end
- start
;
253 /* Allocate bio integrity payload and integrity vectors */
254 bip
= bio_integrity_alloc(bio
, GFP_NOIO
, nr_pages
);
256 printk(KERN_ERR
"could not allocate data integrity bioset\n");
258 status
= BLK_STS_RESOURCE
;
262 bip
->bip_flags
|= BIP_BLOCK_INTEGRITY
;
263 bip
->bip_iter
.bi_size
= len
;
264 bip_set_seed(bip
, bio
->bi_iter
.bi_sector
);
266 if (bi
->flags
& BLK_INTEGRITY_IP_CHECKSUM
)
267 bip
->bip_flags
|= BIP_IP_CHECKSUM
;
270 offset
= offset_in_page(buf
);
271 for (i
= 0 ; i
< nr_pages
; i
++) {
273 bytes
= PAGE_SIZE
- offset
;
281 ret
= bio_integrity_add_page(bio
, virt_to_page(buf
),
285 printk(KERN_ERR
"could not attach integrity payload\n");
286 status
= BLK_STS_RESOURCE
;
298 /* Auto-generate integrity metadata if this is a write */
299 if (bio_data_dir(bio
) == WRITE
) {
300 bio_integrity_process(bio
, &bio
->bi_iter
,
301 bi
->profile
->generate_fn
);
303 bip
->bio_iter
= bio
->bi_iter
;
308 bio
->bi_status
= status
;
313 EXPORT_SYMBOL(bio_integrity_prep
);
316 * bio_integrity_verify_fn - Integrity I/O completion worker
317 * @work: Work struct stored in bio to be verified
319 * Description: This workqueue function is called to complete a READ
320 * request. The function verifies the transferred integrity metadata
321 * and then calls the original bio end_io function.
323 static void bio_integrity_verify_fn(struct work_struct
*work
)
325 struct bio_integrity_payload
*bip
=
326 container_of(work
, struct bio_integrity_payload
, bip_work
);
327 struct bio
*bio
= bip
->bip_bio
;
328 struct blk_integrity
*bi
= blk_get_integrity(bio
->bi_disk
);
331 * At the moment verify is called bio's iterator was advanced
332 * during split and completion, we need to rewind iterator to
333 * it's original position.
335 bio
->bi_status
= bio_integrity_process(bio
, &bip
->bio_iter
,
336 bi
->profile
->verify_fn
);
337 bio_integrity_free(bio
);
342 * __bio_integrity_endio - Integrity I/O completion function
343 * @bio: Protected bio
345 * Description: Completion for integrity I/O
347 * Normally I/O completion is done in interrupt context. However,
348 * verifying I/O integrity is a time-consuming task which must be run
349 * in process context. This function postpones completion
352 bool __bio_integrity_endio(struct bio
*bio
)
354 struct blk_integrity
*bi
= blk_get_integrity(bio
->bi_disk
);
355 struct bio_integrity_payload
*bip
= bio_integrity(bio
);
357 if (bio_op(bio
) == REQ_OP_READ
&& !bio
->bi_status
&&
358 (bip
->bip_flags
& BIP_BLOCK_INTEGRITY
) && bi
->profile
->verify_fn
) {
359 INIT_WORK(&bip
->bip_work
, bio_integrity_verify_fn
);
360 queue_work(kintegrityd_wq
, &bip
->bip_work
);
364 bio_integrity_free(bio
);
369 * bio_integrity_advance - Advance integrity vector
370 * @bio: bio whose integrity vector to update
371 * @bytes_done: number of data bytes that have been completed
373 * Description: This function calculates how many integrity bytes the
374 * number of completed data bytes correspond to and advances the
375 * integrity vector accordingly.
377 void bio_integrity_advance(struct bio
*bio
, unsigned int bytes_done
)
379 struct bio_integrity_payload
*bip
= bio_integrity(bio
);
380 struct blk_integrity
*bi
= blk_get_integrity(bio
->bi_disk
);
381 unsigned bytes
= bio_integrity_bytes(bi
, bytes_done
>> 9);
383 bip
->bip_iter
.bi_sector
+= bytes_done
>> 9;
384 bvec_iter_advance(bip
->bip_vec
, &bip
->bip_iter
, bytes
);
388 * bio_integrity_trim - Trim integrity vector
389 * @bio: bio whose integrity vector to update
391 * Description: Used to trim the integrity vector in a cloned bio.
393 void bio_integrity_trim(struct bio
*bio
)
395 struct bio_integrity_payload
*bip
= bio_integrity(bio
);
396 struct blk_integrity
*bi
= blk_get_integrity(bio
->bi_disk
);
398 bip
->bip_iter
.bi_size
= bio_integrity_bytes(bi
, bio_sectors(bio
));
400 EXPORT_SYMBOL(bio_integrity_trim
);
403 * bio_integrity_clone - Callback for cloning bios with integrity metadata
405 * @bio_src: Original bio
406 * @gfp_mask: Memory allocation mask
408 * Description: Called to allocate a bip when cloning a bio
410 int bio_integrity_clone(struct bio
*bio
, struct bio
*bio_src
,
413 struct bio_integrity_payload
*bip_src
= bio_integrity(bio_src
);
414 struct bio_integrity_payload
*bip
;
416 BUG_ON(bip_src
== NULL
);
418 bip
= bio_integrity_alloc(bio
, gfp_mask
, bip_src
->bip_vcnt
);
422 memcpy(bip
->bip_vec
, bip_src
->bip_vec
,
423 bip_src
->bip_vcnt
* sizeof(struct bio_vec
));
425 bip
->bip_vcnt
= bip_src
->bip_vcnt
;
426 bip
->bip_iter
= bip_src
->bip_iter
;
430 EXPORT_SYMBOL(bio_integrity_clone
);
432 int bioset_integrity_create(struct bio_set
*bs
, int pool_size
)
434 if (mempool_initialized(&bs
->bio_integrity_pool
))
437 if (mempool_init_slab_pool(&bs
->bio_integrity_pool
,
438 pool_size
, bip_slab
))
441 if (biovec_init_pool(&bs
->bvec_integrity_pool
, pool_size
)) {
442 mempool_exit(&bs
->bio_integrity_pool
);
448 EXPORT_SYMBOL(bioset_integrity_create
);
450 void bioset_integrity_free(struct bio_set
*bs
)
452 mempool_exit(&bs
->bio_integrity_pool
);
453 mempool_exit(&bs
->bvec_integrity_pool
);
456 void __init
bio_integrity_init(void)
459 * kintegrityd won't block much but may burn a lot of CPU cycles.
460 * Make it highpri CPU intensive wq with max concurrency of 1.
462 kintegrityd_wq
= alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM
|
463 WQ_HIGHPRI
| WQ_CPU_INTENSIVE
, 1);
465 panic("Failed to create kintegrityd\n");
467 bip_slab
= kmem_cache_create("bio_integrity_payload",
468 sizeof(struct bio_integrity_payload
) +
469 sizeof(struct bio_vec
) * BIP_INLINE_VECS
,
470 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);