bio-integrity.c 19 KB

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