bio-integrity.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * bio-integrity.c - bio data integrity extensions
  3. *
  4. * Copyright (C) 2007, 2008, 2009 Oracle Corporation
  5. * Written by: Martin K. Petersen <martin.petersen@oracle.com>
  6. *
  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.
  10. *
  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.
  15. *
  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,
  19. * USA.
  20. *
  21. */
  22. #include <linux/blkdev.h>
  23. #include <linux/mempool.h>
  24. #include <linux/export.h>
  25. #include <linux/bio.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/slab.h>
  28. #define BIP_INLINE_VECS 4
  29. static struct kmem_cache *bip_slab;
  30. static struct workqueue_struct *kintegrityd_wq;
  31. /**
  32. * bio_integrity_alloc - Allocate integrity payload and attach it to bio
  33. * @bio: bio to attach integrity metadata to
  34. * @gfp_mask: Memory allocation mask
  35. * @nr_vecs: Number of integrity metadata scatter-gather elements
  36. *
  37. * Description: This function prepares a bio for attaching integrity
  38. * metadata. nr_vecs specifies the maximum number of pages containing
  39. * integrity metadata that can be attached.
  40. */
  41. struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
  42. gfp_t gfp_mask,
  43. unsigned int nr_vecs)
  44. {
  45. struct bio_integrity_payload *bip;
  46. struct bio_set *bs = bio->bi_pool;
  47. unsigned long idx = BIO_POOL_NONE;
  48. unsigned inline_vecs;
  49. if (!bs) {
  50. bip = kmalloc(sizeof(struct bio_integrity_payload) +
  51. sizeof(struct bio_vec) * nr_vecs, gfp_mask);
  52. inline_vecs = nr_vecs;
  53. } else {
  54. bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask);
  55. inline_vecs = BIP_INLINE_VECS;
  56. }
  57. if (unlikely(!bip))
  58. return NULL;
  59. memset(bip, 0, sizeof(*bip));
  60. if (nr_vecs > inline_vecs) {
  61. bip->bip_vec = bvec_alloc(gfp_mask, nr_vecs, &idx,
  62. bs->bvec_integrity_pool);
  63. if (!bip->bip_vec)
  64. goto err;
  65. } else {
  66. bip->bip_vec = bip->bip_inline_vecs;
  67. }
  68. bip->bip_slab = idx;
  69. bip->bip_bio = bio;
  70. bio->bi_integrity = bip;
  71. return bip;
  72. err:
  73. mempool_free(bip, bs->bio_integrity_pool);
  74. return NULL;
  75. }
  76. EXPORT_SYMBOL(bio_integrity_alloc);
  77. /**
  78. * bio_integrity_free - Free bio integrity payload
  79. * @bio: bio containing bip to be freed
  80. *
  81. * Description: Used to free the integrity portion of a bio. Usually
  82. * called from bio_free().
  83. */
  84. void bio_integrity_free(struct bio *bio)
  85. {
  86. struct bio_integrity_payload *bip = bio->bi_integrity;
  87. struct bio_set *bs = bio->bi_pool;
  88. if (bip->bip_owns_buf)
  89. kfree(bip->bip_buf);
  90. if (bs) {
  91. if (bip->bip_slab != BIO_POOL_NONE)
  92. bvec_free(bs->bvec_integrity_pool, bip->bip_vec,
  93. bip->bip_slab);
  94. mempool_free(bip, bs->bio_integrity_pool);
  95. } else {
  96. kfree(bip);
  97. }
  98. bio->bi_integrity = NULL;
  99. }
  100. EXPORT_SYMBOL(bio_integrity_free);
  101. static inline unsigned int bip_integrity_vecs(struct bio_integrity_payload *bip)
  102. {
  103. if (bip->bip_slab == BIO_POOL_NONE)
  104. return BIP_INLINE_VECS;
  105. return bvec_nr_vecs(bip->bip_slab);
  106. }
  107. /**
  108. * bio_integrity_add_page - Attach integrity metadata
  109. * @bio: bio to update
  110. * @page: page containing integrity metadata
  111. * @len: number of bytes of integrity metadata in page
  112. * @offset: start offset within page
  113. *
  114. * Description: Attach a page containing integrity metadata to bio.
  115. */
  116. int bio_integrity_add_page(struct bio *bio, struct page *page,
  117. unsigned int len, unsigned int offset)
  118. {
  119. struct bio_integrity_payload *bip = bio->bi_integrity;
  120. struct bio_vec *iv;
  121. if (bip->bip_vcnt >= bip_integrity_vecs(bip)) {
  122. printk(KERN_ERR "%s: bip_vec full\n", __func__);
  123. return 0;
  124. }
  125. iv = bip->bip_vec + bip->bip_vcnt;
  126. iv->bv_page = page;
  127. iv->bv_len = len;
  128. iv->bv_offset = offset;
  129. bip->bip_vcnt++;
  130. return len;
  131. }
  132. EXPORT_SYMBOL(bio_integrity_add_page);
  133. static int bdev_integrity_enabled(struct block_device *bdev, int rw)
  134. {
  135. struct blk_integrity *bi = bdev_get_integrity(bdev);
  136. if (bi == NULL)
  137. return 0;
  138. if (rw == READ && bi->verify_fn != NULL &&
  139. (bi->flags & INTEGRITY_FLAG_READ))
  140. return 1;
  141. if (rw == WRITE && bi->generate_fn != NULL &&
  142. (bi->flags & INTEGRITY_FLAG_WRITE))
  143. return 1;
  144. return 0;
  145. }
  146. /**
  147. * bio_integrity_enabled - Check whether integrity can be passed
  148. * @bio: bio to check
  149. *
  150. * Description: Determines whether bio_integrity_prep() can be called
  151. * on this bio or not. bio data direction and target device must be
  152. * set prior to calling. The functions honors the write_generate and
  153. * read_verify flags in sysfs.
  154. */
  155. int bio_integrity_enabled(struct bio *bio)
  156. {
  157. /* Already protected? */
  158. if (bio_integrity(bio))
  159. return 0;
  160. return bdev_integrity_enabled(bio->bi_bdev, bio_data_dir(bio));
  161. }
  162. EXPORT_SYMBOL(bio_integrity_enabled);
  163. /**
  164. * bio_integrity_hw_sectors - Convert 512b sectors to hardware ditto
  165. * @bi: blk_integrity profile for device
  166. * @sectors: Number of 512 sectors to convert
  167. *
  168. * Description: The block layer calculates everything in 512 byte
  169. * sectors but integrity metadata is done in terms of the hardware
  170. * sector size of the storage device. Convert the block layer sectors
  171. * to physical sectors.
  172. */
  173. static inline unsigned int bio_integrity_hw_sectors(struct blk_integrity *bi,
  174. unsigned int sectors)
  175. {
  176. /* At this point there are only 512b or 4096b DIF/EPP devices */
  177. if (bi->sector_size == 4096)
  178. return sectors >>= 3;
  179. return sectors;
  180. }
  181. static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
  182. unsigned int sectors)
  183. {
  184. return bio_integrity_hw_sectors(bi, sectors) * bi->tuple_size;
  185. }
  186. /**
  187. * bio_integrity_tag_size - Retrieve integrity tag space
  188. * @bio: bio to inspect
  189. *
  190. * Description: Returns the maximum number of tag bytes that can be
  191. * attached to this bio. Filesystems can use this to determine how
  192. * much metadata to attach to an I/O.
  193. */
  194. unsigned int bio_integrity_tag_size(struct bio *bio)
  195. {
  196. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  197. BUG_ON(bio->bi_iter.bi_size == 0);
  198. return bi->tag_size * (bio->bi_iter.bi_size / bi->sector_size);
  199. }
  200. EXPORT_SYMBOL(bio_integrity_tag_size);
  201. static int bio_integrity_tag(struct bio *bio, void *tag_buf, unsigned int len,
  202. int set)
  203. {
  204. struct bio_integrity_payload *bip = bio->bi_integrity;
  205. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  206. unsigned int nr_sectors;
  207. BUG_ON(bip->bip_buf == NULL);
  208. if (bi->tag_size == 0)
  209. return -1;
  210. nr_sectors = bio_integrity_hw_sectors(bi,
  211. DIV_ROUND_UP(len, bi->tag_size));
  212. if (nr_sectors * bi->tuple_size > bip->bip_iter.bi_size) {
  213. printk(KERN_ERR "%s: tag too big for bio: %u > %u\n", __func__,
  214. nr_sectors * bi->tuple_size, bip->bip_iter.bi_size);
  215. return -1;
  216. }
  217. if (set)
  218. bi->set_tag_fn(bip->bip_buf, tag_buf, nr_sectors);
  219. else
  220. bi->get_tag_fn(bip->bip_buf, tag_buf, nr_sectors);
  221. return 0;
  222. }
  223. /**
  224. * bio_integrity_set_tag - Attach a tag buffer to a bio
  225. * @bio: bio to attach buffer to
  226. * @tag_buf: Pointer to a buffer containing tag data
  227. * @len: Length of the included buffer
  228. *
  229. * Description: Use this function to tag a bio by leveraging the extra
  230. * space provided by devices formatted with integrity protection. The
  231. * size of the integrity buffer must be <= to the size reported by
  232. * bio_integrity_tag_size().
  233. */
  234. int bio_integrity_set_tag(struct bio *bio, void *tag_buf, unsigned int len)
  235. {
  236. BUG_ON(bio_data_dir(bio) != WRITE);
  237. return bio_integrity_tag(bio, tag_buf, len, 1);
  238. }
  239. EXPORT_SYMBOL(bio_integrity_set_tag);
  240. /**
  241. * bio_integrity_get_tag - Retrieve a tag buffer from a bio
  242. * @bio: bio to retrieve buffer from
  243. * @tag_buf: Pointer to a buffer for the tag data
  244. * @len: Length of the target buffer
  245. *
  246. * Description: Use this function to retrieve the tag buffer from a
  247. * completed I/O. The size of the integrity buffer must be <= to the
  248. * size reported by bio_integrity_tag_size().
  249. */
  250. int bio_integrity_get_tag(struct bio *bio, void *tag_buf, unsigned int len)
  251. {
  252. BUG_ON(bio_data_dir(bio) != READ);
  253. return bio_integrity_tag(bio, tag_buf, len, 0);
  254. }
  255. EXPORT_SYMBOL(bio_integrity_get_tag);
  256. /**
  257. * bio_integrity_generate - Generate integrity metadata for a bio
  258. * @bio: bio to generate integrity metadata for
  259. *
  260. * Description: Generates integrity metadata for a bio by calling the
  261. * block device's generation callback function. The bio must have a
  262. * bip attached with enough room to accommodate the generated
  263. * integrity metadata.
  264. */
  265. static void bio_integrity_generate(struct bio *bio)
  266. {
  267. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  268. struct blk_integrity_exchg bix;
  269. struct bio_vec bv;
  270. struct bvec_iter iter;
  271. sector_t sector = bio->bi_iter.bi_sector;
  272. unsigned int sectors, total;
  273. void *prot_buf = bio->bi_integrity->bip_buf;
  274. total = 0;
  275. bix.disk_name = bio->bi_bdev->bd_disk->disk_name;
  276. bix.sector_size = bi->sector_size;
  277. bio_for_each_segment(bv, bio, iter) {
  278. void *kaddr = kmap_atomic(bv.bv_page);
  279. bix.data_buf = kaddr + bv.bv_offset;
  280. bix.data_size = bv.bv_len;
  281. bix.prot_buf = prot_buf;
  282. bix.sector = sector;
  283. bi->generate_fn(&bix);
  284. sectors = bv.bv_len / bi->sector_size;
  285. sector += sectors;
  286. prot_buf += sectors * bi->tuple_size;
  287. total += sectors * bi->tuple_size;
  288. BUG_ON(total > bio->bi_integrity->bip_iter.bi_size);
  289. kunmap_atomic(kaddr);
  290. }
  291. }
  292. static inline unsigned short blk_integrity_tuple_size(struct blk_integrity *bi)
  293. {
  294. if (bi)
  295. return bi->tuple_size;
  296. return 0;
  297. }
  298. /**
  299. * bio_integrity_prep - Prepare bio for integrity I/O
  300. * @bio: bio to prepare
  301. *
  302. * Description: Allocates a buffer for integrity metadata, maps the
  303. * pages and attaches them to a bio. The bio must have data
  304. * direction, target device and start sector set priot to calling. In
  305. * the WRITE case, integrity metadata will be generated using the
  306. * block device's integrity function. In the READ case, the buffer
  307. * will be prepared for DMA and a suitable end_io handler set up.
  308. */
  309. int bio_integrity_prep(struct bio *bio)
  310. {
  311. struct bio_integrity_payload *bip;
  312. struct blk_integrity *bi;
  313. struct request_queue *q;
  314. void *buf;
  315. unsigned long start, end;
  316. unsigned int len, nr_pages;
  317. unsigned int bytes, offset, i;
  318. unsigned int sectors;
  319. bi = bdev_get_integrity(bio->bi_bdev);
  320. q = bdev_get_queue(bio->bi_bdev);
  321. BUG_ON(bi == NULL);
  322. BUG_ON(bio_integrity(bio));
  323. sectors = bio_integrity_hw_sectors(bi, bio_sectors(bio));
  324. /* Allocate kernel buffer for protection data */
  325. len = sectors * blk_integrity_tuple_size(bi);
  326. buf = kmalloc(len, GFP_NOIO | q->bounce_gfp);
  327. if (unlikely(buf == NULL)) {
  328. printk(KERN_ERR "could not allocate integrity buffer\n");
  329. return -ENOMEM;
  330. }
  331. end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  332. start = ((unsigned long) buf) >> PAGE_SHIFT;
  333. nr_pages = end - start;
  334. /* Allocate bio integrity payload and integrity vectors */
  335. bip = bio_integrity_alloc(bio, GFP_NOIO, nr_pages);
  336. if (unlikely(bip == NULL)) {
  337. printk(KERN_ERR "could not allocate data integrity bioset\n");
  338. kfree(buf);
  339. return -EIO;
  340. }
  341. bip->bip_owns_buf = 1;
  342. bip->bip_buf = buf;
  343. bip->bip_iter.bi_size = len;
  344. bip->bip_iter.bi_sector = bio->bi_iter.bi_sector;
  345. /* Map it */
  346. offset = offset_in_page(buf);
  347. for (i = 0 ; i < nr_pages ; i++) {
  348. int ret;
  349. bytes = PAGE_SIZE - offset;
  350. if (len <= 0)
  351. break;
  352. if (bytes > len)
  353. bytes = len;
  354. ret = bio_integrity_add_page(bio, virt_to_page(buf),
  355. bytes, offset);
  356. if (ret == 0)
  357. return 0;
  358. if (ret < bytes)
  359. break;
  360. buf += bytes;
  361. len -= bytes;
  362. offset = 0;
  363. }
  364. /* Install custom I/O completion handler if read verify is enabled */
  365. if (bio_data_dir(bio) == READ) {
  366. bip->bip_end_io = bio->bi_end_io;
  367. bio->bi_end_io = bio_integrity_endio;
  368. }
  369. /* Auto-generate integrity metadata if this is a write */
  370. if (bio_data_dir(bio) == WRITE)
  371. bio_integrity_generate(bio);
  372. return 0;
  373. }
  374. EXPORT_SYMBOL(bio_integrity_prep);
  375. /**
  376. * bio_integrity_verify - Verify integrity metadata for a bio
  377. * @bio: bio to verify
  378. *
  379. * Description: This function is called to verify the integrity of a
  380. * bio. The data in the bio io_vec is compared to the integrity
  381. * metadata returned by the HBA.
  382. */
  383. static int bio_integrity_verify(struct bio *bio)
  384. {
  385. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  386. struct blk_integrity_exchg bix;
  387. struct bio_vec *bv;
  388. sector_t sector = bio->bi_integrity->bip_iter.bi_sector;
  389. unsigned int sectors, ret = 0;
  390. void *prot_buf = bio->bi_integrity->bip_buf;
  391. int i;
  392. bix.disk_name = bio->bi_bdev->bd_disk->disk_name;
  393. bix.sector_size = bi->sector_size;
  394. bio_for_each_segment_all(bv, bio, i) {
  395. void *kaddr = kmap_atomic(bv->bv_page);
  396. bix.data_buf = kaddr + bv->bv_offset;
  397. bix.data_size = bv->bv_len;
  398. bix.prot_buf = prot_buf;
  399. bix.sector = sector;
  400. ret = bi->verify_fn(&bix);
  401. if (ret) {
  402. kunmap_atomic(kaddr);
  403. return ret;
  404. }
  405. sectors = bv->bv_len / bi->sector_size;
  406. sector += sectors;
  407. prot_buf += sectors * bi->tuple_size;
  408. kunmap_atomic(kaddr);
  409. }
  410. return ret;
  411. }
  412. /**
  413. * bio_integrity_verify_fn - Integrity I/O completion worker
  414. * @work: Work struct stored in bio to be verified
  415. *
  416. * Description: This workqueue function is called to complete a READ
  417. * request. The function verifies the transferred integrity metadata
  418. * and then calls the original bio end_io function.
  419. */
  420. static void bio_integrity_verify_fn(struct work_struct *work)
  421. {
  422. struct bio_integrity_payload *bip =
  423. container_of(work, struct bio_integrity_payload, bip_work);
  424. struct bio *bio = bip->bip_bio;
  425. int error;
  426. error = bio_integrity_verify(bio);
  427. /* Restore original bio completion handler */
  428. bio->bi_end_io = bip->bip_end_io;
  429. bio_endio_nodec(bio, error);
  430. }
  431. /**
  432. * bio_integrity_endio - Integrity I/O completion function
  433. * @bio: Protected bio
  434. * @error: Pointer to errno
  435. *
  436. * Description: Completion for integrity I/O
  437. *
  438. * Normally I/O completion is done in interrupt context. However,
  439. * verifying I/O integrity is a time-consuming task which must be run
  440. * in process context. This function postpones completion
  441. * accordingly.
  442. */
  443. void bio_integrity_endio(struct bio *bio, int error)
  444. {
  445. struct bio_integrity_payload *bip = bio->bi_integrity;
  446. BUG_ON(bip->bip_bio != bio);
  447. /* In case of an I/O error there is no point in verifying the
  448. * integrity metadata. Restore original bio end_io handler
  449. * and run it.
  450. */
  451. if (error) {
  452. bio->bi_end_io = bip->bip_end_io;
  453. bio_endio(bio, error);
  454. return;
  455. }
  456. INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
  457. queue_work(kintegrityd_wq, &bip->bip_work);
  458. }
  459. EXPORT_SYMBOL(bio_integrity_endio);
  460. /**
  461. * bio_integrity_advance - Advance integrity vector
  462. * @bio: bio whose integrity vector to update
  463. * @bytes_done: number of data bytes that have been completed
  464. *
  465. * Description: This function calculates how many integrity bytes the
  466. * number of completed data bytes correspond to and advances the
  467. * integrity vector accordingly.
  468. */
  469. void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
  470. {
  471. struct bio_integrity_payload *bip = bio->bi_integrity;
  472. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  473. unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
  474. bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
  475. }
  476. EXPORT_SYMBOL(bio_integrity_advance);
  477. /**
  478. * bio_integrity_trim - Trim integrity vector
  479. * @bio: bio whose integrity vector to update
  480. * @offset: offset to first data sector
  481. * @sectors: number of data sectors
  482. *
  483. * Description: Used to trim the integrity vector in a cloned bio.
  484. * The ivec will be advanced corresponding to 'offset' data sectors
  485. * and the length will be truncated corresponding to 'len' data
  486. * sectors.
  487. */
  488. void bio_integrity_trim(struct bio *bio, unsigned int offset,
  489. unsigned int sectors)
  490. {
  491. struct bio_integrity_payload *bip = bio->bi_integrity;
  492. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  493. bio_integrity_advance(bio, offset << 9);
  494. bip->bip_iter.bi_size = bio_integrity_bytes(bi, sectors);
  495. }
  496. EXPORT_SYMBOL(bio_integrity_trim);
  497. /**
  498. * bio_integrity_clone - Callback for cloning bios with integrity metadata
  499. * @bio: New bio
  500. * @bio_src: Original bio
  501. * @gfp_mask: Memory allocation mask
  502. *
  503. * Description: Called to allocate a bip when cloning a bio
  504. */
  505. int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
  506. gfp_t gfp_mask)
  507. {
  508. struct bio_integrity_payload *bip_src = bio_src->bi_integrity;
  509. struct bio_integrity_payload *bip;
  510. BUG_ON(bip_src == NULL);
  511. bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt);
  512. if (bip == NULL)
  513. return -EIO;
  514. memcpy(bip->bip_vec, bip_src->bip_vec,
  515. bip_src->bip_vcnt * sizeof(struct bio_vec));
  516. bip->bip_vcnt = bip_src->bip_vcnt;
  517. bip->bip_iter = bip_src->bip_iter;
  518. return 0;
  519. }
  520. EXPORT_SYMBOL(bio_integrity_clone);
  521. int bioset_integrity_create(struct bio_set *bs, int pool_size)
  522. {
  523. if (bs->bio_integrity_pool)
  524. return 0;
  525. bs->bio_integrity_pool = mempool_create_slab_pool(pool_size, bip_slab);
  526. if (!bs->bio_integrity_pool)
  527. return -1;
  528. bs->bvec_integrity_pool = biovec_create_pool(bs, pool_size);
  529. if (!bs->bvec_integrity_pool) {
  530. mempool_destroy(bs->bio_integrity_pool);
  531. return -1;
  532. }
  533. return 0;
  534. }
  535. EXPORT_SYMBOL(bioset_integrity_create);
  536. void bioset_integrity_free(struct bio_set *bs)
  537. {
  538. if (bs->bio_integrity_pool)
  539. mempool_destroy(bs->bio_integrity_pool);
  540. if (bs->bvec_integrity_pool)
  541. mempool_destroy(bs->bvec_integrity_pool);
  542. }
  543. EXPORT_SYMBOL(bioset_integrity_free);
  544. void __init bio_integrity_init(void)
  545. {
  546. /*
  547. * kintegrityd won't block much but may burn a lot of CPU cycles.
  548. * Make it highpri CPU intensive wq with max concurrency of 1.
  549. */
  550. kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
  551. WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
  552. if (!kintegrityd_wq)
  553. panic("Failed to create kintegrityd\n");
  554. bip_slab = kmem_cache_create("bio_integrity_payload",
  555. sizeof(struct bio_integrity_payload) +
  556. sizeof(struct bio_vec) * BIP_INLINE_VECS,
  557. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  558. if (!bip_slab)
  559. panic("Failed to create slab\n");
  560. }