blk-integrity.c 11 KB

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