blk-integrity.c 11 KB

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