bio-integrity.c 13 KB

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