0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/blk-integrity.h>
0010 #include <linux/mempool.h>
0011 #include <linux/export.h>
0012 #include <linux/bio.h>
0013 #include <linux/workqueue.h>
0014 #include <linux/slab.h>
0015 #include "blk.h"
0016
0017 static struct kmem_cache *bip_slab;
0018 static struct workqueue_struct *kintegrityd_wq;
0019
0020 void blk_flush_integrity(void)
0021 {
0022 flush_workqueue(kintegrityd_wq);
0023 }
0024
0025 static void __bio_integrity_free(struct bio_set *bs,
0026 struct bio_integrity_payload *bip)
0027 {
0028 if (bs && mempool_initialized(&bs->bio_integrity_pool)) {
0029 if (bip->bip_vec)
0030 bvec_free(&bs->bvec_integrity_pool, bip->bip_vec,
0031 bip->bip_max_vcnt);
0032 mempool_free(bip, &bs->bio_integrity_pool);
0033 } else {
0034 kfree(bip);
0035 }
0036 }
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
0049 gfp_t gfp_mask,
0050 unsigned int nr_vecs)
0051 {
0052 struct bio_integrity_payload *bip;
0053 struct bio_set *bs = bio->bi_pool;
0054 unsigned inline_vecs;
0055
0056 if (WARN_ON_ONCE(bio_has_crypt_ctx(bio)))
0057 return ERR_PTR(-EOPNOTSUPP);
0058
0059 if (!bs || !mempool_initialized(&bs->bio_integrity_pool)) {
0060 bip = kmalloc(struct_size(bip, bip_inline_vecs, nr_vecs), gfp_mask);
0061 inline_vecs = nr_vecs;
0062 } else {
0063 bip = mempool_alloc(&bs->bio_integrity_pool, gfp_mask);
0064 inline_vecs = BIO_INLINE_VECS;
0065 }
0066
0067 if (unlikely(!bip))
0068 return ERR_PTR(-ENOMEM);
0069
0070 memset(bip, 0, sizeof(*bip));
0071
0072 if (nr_vecs > inline_vecs) {
0073 bip->bip_max_vcnt = nr_vecs;
0074 bip->bip_vec = bvec_alloc(&bs->bvec_integrity_pool,
0075 &bip->bip_max_vcnt, gfp_mask);
0076 if (!bip->bip_vec)
0077 goto err;
0078 } else {
0079 bip->bip_vec = bip->bip_inline_vecs;
0080 bip->bip_max_vcnt = inline_vecs;
0081 }
0082
0083 bip->bip_bio = bio;
0084 bio->bi_integrity = bip;
0085 bio->bi_opf |= REQ_INTEGRITY;
0086
0087 return bip;
0088 err:
0089 __bio_integrity_free(bs, bip);
0090 return ERR_PTR(-ENOMEM);
0091 }
0092 EXPORT_SYMBOL(bio_integrity_alloc);
0093
0094
0095
0096
0097
0098
0099
0100
0101 void bio_integrity_free(struct bio *bio)
0102 {
0103 struct bio_integrity_payload *bip = bio_integrity(bio);
0104 struct bio_set *bs = bio->bi_pool;
0105
0106 if (bip->bip_flags & BIP_BLOCK_INTEGRITY)
0107 kfree(bvec_virt(bip->bip_vec));
0108
0109 __bio_integrity_free(bs, bip);
0110 bio->bi_integrity = NULL;
0111 bio->bi_opf &= ~REQ_INTEGRITY;
0112 }
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123 int bio_integrity_add_page(struct bio *bio, struct page *page,
0124 unsigned int len, unsigned int offset)
0125 {
0126 struct bio_integrity_payload *bip = bio_integrity(bio);
0127 struct bio_vec *iv;
0128
0129 if (bip->bip_vcnt >= bip->bip_max_vcnt) {
0130 printk(KERN_ERR "%s: bip_vec full\n", __func__);
0131 return 0;
0132 }
0133
0134 iv = bip->bip_vec + bip->bip_vcnt;
0135
0136 if (bip->bip_vcnt &&
0137 bvec_gap_to_prev(&bdev_get_queue(bio->bi_bdev)->limits,
0138 &bip->bip_vec[bip->bip_vcnt - 1], offset))
0139 return 0;
0140
0141 iv->bv_page = page;
0142 iv->bv_len = len;
0143 iv->bv_offset = offset;
0144 bip->bip_vcnt++;
0145
0146 return len;
0147 }
0148 EXPORT_SYMBOL(bio_integrity_add_page);
0149
0150
0151
0152
0153
0154
0155
0156 static blk_status_t bio_integrity_process(struct bio *bio,
0157 struct bvec_iter *proc_iter, integrity_processing_fn *proc_fn)
0158 {
0159 struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0160 struct blk_integrity_iter iter;
0161 struct bvec_iter bviter;
0162 struct bio_vec bv;
0163 struct bio_integrity_payload *bip = bio_integrity(bio);
0164 blk_status_t ret = BLK_STS_OK;
0165
0166 iter.disk_name = bio->bi_bdev->bd_disk->disk_name;
0167 iter.interval = 1 << bi->interval_exp;
0168 iter.tuple_size = bi->tuple_size;
0169 iter.seed = proc_iter->bi_sector;
0170 iter.prot_buf = bvec_virt(bip->bip_vec);
0171
0172 __bio_for_each_segment(bv, bio, bviter, *proc_iter) {
0173 void *kaddr = bvec_kmap_local(&bv);
0174
0175 iter.data_buf = kaddr;
0176 iter.data_size = bv.bv_len;
0177 ret = proc_fn(&iter);
0178 kunmap_local(kaddr);
0179
0180 if (ret)
0181 break;
0182
0183 }
0184 return ret;
0185 }
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199 bool bio_integrity_prep(struct bio *bio)
0200 {
0201 struct bio_integrity_payload *bip;
0202 struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0203 void *buf;
0204 unsigned long start, end;
0205 unsigned int len, nr_pages;
0206 unsigned int bytes, offset, i;
0207 unsigned int intervals;
0208 blk_status_t status;
0209
0210 if (!bi)
0211 return true;
0212
0213 if (bio_op(bio) != REQ_OP_READ && bio_op(bio) != REQ_OP_WRITE)
0214 return true;
0215
0216 if (!bio_sectors(bio))
0217 return true;
0218
0219
0220 if (bio_integrity(bio))
0221 return true;
0222
0223 if (bio_data_dir(bio) == READ) {
0224 if (!bi->profile->verify_fn ||
0225 !(bi->flags & BLK_INTEGRITY_VERIFY))
0226 return true;
0227 } else {
0228 if (!bi->profile->generate_fn ||
0229 !(bi->flags & BLK_INTEGRITY_GENERATE))
0230 return true;
0231 }
0232 intervals = bio_integrity_intervals(bi, bio_sectors(bio));
0233
0234
0235 len = intervals * bi->tuple_size;
0236 buf = kmalloc(len, GFP_NOIO);
0237 status = BLK_STS_RESOURCE;
0238 if (unlikely(buf == NULL)) {
0239 printk(KERN_ERR "could not allocate integrity buffer\n");
0240 goto err_end_io;
0241 }
0242
0243 end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
0244 start = ((unsigned long) buf) >> PAGE_SHIFT;
0245 nr_pages = end - start;
0246
0247
0248 bip = bio_integrity_alloc(bio, GFP_NOIO, nr_pages);
0249 if (IS_ERR(bip)) {
0250 printk(KERN_ERR "could not allocate data integrity bioset\n");
0251 kfree(buf);
0252 status = BLK_STS_RESOURCE;
0253 goto err_end_io;
0254 }
0255
0256 bip->bip_flags |= BIP_BLOCK_INTEGRITY;
0257 bip->bip_iter.bi_size = len;
0258 bip_set_seed(bip, bio->bi_iter.bi_sector);
0259
0260 if (bi->flags & BLK_INTEGRITY_IP_CHECKSUM)
0261 bip->bip_flags |= BIP_IP_CHECKSUM;
0262
0263
0264 offset = offset_in_page(buf);
0265 for (i = 0 ; i < nr_pages ; i++) {
0266 int ret;
0267 bytes = PAGE_SIZE - offset;
0268
0269 if (len <= 0)
0270 break;
0271
0272 if (bytes > len)
0273 bytes = len;
0274
0275 ret = bio_integrity_add_page(bio, virt_to_page(buf),
0276 bytes, offset);
0277
0278 if (ret == 0) {
0279 printk(KERN_ERR "could not attach integrity payload\n");
0280 status = BLK_STS_RESOURCE;
0281 goto err_end_io;
0282 }
0283
0284 if (ret < bytes)
0285 break;
0286
0287 buf += bytes;
0288 len -= bytes;
0289 offset = 0;
0290 }
0291
0292
0293 if (bio_data_dir(bio) == WRITE) {
0294 bio_integrity_process(bio, &bio->bi_iter,
0295 bi->profile->generate_fn);
0296 } else {
0297 bip->bio_iter = bio->bi_iter;
0298 }
0299 return true;
0300
0301 err_end_io:
0302 bio->bi_status = status;
0303 bio_endio(bio);
0304 return false;
0305
0306 }
0307 EXPORT_SYMBOL(bio_integrity_prep);
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317 static void bio_integrity_verify_fn(struct work_struct *work)
0318 {
0319 struct bio_integrity_payload *bip =
0320 container_of(work, struct bio_integrity_payload, bip_work);
0321 struct bio *bio = bip->bip_bio;
0322 struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0323
0324
0325
0326
0327
0328
0329 bio->bi_status = bio_integrity_process(bio, &bip->bio_iter,
0330 bi->profile->verify_fn);
0331 bio_integrity_free(bio);
0332 bio_endio(bio);
0333 }
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346 bool __bio_integrity_endio(struct bio *bio)
0347 {
0348 struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0349 struct bio_integrity_payload *bip = bio_integrity(bio);
0350
0351 if (bio_op(bio) == REQ_OP_READ && !bio->bi_status &&
0352 (bip->bip_flags & BIP_BLOCK_INTEGRITY) && bi->profile->verify_fn) {
0353 INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
0354 queue_work(kintegrityd_wq, &bip->bip_work);
0355 return false;
0356 }
0357
0358 bio_integrity_free(bio);
0359 return true;
0360 }
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371 void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
0372 {
0373 struct bio_integrity_payload *bip = bio_integrity(bio);
0374 struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0375 unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
0376
0377 bip->bip_iter.bi_sector += bio_integrity_intervals(bi, bytes_done >> 9);
0378 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
0379 }
0380
0381
0382
0383
0384
0385
0386
0387 void bio_integrity_trim(struct bio *bio)
0388 {
0389 struct bio_integrity_payload *bip = bio_integrity(bio);
0390 struct blk_integrity *bi = blk_get_integrity(bio->bi_bdev->bd_disk);
0391
0392 bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
0393 }
0394 EXPORT_SYMBOL(bio_integrity_trim);
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404 int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
0405 gfp_t gfp_mask)
0406 {
0407 struct bio_integrity_payload *bip_src = bio_integrity(bio_src);
0408 struct bio_integrity_payload *bip;
0409
0410 BUG_ON(bip_src == NULL);
0411
0412 bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt);
0413 if (IS_ERR(bip))
0414 return PTR_ERR(bip);
0415
0416 memcpy(bip->bip_vec, bip_src->bip_vec,
0417 bip_src->bip_vcnt * sizeof(struct bio_vec));
0418
0419 bip->bip_vcnt = bip_src->bip_vcnt;
0420 bip->bip_iter = bip_src->bip_iter;
0421
0422 return 0;
0423 }
0424
0425 int bioset_integrity_create(struct bio_set *bs, int pool_size)
0426 {
0427 if (mempool_initialized(&bs->bio_integrity_pool))
0428 return 0;
0429
0430 if (mempool_init_slab_pool(&bs->bio_integrity_pool,
0431 pool_size, bip_slab))
0432 return -1;
0433
0434 if (biovec_init_pool(&bs->bvec_integrity_pool, pool_size)) {
0435 mempool_exit(&bs->bio_integrity_pool);
0436 return -1;
0437 }
0438
0439 return 0;
0440 }
0441 EXPORT_SYMBOL(bioset_integrity_create);
0442
0443 void bioset_integrity_free(struct bio_set *bs)
0444 {
0445 mempool_exit(&bs->bio_integrity_pool);
0446 mempool_exit(&bs->bvec_integrity_pool);
0447 }
0448
0449 void __init bio_integrity_init(void)
0450 {
0451
0452
0453
0454
0455 kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
0456 WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
0457 if (!kintegrityd_wq)
0458 panic("Failed to create kintegrityd\n");
0459
0460 bip_slab = kmem_cache_create("bio_integrity_payload",
0461 sizeof(struct bio_integrity_payload) +
0462 sizeof(struct bio_vec) * BIO_INLINE_VECS,
0463 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
0464 }