blk-integrity.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. /**
  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. struct bvec_iter iter;
  44. int prev = 0;
  45. bio_for_each_integrity_vec(iv, bio, iter) {
  46. if (prev) {
  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. prev = 1;
  60. ivprv = iv;
  61. }
  62. return segments;
  63. }
  64. EXPORT_SYMBOL(blk_rq_count_integrity_sg);
  65. /**
  66. * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
  67. * @q: request queue
  68. * @bio: bio with integrity metadata attached
  69. * @sglist: target scatterlist
  70. *
  71. * Description: Map the integrity vectors in request into a
  72. * scatterlist. The scatterlist must be big enough to hold all
  73. * elements. I.e. sized using blk_rq_count_integrity_sg().
  74. */
  75. int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
  76. struct scatterlist *sglist)
  77. {
  78. struct bio_vec iv, ivprv = { NULL };
  79. struct scatterlist *sg = NULL;
  80. unsigned int segments = 0;
  81. struct bvec_iter iter;
  82. int prev = 0;
  83. bio_for_each_integrity_vec(iv, bio, iter) {
  84. if (prev) {
  85. if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv))
  86. goto new_segment;
  87. if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv))
  88. goto new_segment;
  89. if (sg->length + iv.bv_len > queue_max_segment_size(q))
  90. goto new_segment;
  91. sg->length += iv.bv_len;
  92. } else {
  93. new_segment:
  94. if (!sg)
  95. sg = sglist;
  96. else {
  97. sg_unmark_end(sg);
  98. sg = sg_next(sg);
  99. }
  100. sg_set_page(sg, iv.bv_page, iv.bv_len, iv.bv_offset);
  101. segments++;
  102. }
  103. prev = 1;
  104. ivprv = iv;
  105. }
  106. if (sg)
  107. sg_mark_end(sg);
  108. return segments;
  109. }
  110. EXPORT_SYMBOL(blk_rq_map_integrity_sg);
  111. /**
  112. * blk_integrity_compare - Compare integrity profile of two disks
  113. * @gd1: Disk to compare
  114. * @gd2: Disk to compare
  115. *
  116. * Description: Meta-devices like DM and MD need to verify that all
  117. * sub-devices use the same integrity format before advertising to
  118. * upper layers that they can send/receive integrity metadata. This
  119. * function can be used to check whether two gendisk devices have
  120. * compatible integrity formats.
  121. */
  122. int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
  123. {
  124. struct blk_integrity *b1 = &gd1->queue->integrity;
  125. struct blk_integrity *b2 = &gd2->queue->integrity;
  126. if (!b1->profile && !b2->profile)
  127. return 0;
  128. if (!b1->profile || !b2->profile)
  129. return -1;
  130. if (b1->interval_exp != b2->interval_exp) {
  131. pr_err("%s: %s/%s protection interval %u != %u\n",
  132. __func__, gd1->disk_name, gd2->disk_name,
  133. 1 << b1->interval_exp, 1 << b2->interval_exp);
  134. return -1;
  135. }
  136. if (b1->tuple_size != b2->tuple_size) {
  137. pr_err("%s: %s/%s tuple sz %u != %u\n", __func__,
  138. gd1->disk_name, gd2->disk_name,
  139. b1->tuple_size, b2->tuple_size);
  140. return -1;
  141. }
  142. if (b1->tag_size && b2->tag_size && (b1->tag_size != b2->tag_size)) {
  143. pr_err("%s: %s/%s tag sz %u != %u\n", __func__,
  144. gd1->disk_name, gd2->disk_name,
  145. b1->tag_size, b2->tag_size);
  146. return -1;
  147. }
  148. if (b1->profile != b2->profile) {
  149. pr_err("%s: %s/%s type %s != %s\n", __func__,
  150. gd1->disk_name, gd2->disk_name,
  151. b1->profile->name, b2->profile->name);
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. EXPORT_SYMBOL(blk_integrity_compare);
  157. bool blk_integrity_merge_rq(struct request_queue *q, struct request *req,
  158. struct request *next)
  159. {
  160. if (blk_integrity_rq(req) == 0 && blk_integrity_rq(next) == 0)
  161. return true;
  162. if (blk_integrity_rq(req) == 0 || blk_integrity_rq(next) == 0)
  163. return false;
  164. if (bio_integrity(req->bio)->bip_flags !=
  165. bio_integrity(next->bio)->bip_flags)
  166. return false;
  167. if (req->nr_integrity_segments + next->nr_integrity_segments >
  168. q->limits.max_integrity_segments)
  169. return false;
  170. if (integrity_req_gap_back_merge(req, next->bio))
  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 gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj);
  205. struct blk_integrity *bi = &disk->queue->integrity;
  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 gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj);
  215. struct blk_integrity *bi = &disk->queue->integrity;
  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->profile && bi->profile->name)
  226. return sprintf(page, "%s\n", bi->profile->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. return sprintf(page, "%u\n", bi->tag_size);
  233. }
  234. static ssize_t integrity_interval_show(struct blk_integrity *bi, char *page)
  235. {
  236. return sprintf(page, "%u\n",
  237. bi->interval_exp ? 1 << bi->interval_exp : 0);
  238. }
  239. static ssize_t integrity_verify_store(struct blk_integrity *bi,
  240. const char *page, size_t count)
  241. {
  242. char *p = (char *) page;
  243. unsigned long val = simple_strtoul(p, &p, 10);
  244. if (val)
  245. bi->flags |= BLK_INTEGRITY_VERIFY;
  246. else
  247. bi->flags &= ~BLK_INTEGRITY_VERIFY;
  248. return count;
  249. }
  250. static ssize_t integrity_verify_show(struct blk_integrity *bi, char *page)
  251. {
  252. return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_VERIFY) != 0);
  253. }
  254. static ssize_t integrity_generate_store(struct blk_integrity *bi,
  255. const char *page, size_t count)
  256. {
  257. char *p = (char *) page;
  258. unsigned long val = simple_strtoul(p, &p, 10);
  259. if (val)
  260. bi->flags |= BLK_INTEGRITY_GENERATE;
  261. else
  262. bi->flags &= ~BLK_INTEGRITY_GENERATE;
  263. return count;
  264. }
  265. static ssize_t integrity_generate_show(struct blk_integrity *bi, char *page)
  266. {
  267. return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_GENERATE) != 0);
  268. }
  269. static ssize_t integrity_device_show(struct blk_integrity *bi, char *page)
  270. {
  271. return sprintf(page, "%u\n",
  272. (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) != 0);
  273. }
  274. static struct integrity_sysfs_entry integrity_format_entry = {
  275. .attr = { .name = "format", .mode = 0444 },
  276. .show = integrity_format_show,
  277. };
  278. static struct integrity_sysfs_entry integrity_tag_size_entry = {
  279. .attr = { .name = "tag_size", .mode = 0444 },
  280. .show = integrity_tag_size_show,
  281. };
  282. static struct integrity_sysfs_entry integrity_interval_entry = {
  283. .attr = { .name = "protection_interval_bytes", .mode = 0444 },
  284. .show = integrity_interval_show,
  285. };
  286. static struct integrity_sysfs_entry integrity_verify_entry = {
  287. .attr = { .name = "read_verify", .mode = 0644 },
  288. .show = integrity_verify_show,
  289. .store = integrity_verify_store,
  290. };
  291. static struct integrity_sysfs_entry integrity_generate_entry = {
  292. .attr = { .name = "write_generate", .mode = 0644 },
  293. .show = integrity_generate_show,
  294. .store = integrity_generate_store,
  295. };
  296. static struct integrity_sysfs_entry integrity_device_entry = {
  297. .attr = { .name = "device_is_integrity_capable", .mode = 0444 },
  298. .show = integrity_device_show,
  299. };
  300. static struct attribute *integrity_attrs[] = {
  301. &integrity_format_entry.attr,
  302. &integrity_tag_size_entry.attr,
  303. &integrity_interval_entry.attr,
  304. &integrity_verify_entry.attr,
  305. &integrity_generate_entry.attr,
  306. &integrity_device_entry.attr,
  307. NULL,
  308. };
  309. static const struct sysfs_ops integrity_ops = {
  310. .show = &integrity_attr_show,
  311. .store = &integrity_attr_store,
  312. };
  313. static struct kobj_type integrity_ktype = {
  314. .default_attrs = integrity_attrs,
  315. .sysfs_ops = &integrity_ops,
  316. };
  317. static blk_status_t blk_integrity_nop_fn(struct blk_integrity_iter *iter)
  318. {
  319. return BLK_STS_OK;
  320. }
  321. static const struct blk_integrity_profile nop_profile = {
  322. .name = "nop",
  323. .generate_fn = blk_integrity_nop_fn,
  324. .verify_fn = blk_integrity_nop_fn,
  325. };
  326. /**
  327. * blk_integrity_register - Register a gendisk as being integrity-capable
  328. * @disk: struct gendisk pointer to make integrity-aware
  329. * @template: block integrity profile to register
  330. *
  331. * Description: When a device needs to advertise itself as being able to
  332. * send/receive integrity metadata it must use this function to register
  333. * the capability with the block layer. The template is a blk_integrity
  334. * struct with values appropriate for the underlying hardware. See
  335. * Documentation/block/data-integrity.txt.
  336. */
  337. void blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
  338. {
  339. struct blk_integrity *bi = &disk->queue->integrity;
  340. bi->flags = BLK_INTEGRITY_VERIFY | BLK_INTEGRITY_GENERATE |
  341. template->flags;
  342. bi->interval_exp = template->interval_exp ? :
  343. ilog2(queue_logical_block_size(disk->queue));
  344. bi->profile = template->profile ? template->profile : &nop_profile;
  345. bi->tuple_size = template->tuple_size;
  346. bi->tag_size = template->tag_size;
  347. disk->queue->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
  348. }
  349. EXPORT_SYMBOL(blk_integrity_register);
  350. /**
  351. * blk_integrity_unregister - Unregister block integrity profile
  352. * @disk: disk whose integrity profile to unregister
  353. *
  354. * Description: This function unregisters the integrity capability from
  355. * a block device.
  356. */
  357. void blk_integrity_unregister(struct gendisk *disk)
  358. {
  359. disk->queue->backing_dev_info->capabilities &= ~BDI_CAP_STABLE_WRITES;
  360. memset(&disk->queue->integrity, 0, sizeof(struct blk_integrity));
  361. }
  362. EXPORT_SYMBOL(blk_integrity_unregister);
  363. void blk_integrity_add(struct gendisk *disk)
  364. {
  365. if (kobject_init_and_add(&disk->integrity_kobj, &integrity_ktype,
  366. &disk_to_dev(disk)->kobj, "%s", "integrity"))
  367. return;
  368. kobject_uevent(&disk->integrity_kobj, KOBJ_ADD);
  369. }
  370. void blk_integrity_del(struct gendisk *disk)
  371. {
  372. kobject_uevent(&disk->integrity_kobj, KOBJ_REMOVE);
  373. kobject_del(&disk->integrity_kobj);
  374. kobject_put(&disk->integrity_kobj);
  375. }