bio-integrity.c 14 KB

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