]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/bio-integrity.c
2 * bio-integrity.c - bio data integrity extensions
4 * Copyright (C) 2007, 2008, 2009 Oracle Corporation
5 * Written by: Martin K. Petersen <martin.petersen@oracle.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to
18 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
23 #include <linux/blkdev.h>
24 #include <linux/mempool.h>
25 #include <linux/export.h>
26 #include <linux/bio.h>
27 #include <linux/workqueue.h>
28 #include <linux/slab.h>
30 struct integrity_slab
{
31 struct kmem_cache
*slab
;
32 unsigned short nr_vecs
;
36 #define IS(x) { .nr_vecs = x, .name = "bip-"__stringify(x) }
37 struct integrity_slab bip_slab
[BIOVEC_NR_POOLS
] __read_mostly
= {
38 IS(1), IS(4), IS(16), IS(64), IS(128), IS(BIO_MAX_PAGES
),
42 static struct workqueue_struct
*kintegrityd_wq
;
44 static inline unsigned int vecs_to_idx(unsigned int nr
)
57 case 129 ... BIO_MAX_PAGES
:
64 static inline int use_bip_pool(unsigned int idx
)
66 if (idx
== BIOVEC_MAX_IDX
)
73 * bio_integrity_alloc - Allocate integrity payload and attach it to bio
74 * @bio: bio to attach integrity metadata to
75 * @gfp_mask: Memory allocation mask
76 * @nr_vecs: Number of integrity metadata scatter-gather elements
78 * Description: This function prepares a bio for attaching integrity
79 * metadata. nr_vecs specifies the maximum number of pages containing
80 * integrity metadata that can be attached.
82 struct bio_integrity_payload
*bio_integrity_alloc(struct bio
*bio
,
86 struct bio_integrity_payload
*bip
;
87 unsigned int idx
= vecs_to_idx(nr_vecs
);
88 struct bio_set
*bs
= bio
->bi_pool
;
96 /* Lower order allocations come straight from slab */
97 if (!use_bip_pool(idx
))
98 bip
= kmem_cache_alloc(bip_slab
[idx
].slab
, gfp_mask
);
100 /* Use mempool if lower order alloc failed or max vecs were requested */
102 idx
= BIOVEC_MAX_IDX
; /* so we free the payload properly later */
103 bip
= mempool_alloc(bs
->bio_integrity_pool
, gfp_mask
);
105 if (unlikely(bip
== NULL
)) {
106 printk(KERN_ERR
"%s: could not alloc bip\n", __func__
);
111 memset(bip
, 0, sizeof(*bip
));
115 bio
->bi_integrity
= bip
;
119 EXPORT_SYMBOL(bio_integrity_alloc
);
122 * bio_integrity_free - Free bio integrity payload
123 * @bio: bio containing bip to be freed
125 * Description: Used to free the integrity portion of a bio. Usually
126 * called from bio_free().
128 void bio_integrity_free(struct bio
*bio
)
130 struct bio_integrity_payload
*bip
= bio
->bi_integrity
;
131 struct bio_set
*bs
= bio
->bi_pool
;
138 /* A cloned bio doesn't own the integrity metadata */
139 if (!bio_flagged(bio
, BIO_CLONED
) && !bio_flagged(bio
, BIO_FS_INTEGRITY
)
140 && bip
->bip_buf
!= NULL
)
143 if (use_bip_pool(bip
->bip_slab
))
144 mempool_free(bip
, bs
->bio_integrity_pool
);
146 kmem_cache_free(bip_slab
[bip
->bip_slab
].slab
, bip
);
148 bio
->bi_integrity
= NULL
;
150 EXPORT_SYMBOL(bio_integrity_free
);
153 * bio_integrity_add_page - Attach integrity metadata
154 * @bio: bio to update
155 * @page: page containing integrity metadata
156 * @len: number of bytes of integrity metadata in page
157 * @offset: start offset within page
159 * Description: Attach a page containing integrity metadata to bio.
161 int bio_integrity_add_page(struct bio
*bio
, struct page
*page
,
162 unsigned int len
, unsigned int offset
)
164 struct bio_integrity_payload
*bip
= bio
->bi_integrity
;
167 if (bip
->bip_vcnt
>= bvec_nr_vecs(bip
->bip_slab
)) {
168 printk(KERN_ERR
"%s: bip_vec full\n", __func__
);
172 iv
= bip_vec_idx(bip
, bip
->bip_vcnt
);
177 iv
->bv_offset
= offset
;
182 EXPORT_SYMBOL(bio_integrity_add_page
);
184 static int bdev_integrity_enabled(struct block_device
*bdev
, int rw
)
186 struct blk_integrity
*bi
= bdev_get_integrity(bdev
);
191 if (rw
== READ
&& bi
->verify_fn
!= NULL
&&
192 (bi
->flags
& INTEGRITY_FLAG_READ
))
195 if (rw
== WRITE
&& bi
->generate_fn
!= NULL
&&
196 (bi
->flags
& INTEGRITY_FLAG_WRITE
))
203 * bio_integrity_enabled - Check whether integrity can be passed
206 * Description: Determines whether bio_integrity_prep() can be called
207 * on this bio or not. bio data direction and target device must be
208 * set prior to calling. The functions honors the write_generate and
209 * read_verify flags in sysfs.
211 int bio_integrity_enabled(struct bio
*bio
)
213 /* Already protected? */
214 if (bio_integrity(bio
))
217 return bdev_integrity_enabled(bio
->bi_bdev
, bio_data_dir(bio
));
219 EXPORT_SYMBOL(bio_integrity_enabled
);
222 * bio_integrity_hw_sectors - Convert 512b sectors to hardware ditto
223 * @bi: blk_integrity profile for device
224 * @sectors: Number of 512 sectors to convert
226 * Description: The block layer calculates everything in 512 byte
227 * sectors but integrity metadata is done in terms of the hardware
228 * sector size of the storage device. Convert the block layer sectors
229 * to physical sectors.
231 static inline unsigned int bio_integrity_hw_sectors(struct blk_integrity
*bi
,
232 unsigned int sectors
)
234 /* At this point there are only 512b or 4096b DIF/EPP devices */
235 if (bi
->sector_size
== 4096)
236 return sectors
>>= 3;
242 * bio_integrity_tag_size - Retrieve integrity tag space
243 * @bio: bio to inspect
245 * Description: Returns the maximum number of tag bytes that can be
246 * attached to this bio. Filesystems can use this to determine how
247 * much metadata to attach to an I/O.
249 unsigned int bio_integrity_tag_size(struct bio
*bio
)
251 struct blk_integrity
*bi
= bdev_get_integrity(bio
->bi_bdev
);
253 BUG_ON(bio
->bi_size
== 0);
255 return bi
->tag_size
* (bio
->bi_size
/ bi
->sector_size
);
257 EXPORT_SYMBOL(bio_integrity_tag_size
);
259 int bio_integrity_tag(struct bio
*bio
, void *tag_buf
, unsigned int len
, int set
)
261 struct bio_integrity_payload
*bip
= bio
->bi_integrity
;
262 struct blk_integrity
*bi
= bdev_get_integrity(bio
->bi_bdev
);
263 unsigned int nr_sectors
;
265 BUG_ON(bip
->bip_buf
== NULL
);
267 if (bi
->tag_size
== 0)
270 nr_sectors
= bio_integrity_hw_sectors(bi
,
271 DIV_ROUND_UP(len
, bi
->tag_size
));
273 if (nr_sectors
* bi
->tuple_size
> bip
->bip_size
) {
274 printk(KERN_ERR
"%s: tag too big for bio: %u > %u\n",
275 __func__
, nr_sectors
* bi
->tuple_size
, bip
->bip_size
);
280 bi
->set_tag_fn(bip
->bip_buf
, tag_buf
, nr_sectors
);
282 bi
->get_tag_fn(bip
->bip_buf
, tag_buf
, nr_sectors
);
288 * bio_integrity_set_tag - Attach a tag buffer to a bio
289 * @bio: bio to attach buffer to
290 * @tag_buf: Pointer to a buffer containing tag data
291 * @len: Length of the included buffer
293 * Description: Use this function to tag a bio by leveraging the extra
294 * space provided by devices formatted with integrity protection. The
295 * size of the integrity buffer must be <= to the size reported by
296 * bio_integrity_tag_size().
298 int bio_integrity_set_tag(struct bio
*bio
, void *tag_buf
, unsigned int len
)
300 BUG_ON(bio_data_dir(bio
) != WRITE
);
302 return bio_integrity_tag(bio
, tag_buf
, len
, 1);
304 EXPORT_SYMBOL(bio_integrity_set_tag
);
307 * bio_integrity_get_tag - Retrieve a tag buffer from a bio
308 * @bio: bio to retrieve buffer from
309 * @tag_buf: Pointer to a buffer for the tag data
310 * @len: Length of the target buffer
312 * Description: Use this function to retrieve the tag buffer from a
313 * completed I/O. The size of the integrity buffer must be <= to the
314 * size reported by bio_integrity_tag_size().
316 int bio_integrity_get_tag(struct bio
*bio
, void *tag_buf
, unsigned int len
)
318 BUG_ON(bio_data_dir(bio
) != READ
);
320 return bio_integrity_tag(bio
, tag_buf
, len
, 0);
322 EXPORT_SYMBOL(bio_integrity_get_tag
);
325 * bio_integrity_generate - Generate integrity metadata for a bio
326 * @bio: bio to generate integrity metadata for
328 * Description: Generates integrity metadata for a bio by calling the
329 * block device's generation callback function. The bio must have a
330 * bip attached with enough room to accommodate the generated
331 * integrity metadata.
333 static void bio_integrity_generate(struct bio
*bio
)
335 struct blk_integrity
*bi
= bdev_get_integrity(bio
->bi_bdev
);
336 struct blk_integrity_exchg bix
;
338 sector_t sector
= bio
->bi_sector
;
339 unsigned int i
, sectors
, total
;
340 void *prot_buf
= bio
->bi_integrity
->bip_buf
;
343 bix
.disk_name
= bio
->bi_bdev
->bd_disk
->disk_name
;
344 bix
.sector_size
= bi
->sector_size
;
346 bio_for_each_segment(bv
, bio
, i
) {
347 void *kaddr
= kmap_atomic(bv
->bv_page
);
348 bix
.data_buf
= kaddr
+ bv
->bv_offset
;
349 bix
.data_size
= bv
->bv_len
;
350 bix
.prot_buf
= prot_buf
;
353 bi
->generate_fn(&bix
);
355 sectors
= bv
->bv_len
/ bi
->sector_size
;
357 prot_buf
+= sectors
* bi
->tuple_size
;
358 total
+= sectors
* bi
->tuple_size
;
359 BUG_ON(total
> bio
->bi_integrity
->bip_size
);
361 kunmap_atomic(kaddr
);
365 static inline unsigned short blk_integrity_tuple_size(struct blk_integrity
*bi
)
368 return bi
->tuple_size
;
374 * bio_integrity_prep - Prepare bio for integrity I/O
375 * @bio: bio to prepare
377 * Description: Allocates a buffer for integrity metadata, maps the
378 * pages and attaches them to a bio. The bio must have data
379 * direction, target device and start sector set priot to calling. In
380 * the WRITE case, integrity metadata will be generated using the
381 * block device's integrity function. In the READ case, the buffer
382 * will be prepared for DMA and a suitable end_io handler set up.
384 int bio_integrity_prep(struct bio
*bio
)
386 struct bio_integrity_payload
*bip
;
387 struct blk_integrity
*bi
;
388 struct request_queue
*q
;
390 unsigned long start
, end
;
391 unsigned int len
, nr_pages
;
392 unsigned int bytes
, offset
, i
;
393 unsigned int sectors
;
395 bi
= bdev_get_integrity(bio
->bi_bdev
);
396 q
= bdev_get_queue(bio
->bi_bdev
);
398 BUG_ON(bio_integrity(bio
));
400 sectors
= bio_integrity_hw_sectors(bi
, bio_sectors(bio
));
402 /* Allocate kernel buffer for protection data */
403 len
= sectors
* blk_integrity_tuple_size(bi
);
404 buf
= kmalloc(len
, GFP_NOIO
| q
->bounce_gfp
);
405 if (unlikely(buf
== NULL
)) {
406 printk(KERN_ERR
"could not allocate integrity buffer\n");
410 end
= (((unsigned long) buf
) + len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
411 start
= ((unsigned long) buf
) >> PAGE_SHIFT
;
412 nr_pages
= end
- start
;
414 /* Allocate bio integrity payload and integrity vectors */
415 bip
= bio_integrity_alloc(bio
, GFP_NOIO
, nr_pages
);
416 if (unlikely(bip
== NULL
)) {
417 printk(KERN_ERR
"could not allocate data integrity bioset\n");
424 bip
->bip_sector
= bio
->bi_sector
;
427 offset
= offset_in_page(buf
);
428 for (i
= 0 ; i
< nr_pages
; i
++) {
430 bytes
= PAGE_SIZE
- offset
;
438 ret
= bio_integrity_add_page(bio
, virt_to_page(buf
),
452 /* Install custom I/O completion handler if read verify is enabled */
453 if (bio_data_dir(bio
) == READ
) {
454 bip
->bip_end_io
= bio
->bi_end_io
;
455 bio
->bi_end_io
= bio_integrity_endio
;
458 /* Auto-generate integrity metadata if this is a write */
459 if (bio_data_dir(bio
) == WRITE
)
460 bio_integrity_generate(bio
);
464 EXPORT_SYMBOL(bio_integrity_prep
);
467 * bio_integrity_verify - Verify integrity metadata for a bio
468 * @bio: bio to verify
470 * Description: This function is called to verify the integrity of a
471 * bio. The data in the bio io_vec is compared to the integrity
472 * metadata returned by the HBA.
474 static int bio_integrity_verify(struct bio
*bio
)
476 struct blk_integrity
*bi
= bdev_get_integrity(bio
->bi_bdev
);
477 struct blk_integrity_exchg bix
;
479 sector_t sector
= bio
->bi_integrity
->bip_sector
;
480 unsigned int i
, sectors
, total
, ret
;
481 void *prot_buf
= bio
->bi_integrity
->bip_buf
;
484 bix
.disk_name
= bio
->bi_bdev
->bd_disk
->disk_name
;
485 bix
.sector_size
= bi
->sector_size
;
487 bio_for_each_segment(bv
, bio
, i
) {
488 void *kaddr
= kmap_atomic(bv
->bv_page
);
489 bix
.data_buf
= kaddr
+ bv
->bv_offset
;
490 bix
.data_size
= bv
->bv_len
;
491 bix
.prot_buf
= prot_buf
;
494 ret
= bi
->verify_fn(&bix
);
497 kunmap_atomic(kaddr
);
501 sectors
= bv
->bv_len
/ bi
->sector_size
;
503 prot_buf
+= sectors
* bi
->tuple_size
;
504 total
+= sectors
* bi
->tuple_size
;
505 BUG_ON(total
> bio
->bi_integrity
->bip_size
);
507 kunmap_atomic(kaddr
);
514 * bio_integrity_verify_fn - Integrity I/O completion worker
515 * @work: Work struct stored in bio to be verified
517 * Description: This workqueue function is called to complete a READ
518 * request. The function verifies the transferred integrity metadata
519 * and then calls the original bio end_io function.
521 static void bio_integrity_verify_fn(struct work_struct
*work
)
523 struct bio_integrity_payload
*bip
=
524 container_of(work
, struct bio_integrity_payload
, bip_work
);
525 struct bio
*bio
= bip
->bip_bio
;
528 error
= bio_integrity_verify(bio
);
530 /* Restore original bio completion handler */
531 bio
->bi_end_io
= bip
->bip_end_io
;
532 bio_endio(bio
, error
);
536 * bio_integrity_endio - Integrity I/O completion function
537 * @bio: Protected bio
538 * @error: Pointer to errno
540 * Description: Completion for integrity I/O
542 * Normally I/O completion is done in interrupt context. However,
543 * verifying I/O integrity is a time-consuming task which must be run
544 * in process context. This function postpones completion
547 void bio_integrity_endio(struct bio
*bio
, int error
)
549 struct bio_integrity_payload
*bip
= bio
->bi_integrity
;
551 BUG_ON(bip
->bip_bio
!= bio
);
553 /* In case of an I/O error there is no point in verifying the
554 * integrity metadata. Restore original bio end_io handler
558 bio
->bi_end_io
= bip
->bip_end_io
;
559 bio_endio(bio
, error
);
564 INIT_WORK(&bip
->bip_work
, bio_integrity_verify_fn
);
565 queue_work(kintegrityd_wq
, &bip
->bip_work
);
567 EXPORT_SYMBOL(bio_integrity_endio
);
570 * bio_integrity_mark_head - Advance bip_vec skip bytes
571 * @bip: Integrity vector to advance
572 * @skip: Number of bytes to advance it
574 void bio_integrity_mark_head(struct bio_integrity_payload
*bip
,
580 bip_for_each_vec(iv
, bip
, i
) {
584 } else if (skip
>= iv
->bv_len
) {
586 } else { /* skip < iv->bv_len) */
587 iv
->bv_offset
+= skip
;
596 * bio_integrity_mark_tail - Truncate bip_vec to be len bytes long
597 * @bip: Integrity vector to truncate
598 * @len: New length of integrity vector
600 void bio_integrity_mark_tail(struct bio_integrity_payload
*bip
,
606 bip_for_each_vec(iv
, bip
, i
) {
610 } else if (len
>= iv
->bv_len
) {
612 } else { /* len < iv->bv_len) */
620 * bio_integrity_advance - Advance integrity vector
621 * @bio: bio whose integrity vector to update
622 * @bytes_done: number of data bytes that have been completed
624 * Description: This function calculates how many integrity bytes the
625 * number of completed data bytes correspond to and advances the
626 * integrity vector accordingly.
628 void bio_integrity_advance(struct bio
*bio
, unsigned int bytes_done
)
630 struct bio_integrity_payload
*bip
= bio
->bi_integrity
;
631 struct blk_integrity
*bi
= bdev_get_integrity(bio
->bi_bdev
);
632 unsigned int nr_sectors
;
637 nr_sectors
= bio_integrity_hw_sectors(bi
, bytes_done
>> 9);
638 bio_integrity_mark_head(bip
, nr_sectors
* bi
->tuple_size
);
640 EXPORT_SYMBOL(bio_integrity_advance
);
643 * bio_integrity_trim - Trim integrity vector
644 * @bio: bio whose integrity vector to update
645 * @offset: offset to first data sector
646 * @sectors: number of data sectors
648 * Description: Used to trim the integrity vector in a cloned bio.
649 * The ivec will be advanced corresponding to 'offset' data sectors
650 * and the length will be truncated corresponding to 'len' data
653 void bio_integrity_trim(struct bio
*bio
, unsigned int offset
,
654 unsigned int sectors
)
656 struct bio_integrity_payload
*bip
= bio
->bi_integrity
;
657 struct blk_integrity
*bi
= bdev_get_integrity(bio
->bi_bdev
);
658 unsigned int nr_sectors
;
662 BUG_ON(!bio_flagged(bio
, BIO_CLONED
));
664 nr_sectors
= bio_integrity_hw_sectors(bi
, sectors
);
665 bip
->bip_sector
= bip
->bip_sector
+ offset
;
666 bio_integrity_mark_head(bip
, offset
* bi
->tuple_size
);
667 bio_integrity_mark_tail(bip
, sectors
* bi
->tuple_size
);
669 EXPORT_SYMBOL(bio_integrity_trim
);
672 * bio_integrity_split - Split integrity metadata
673 * @bio: Protected bio
674 * @bp: Resulting bio_pair
677 * Description: Splits an integrity page into a bio_pair.
679 void bio_integrity_split(struct bio
*bio
, struct bio_pair
*bp
, int sectors
)
681 struct blk_integrity
*bi
;
682 struct bio_integrity_payload
*bip
= bio
->bi_integrity
;
683 unsigned int nr_sectors
;
685 if (bio_integrity(bio
) == 0)
688 bi
= bdev_get_integrity(bio
->bi_bdev
);
690 BUG_ON(bip
->bip_vcnt
!= 1);
692 nr_sectors
= bio_integrity_hw_sectors(bi
, sectors
);
694 bp
->bio1
.bi_integrity
= &bp
->bip1
;
695 bp
->bio2
.bi_integrity
= &bp
->bip2
;
697 bp
->iv1
= bip
->bip_vec
[0];
698 bp
->iv2
= bip
->bip_vec
[0];
700 bp
->bip1
.bip_vec
[0] = bp
->iv1
;
701 bp
->bip2
.bip_vec
[0] = bp
->iv2
;
703 bp
->iv1
.bv_len
= sectors
* bi
->tuple_size
;
704 bp
->iv2
.bv_offset
+= sectors
* bi
->tuple_size
;
705 bp
->iv2
.bv_len
-= sectors
* bi
->tuple_size
;
707 bp
->bip1
.bip_sector
= bio
->bi_integrity
->bip_sector
;
708 bp
->bip2
.bip_sector
= bio
->bi_integrity
->bip_sector
+ nr_sectors
;
710 bp
->bip1
.bip_vcnt
= bp
->bip2
.bip_vcnt
= 1;
711 bp
->bip1
.bip_idx
= bp
->bip2
.bip_idx
= 0;
713 EXPORT_SYMBOL(bio_integrity_split
);
716 * bio_integrity_clone - Callback for cloning bios with integrity metadata
718 * @bio_src: Original bio
719 * @gfp_mask: Memory allocation mask
721 * Description: Called to allocate a bip when cloning a bio
723 int bio_integrity_clone(struct bio
*bio
, struct bio
*bio_src
,
726 struct bio_integrity_payload
*bip_src
= bio_src
->bi_integrity
;
727 struct bio_integrity_payload
*bip
;
729 BUG_ON(bip_src
== NULL
);
731 bip
= bio_integrity_alloc(bio
, gfp_mask
, bip_src
->bip_vcnt
);
736 memcpy(bip
->bip_vec
, bip_src
->bip_vec
,
737 bip_src
->bip_vcnt
* sizeof(struct bio_vec
));
739 bip
->bip_sector
= bip_src
->bip_sector
;
740 bip
->bip_vcnt
= bip_src
->bip_vcnt
;
741 bip
->bip_idx
= bip_src
->bip_idx
;
745 EXPORT_SYMBOL(bio_integrity_clone
);
747 int bioset_integrity_create(struct bio_set
*bs
, int pool_size
)
749 unsigned int max_slab
= vecs_to_idx(BIO_MAX_PAGES
);
751 if (bs
->bio_integrity_pool
)
754 bs
->bio_integrity_pool
=
755 mempool_create_slab_pool(pool_size
, bip_slab
[max_slab
].slab
);
757 if (!bs
->bio_integrity_pool
)
762 EXPORT_SYMBOL(bioset_integrity_create
);
764 void bioset_integrity_free(struct bio_set
*bs
)
766 if (bs
->bio_integrity_pool
)
767 mempool_destroy(bs
->bio_integrity_pool
);
769 EXPORT_SYMBOL(bioset_integrity_free
);
771 void __init
bio_integrity_init(void)
776 * kintegrityd won't block much but may burn a lot of CPU cycles.
777 * Make it highpri CPU intensive wq with max concurrency of 1.
779 kintegrityd_wq
= alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM
|
780 WQ_HIGHPRI
| WQ_CPU_INTENSIVE
, 1);
782 panic("Failed to create kintegrityd\n");
784 for (i
= 0 ; i
< BIOVEC_NR_POOLS
; i
++) {
787 size
= sizeof(struct bio_integrity_payload
)
788 + bip_slab
[i
].nr_vecs
* sizeof(struct bio_vec
);
791 kmem_cache_create(bip_slab
[i
].name
, size
, 0,
792 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);