blk-integrity.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * blk-integrity.c - Block layer data integrity extensions
  3. *
  4. * Copyright (C) 2007, 2008 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/backing-dev.h>
  24. #include <linux/mempool.h>
  25. #include <linux/bio.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/export.h>
  28. #include <linux/slab.h>
  29. #include "blk.h"
  30. static struct kmem_cache *integrity_cachep;
  31. static const char *bi_unsupported_name = "unsupported";
  32. /**
  33. * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
  34. * @q: request queue
  35. * @bio: bio with integrity metadata attached
  36. *
  37. * Description: Returns the number of elements required in a
  38. * scatterlist corresponding to the integrity metadata in a bio.
  39. */
  40. int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
  41. {
  42. struct bio_vec iv, ivprv = { NULL };
  43. unsigned int segments = 0;
  44. unsigned int seg_size = 0;
  45. struct bvec_iter iter;
  46. int prev = 0;
  47. bio_for_each_integrity_vec(iv, bio, iter) {
  48. if (prev) {
  49. if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv))
  50. goto new_segment;
  51. if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv))
  52. goto new_segment;
  53. if (seg_size + iv.bv_len > queue_max_segment_size(q))
  54. goto new_segment;
  55. seg_size += iv.bv_len;
  56. } else {
  57. new_segment:
  58. segments++;
  59. seg_size = iv.bv_len;
  60. }
  61. prev = 1;
  62. ivprv = iv;
  63. }
  64. return segments;
  65. }
  66. EXPORT_SYMBOL(blk_rq_count_integrity_sg);
  67. /**
  68. * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
  69. * @q: request queue
  70. * @bio: bio with integrity metadata attached
  71. * @sglist: target scatterlist
  72. *
  73. * Description: Map the integrity vectors in request into a
  74. * scatterlist. The scatterlist must be big enough to hold all
  75. * elements. I.e. sized using blk_rq_count_integrity_sg().
  76. */
  77. int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
  78. struct scatterlist *sglist)
  79. {
  80. struct bio_vec iv, ivprv = { NULL };
  81. struct scatterlist *sg = NULL;
  82. unsigned int segments = 0;
  83. struct bvec_iter iter;
  84. int prev = 0;
  85. bio_for_each_integrity_vec(iv, bio, iter) {
  86. if (prev) {
  87. if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv))
  88. goto new_segment;
  89. if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv))
  90. goto new_segment;
  91. if (sg->length + iv.bv_len > queue_max_segment_size(q))
  92. goto new_segment;
  93. sg->length += iv.bv_len;
  94. } else {
  95. new_segment:
  96. if (!sg)
  97. sg = sglist;
  98. else {
  99. sg_unmark_end(sg);
  100. sg = sg_next(sg);
  101. }
  102. sg_set_page(sg, iv.bv_page, iv.bv_len, iv.bv_offset);
  103. segments++;
  104. }
  105. prev = 1;
  106. ivprv = iv;
  107. }
  108. if (sg)
  109. sg_mark_end(sg);
  110. return segments;
  111. }
  112. EXPORT_SYMBOL(blk_rq_map_integrity_sg);
  113. /**
  114. * blk_integrity_compare - Compare integrity profile of two disks
  115. * @gd1: Disk to compare
  116. * @gd2: Disk to compare
  117. *
  118. * Description: Meta-devices like DM and MD need to verify that all
  119. * sub-devices use the same integrity format before advertising to
  120. * upper layers that they can send/receive integrity metadata. This
  121. * function can be used to check whether two gendisk devices have
  122. * compatible integrity formats.
  123. */
  124. int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
  125. {
  126. struct blk_integrity *b1 = gd1->integrity;
  127. struct blk_integrity *b2 = gd2->integrity;
  128. if (!b1 && !b2)
  129. return 0;
  130. if (!b1 || !b2)
  131. return -1;
  132. if (b1->interval != b2->interval) {
  133. pr_err("%s: %s/%s protection interval %u != %u\n",
  134. __func__, gd1->disk_name, gd2->disk_name,
  135. b1->interval, b2->interval);
  136. return -1;
  137. }
  138. if (b1->tuple_size != b2->tuple_size) {
  139. printk(KERN_ERR "%s: %s/%s tuple sz %u != %u\n", __func__,
  140. gd1->disk_name, gd2->disk_name,
  141. b1->tuple_size, b2->tuple_size);
  142. return -1;
  143. }
  144. if (b1->tag_size && b2->tag_size && (b1->tag_size != b2->tag_size)) {
  145. printk(KERN_ERR "%s: %s/%s tag sz %u != %u\n", __func__,
  146. gd1->disk_name, gd2->disk_name,
  147. b1->tag_size, b2->tag_size);
  148. return -1;
  149. }
  150. if (strcmp(b1->name, b2->name)) {
  151. printk(KERN_ERR "%s: %s/%s type %s != %s\n", __func__,
  152. gd1->disk_name, gd2->disk_name,
  153. b1->name, b2->name);
  154. return -1;
  155. }
  156. return 0;
  157. }
  158. EXPORT_SYMBOL(blk_integrity_compare);
  159. bool blk_integrity_merge_rq(struct request_queue *q, struct request *req,
  160. struct request *next)
  161. {
  162. if (blk_integrity_rq(req) == 0 && blk_integrity_rq(next) == 0)
  163. return true;
  164. if (blk_integrity_rq(req) == 0 || blk_integrity_rq(next) == 0)
  165. return false;
  166. if (bio_integrity(req->bio)->bip_flags !=
  167. bio_integrity(next->bio)->bip_flags)
  168. return false;
  169. if (req->nr_integrity_segments + next->nr_integrity_segments >
  170. q->limits.max_integrity_segments)
  171. return false;
  172. return true;
  173. }
  174. EXPORT_SYMBOL(blk_integrity_merge_rq);
  175. bool blk_integrity_merge_bio(struct request_queue *q, struct request *req,
  176. struct bio *bio)
  177. {
  178. int nr_integrity_segs;
  179. struct bio *next = bio->bi_next;
  180. if (blk_integrity_rq(req) == 0 && bio_integrity(bio) == NULL)
  181. return true;
  182. if (blk_integrity_rq(req) == 0 || bio_integrity(bio) == NULL)
  183. return false;
  184. if (bio_integrity(req->bio)->bip_flags != bio_integrity(bio)->bip_flags)
  185. return false;
  186. bio->bi_next = NULL;
  187. nr_integrity_segs = blk_rq_count_integrity_sg(q, bio);
  188. bio->bi_next = next;
  189. if (req->nr_integrity_segments + nr_integrity_segs >
  190. q->limits.max_integrity_segments)
  191. return false;
  192. req->nr_integrity_segments += nr_integrity_segs;
  193. return true;
  194. }
  195. EXPORT_SYMBOL(blk_integrity_merge_bio);
  196. struct integrity_sysfs_entry {
  197. struct attribute attr;
  198. ssize_t (*show)(struct blk_integrity *, char *);
  199. ssize_t (*store)(struct blk_integrity *, const char *, size_t);
  200. };
  201. static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr,
  202. char *page)
  203. {
  204. struct blk_integrity *bi =
  205. container_of(kobj, struct blk_integrity, kobj);
  206. struct integrity_sysfs_entry *entry =
  207. container_of(attr, struct integrity_sysfs_entry, attr);
  208. return entry->show(bi, page);
  209. }
  210. static ssize_t integrity_attr_store(struct kobject *kobj,
  211. struct attribute *attr, const char *page,
  212. size_t count)
  213. {
  214. struct blk_integrity *bi =
  215. container_of(kobj, struct blk_integrity, kobj);
  216. struct integrity_sysfs_entry *entry =
  217. container_of(attr, struct integrity_sysfs_entry, attr);
  218. ssize_t ret = 0;
  219. if (entry->store)
  220. ret = entry->store(bi, page, count);
  221. return ret;
  222. }
  223. static ssize_t integrity_format_show(struct blk_integrity *bi, char *page)
  224. {
  225. if (bi != NULL && bi->name != NULL)
  226. return sprintf(page, "%s\n", bi->name);
  227. else
  228. return sprintf(page, "none\n");
  229. }
  230. static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page)
  231. {
  232. if (bi != NULL)
  233. return sprintf(page, "%u\n", bi->tag_size);
  234. else
  235. return sprintf(page, "0\n");
  236. }
  237. static ssize_t integrity_verify_store(struct blk_integrity *bi,
  238. const char *page, size_t count)
  239. {
  240. char *p = (char *) page;
  241. unsigned long val = simple_strtoul(p, &p, 10);
  242. if (val)
  243. bi->flags |= BLK_INTEGRITY_VERIFY;
  244. else
  245. bi->flags &= ~BLK_INTEGRITY_VERIFY;
  246. return count;
  247. }
  248. static ssize_t integrity_verify_show(struct blk_integrity *bi, char *page)
  249. {
  250. return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_VERIFY) != 0);
  251. }
  252. static ssize_t integrity_generate_store(struct blk_integrity *bi,
  253. const char *page, size_t count)
  254. {
  255. char *p = (char *) page;
  256. unsigned long val = simple_strtoul(p, &p, 10);
  257. if (val)
  258. bi->flags |= BLK_INTEGRITY_GENERATE;
  259. else
  260. bi->flags &= ~BLK_INTEGRITY_GENERATE;
  261. return count;
  262. }
  263. static ssize_t integrity_generate_show(struct blk_integrity *bi, char *page)
  264. {
  265. return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_GENERATE) != 0);
  266. }
  267. static ssize_t integrity_device_show(struct blk_integrity *bi, char *page)
  268. {
  269. return sprintf(page, "%u\n",
  270. (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) != 0);
  271. }
  272. static struct integrity_sysfs_entry integrity_format_entry = {
  273. .attr = { .name = "format", .mode = S_IRUGO },
  274. .show = integrity_format_show,
  275. };
  276. static struct integrity_sysfs_entry integrity_tag_size_entry = {
  277. .attr = { .name = "tag_size", .mode = S_IRUGO },
  278. .show = integrity_tag_size_show,
  279. };
  280. static struct integrity_sysfs_entry integrity_verify_entry = {
  281. .attr = { .name = "read_verify", .mode = S_IRUGO | S_IWUSR },
  282. .show = integrity_verify_show,
  283. .store = integrity_verify_store,
  284. };
  285. static struct integrity_sysfs_entry integrity_generate_entry = {
  286. .attr = { .name = "write_generate", .mode = S_IRUGO | S_IWUSR },
  287. .show = integrity_generate_show,
  288. .store = integrity_generate_store,
  289. };
  290. static struct integrity_sysfs_entry integrity_device_entry = {
  291. .attr = { .name = "device_is_integrity_capable", .mode = S_IRUGO },
  292. .show = integrity_device_show,
  293. };
  294. static struct attribute *integrity_attrs[] = {
  295. &integrity_format_entry.attr,
  296. &integrity_tag_size_entry.attr,
  297. &integrity_verify_entry.attr,
  298. &integrity_generate_entry.attr,
  299. &integrity_device_entry.attr,
  300. NULL,
  301. };
  302. static const struct sysfs_ops integrity_ops = {
  303. .show = &integrity_attr_show,
  304. .store = &integrity_attr_store,
  305. };
  306. static int __init blk_dev_integrity_init(void)
  307. {
  308. integrity_cachep = kmem_cache_create("blkdev_integrity",
  309. sizeof(struct blk_integrity),
  310. 0, SLAB_PANIC, NULL);
  311. return 0;
  312. }
  313. subsys_initcall(blk_dev_integrity_init);
  314. static void blk_integrity_release(struct kobject *kobj)
  315. {
  316. struct blk_integrity *bi =
  317. container_of(kobj, struct blk_integrity, kobj);
  318. kmem_cache_free(integrity_cachep, bi);
  319. }
  320. static struct kobj_type integrity_ktype = {
  321. .default_attrs = integrity_attrs,
  322. .sysfs_ops = &integrity_ops,
  323. .release = blk_integrity_release,
  324. };
  325. bool blk_integrity_is_initialized(struct gendisk *disk)
  326. {
  327. struct blk_integrity *bi = blk_get_integrity(disk);
  328. return (bi && bi->name && strcmp(bi->name, bi_unsupported_name) != 0);
  329. }
  330. EXPORT_SYMBOL(blk_integrity_is_initialized);
  331. /**
  332. * blk_integrity_register - Register a gendisk as being integrity-capable
  333. * @disk: struct gendisk pointer to make integrity-aware
  334. * @template: optional integrity profile to register
  335. *
  336. * Description: When a device needs to advertise itself as being able
  337. * to send/receive integrity metadata it must use this function to
  338. * register the capability with the block layer. The template is a
  339. * blk_integrity struct with values appropriate for the underlying
  340. * hardware. If template is NULL the new profile is allocated but
  341. * not filled out. See Documentation/block/data-integrity.txt.
  342. */
  343. int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
  344. {
  345. struct blk_integrity *bi;
  346. BUG_ON(disk == NULL);
  347. if (disk->integrity == NULL) {
  348. bi = kmem_cache_alloc(integrity_cachep,
  349. GFP_KERNEL | __GFP_ZERO);
  350. if (!bi)
  351. return -1;
  352. if (kobject_init_and_add(&bi->kobj, &integrity_ktype,
  353. &disk_to_dev(disk)->kobj,
  354. "%s", "integrity")) {
  355. kmem_cache_free(integrity_cachep, bi);
  356. return -1;
  357. }
  358. kobject_uevent(&bi->kobj, KOBJ_ADD);
  359. bi->flags |= BLK_INTEGRITY_VERIFY | BLK_INTEGRITY_GENERATE;
  360. bi->interval = queue_logical_block_size(disk->queue);
  361. disk->integrity = bi;
  362. } else
  363. bi = disk->integrity;
  364. /* Use the provided profile as template */
  365. if (template != NULL) {
  366. bi->name = template->name;
  367. bi->generate_fn = template->generate_fn;
  368. bi->verify_fn = template->verify_fn;
  369. bi->tuple_size = template->tuple_size;
  370. bi->tag_size = template->tag_size;
  371. bi->flags |= template->flags;
  372. } else
  373. bi->name = bi_unsupported_name;
  374. disk->queue->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES;
  375. return 0;
  376. }
  377. EXPORT_SYMBOL(blk_integrity_register);
  378. /**
  379. * blk_integrity_unregister - Remove block integrity profile
  380. * @disk: disk whose integrity profile to deallocate
  381. *
  382. * Description: This function frees all memory used by the block
  383. * integrity profile. To be called at device teardown.
  384. */
  385. void blk_integrity_unregister(struct gendisk *disk)
  386. {
  387. struct blk_integrity *bi;
  388. if (!disk || !disk->integrity)
  389. return;
  390. disk->queue->backing_dev_info.capabilities &= ~BDI_CAP_STABLE_WRITES;
  391. bi = disk->integrity;
  392. kobject_uevent(&bi->kobj, KOBJ_REMOVE);
  393. kobject_del(&bi->kobj);
  394. kobject_put(&bi->kobj);
  395. disk->integrity = NULL;
  396. }
  397. EXPORT_SYMBOL(blk_integrity_unregister);