bio-integrity.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. /* A cloned bio doesn't own the integrity metadata */
  89. if (!bio_flagged(bio, BIO_CLONED) && !bio_flagged(bio, BIO_FS_INTEGRITY)
  90. && bip->bip_buf != NULL)
  91. kfree(bip->bip_buf);
  92. if (bs) {
  93. if (bip->bip_slab != BIO_POOL_NONE)
  94. bvec_free(bs->bvec_integrity_pool, bip->bip_vec,
  95. bip->bip_slab);
  96. mempool_free(bip, bs->bio_integrity_pool);
  97. } else {
  98. kfree(bip);
  99. }
  100. bio->bi_integrity = NULL;
  101. }
  102. EXPORT_SYMBOL(bio_integrity_free);
  103. /**
  104. * bio_integrity_add_page - Attach integrity metadata
  105. * @bio: bio to update
  106. * @page: page containing integrity metadata
  107. * @len: number of bytes of integrity metadata in page
  108. * @offset: start offset within page
  109. *
  110. * Description: Attach a page containing integrity metadata to bio.
  111. */
  112. int bio_integrity_add_page(struct bio *bio, struct page *page,
  113. unsigned int len, unsigned int offset)
  114. {
  115. struct bio_integrity_payload *bip = bio->bi_integrity;
  116. struct bio_vec *iv;
  117. if (bip->bip_vcnt >= bvec_nr_vecs(bip->bip_slab)) {
  118. printk(KERN_ERR "%s: bip_vec full\n", __func__);
  119. return 0;
  120. }
  121. iv = bip_vec_idx(bip, bip->bip_vcnt);
  122. BUG_ON(iv == NULL);
  123. iv->bv_page = page;
  124. iv->bv_len = len;
  125. iv->bv_offset = offset;
  126. bip->bip_vcnt++;
  127. return len;
  128. }
  129. EXPORT_SYMBOL(bio_integrity_add_page);
  130. static int bdev_integrity_enabled(struct block_device *bdev, int rw)
  131. {
  132. struct blk_integrity *bi = bdev_get_integrity(bdev);
  133. if (bi == NULL)
  134. return 0;
  135. if (rw == READ && bi->verify_fn != NULL &&
  136. (bi->flags & INTEGRITY_FLAG_READ))
  137. return 1;
  138. if (rw == WRITE && bi->generate_fn != NULL &&
  139. (bi->flags & INTEGRITY_FLAG_WRITE))
  140. return 1;
  141. return 0;
  142. }
  143. /**
  144. * bio_integrity_enabled - Check whether integrity can be passed
  145. * @bio: bio to check
  146. *
  147. * Description: Determines whether bio_integrity_prep() can be called
  148. * on this bio or not. bio data direction and target device must be
  149. * set prior to calling. The functions honors the write_generate and
  150. * read_verify flags in sysfs.
  151. */
  152. int bio_integrity_enabled(struct bio *bio)
  153. {
  154. /* Already protected? */
  155. if (bio_integrity(bio))
  156. return 0;
  157. return bdev_integrity_enabled(bio->bi_bdev, bio_data_dir(bio));
  158. }
  159. EXPORT_SYMBOL(bio_integrity_enabled);
  160. /**
  161. * bio_integrity_hw_sectors - Convert 512b sectors to hardware ditto
  162. * @bi: blk_integrity profile for device
  163. * @sectors: Number of 512 sectors to convert
  164. *
  165. * Description: The block layer calculates everything in 512 byte
  166. * sectors but integrity metadata is done in terms of the hardware
  167. * sector size of the storage device. Convert the block layer sectors
  168. * to physical sectors.
  169. */
  170. static inline unsigned int bio_integrity_hw_sectors(struct blk_integrity *bi,
  171. unsigned int sectors)
  172. {
  173. /* At this point there are only 512b or 4096b DIF/EPP devices */
  174. if (bi->sector_size == 4096)
  175. return sectors >>= 3;
  176. return sectors;
  177. }
  178. /**
  179. * bio_integrity_tag_size - Retrieve integrity tag space
  180. * @bio: bio to inspect
  181. *
  182. * Description: Returns the maximum number of tag bytes that can be
  183. * attached to this bio. Filesystems can use this to determine how
  184. * much metadata to attach to an I/O.
  185. */
  186. unsigned int bio_integrity_tag_size(struct bio *bio)
  187. {
  188. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  189. BUG_ON(bio->bi_size == 0);
  190. return bi->tag_size * (bio->bi_size / bi->sector_size);
  191. }
  192. EXPORT_SYMBOL(bio_integrity_tag_size);
  193. int bio_integrity_tag(struct bio *bio, void *tag_buf, unsigned int len, int set)
  194. {
  195. struct bio_integrity_payload *bip = bio->bi_integrity;
  196. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  197. unsigned int nr_sectors;
  198. BUG_ON(bip->bip_buf == NULL);
  199. if (bi->tag_size == 0)
  200. return -1;
  201. nr_sectors = bio_integrity_hw_sectors(bi,
  202. DIV_ROUND_UP(len, bi->tag_size));
  203. if (nr_sectors * bi->tuple_size > bip->bip_size) {
  204. printk(KERN_ERR "%s: tag too big for bio: %u > %u\n",
  205. __func__, nr_sectors * bi->tuple_size, bip->bip_size);
  206. return -1;
  207. }
  208. if (set)
  209. bi->set_tag_fn(bip->bip_buf, tag_buf, nr_sectors);
  210. else
  211. bi->get_tag_fn(bip->bip_buf, tag_buf, nr_sectors);
  212. return 0;
  213. }
  214. /**
  215. * bio_integrity_set_tag - Attach a tag buffer to a bio
  216. * @bio: bio to attach buffer to
  217. * @tag_buf: Pointer to a buffer containing tag data
  218. * @len: Length of the included buffer
  219. *
  220. * Description: Use this function to tag a bio by leveraging the extra
  221. * space provided by devices formatted with integrity protection. The
  222. * size of the integrity buffer must be <= to the size reported by
  223. * bio_integrity_tag_size().
  224. */
  225. int bio_integrity_set_tag(struct bio *bio, void *tag_buf, unsigned int len)
  226. {
  227. BUG_ON(bio_data_dir(bio) != WRITE);
  228. return bio_integrity_tag(bio, tag_buf, len, 1);
  229. }
  230. EXPORT_SYMBOL(bio_integrity_set_tag);
  231. /**
  232. * bio_integrity_get_tag - Retrieve a tag buffer from a bio
  233. * @bio: bio to retrieve buffer from
  234. * @tag_buf: Pointer to a buffer for the tag data
  235. * @len: Length of the target buffer
  236. *
  237. * Description: Use this function to retrieve the tag buffer from a
  238. * completed I/O. The size of the integrity buffer must be <= to the
  239. * size reported by bio_integrity_tag_size().
  240. */
  241. int bio_integrity_get_tag(struct bio *bio, void *tag_buf, unsigned int len)
  242. {
  243. BUG_ON(bio_data_dir(bio) != READ);
  244. return bio_integrity_tag(bio, tag_buf, len, 0);
  245. }
  246. EXPORT_SYMBOL(bio_integrity_get_tag);
  247. /**
  248. * bio_integrity_generate - Generate integrity metadata for a bio
  249. * @bio: bio to generate integrity metadata for
  250. *
  251. * Description: Generates integrity metadata for a bio by calling the
  252. * block device's generation callback function. The bio must have a
  253. * bip attached with enough room to accommodate the generated
  254. * integrity metadata.
  255. */
  256. static void bio_integrity_generate(struct bio *bio)
  257. {
  258. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  259. struct blk_integrity_exchg bix;
  260. struct bio_vec *bv;
  261. sector_t sector = bio->bi_sector;
  262. unsigned int i, sectors, total;
  263. void *prot_buf = bio->bi_integrity->bip_buf;
  264. total = 0;
  265. bix.disk_name = bio->bi_bdev->bd_disk->disk_name;
  266. bix.sector_size = bi->sector_size;
  267. bio_for_each_segment(bv, bio, i) {
  268. void *kaddr = kmap_atomic(bv->bv_page);
  269. bix.data_buf = kaddr + bv->bv_offset;
  270. bix.data_size = bv->bv_len;
  271. bix.prot_buf = prot_buf;
  272. bix.sector = sector;
  273. bi->generate_fn(&bix);
  274. sectors = bv->bv_len / bi->sector_size;
  275. sector += sectors;
  276. prot_buf += sectors * bi->tuple_size;
  277. total += sectors * bi->tuple_size;
  278. BUG_ON(total > bio->bi_integrity->bip_size);
  279. kunmap_atomic(kaddr);
  280. }
  281. }
  282. static inline unsigned short blk_integrity_tuple_size(struct blk_integrity *bi)
  283. {
  284. if (bi)
  285. return bi->tuple_size;
  286. return 0;
  287. }
  288. /**
  289. * bio_integrity_prep - Prepare bio for integrity I/O
  290. * @bio: bio to prepare
  291. *
  292. * Description: Allocates a buffer for integrity metadata, maps the
  293. * pages and attaches them to a bio. The bio must have data
  294. * direction, target device and start sector set priot to calling. In
  295. * the WRITE case, integrity metadata will be generated using the
  296. * block device's integrity function. In the READ case, the buffer
  297. * will be prepared for DMA and a suitable end_io handler set up.
  298. */
  299. int bio_integrity_prep(struct bio *bio)
  300. {
  301. struct bio_integrity_payload *bip;
  302. struct blk_integrity *bi;
  303. struct request_queue *q;
  304. void *buf;
  305. unsigned long start, end;
  306. unsigned int len, nr_pages;
  307. unsigned int bytes, offset, i;
  308. unsigned int sectors;
  309. bi = bdev_get_integrity(bio->bi_bdev);
  310. q = bdev_get_queue(bio->bi_bdev);
  311. BUG_ON(bi == NULL);
  312. BUG_ON(bio_integrity(bio));
  313. sectors = bio_integrity_hw_sectors(bi, bio_sectors(bio));
  314. /* Allocate kernel buffer for protection data */
  315. len = sectors * blk_integrity_tuple_size(bi);
  316. buf = kmalloc(len, GFP_NOIO | q->bounce_gfp);
  317. if (unlikely(buf == NULL)) {
  318. printk(KERN_ERR "could not allocate integrity buffer\n");
  319. return -ENOMEM;
  320. }
  321. end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  322. start = ((unsigned long) buf) >> PAGE_SHIFT;
  323. nr_pages = end - start;
  324. /* Allocate bio integrity payload and integrity vectors */
  325. bip = bio_integrity_alloc(bio, GFP_NOIO, nr_pages);
  326. if (unlikely(bip == NULL)) {
  327. printk(KERN_ERR "could not allocate data integrity bioset\n");
  328. kfree(buf);
  329. return -EIO;
  330. }
  331. bip->bip_buf = buf;
  332. bip->bip_size = len;
  333. bip->bip_sector = bio->bi_sector;
  334. /* Map it */
  335. offset = offset_in_page(buf);
  336. for (i = 0 ; i < nr_pages ; i++) {
  337. int ret;
  338. bytes = PAGE_SIZE - offset;
  339. if (len <= 0)
  340. break;
  341. if (bytes > len)
  342. bytes = len;
  343. ret = bio_integrity_add_page(bio, virt_to_page(buf),
  344. bytes, offset);
  345. if (ret == 0)
  346. return 0;
  347. if (ret < bytes)
  348. break;
  349. buf += bytes;
  350. len -= bytes;
  351. offset = 0;
  352. }
  353. /* Install custom I/O completion handler if read verify is enabled */
  354. if (bio_data_dir(bio) == READ) {
  355. bip->bip_end_io = bio->bi_end_io;
  356. bio->bi_end_io = bio_integrity_endio;
  357. }
  358. /* Auto-generate integrity metadata if this is a write */
  359. if (bio_data_dir(bio) == WRITE)
  360. bio_integrity_generate(bio);
  361. return 0;
  362. }
  363. EXPORT_SYMBOL(bio_integrity_prep);
  364. /**
  365. * bio_integrity_verify - Verify integrity metadata for a bio
  366. * @bio: bio to verify
  367. *
  368. * Description: This function is called to verify the integrity of a
  369. * bio. The data in the bio io_vec is compared to the integrity
  370. * metadata returned by the HBA.
  371. */
  372. static int bio_integrity_verify(struct bio *bio)
  373. {
  374. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  375. struct blk_integrity_exchg bix;
  376. struct bio_vec *bv;
  377. sector_t sector = bio->bi_integrity->bip_sector;
  378. unsigned int i, sectors, total, ret;
  379. void *prot_buf = bio->bi_integrity->bip_buf;
  380. ret = total = 0;
  381. bix.disk_name = bio->bi_bdev->bd_disk->disk_name;
  382. bix.sector_size = bi->sector_size;
  383. bio_for_each_segment(bv, bio, i) {
  384. void *kaddr = kmap_atomic(bv->bv_page);
  385. bix.data_buf = kaddr + bv->bv_offset;
  386. bix.data_size = bv->bv_len;
  387. bix.prot_buf = prot_buf;
  388. bix.sector = sector;
  389. ret = bi->verify_fn(&bix);
  390. if (ret) {
  391. kunmap_atomic(kaddr);
  392. return ret;
  393. }
  394. sectors = bv->bv_len / bi->sector_size;
  395. sector += sectors;
  396. prot_buf += sectors * bi->tuple_size;
  397. total += sectors * bi->tuple_size;
  398. BUG_ON(total > bio->bi_integrity->bip_size);
  399. kunmap_atomic(kaddr);
  400. }
  401. return ret;
  402. }
  403. /**
  404. * bio_integrity_verify_fn - Integrity I/O completion worker
  405. * @work: Work struct stored in bio to be verified
  406. *
  407. * Description: This workqueue function is called to complete a READ
  408. * request. The function verifies the transferred integrity metadata
  409. * and then calls the original bio end_io function.
  410. */
  411. static void bio_integrity_verify_fn(struct work_struct *work)
  412. {
  413. struct bio_integrity_payload *bip =
  414. container_of(work, struct bio_integrity_payload, bip_work);
  415. struct bio *bio = bip->bip_bio;
  416. int error;
  417. error = bio_integrity_verify(bio);
  418. /* Restore original bio completion handler */
  419. bio->bi_end_io = bip->bip_end_io;
  420. bio_endio(bio, error);
  421. }
  422. /**
  423. * bio_integrity_endio - Integrity I/O completion function
  424. * @bio: Protected bio
  425. * @error: Pointer to errno
  426. *
  427. * Description: Completion for integrity I/O
  428. *
  429. * Normally I/O completion is done in interrupt context. However,
  430. * verifying I/O integrity is a time-consuming task which must be run
  431. * in process context. This function postpones completion
  432. * accordingly.
  433. */
  434. void bio_integrity_endio(struct bio *bio, int error)
  435. {
  436. struct bio_integrity_payload *bip = bio->bi_integrity;
  437. BUG_ON(bip->bip_bio != bio);
  438. /* In case of an I/O error there is no point in verifying the
  439. * integrity metadata. Restore original bio end_io handler
  440. * and run it.
  441. */
  442. if (error) {
  443. bio->bi_end_io = bip->bip_end_io;
  444. bio_endio(bio, error);
  445. return;
  446. }
  447. INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
  448. queue_work(kintegrityd_wq, &bip->bip_work);
  449. }
  450. EXPORT_SYMBOL(bio_integrity_endio);
  451. /**
  452. * bio_integrity_mark_head - Advance bip_vec skip bytes
  453. * @bip: Integrity vector to advance
  454. * @skip: Number of bytes to advance it
  455. */
  456. void bio_integrity_mark_head(struct bio_integrity_payload *bip,
  457. unsigned int skip)
  458. {
  459. struct bio_vec *iv;
  460. unsigned int i;
  461. bip_for_each_vec(iv, bip, i) {
  462. if (skip == 0) {
  463. bip->bip_idx = i;
  464. return;
  465. } else if (skip >= iv->bv_len) {
  466. skip -= iv->bv_len;
  467. } else { /* skip < iv->bv_len) */
  468. iv->bv_offset += skip;
  469. iv->bv_len -= skip;
  470. bip->bip_idx = i;
  471. return;
  472. }
  473. }
  474. }
  475. /**
  476. * bio_integrity_mark_tail - Truncate bip_vec to be len bytes long
  477. * @bip: Integrity vector to truncate
  478. * @len: New length of integrity vector
  479. */
  480. void bio_integrity_mark_tail(struct bio_integrity_payload *bip,
  481. unsigned int len)
  482. {
  483. struct bio_vec *iv;
  484. unsigned int i;
  485. bip_for_each_vec(iv, bip, i) {
  486. if (len == 0) {
  487. bip->bip_vcnt = i;
  488. return;
  489. } else if (len >= iv->bv_len) {
  490. len -= iv->bv_len;
  491. } else { /* len < iv->bv_len) */
  492. iv->bv_len = len;
  493. len = 0;
  494. }
  495. }
  496. }
  497. /**
  498. * bio_integrity_advance - Advance integrity vector
  499. * @bio: bio whose integrity vector to update
  500. * @bytes_done: number of data bytes that have been completed
  501. *
  502. * Description: This function calculates how many integrity bytes the
  503. * number of completed data bytes correspond to and advances the
  504. * integrity vector accordingly.
  505. */
  506. void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
  507. {
  508. struct bio_integrity_payload *bip = bio->bi_integrity;
  509. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  510. unsigned int nr_sectors;
  511. BUG_ON(bip == NULL);
  512. BUG_ON(bi == NULL);
  513. nr_sectors = bio_integrity_hw_sectors(bi, bytes_done >> 9);
  514. bio_integrity_mark_head(bip, nr_sectors * bi->tuple_size);
  515. }
  516. EXPORT_SYMBOL(bio_integrity_advance);
  517. /**
  518. * bio_integrity_trim - Trim integrity vector
  519. * @bio: bio whose integrity vector to update
  520. * @offset: offset to first data sector
  521. * @sectors: number of data sectors
  522. *
  523. * Description: Used to trim the integrity vector in a cloned bio.
  524. * The ivec will be advanced corresponding to 'offset' data sectors
  525. * and the length will be truncated corresponding to 'len' data
  526. * sectors.
  527. */
  528. void bio_integrity_trim(struct bio *bio, unsigned int offset,
  529. unsigned int sectors)
  530. {
  531. struct bio_integrity_payload *bip = bio->bi_integrity;
  532. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  533. unsigned int nr_sectors;
  534. BUG_ON(bip == NULL);
  535. BUG_ON(bi == NULL);
  536. BUG_ON(!bio_flagged(bio, BIO_CLONED));
  537. nr_sectors = bio_integrity_hw_sectors(bi, sectors);
  538. bip->bip_sector = bip->bip_sector + offset;
  539. bio_integrity_mark_head(bip, offset * bi->tuple_size);
  540. bio_integrity_mark_tail(bip, sectors * bi->tuple_size);
  541. }
  542. EXPORT_SYMBOL(bio_integrity_trim);
  543. /**
  544. * bio_integrity_split - Split integrity metadata
  545. * @bio: Protected bio
  546. * @bp: Resulting bio_pair
  547. * @sectors: Offset
  548. *
  549. * Description: Splits an integrity page into a bio_pair.
  550. */
  551. void bio_integrity_split(struct bio *bio, struct bio_pair *bp, int sectors)
  552. {
  553. struct blk_integrity *bi;
  554. struct bio_integrity_payload *bip = bio->bi_integrity;
  555. unsigned int nr_sectors;
  556. if (bio_integrity(bio) == 0)
  557. return;
  558. bi = bdev_get_integrity(bio->bi_bdev);
  559. BUG_ON(bi == NULL);
  560. BUG_ON(bip->bip_vcnt != 1);
  561. nr_sectors = bio_integrity_hw_sectors(bi, sectors);
  562. bp->bio1.bi_integrity = &bp->bip1;
  563. bp->bio2.bi_integrity = &bp->bip2;
  564. bp->iv1 = bip->bip_vec[bip->bip_idx];
  565. bp->iv2 = bip->bip_vec[bip->bip_idx];
  566. bp->bip1.bip_vec = &bp->iv1;
  567. bp->bip2.bip_vec = &bp->iv2;
  568. bp->iv1.bv_len = sectors * bi->tuple_size;
  569. bp->iv2.bv_offset += sectors * bi->tuple_size;
  570. bp->iv2.bv_len -= sectors * bi->tuple_size;
  571. bp->bip1.bip_sector = bio->bi_integrity->bip_sector;
  572. bp->bip2.bip_sector = bio->bi_integrity->bip_sector + nr_sectors;
  573. bp->bip1.bip_vcnt = bp->bip2.bip_vcnt = 1;
  574. bp->bip1.bip_idx = bp->bip2.bip_idx = 0;
  575. }
  576. EXPORT_SYMBOL(bio_integrity_split);
  577. /**
  578. * bio_integrity_clone - Callback for cloning bios with integrity metadata
  579. * @bio: New bio
  580. * @bio_src: Original bio
  581. * @gfp_mask: Memory allocation mask
  582. *
  583. * Description: Called to allocate a bip when cloning a bio
  584. */
  585. int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
  586. gfp_t gfp_mask)
  587. {
  588. struct bio_integrity_payload *bip_src = bio_src->bi_integrity;
  589. struct bio_integrity_payload *bip;
  590. BUG_ON(bip_src == NULL);
  591. bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt);
  592. if (bip == NULL)
  593. return -EIO;
  594. memcpy(bip->bip_vec, bip_src->bip_vec,
  595. bip_src->bip_vcnt * sizeof(struct bio_vec));
  596. bip->bip_sector = bip_src->bip_sector;
  597. bip->bip_vcnt = bip_src->bip_vcnt;
  598. bip->bip_idx = bip_src->bip_idx;
  599. return 0;
  600. }
  601. EXPORT_SYMBOL(bio_integrity_clone);
  602. int bioset_integrity_create(struct bio_set *bs, int pool_size)
  603. {
  604. if (bs->bio_integrity_pool)
  605. return 0;
  606. bs->bio_integrity_pool = mempool_create_slab_pool(pool_size, bip_slab);
  607. bs->bvec_integrity_pool = biovec_create_pool(bs, pool_size);
  608. if (!bs->bvec_integrity_pool)
  609. return -1;
  610. if (!bs->bio_integrity_pool)
  611. return -1;
  612. return 0;
  613. }
  614. EXPORT_SYMBOL(bioset_integrity_create);
  615. void bioset_integrity_free(struct bio_set *bs)
  616. {
  617. if (bs->bio_integrity_pool)
  618. mempool_destroy(bs->bio_integrity_pool);
  619. if (bs->bvec_integrity_pool)
  620. mempool_destroy(bs->bio_integrity_pool);
  621. }
  622. EXPORT_SYMBOL(bioset_integrity_free);
  623. void __init bio_integrity_init(void)
  624. {
  625. /*
  626. * kintegrityd won't block much but may burn a lot of CPU cycles.
  627. * Make it highpri CPU intensive wq with max concurrency of 1.
  628. */
  629. kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
  630. WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
  631. if (!kintegrityd_wq)
  632. panic("Failed to create kintegrityd\n");
  633. bip_slab = kmem_cache_create("bio_integrity_payload",
  634. sizeof(struct bio_integrity_payload) +
  635. sizeof(struct bio_vec) * BIP_INLINE_VECS,
  636. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  637. if (!bip_slab)
  638. panic("Failed to create slab\n");
  639. }