25 #include <linux/export.h>
28 #include <linux/slab.h>
36 #define IS(x) { .nr_vecs = x, .name = "bip-"__stringify(x) }
38 IS(1),
IS(4),
IS(16),
IS(64),
IS(128),
IS(BIO_MAX_PAGES),
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)
86 struct bio_integrity_payload *bip;
87 unsigned int idx = vecs_to_idx(nr_vecs);
88 struct bio_set *bs = bio->bi_pool;
97 if (!use_bip_pool(idx))
102 idx = BIOVEC_MAX_IDX;
111 memset(bip, 0,
sizeof(*bip));
115 bio->bi_integrity = bip;
130 struct bio_integrity_payload *bip = bio->bi_integrity;
131 struct bio_set *bs = bio->bi_pool;
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))
148 bio->bi_integrity =
NULL;
162 unsigned int len,
unsigned int offset)
164 struct bio_integrity_payload *bip = bio->bi_integrity;
172 iv = bip_vec_idx(bip, bip->bip_vcnt);
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))
214 if (bio_integrity(bio))
217 return bdev_integrity_enabled(bio->bi_bdev, bio_data_dir(bio));
231 static inline unsigned int bio_integrity_hw_sectors(
struct blk_integrity *bi,
235 if (bi->sector_size == 4096)
236 return sectors >>= 3;
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);
261 struct bio_integrity_payload *bip = bio->bi_integrity;
262 struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
267 if (bi->tag_size == 0)
270 nr_sectors = bio_integrity_hw_sectors(bi,
273 if (nr_sectors * bi->tuple_size > bip->bip_size) {
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);
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;
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) {
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);
365 static inline unsigned short blk_integrity_tuple_size(
struct blk_integrity *bi)
368 return bi->tuple_size;
386 struct bio_integrity_payload *bip;
387 struct blk_integrity *
bi;
391 unsigned int len, nr_pages;
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));
403 len = sectors * blk_integrity_tuple_size(bi);
412 nr_pages = end -
start;
424 bip->bip_sector = bio->bi_sector;
428 for (i = 0 ; i < nr_pages ; i++) {
453 if (bio_data_dir(bio) ==
READ) {
454 bip->bip_end_io = bio->bi_end_io;
459 if (bio_data_dir(bio) ==
WRITE)
460 bio_integrity_generate(bio);
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;
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) {
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);
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);
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);
531 bio->bi_end_io = bip->bip_end_io;
549 struct bio_integrity_payload *bip = bio->bi_integrity;
551 BUG_ON(bip->bip_bio != bio);
558 bio->bi_end_io = bip->bip_end_io;
564 INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
580 bip_for_each_vec(iv, bip, i) {
584 }
else if (skip >= iv->bv_len) {
587 iv->bv_offset +=
skip;
606 bip_for_each_vec(iv, bip, i) {
610 }
else if (len >= iv->bv_len) {
630 struct bio_integrity_payload *bip = bio->bi_integrity;
631 struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
637 nr_sectors = bio_integrity_hw_sectors(bi, bytes_done >> 9);
654 unsigned int sectors)
656 struct bio_integrity_payload *bip = bio->bi_integrity;
657 struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
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;
681 struct blk_integrity *
bi;
682 struct bio_integrity_payload *bip = bio->bi_integrity;
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;
726 struct bio_integrity_payload *bip_src = bio_src->bi_integrity;
727 struct bio_integrity_payload *bip;
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;
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)
766 if (bs->bio_integrity_pool)
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);