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