original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

759 lines
20 KiB

include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h percpu.h is included by sched.h and module.h and thus ends up being included when building most .c files. percpu.h includes slab.h which in turn includes gfp.h making everything defined by the two files universally available and complicating inclusion dependencies. percpu.h -> slab.h dependency is about to be removed. Prepare for this change by updating users of gfp and slab facilities include those headers directly instead of assuming availability. As this conversion needs to touch large number of source files, the following script is used as the basis of conversion. http://userweb.kernel.org/~tj/misc/slabh-sweep.py The script does the followings. * Scan files for gfp and slab usages and update includes such that only the necessary includes are there. ie. if only gfp is used, gfp.h, if slab is used, slab.h. * When the script inserts a new include, it looks at the include blocks and try to put the new include such that its order conforms to its surrounding. It's put in the include block which contains core kernel includes, in the same order that the rest are ordered - alphabetical, Christmas tree, rev-Xmas-tree or at the end if there doesn't seem to be any matching order. * If the script can't find a place to put a new include (mostly because the file doesn't have fitting include block), it prints out an error message indicating which .h file needs to be added to the file. The conversion was done in the following steps. 1. The initial automatic conversion of all .c files updated slightly over 4000 files, deleting around 700 includes and adding ~480 gfp.h and ~3000 slab.h inclusions. The script emitted errors for ~400 files. 2. Each error was manually checked. Some didn't need the inclusion, some needed manual addition while adding it to implementation .h or embedding .c file was more appropriate for others. This step added inclusions to around 150 files. 3. The script was run again and the output was compared to the edits from #2 to make sure no file was left behind. 4. Several build tests were done and a couple of problems were fixed. e.g. lib/decompress_*.c used malloc/free() wrappers around slab APIs requiring slab.h to be added manually. 5. The script was run on all .h files but without automatically editing them as sprinkling gfp.h and slab.h inclusions around .h files could easily lead to inclusion dependency hell. Most gfp.h inclusion directives were ignored as stuff from gfp.h was usually wildly available and often used in preprocessor macros. Each slab.h inclusion directive was examined and added manually as necessary. 6. percpu.h was updated not to include slab.h. 7. Build test were done on the following configurations and failures were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my distributed build env didn't work with gcov compiles) and a few more options had to be turned off depending on archs to make things build (like ipr on powerpc/64 which failed due to missing writeq). * x86 and x86_64 UP and SMP allmodconfig and a custom test config. * powerpc and powerpc64 SMP allmodconfig * sparc and sparc64 SMP allmodconfig * ia64 SMP allmodconfig * s390 SMP allmodconfig * alpha SMP allmodconfig * um on x86_64 SMP allmodconfig 8. percpu.h modifications were reverted so that it could be applied as a separate patch and serve as bisection point. Given the fact that I had only a couple of failures from tests on step 6, I'm fairly confident about the coverage of this conversion patch. If there is a breakage, it's likely to be something in one of the arch headers which should be easily discoverable easily on most builds of the specific arch. Signed-off-by: Tejun Heo <tj@kernel.org> Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
12 years ago
  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. #define BIP_INLINE_VECS 4
  29. static struct kmem_cache *bip_slab;
  30. static struct workqueue_struct *kintegrityd_wq;
  31. /**
  32. * bio_integrity_alloc - Allocate integrity payload and attach it to bio
  33. * @bio: bio to attach integrity metadata to
  34. * @gfp_mask: Memory allocation mask
  35. * @nr_vecs: Number of integrity metadata scatter-gather elements
  36. *
  37. * Description: This function prepares a bio for attaching integrity
  38. * metadata. nr_vecs specifies the maximum number of pages containing
  39. * integrity metadata that can be attached.
  40. */
  41. struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
  42. gfp_t gfp_mask,
  43. unsigned int nr_vecs)
  44. {
  45. struct bio_integrity_payload *bip;
  46. struct bio_set *bs = bio->bi_pool;
  47. unsigned long idx = BIO_POOL_NONE;
  48. unsigned inline_vecs;
  49. if (!bs) {
  50. bip = kmalloc(sizeof(struct bio_integrity_payload) +
  51. sizeof(struct bio_vec) * nr_vecs, gfp_mask);
  52. inline_vecs = nr_vecs;
  53. } else {
  54. bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask);
  55. inline_vecs = BIP_INLINE_VECS;
  56. }
  57. if (unlikely(!bip))
  58. return NULL;
  59. memset(bip, 0, sizeof(*bip));
  60. if (nr_vecs > inline_vecs) {
  61. bip->bip_vec = bvec_alloc(gfp_mask, nr_vecs, &idx,
  62. bs->bvec_integrity_pool);
  63. if (!bip->bip_vec)
  64. goto err;
  65. } else {
  66. bip->bip_vec = bip->bip_inline_vecs;
  67. }
  68. bip->bip_slab = idx;
  69. bip->bip_bio = bio;
  70. bio->bi_integrity = bip;
  71. return bip;
  72. err:
  73. mempool_free(bip, bs->bio_integrity_pool);
  74. return NULL;
  75. }
  76. EXPORT_SYMBOL(bio_integrity_alloc);
  77. /**
  78. * bio_integrity_free - Free bio integrity payload
  79. * @bio: bio containing bip to be freed
  80. *
  81. * Description: Used to free the integrity portion of a bio. Usually
  82. * called from bio_free().
  83. */
  84. void bio_integrity_free(struct bio *bio)
  85. {
  86. struct bio_integrity_payload *bip = bio->bi_integrity;
  87. struct bio_set *bs = bio->bi_pool;
  88. if (bip->bip_owns_buf)
  89. kfree(bip->bip_buf);
  90. if (bs) {
  91. if (bip->bip_slab != BIO_POOL_NONE)
  92. bvec_free(bs->bvec_integrity_pool, bip->bip_vec,
  93. bip->bip_slab);
  94. mempool_free(bip, bs->bio_integrity_pool);
  95. } else {
  96. kfree(bip);
  97. }
  98. bio->bi_integrity = NULL;
  99. }
  100. EXPORT_SYMBOL(bio_integrity_free);
  101. /**
  102. * bio_integrity_add_page - Attach integrity metadata
  103. * @bio: bio to update
  104. * @page: page containing integrity metadata
  105. * @len: number of bytes of integrity metadata in page
  106. * @offset: start offset within page
  107. *
  108. * Description: Attach a page containing integrity metadata to bio.
  109. */
  110. int bio_integrity_add_page(struct bio *bio, struct page *page,
  111. unsigned int len, unsigned int offset)
  112. {
  113. struct bio_integrity_payload *bip = bio->bi_integrity;
  114. struct bio_vec *iv;
  115. if (bip->bip_vcnt >= bvec_nr_vecs(bip->bip_slab)) {
  116. printk(KERN_ERR "%s: bip_vec full\n", __func__);
  117. return 0;
  118. }
  119. iv = bip_vec_idx(bip, bip->bip_vcnt);
  120. BUG_ON(iv == NULL);
  121. iv->bv_page = page;
  122. iv->bv_len = len;
  123. iv->bv_offset = offset;
  124. bip->bip_vcnt++;
  125. return len;
  126. }
  127. EXPORT_SYMBOL(bio_integrity_add_page);
  128. static int bdev_integrity_enabled(struct block_device *bdev, int rw)
  129. {
  130. struct blk_integrity *bi = bdev_get_integrity(bdev);
  131. if (bi == NULL)
  132. return 0;
  133. if (rw == READ && bi->verify_fn != NULL &&
  134. (bi->flags & INTEGRITY_FLAG_READ))
  135. return 1;
  136. if (rw == WRITE && bi->generate_fn != NULL &&
  137. (bi->flags & INTEGRITY_FLAG_WRITE))
  138. return 1;
  139. return 0;
  140. }
  141. /**
  142. * bio_integrity_enabled - Check whether integrity can be passed
  143. * @bio: bio to check
  144. *
  145. * Description: Determines whether bio_integrity_prep() can be called
  146. * on this bio or not. bio data direction and target device must be
  147. * set prior to calling. The functions honors the write_generate and
  148. * read_verify flags in sysfs.
  149. */
  150. int bio_integrity_enabled(struct bio *bio)
  151. {
  152. /* Already protected? */
  153. if (bio_integrity(bio))
  154. return 0;
  155. return bdev_integrity_enabled(bio->bi_bdev, bio_data_dir(bio));
  156. }
  157. EXPORT_SYMBOL(bio_integrity_enabled);
  158. /**
  159. * bio_integrity_hw_sectors - Convert 512b sectors to hardware ditto
  160. * @bi: blk_integrity profile for device
  161. * @sectors: Number of 512 sectors to convert
  162. *
  163. * Description: The block layer calculates everything in 512 byte
  164. * sectors but integrity metadata is done in terms of the hardware
  165. * sector size of the storage device. Convert the block layer sectors
  166. * to physical sectors.
  167. */
  168. static inline unsigned int bio_integrity_hw_sectors(struct blk_integrity *bi,
  169. unsigned int sectors)
  170. {
  171. /* At this point there are only 512b or 4096b DIF/EPP devices */
  172. if (bi->sector_size == 4096)
  173. return sectors >>= 3;
  174. return sectors;
  175. }
  176. /**
  177. * bio_integrity_tag_size - Retrieve integrity tag space
  178. * @bio: bio to inspect
  179. *
  180. * Description: Returns the maximum number of tag bytes that can be
  181. * attached to this bio. Filesystems can use this to determine how
  182. * much metadata to attach to an I/O.
  183. */
  184. unsigned int bio_integrity_tag_size(struct bio *bio)
  185. {
  186. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  187. BUG_ON(bio->bi_size == 0);
  188. return bi->tag_size * (bio->bi_size / bi->sector_size);
  189. }
  190. EXPORT_SYMBOL(bio_integrity_tag_size);
  191. int bio_integrity_tag(struct bio *bio, void *tag_buf, unsigned int len, int set)
  192. {
  193. struct bio_integrity_payload *bip = bio->bi_integrity;
  194. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  195. unsigned int nr_sectors;
  196. BUG_ON(bip->bip_buf == NULL);
  197. if (bi->tag_size == 0)
  198. return -1;
  199. nr_sectors = bio_integrity_hw_sectors(bi,
  200. DIV_ROUND_UP(len, bi->tag_size));
  201. if (nr_sectors * bi->tuple_size > bip->bip_size) {
  202. printk(KERN_ERR "%s: tag too big for bio: %u > %u\n",
  203. __func__, nr_sectors * bi->tuple_size, bip->bip_size);
  204. return -1;
  205. }
  206. if (set)
  207. bi->set_tag_fn(bip->bip_buf, tag_buf, nr_sectors);
  208. else
  209. bi->get_tag_fn(bip->bip_buf, tag_buf, nr_sectors);
  210. return 0;
  211. }
  212. /**
  213. * bio_integrity_set_tag - Attach a tag buffer to a bio
  214. * @bio: bio to attach buffer to
  215. * @tag_buf: Pointer to a buffer containing tag data
  216. * @len: Length of the included buffer
  217. *
  218. * Description: Use this function to tag a bio by leveraging the extra
  219. * space provided by devices formatted with integrity protection. The
  220. * size of the integrity buffer must be <= to the size reported by
  221. * bio_integrity_tag_size().
  222. */
  223. int bio_integrity_set_tag(struct bio *bio, void *tag_buf, unsigned int len)
  224. {
  225. BUG_ON(bio_data_dir(bio) != WRITE);
  226. return bio_integrity_tag(bio, tag_buf, len, 1);
  227. }
  228. EXPORT_SYMBOL(bio_integrity_set_tag);
  229. /**
  230. * bio_integrity_get_tag - Retrieve a tag buffer from a bio
  231. * @bio: bio to retrieve buffer from
  232. * @tag_buf: Pointer to a buffer for the tag data
  233. * @len: Length of the target buffer
  234. *
  235. * Description: Use this function to retrieve the tag buffer from a
  236. * completed I/O. The size of the integrity buffer must be <= to the
  237. * size reported by bio_integrity_tag_size().
  238. */
  239. int bio_integrity_get_tag(struct bio *bio, void *tag_buf, unsigned int len)
  240. {
  241. BUG_ON(bio_data_dir(bio) != READ);
  242. return bio_integrity_tag(bio, tag_buf, len, 0);
  243. }
  244. EXPORT_SYMBOL(bio_integrity_get_tag);
  245. /**
  246. * bio_integrity_generate - Generate integrity metadata for a bio
  247. * @bio: bio to generate integrity metadata for
  248. *
  249. * Description: Generates integrity metadata for a bio by calling the
  250. * block device's generation callback function. The bio must have a
  251. * bip attached with enough room to accommodate the generated
  252. * integrity metadata.
  253. */
  254. static void bio_integrity_generate(struct bio *bio)
  255. {
  256. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  257. struct blk_integrity_exchg bix;
  258. struct bio_vec *bv;
  259. sector_t sector = bio->bi_sector;
  260. unsigned int i, sectors, total;
  261. void *prot_buf = bio->bi_integrity->bip_buf;
  262. total = 0;
  263. bix.disk_name = bio->bi_bdev->bd_disk->disk_name;
  264. bix.sector_size = bi->sector_size;
  265. bio_for_each_segment(bv, bio, i) {
  266. void *kaddr = kmap_atomic(bv->bv_page);
  267. bix.data_buf = kaddr + bv->bv_offset;
  268. bix.data_size = bv->bv_len;
  269. bix.prot_buf = prot_buf;
  270. bix.sector = sector;
  271. bi->generate_fn(&bix);
  272. sectors = bv->bv_len / bi->sector_size;
  273. sector += sectors;
  274. prot_buf += sectors * bi->tuple_size;
  275. total += sectors * bi->tuple_size;
  276. BUG_ON(total > bio->bi_integrity->bip_size);
  277. kunmap_atomic(kaddr);
  278. }
  279. }
  280. static inline unsigned short blk_integrity_tuple_size(struct blk_integrity *bi)
  281. {
  282. if (bi)
  283. return bi->tuple_size;
  284. return 0;
  285. }
  286. /**
  287. * bio_integrity_prep - Prepare bio for integrity I/O
  288. * @bio: bio to prepare
  289. *
  290. * Description: Allocates a buffer for integrity metadata, maps the
  291. * pages and attaches them to a bio. The bio must have data
  292. * direction, target device and start sector set priot to calling. In
  293. * the WRITE case, integrity metadata will be generated using the
  294. * block device's integrity function. In the READ case, the buffer
  295. * will be prepared for DMA and a suitable end_io handler set up.
  296. */
  297. int bio_integrity_prep(struct bio *bio)
  298. {
  299. struct bio_integrity_payload *bip;
  300. struct blk_integrity *bi;
  301. struct request_queue *q;
  302. void *buf;
  303. unsigned long start, end;
  304. unsigned int len, nr_pages;
  305. unsigned int bytes, offset, i;
  306. unsigned int sectors;
  307. bi = bdev_get_integrity(bio->bi_bdev);
  308. q = bdev_get_queue(bio->bi_bdev);
  309. BUG_ON(bi == NULL);
  310. BUG_ON(bio_integrity(bio));
  311. sectors = bio_integrity_hw_sectors(bi, bio_sectors(bio));
  312. /* Allocate kernel buffer for protection data */
  313. len = sectors * blk_integrity_tuple_size(bi);
  314. buf = kmalloc(len, GFP_NOIO | q->bounce_gfp);
  315. if (unlikely(buf == NULL)) {
  316. printk(KERN_ERR "could not allocate integrity buffer\n");
  317. return -ENOMEM;
  318. }
  319. end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  320. start = ((unsigned long) buf) >> PAGE_SHIFT;
  321. nr_pages = end - start;
  322. /* Allocate bio integrity payload and integrity vectors */
  323. bip = bio_integrity_alloc(bio, GFP_NOIO, nr_pages);
  324. if (unlikely(bip == NULL)) {
  325. printk(KERN_ERR "could not allocate data integrity bioset\n");
  326. kfree(buf);
  327. return -EIO;
  328. }
  329. bip->bip_owns_buf = 1;
  330. bip->bip_buf = buf;
  331. bip->bip_size = len;
  332. bip->bip_sector = bio->bi_sector;
  333. /* Map it */
  334. offset = offset_in_page(buf);
  335. for (i = 0 ; i < nr_pages ; i++) {
  336. int ret;
  337. bytes = PAGE_SIZE - offset;
  338. if (len <= 0)
  339. break;
  340. if (bytes > len)
  341. bytes = len;
  342. ret = bio_integrity_add_page(bio, virt_to_page(buf),
  343. bytes, offset);
  344. if (ret == 0)
  345. return 0;
  346. if (ret < bytes)
  347. break;
  348. buf += bytes;
  349. len -= bytes;
  350. offset = 0;
  351. }
  352. /* Install custom I/O completion handler if read verify is enabled */
  353. if (bio_data_dir(bio) == READ) {
  354. bip->bip_end_io = bio->bi_end_io;
  355. bio->bi_end_io = bio_integrity_endio;
  356. }
  357. /* Auto-generate integrity metadata if this is a write */
  358. if (bio_data_dir(bio) == WRITE)
  359. bio_integrity_generate(bio);
  360. return 0;
  361. }
  362. EXPORT_SYMBOL(bio_integrity_prep);
  363. /**
  364. * bio_integrity_verify - Verify integrity metadata for a bio
  365. * @bio: bio to verify
  366. *
  367. * Description: This function is called to verify the integrity of a
  368. * bio. The data in the bio io_vec is compared to the integrity
  369. * metadata returned by the HBA.
  370. */
  371. static int bio_integrity_verify(struct bio *bio)
  372. {
  373. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  374. struct blk_integrity_exchg bix;
  375. struct bio_vec *bv;
  376. sector_t sector = bio->bi_integrity->bip_sector;
  377. unsigned int i, sectors, total, ret;
  378. void *prot_buf = bio->bi_integrity->bip_buf;
  379. ret = total = 0;
  380. bix.disk_name = bio->bi_bdev->bd_disk->disk_name;
  381. bix.sector_size = bi->sector_size;
  382. bio_for_each_segment(bv, bio, i) {
  383. void *kaddr = kmap_atomic(bv->bv_page);
  384. bix.data_buf = kaddr + bv->bv_offset;
  385. bix.data_size = bv->bv_len;
  386. bix.prot_buf = prot_buf;
  387. bix.sector = sector;
  388. ret = bi->verify_fn(&bix);
  389. if (ret) {
  390. kunmap_atomic(kaddr);
  391. return ret;
  392. }
  393. sectors = bv->bv_len / bi->sector_size;
  394. sector += sectors;
  395. prot_buf += sectors * bi->tuple_size;
  396. total += sectors * bi->tuple_size;
  397. BUG_ON(total > bio->bi_integrity->bip_size);
  398. kunmap_atomic(kaddr);
  399. }
  400. return ret;
  401. }
  402. /**
  403. * bio_integrity_verify_fn - Integrity I/O completion worker
  404. * @work: Work struct stored in bio to be verified
  405. *
  406. * Description: This workqueue function is called to complete a READ
  407. * request. The function verifies the transferred integrity metadata
  408. * and then calls the original bio end_io function.
  409. */
  410. static void bio_integrity_verify_fn(struct work_struct *work)
  411. {
  412. struct bio_integrity_payload *bip =
  413. container_of(work, struct bio_integrity_payload, bip_work);
  414. struct bio *bio = bip->bip_bio;
  415. int error;
  416. error = bio_integrity_verify(bio);
  417. /* Restore original bio completion handler */
  418. bio->bi_end_io = bip->bip_end_io;
  419. bio_endio(bio, error);
  420. }
  421. /**
  422. * bio_integrity_endio - Integrity I/O completion function
  423. * @bio: Protected bio
  424. * @error: Pointer to errno
  425. *
  426. * Description: Completion for integrity I/O
  427. *
  428. * Normally I/O completion is done in interrupt context. However,
  429. * verifying I/O integrity is a time-consuming task which must be run
  430. * in process context. This function postpones completion
  431. * accordingly.
  432. */
  433. void bio_integrity_endio(struct bio *bio, int error)
  434. {
  435. struct bio_integrity_payload *bip = bio->bi_integrity;
  436. BUG_ON(bip->bip_bio != bio);
  437. /* In case of an I/O error there is no point in verifying the
  438. * integrity metadata. Restore original bio end_io handler
  439. * and run it.
  440. */
  441. if (error) {
  442. bio->bi_end_io = bip->bip_end_io;
  443. bio_endio(bio, error);
  444. return;
  445. }
  446. INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
  447. queue_work(kintegrityd_wq, &bip->bip_work);
  448. }
  449. EXPORT_SYMBOL(bio_integrity_endio);
  450. /**
  451. * bio_integrity_mark_head - Advance bip_vec skip bytes
  452. * @bip: Integrity vector to advance
  453. * @skip: Number of bytes to advance it
  454. */
  455. void bio_integrity_mark_head(struct bio_integrity_payload *bip,
  456. unsigned int skip)
  457. {
  458. struct bio_vec *iv;
  459. unsigned int i;
  460. bip_for_each_vec(iv, bip, i) {
  461. if (skip == 0) {
  462. bip->bip_idx = i;
  463. return;
  464. } else if (skip >= iv->bv_len) {
  465. skip -= iv->bv_len;
  466. } else { /* skip < iv->bv_len) */
  467. iv->bv_offset += skip;
  468. iv->bv_len -= skip;
  469. bip->bip_idx = i;
  470. return;
  471. }
  472. }
  473. }
  474. /**
  475. * bio_integrity_mark_tail - Truncate bip_vec to be len bytes long
  476. * @bip: Integrity vector to truncate
  477. * @len: New length of integrity vector
  478. */
  479. void bio_integrity_mark_tail(struct bio_integrity_payload *bip,
  480. unsigned int len)
  481. {
  482. struct bio_vec *iv;
  483. unsigned int i;
  484. bip_for_each_vec(iv, bip, i) {
  485. if (len == 0) {
  486. bip->bip_vcnt = i;
  487. return;
  488. } else if (len >= iv->bv_len) {
  489. len -= iv->bv_len;
  490. } else { /* len < iv->bv_len) */
  491. iv->bv_len = len;
  492. len = 0;
  493. }
  494. }
  495. }
  496. /**
  497. * bio_integrity_advance - Advance integrity vector
  498. * @bio: bio whose integrity vector to update
  499. * @bytes_done: number of data bytes that have been completed
  500. *
  501. * Description: This function calculates how many integrity bytes the
  502. * number of completed data bytes correspond to and advances the
  503. * integrity vector accordingly.
  504. */
  505. void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
  506. {
  507. struct bio_integrity_payload *bip = bio->bi_integrity;
  508. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  509. unsigned int nr_sectors;
  510. BUG_ON(bip == NULL);
  511. BUG_ON(bi == NULL);
  512. nr_sectors = bio_integrity_hw_sectors(bi, bytes_done >> 9);
  513. bio_integrity_mark_head(bip, nr_sectors * bi->tuple_size);
  514. }
  515. EXPORT_SYMBOL(bio_integrity_advance);
  516. /**
  517. * bio_integrity_trim - Trim integrity vector
  518. * @bio: bio whose integrity vector to update
  519. * @offset: offset to first data sector
  520. * @sectors: number of data sectors
  521. *
  522. * Description: Used to trim the integrity vector in a cloned bio.
  523. * The ivec will be advanced corresponding to 'offset' data sectors
  524. * and the length will be truncated corresponding to 'len' data
  525. * sectors.
  526. */
  527. void bio_integrity_trim(struct bio *bio, unsigned int offset,
  528. unsigned int sectors)
  529. {
  530. struct bio_integrity_payload *bip = bio->bi_integrity;
  531. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  532. unsigned int nr_sectors;
  533. BUG_ON(bip == NULL);
  534. BUG_ON(bi == NULL);
  535. BUG_ON(!bio_flagged(bio, BIO_CLONED));
  536. nr_sectors = bio_integrity_hw_sectors(bi, sectors);
  537. bip->bip_sector = bip->bip_sector + offset;
  538. bio_integrity_mark_head(bip, offset * bi->tuple_size);
  539. bio_integrity_mark_tail(bip, sectors * bi->tuple_size);
  540. }
  541. EXPORT_SYMBOL(bio_integrity_trim);
  542. /**
  543. * bio_integrity_split - Split integrity metadata
  544. * @bio: Protected bio
  545. * @bp: Resulting bio_pair
  546. * @sectors: Offset
  547. *
  548. * Description: Splits an integrity page into a bio_pair.
  549. */
  550. void bio_integrity_split(struct bio *bio, struct bio_pair *bp, int sectors)
  551. {
  552. struct blk_integrity *bi;
  553. struct bio_integrity_payload *bip = bio->bi_integrity;
  554. unsigned int nr_sectors;
  555. if (bio_integrity(bio) == 0)
  556. return;
  557. bi = bdev_get_integrity(bio->bi_bdev);
  558. BUG_ON(bi == NULL);
  559. BUG_ON(bip->bip_vcnt != 1);
  560. nr_sectors = bio_integrity_hw_sectors(bi, sectors);
  561. bp->bio1.bi_integrity = &bp->bip1;
  562. bp->bio2.bi_integrity = &bp->bip2;
  563. bp->iv1 = bip->bip_vec[bip->bip_idx];
  564. bp->iv2 = bip->bip_vec[bip->bip_idx];
  565. bp->bip1.bip_vec = &bp->iv1;
  566. bp->bip2.bip_vec = &bp->iv2;
  567. bp->iv1.bv_len = sectors * bi->tuple_size;
  568. bp->iv2.bv_offset += sectors * bi->tuple_size;
  569. bp->iv2.bv_len -= sectors * bi->tuple_size;
  570. bp->bip1.bip_sector = bio->bi_integrity->bip_sector;
  571. bp->bip2.bip_sector = bio->bi_integrity->bip_sector + nr_sectors;
  572. bp->bip1.bip_vcnt = bp->bip2.bip_vcnt = 1;
  573. bp->bip1.bip_idx = bp->bip2.bip_idx = 0;
  574. }
  575. EXPORT_SYMBOL(bio_integrity_split);
  576. /**
  577. * bio_integrity_clone - Callback for cloning bios with integrity metadata
  578. * @bio: New bio
  579. * @bio_src: Original bio
  580. * @gfp_mask: Memory allocation mask
  581. *
  582. * Description: Called to allocate a bip when cloning a bio
  583. */
  584. int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
  585. gfp_t gfp_mask)
  586. {
  587. struct bio_integrity_payload *bip_src = bio_src->bi_integrity;
  588. struct bio_integrity_payload *bip;
  589. BUG_ON(bip_src == NULL);
  590. bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt);
  591. if (bip == NULL)
  592. return -EIO;
  593. memcpy(bip->bip_vec, bip_src->bip_vec,
  594. bip_src->bip_vcnt * sizeof(struct bio_vec));
  595. bip->bip_sector = bip_src->bip_sector;
  596. bip->bip_vcnt = bip_src->bip_vcnt;
  597. bip->bip_idx = bip_src->bip_idx;
  598. return 0;
  599. }
  600. EXPORT_SYMBOL(bio_integrity_clone);
  601. int bioset_integrity_create(struct bio_set *bs, int pool_size)
  602. {
  603. if (bs->bio_integrity_pool)
  604. return 0;
  605. bs->bio_integrity_pool = mempool_create_slab_pool(pool_size, bip_slab);
  606. if (!bs->bio_integrity_pool)
  607. return -1;
  608. bs->bvec_integrity_pool = biovec_create_pool(bs, pool_size);
  609. if (!bs->bvec_integrity_pool) {
  610. mempool_destroy(bs->bio_integrity_pool);
  611. return -1;
  612. }
  613. return 0;
  614. }
  615. EXPORT_SYMBOL(bioset_integrity_create);
  616. void bioset_integrity_free(struct bio_set *bs)
  617. {
  618. if (bs->bio_integrity_pool)
  619. mempool_destroy(bs->bio_integrity_pool);
  620. if (bs->bvec_integrity_pool)
  621. mempool_destroy(bs->bvec_integrity_pool);
  622. }
  623. EXPORT_SYMBOL(bioset_integrity_free);
  624. void __init bio_integrity_init(void)
  625. {
  626. /*
  627. * kintegrityd won't block much but may burn a lot of CPU cycles.
  628. * Make it highpri CPU intensive wq with max concurrency of 1.
  629. */
  630. kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
  631. WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
  632. if (!kintegrityd_wq)
  633. panic("Failed to create kintegrityd\n");
  634. bip_slab = kmem_cache_create("bio_integrity_payload",
  635. sizeof(struct bio_integrity_payload) +
  636. sizeof(struct bio_vec) * BIP_INLINE_VECS,
  637. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  638. if (!bip_slab)
  639. panic("Failed to create slab\n");
  640. }