bio-integrity.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. void blk_flush_integrity(void)
  32. {
  33. flush_workqueue(kintegrityd_wq);
  34. }
  35. /**
  36. * bio_integrity_alloc - Allocate integrity payload and attach it to bio
  37. * @bio: bio to attach integrity metadata to
  38. * @gfp_mask: Memory allocation mask
  39. * @nr_vecs: Number of integrity metadata scatter-gather elements
  40. *
  41. * Description: This function prepares a bio for attaching integrity
  42. * metadata. nr_vecs specifies the maximum number of pages containing
  43. * integrity metadata that can be attached.
  44. */
  45. struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
  46. gfp_t gfp_mask,
  47. unsigned int nr_vecs)
  48. {
  49. struct bio_integrity_payload *bip;
  50. struct bio_set *bs = bio->bi_pool;
  51. unsigned long idx = BIO_POOL_NONE;
  52. unsigned inline_vecs;
  53. if (!bs || !bs->bio_integrity_pool) {
  54. bip = kmalloc(sizeof(struct bio_integrity_payload) +
  55. sizeof(struct bio_vec) * nr_vecs, gfp_mask);
  56. inline_vecs = nr_vecs;
  57. } else {
  58. bip = mempool_alloc(bs->bio_integrity_pool, gfp_mask);
  59. inline_vecs = BIP_INLINE_VECS;
  60. }
  61. if (unlikely(!bip))
  62. return ERR_PTR(-ENOMEM);
  63. memset(bip, 0, sizeof(*bip));
  64. if (nr_vecs > inline_vecs) {
  65. bip->bip_vec = bvec_alloc(gfp_mask, nr_vecs, &idx,
  66. bs->bvec_integrity_pool);
  67. if (!bip->bip_vec)
  68. goto err;
  69. bip->bip_max_vcnt = bvec_nr_vecs(idx);
  70. } else {
  71. bip->bip_vec = bip->bip_inline_vecs;
  72. bip->bip_max_vcnt = inline_vecs;
  73. }
  74. bip->bip_slab = idx;
  75. bip->bip_bio = bio;
  76. bio->bi_integrity = bip;
  77. bio->bi_rw |= REQ_INTEGRITY;
  78. return bip;
  79. err:
  80. mempool_free(bip, bs->bio_integrity_pool);
  81. return ERR_PTR(-ENOMEM);
  82. }
  83. EXPORT_SYMBOL(bio_integrity_alloc);
  84. /**
  85. * bio_integrity_free - Free bio integrity payload
  86. * @bio: bio containing bip to be freed
  87. *
  88. * Description: Used to free the integrity portion of a bio. Usually
  89. * called from bio_free().
  90. */
  91. void bio_integrity_free(struct bio *bio)
  92. {
  93. struct bio_integrity_payload *bip = bio_integrity(bio);
  94. struct bio_set *bs = bio->bi_pool;
  95. if (bip->bip_flags & BIP_BLOCK_INTEGRITY)
  96. kfree(page_address(bip->bip_vec->bv_page) +
  97. bip->bip_vec->bv_offset);
  98. if (bs && bs->bio_integrity_pool) {
  99. if (bip->bip_slab != BIO_POOL_NONE)
  100. bvec_free(bs->bvec_integrity_pool, bip->bip_vec,
  101. bip->bip_slab);
  102. mempool_free(bip, bs->bio_integrity_pool);
  103. } else {
  104. kfree(bip);
  105. }
  106. bio->bi_integrity = NULL;
  107. }
  108. EXPORT_SYMBOL(bio_integrity_free);
  109. /**
  110. * bio_integrity_add_page - Attach integrity metadata
  111. * @bio: bio to update
  112. * @page: page containing integrity metadata
  113. * @len: number of bytes of integrity metadata in page
  114. * @offset: start offset within page
  115. *
  116. * Description: Attach a page containing integrity metadata to bio.
  117. */
  118. int bio_integrity_add_page(struct bio *bio, struct page *page,
  119. unsigned int len, unsigned int offset)
  120. {
  121. struct bio_integrity_payload *bip = bio_integrity(bio);
  122. struct bio_vec *iv;
  123. if (bip->bip_vcnt >= bip->bip_max_vcnt) {
  124. printk(KERN_ERR "%s: bip_vec full\n", __func__);
  125. return 0;
  126. }
  127. iv = bip->bip_vec + bip->bip_vcnt;
  128. if (bip->bip_vcnt &&
  129. bvec_gap_to_prev(bdev_get_queue(bio->bi_bdev),
  130. &bip->bip_vec[bip->bip_vcnt - 1], offset))
  131. return 0;
  132. iv->bv_page = page;
  133. iv->bv_len = len;
  134. iv->bv_offset = offset;
  135. bip->bip_vcnt++;
  136. return len;
  137. }
  138. EXPORT_SYMBOL(bio_integrity_add_page);
  139. /**
  140. * bio_integrity_enabled - Check whether integrity can be passed
  141. * @bio: bio to check
  142. *
  143. * Description: Determines whether bio_integrity_prep() can be called
  144. * on this bio or not. bio data direction and target device must be
  145. * set prior to calling. The functions honors the write_generate and
  146. * read_verify flags in sysfs.
  147. */
  148. bool bio_integrity_enabled(struct bio *bio)
  149. {
  150. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  151. if (!bio_is_rw(bio))
  152. return false;
  153. /* Already protected? */
  154. if (bio_integrity(bio))
  155. return false;
  156. if (bi == NULL)
  157. return false;
  158. if (bio_data_dir(bio) == READ && bi->profile->verify_fn != NULL &&
  159. (bi->flags & BLK_INTEGRITY_VERIFY))
  160. return true;
  161. if (bio_data_dir(bio) == WRITE && bi->profile->generate_fn != NULL &&
  162. (bi->flags & BLK_INTEGRITY_GENERATE))
  163. return true;
  164. return false;
  165. }
  166. EXPORT_SYMBOL(bio_integrity_enabled);
  167. /**
  168. * bio_integrity_intervals - Return number of integrity intervals for a bio
  169. * @bi: blk_integrity profile for device
  170. * @sectors: Size of the bio in 512-byte sectors
  171. *
  172. * Description: The block layer calculates everything in 512 byte
  173. * sectors but integrity metadata is done in terms of the data integrity
  174. * interval size of the storage device. Convert the block layer sectors
  175. * to the appropriate number of integrity intervals.
  176. */
  177. static inline unsigned int bio_integrity_intervals(struct blk_integrity *bi,
  178. unsigned int sectors)
  179. {
  180. return sectors >> (bi->interval_exp - 9);
  181. }
  182. static inline unsigned int bio_integrity_bytes(struct blk_integrity *bi,
  183. unsigned int sectors)
  184. {
  185. return bio_integrity_intervals(bi, sectors) * bi->tuple_size;
  186. }
  187. /**
  188. * bio_integrity_process - Process integrity metadata for a bio
  189. * @bio: bio to generate/verify integrity metadata for
  190. * @proc_fn: Pointer to the relevant processing function
  191. */
  192. static int bio_integrity_process(struct bio *bio,
  193. integrity_processing_fn *proc_fn)
  194. {
  195. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  196. struct blk_integrity_iter iter;
  197. struct bvec_iter bviter;
  198. struct bio_vec bv;
  199. struct bio_integrity_payload *bip = bio_integrity(bio);
  200. unsigned int ret = 0;
  201. void *prot_buf = page_address(bip->bip_vec->bv_page) +
  202. bip->bip_vec->bv_offset;
  203. iter.disk_name = bio->bi_bdev->bd_disk->disk_name;
  204. iter.interval = 1 << bi->interval_exp;
  205. iter.seed = bip_get_seed(bip);
  206. iter.prot_buf = prot_buf;
  207. bio_for_each_segment(bv, bio, bviter) {
  208. void *kaddr = kmap_atomic(bv.bv_page);
  209. iter.data_buf = kaddr + bv.bv_offset;
  210. iter.data_size = bv.bv_len;
  211. ret = proc_fn(&iter);
  212. if (ret) {
  213. kunmap_atomic(kaddr);
  214. return ret;
  215. }
  216. kunmap_atomic(kaddr);
  217. }
  218. return ret;
  219. }
  220. /**
  221. * bio_integrity_prep - Prepare bio for integrity I/O
  222. * @bio: bio to prepare
  223. *
  224. * Description: Allocates a buffer for integrity metadata, maps the
  225. * pages and attaches them to a bio. The bio must have data
  226. * direction, target device and start sector set priot to calling. In
  227. * the WRITE case, integrity metadata will be generated using the
  228. * block device's integrity function. In the READ case, the buffer
  229. * will be prepared for DMA and a suitable end_io handler set up.
  230. */
  231. int bio_integrity_prep(struct bio *bio)
  232. {
  233. struct bio_integrity_payload *bip;
  234. struct blk_integrity *bi;
  235. struct request_queue *q;
  236. void *buf;
  237. unsigned long start, end;
  238. unsigned int len, nr_pages;
  239. unsigned int bytes, offset, i;
  240. unsigned int intervals;
  241. bi = bdev_get_integrity(bio->bi_bdev);
  242. q = bdev_get_queue(bio->bi_bdev);
  243. BUG_ON(bi == NULL);
  244. BUG_ON(bio_integrity(bio));
  245. intervals = bio_integrity_intervals(bi, bio_sectors(bio));
  246. /* Allocate kernel buffer for protection data */
  247. len = intervals * bi->tuple_size;
  248. buf = kmalloc(len, GFP_NOIO | q->bounce_gfp);
  249. if (unlikely(buf == NULL)) {
  250. printk(KERN_ERR "could not allocate integrity buffer\n");
  251. return -ENOMEM;
  252. }
  253. end = (((unsigned long) buf) + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  254. start = ((unsigned long) buf) >> PAGE_SHIFT;
  255. nr_pages = end - start;
  256. /* Allocate bio integrity payload and integrity vectors */
  257. bip = bio_integrity_alloc(bio, GFP_NOIO, nr_pages);
  258. if (IS_ERR(bip)) {
  259. printk(KERN_ERR "could not allocate data integrity bioset\n");
  260. kfree(buf);
  261. return PTR_ERR(bip);
  262. }
  263. bip->bip_flags |= BIP_BLOCK_INTEGRITY;
  264. bip->bip_iter.bi_size = len;
  265. bip_set_seed(bip, bio->bi_iter.bi_sector);
  266. if (bi->flags & BLK_INTEGRITY_IP_CHECKSUM)
  267. bip->bip_flags |= BIP_IP_CHECKSUM;
  268. /* Map it */
  269. offset = offset_in_page(buf);
  270. for (i = 0 ; i < nr_pages ; i++) {
  271. int ret;
  272. bytes = PAGE_SIZE - offset;
  273. if (len <= 0)
  274. break;
  275. if (bytes > len)
  276. bytes = len;
  277. ret = bio_integrity_add_page(bio, virt_to_page(buf),
  278. bytes, offset);
  279. if (ret == 0)
  280. return 0;
  281. if (ret < bytes)
  282. break;
  283. buf += bytes;
  284. len -= bytes;
  285. offset = 0;
  286. }
  287. /* Install custom I/O completion handler if read verify is enabled */
  288. if (bio_data_dir(bio) == READ) {
  289. bip->bip_end_io = bio->bi_end_io;
  290. bio->bi_end_io = bio_integrity_endio;
  291. }
  292. /* Auto-generate integrity metadata if this is a write */
  293. if (bio_data_dir(bio) == WRITE)
  294. bio_integrity_process(bio, bi->profile->generate_fn);
  295. return 0;
  296. }
  297. EXPORT_SYMBOL(bio_integrity_prep);
  298. /**
  299. * bio_integrity_verify_fn - Integrity I/O completion worker
  300. * @work: Work struct stored in bio to be verified
  301. *
  302. * Description: This workqueue function is called to complete a READ
  303. * request. The function verifies the transferred integrity metadata
  304. * and then calls the original bio end_io function.
  305. */
  306. static void bio_integrity_verify_fn(struct work_struct *work)
  307. {
  308. struct bio_integrity_payload *bip =
  309. container_of(work, struct bio_integrity_payload, bip_work);
  310. struct bio *bio = bip->bip_bio;
  311. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  312. bio->bi_error = bio_integrity_process(bio, bi->profile->verify_fn);
  313. /* Restore original bio completion handler */
  314. bio->bi_end_io = bip->bip_end_io;
  315. bio_endio(bio);
  316. }
  317. /**
  318. * bio_integrity_endio - Integrity I/O completion function
  319. * @bio: Protected bio
  320. * @error: Pointer to errno
  321. *
  322. * Description: Completion for integrity I/O
  323. *
  324. * Normally I/O completion is done in interrupt context. However,
  325. * verifying I/O integrity is a time-consuming task which must be run
  326. * in process context. This function postpones completion
  327. * accordingly.
  328. */
  329. void bio_integrity_endio(struct bio *bio)
  330. {
  331. struct bio_integrity_payload *bip = bio_integrity(bio);
  332. BUG_ON(bip->bip_bio != bio);
  333. /* In case of an I/O error there is no point in verifying the
  334. * integrity metadata. Restore original bio end_io handler
  335. * and run it.
  336. */
  337. if (bio->bi_error) {
  338. bio->bi_end_io = bip->bip_end_io;
  339. bio_endio(bio);
  340. return;
  341. }
  342. INIT_WORK(&bip->bip_work, bio_integrity_verify_fn);
  343. queue_work(kintegrityd_wq, &bip->bip_work);
  344. }
  345. EXPORT_SYMBOL(bio_integrity_endio);
  346. /**
  347. * bio_integrity_advance - Advance integrity vector
  348. * @bio: bio whose integrity vector to update
  349. * @bytes_done: number of data bytes that have been completed
  350. *
  351. * Description: This function calculates how many integrity bytes the
  352. * number of completed data bytes correspond to and advances the
  353. * integrity vector accordingly.
  354. */
  355. void bio_integrity_advance(struct bio *bio, unsigned int bytes_done)
  356. {
  357. struct bio_integrity_payload *bip = bio_integrity(bio);
  358. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  359. unsigned bytes = bio_integrity_bytes(bi, bytes_done >> 9);
  360. bvec_iter_advance(bip->bip_vec, &bip->bip_iter, bytes);
  361. }
  362. EXPORT_SYMBOL(bio_integrity_advance);
  363. /**
  364. * bio_integrity_trim - Trim integrity vector
  365. * @bio: bio whose integrity vector to update
  366. * @offset: offset to first data sector
  367. * @sectors: number of data sectors
  368. *
  369. * Description: Used to trim the integrity vector in a cloned bio.
  370. * The ivec will be advanced corresponding to 'offset' data sectors
  371. * and the length will be truncated corresponding to 'len' data
  372. * sectors.
  373. */
  374. void bio_integrity_trim(struct bio *bio, unsigned int offset,
  375. unsigned int sectors)
  376. {
  377. struct bio_integrity_payload *bip = bio_integrity(bio);
  378. struct blk_integrity *bi = bdev_get_integrity(bio->bi_bdev);
  379. bio_integrity_advance(bio, offset << 9);
  380. bip->bip_iter.bi_size = bio_integrity_bytes(bi, sectors);
  381. }
  382. EXPORT_SYMBOL(bio_integrity_trim);
  383. /**
  384. * bio_integrity_clone - Callback for cloning bios with integrity metadata
  385. * @bio: New bio
  386. * @bio_src: Original bio
  387. * @gfp_mask: Memory allocation mask
  388. *
  389. * Description: Called to allocate a bip when cloning a bio
  390. */
  391. int bio_integrity_clone(struct bio *bio, struct bio *bio_src,
  392. gfp_t gfp_mask)
  393. {
  394. struct bio_integrity_payload *bip_src = bio_integrity(bio_src);
  395. struct bio_integrity_payload *bip;
  396. BUG_ON(bip_src == NULL);
  397. bip = bio_integrity_alloc(bio, gfp_mask, bip_src->bip_vcnt);
  398. if (IS_ERR(bip))
  399. return PTR_ERR(bip);
  400. memcpy(bip->bip_vec, bip_src->bip_vec,
  401. bip_src->bip_vcnt * sizeof(struct bio_vec));
  402. bip->bip_vcnt = bip_src->bip_vcnt;
  403. bip->bip_iter = bip_src->bip_iter;
  404. return 0;
  405. }
  406. EXPORT_SYMBOL(bio_integrity_clone);
  407. int bioset_integrity_create(struct bio_set *bs, int pool_size)
  408. {
  409. if (bs->bio_integrity_pool)
  410. return 0;
  411. bs->bio_integrity_pool = mempool_create_slab_pool(pool_size, bip_slab);
  412. if (!bs->bio_integrity_pool)
  413. return -1;
  414. bs->bvec_integrity_pool = biovec_create_pool(pool_size);
  415. if (!bs->bvec_integrity_pool) {
  416. mempool_destroy(bs->bio_integrity_pool);
  417. return -1;
  418. }
  419. return 0;
  420. }
  421. EXPORT_SYMBOL(bioset_integrity_create);
  422. void bioset_integrity_free(struct bio_set *bs)
  423. {
  424. if (bs->bio_integrity_pool)
  425. mempool_destroy(bs->bio_integrity_pool);
  426. if (bs->bvec_integrity_pool)
  427. mempool_destroy(bs->bvec_integrity_pool);
  428. }
  429. EXPORT_SYMBOL(bioset_integrity_free);
  430. void __init bio_integrity_init(void)
  431. {
  432. /*
  433. * kintegrityd won't block much but may burn a lot of CPU cycles.
  434. * Make it highpri CPU intensive wq with max concurrency of 1.
  435. */
  436. kintegrityd_wq = alloc_workqueue("kintegrityd", WQ_MEM_RECLAIM |
  437. WQ_HIGHPRI | WQ_CPU_INTENSIVE, 1);
  438. if (!kintegrityd_wq)
  439. panic("Failed to create kintegrityd\n");
  440. bip_slab = kmem_cache_create("bio_integrity_payload",
  441. sizeof(struct bio_integrity_payload) +
  442. sizeof(struct bio_vec) * BIP_INLINE_VECS,
  443. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  444. }