bio-integrity.c 21 KB

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