^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * bio-integrity.c - bio data integrity extensions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2007, 2008, 2009 Oracle Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Written by: Martin K. Petersen <martin.petersen@oracle.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/mempool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "blk.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define BIP_INLINE_VECS 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static struct kmem_cache *bip_slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static struct workqueue_struct *kintegrityd_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) void blk_flush_integrity(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) flush_workqueue(kintegrityd_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static void __bio_integrity_free(struct bio_set *bs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct bio_integrity_payload *bip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (bs && mempool_initialized(&bs->bio_integrity_pool)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (bip->bip_vec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) bvec_free(&bs->bvec_integrity_pool, bip->bip_vec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) bip->bip_slab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) mempool_free(bip, &bs->bio_integrity_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) kfree(bip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * bio_integrity_alloc - Allocate integrity payload and attach it to bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * @bio: bio to attach integrity metadata to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * @gfp_mask: Memory allocation mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * @nr_vecs: Number of integrity metadata scatter-gather elements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Description: This function prepares a bio for attaching integrity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * metadata. nr_vecs specifies the maximum number of pages containing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * integrity metadata that can be attached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) gfp_t gfp_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int nr_vecs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct bio_integrity_payload *bip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct bio_set *bs = bio->bi_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned inline_vecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return ERR_PTR(-EOPNOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!bs || !mempool_initialized(&bs->bio_integrity_pool)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) bip = kmalloc(struct_size(bip, bip_inline_vecs, nr_vecs), gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) inline_vecs = nr_vecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) bip = mempool_alloc(&bs->bio_integrity_pool, gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) inline_vecs = BIP_INLINE_VECS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (unlikely(!bip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) memset(bip, 0, sizeof(*bip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (nr_vecs > inline_vecs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned long idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) bip->bip_vec = bvec_alloc(gfp_mask, nr_vecs, &idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) &bs->bvec_integrity_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (!bip->bip_vec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) bip->bip_max_vcnt = bvec_nr_vecs(idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) bip->bip_slab = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) bip->bip_vec = bip->bip_inline_vecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) bip->bip_max_vcnt = inline_vecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) bip->bip_bio = bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) bio->bi_integrity = bip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) bio->bi_opf |= REQ_INTEGRITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return bip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) __bio_integrity_free(bs, bip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) EXPORT_SYMBOL(bio_integrity_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * bio_integrity_free - Free bio integrity payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * @bio: bio containing bip to be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * Description: Used to free the integrity portion of a bio. Usually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * called from bio_free().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) void bio_integrity_free(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct bio_integrity_payload *bip = bio_integrity(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct bio_set *bs = bio->bi_pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (bip->bip_flags & BIP_BLOCK_INTEGRITY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) kfree(page_address(bip->bip_vec->bv_page) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) bip->bip_vec->bv_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) __bio_integrity_free(bs, bip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) bio->bi_integrity = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) bio->bi_opf &= ~REQ_INTEGRITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * bio_integrity_add_page - Attach integrity metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * @bio: bio to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * @page: page containing integrity metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * @len: number of bytes of integrity metadata in page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * @offset: start offset within page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * Description: Attach a page containing integrity metadata to bio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int bio_integrity_add_page(struct bio *bio, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) unsigned int len, unsigned int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct bio_integrity_payload *bip = bio_integrity(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct bio_vec *iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (bip->bip_vcnt >= bip->bip_max_vcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) printk(KERN_ERR "%s: bip_vec full\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) iv = bip->bip_vec + bip->bip_vcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (bip->bip_vcnt &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) bvec_gap_to_prev(bio->bi_disk->queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) &bip->bip_vec[bip->bip_vcnt - 1], offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) iv->bv_page = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) iv->bv_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) iv->bv_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) bip->bip_vcnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) EXPORT_SYMBOL(bio_integrity_add_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * bio_integrity_process - Process integrity metadata for a bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * @bio: bio to generate/verify integrity metadata for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * @proc_iter: iterator to process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * @proc_fn: Pointer to the relevant processing function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static blk_status_t bio_integrity_process(struct bio *bio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct bvec_iter *proc_iter, integrity_processing_fn *proc_fn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct blk_integrity_iter iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct bvec_iter bviter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct bio_vec bv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct bio_integrity_payload *bip = bio_integrity(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) blk_status_t ret = BLK_STS_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) void *prot_buf = page_address(bip->bip_vec->bv_page) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) bip->bip_vec->bv_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) iter.disk_name = bio->bi_disk->disk_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) iter.interval = 1 << bi->interval_exp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) iter.seed = proc_iter->bi_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) iter.prot_buf = prot_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) __bio_for_each_segment(bv, bio, bviter, *proc_iter) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) void *kaddr = kmap_atomic(bv.bv_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) iter.data_buf = kaddr + bv.bv_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) iter.data_size = bv.bv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ret = proc_fn(&iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) kunmap_atomic(kaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * bio_integrity_prep - Prepare bio for integrity I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * @bio: bio to prepare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * Description: Checks if the bio already has an integrity payload attached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * If it does, the payload has been generated by another kernel subsystem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * and we just pass it through. Otherwise allocates integrity payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * The bio must have data direction, target device and start sector set priot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * to calling. In the WRITE case, integrity metadata will be generated using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * the block device's integrity function. In the READ case, the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * will be prepared for DMA and a suitable end_io handler set up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) bool bio_integrity_prep(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct bio_integrity_payload *bip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct request_queue *q = bio->bi_disk->queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) unsigned long start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) unsigned int len, nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) unsigned int bytes, offset, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) unsigned int intervals;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) blk_status_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (!bi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!bio_sectors(bio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /* Already protected? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (bio_integrity(bio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (bio_data_dir(bio) == READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (!bi->profile->verify_fn ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) !(bi->flags & BLK_INTEGRITY_VERIFY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!bi->profile->generate_fn ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) !(bi->flags & BLK_INTEGRITY_GENERATE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) intervals = bio_integrity_intervals(bi, bio_sectors(bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /* Allocate kernel buffer for protection data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) len = intervals * bi->tuple_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) buf = kmalloc(len, GFP_NOIO | q->bounce_gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) status = BLK_STS_RESOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (unlikely(buf == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) printk(KERN_ERR "could not allocate integrity buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) goto err_end_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) start = ((unsigned long) buf) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) nr_pages = end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* Allocate bio integrity payload and integrity vectors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) bip = bio_integrity_alloc(bio, GFP_NOIO, nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (IS_ERR(bip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) printk(KERN_ERR "could not allocate data integrity bioset\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) status = BLK_STS_RESOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) goto err_end_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) bip->bip_flags |= BIP_BLOCK_INTEGRITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) bip->bip_iter.bi_size = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) bip_set_seed(bip, bio->bi_iter.bi_sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (bi->flags & BLK_INTEGRITY_IP_CHECKSUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) bip->bip_flags |= BIP_IP_CHECKSUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* Map it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) offset = offset_in_page(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) for (i = 0 ; i < nr_pages ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) bytes = PAGE_SIZE - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (len <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (bytes > len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) bytes = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ret = bio_integrity_add_page(bio, virt_to_page(buf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) bytes, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) printk(KERN_ERR "could not attach integrity payload\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) status = BLK_STS_RESOURCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) goto err_end_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (ret < bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) buf += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) len -= bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /* Auto-generate integrity metadata if this is a write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (bio_data_dir(bio) == WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) bio_integrity_process(bio, &bio->bi_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) bi->profile->generate_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) bip->bio_iter = bio->bi_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) err_end_io:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) bio->bi_status = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) bio_endio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) EXPORT_SYMBOL(bio_integrity_prep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * bio_integrity_verify_fn - Integrity I/O completion worker
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * @work: Work struct stored in bio to be verified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * Description: This workqueue function is called to complete a READ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * request. The function verifies the transferred integrity metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * and then calls the original bio end_io function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static void bio_integrity_verify_fn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct bio_integrity_payload *bip =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) container_of(work, struct bio_integrity_payload, bip_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct bio *bio = bip->bip_bio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * At the moment verify is called bio's iterator was advanced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * during split and completion, we need to rewind iterator to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * it's original position.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) bio->bi_status = bio_integrity_process(bio, &bip->bio_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) bi->profile->verify_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) bio_integrity_free(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) bio_endio(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * __bio_integrity_endio - Integrity I/O completion function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * @bio: Protected bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * Description: Completion for integrity I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * Normally I/O completion is done in interrupt context. However,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * verifying I/O integrity is a time-consuming task which must be run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * in process context. This function postpones completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) bool __bio_integrity_endio(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct bio_integrity_payload *bip = bio_integrity(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (bio_op(bio) == REQ_OP_READ && !bio->bi_status &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) (bip->bip_flags & BIP_BLOCK_INTEGRITY) && bi->profile->verify_fn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) queue_work(kintegrityd_wq, &bip->bip_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) bio_integrity_free(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * bio_integrity_advance - Advance integrity vector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * @bio: bio whose integrity vector to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * @bytes_done: number of data bytes that have been completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * Description: This function calculates how many integrity bytes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * number of completed data bytes correspond to and advances the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * integrity vector accordingly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct bio_integrity_payload *bip = bio_integrity(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) bip->bip_iter.bi_sector += bio_integrity_intervals(bi, bytes_done >> 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * bio_integrity_trim - Trim integrity vector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * @bio: bio whose integrity vector to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * Description: Used to trim the integrity vector in a cloned bio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) void bio_integrity_trim(struct bio *bio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct bio_integrity_payload *bip = bio_integrity(bio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) struct blk_integrity *bi = blk_get_integrity(bio->bi_disk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) EXPORT_SYMBOL(bio_integrity_trim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * bio_integrity_clone - Callback for cloning bios with integrity metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * @bio: New bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * @bio_src: Original bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * @gfp_mask: Memory allocation mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * Description: Called to allocate a bip when cloning a bio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct bio_integrity_payload *bip_src = bio_integrity(bio_src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) struct bio_integrity_payload *bip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) BUG_ON(bip_src == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (IS_ERR(bip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return PTR_ERR(bip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) memcpy(bip->bip_vec, bip_src->bip_vec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) bip_src->bip_vcnt * sizeof(struct bio_vec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) bip->bip_vcnt = bip_src->bip_vcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) bip->bip_iter = bip_src->bip_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) EXPORT_SYMBOL(bio_integrity_clone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) int bioset_integrity_create(struct bio_set *bs, int pool_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (mempool_initialized(&bs->bio_integrity_pool))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (mempool_init_slab_pool(&bs->bio_integrity_pool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) pool_size, bip_slab))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (biovec_init_pool(&bs->bvec_integrity_pool, pool_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) mempool_exit(&bs->bio_integrity_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) EXPORT_SYMBOL(bioset_integrity_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) void bioset_integrity_free(struct bio_set *bs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) mempool_exit(&bs->bio_integrity_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) mempool_exit(&bs->bvec_integrity_pool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) void __init bio_integrity_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * kintegrityd won't block much but may burn a lot of CPU cycles.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * Make it highpri CPU intensive wq with max concurrency of 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (!kintegrityd_wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) panic("Failed to create kintegrityd\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) bip_slab = kmem_cache_create("bio_integrity_payload",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) sizeof(struct bio_integrity_payload) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) sizeof(struct bio_vec) * BIP_INLINE_VECS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }