bio-integrity.c 21 KB

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