bio-integrity.c 20 KB

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