bio-integrity.c 21 KB

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