blob: f548b64be09242a77f7db6ac7cedd5f68ee397b7 [file] [log] [blame]
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001/*
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
23#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040024#include <linux/backing-dev.h>
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020025#include <linux/mempool.h>
26#include <linux/bio.h>
27#include <linux/scatterlist.h>
Paul Gortmakerd5decd32011-05-26 16:00:52 -040028#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020030
31#include "blk.h"
32
33static struct kmem_cache *integrity_cachep;
34
Mike Snitzera63a5cf2011-04-01 21:02:31 +020035static const char *bi_unsupported_name = "unsupported";
36
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020037/**
38 * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
Martin K. Petersen13f05c82010-09-10 20:50:10 +020039 * @q: request queue
40 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020041 *
42 * Description: Returns the number of elements required in a
Martin K. Petersen13f05c82010-09-10 20:50:10 +020043 * scatterlist corresponding to the integrity metadata in a bio.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020044 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020045int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020046{
Kent Overstreetd57a5f72013-11-23 17:20:16 -080047 struct bio_vec iv, ivprv = { NULL };
Martin K. Petersen13f05c82010-09-10 20:50:10 +020048 unsigned int segments = 0;
49 unsigned int seg_size = 0;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080050 struct bvec_iter iter;
51 int prev = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020052
Kent Overstreetd57a5f72013-11-23 17:20:16 -080053 bio_for_each_integrity_vec(iv, bio, iter) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020054
Kent Overstreetd57a5f72013-11-23 17:20:16 -080055 if (prev) {
56 if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020057 goto new_segment;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020058
Kent Overstreetd57a5f72013-11-23 17:20:16 -080059 if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020060 goto new_segment;
61
Kent Overstreetd57a5f72013-11-23 17:20:16 -080062 if (seg_size + iv.bv_len > queue_max_segment_size(q))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020063 goto new_segment;
64
Kent Overstreetd57a5f72013-11-23 17:20:16 -080065 seg_size += iv.bv_len;
Martin K. Petersen13f05c82010-09-10 20:50:10 +020066 } else {
67new_segment:
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020068 segments++;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080069 seg_size = iv.bv_len;
Martin K. Petersen13f05c82010-09-10 20:50:10 +020070 }
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020071
Kent Overstreetd57a5f72013-11-23 17:20:16 -080072 prev = 1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020073 ivprv = iv;
74 }
75
76 return segments;
77}
78EXPORT_SYMBOL(blk_rq_count_integrity_sg);
79
80/**
81 * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
Martin K. Petersen13f05c82010-09-10 20:50:10 +020082 * @q: request queue
83 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020084 * @sglist: target scatterlist
85 *
86 * Description: Map the integrity vectors in request into a
87 * scatterlist. The scatterlist must be big enough to hold all
88 * elements. I.e. sized using blk_rq_count_integrity_sg().
89 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020090int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
91 struct scatterlist *sglist)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020092{
Kent Overstreetd57a5f72013-11-23 17:20:16 -080093 struct bio_vec iv, ivprv = { NULL };
Martin K. Petersen13f05c82010-09-10 20:50:10 +020094 struct scatterlist *sg = NULL;
95 unsigned int segments = 0;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080096 struct bvec_iter iter;
97 int prev = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020098
Kent Overstreetd57a5f72013-11-23 17:20:16 -080099 bio_for_each_integrity_vec(iv, bio, iter) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200100
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800101 if (prev) {
102 if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv))
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200103 goto new_segment;
104
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800105 if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv))
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200106 goto new_segment;
107
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800108 if (sg->length + iv.bv_len > queue_max_segment_size(q))
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200109 goto new_segment;
110
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800111 sg->length += iv.bv_len;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200112 } else {
113new_segment:
114 if (!sg)
115 sg = sglist;
116 else {
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030117 sg_unmark_end(sg);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200118 sg = sg_next(sg);
119 }
120
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800121 sg_set_page(sg, iv.bv_page, iv.bv_len, iv.bv_offset);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200122 segments++;
123 }
124
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800125 prev = 1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200126 ivprv = iv;
127 }
128
129 if (sg)
130 sg_mark_end(sg);
131
132 return segments;
133}
134EXPORT_SYMBOL(blk_rq_map_integrity_sg);
135
136/**
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400137 * blk_integrity_compare - Compare integrity profile of two disks
138 * @gd1: Disk to compare
139 * @gd2: Disk to compare
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200140 *
141 * Description: Meta-devices like DM and MD need to verify that all
142 * sub-devices use the same integrity format before advertising to
143 * upper layers that they can send/receive integrity metadata. This
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400144 * function can be used to check whether two gendisk devices have
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200145 * compatible integrity formats.
146 */
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400147int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200148{
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400149 struct blk_integrity *b1 = gd1->integrity;
150 struct blk_integrity *b2 = gd2->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200151
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400152 if (!b1 && !b2)
153 return 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200154
155 if (!b1 || !b2)
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400156 return -1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200157
Martin K. Petersen3be91c42014-09-26 19:19:59 -0400158 if (b1->interval != b2->interval) {
159 pr_err("%s: %s/%s protection interval %u != %u\n",
160 __func__, gd1->disk_name, gd2->disk_name,
161 b1->interval, b2->interval);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200162 return -1;
163 }
164
165 if (b1->tuple_size != b2->tuple_size) {
166 printk(KERN_ERR "%s: %s/%s tuple sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400167 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200168 b1->tuple_size, b2->tuple_size);
169 return -1;
170 }
171
172 if (b1->tag_size && b2->tag_size && (b1->tag_size != b2->tag_size)) {
173 printk(KERN_ERR "%s: %s/%s tag sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400174 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200175 b1->tag_size, b2->tag_size);
176 return -1;
177 }
178
179 if (strcmp(b1->name, b2->name)) {
180 printk(KERN_ERR "%s: %s/%s type %s != %s\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400181 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200182 b1->name, b2->name);
183 return -1;
184 }
185
186 return 0;
187}
188EXPORT_SYMBOL(blk_integrity_compare);
189
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400190bool blk_integrity_merge_rq(struct request_queue *q, struct request *req,
191 struct request *next)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200192{
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400193 if (blk_integrity_rq(req) == 0 && blk_integrity_rq(next) == 0)
194 return true;
195
196 if (blk_integrity_rq(req) == 0 || blk_integrity_rq(next) == 0)
197 return false;
198
199 if (bio_integrity(req->bio)->bip_flags !=
200 bio_integrity(next->bio)->bip_flags)
201 return false;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200202
203 if (req->nr_integrity_segments + next->nr_integrity_segments >
204 q->limits.max_integrity_segments)
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400205 return false;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200206
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400207 return true;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200208}
209EXPORT_SYMBOL(blk_integrity_merge_rq);
210
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400211bool blk_integrity_merge_bio(struct request_queue *q, struct request *req,
212 struct bio *bio)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200213{
214 int nr_integrity_segs;
215 struct bio *next = bio->bi_next;
216
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400217 if (blk_integrity_rq(req) == 0 && bio_integrity(bio) == NULL)
218 return true;
219
220 if (blk_integrity_rq(req) == 0 || bio_integrity(bio) == NULL)
221 return false;
222
223 if (bio_integrity(req->bio)->bip_flags != bio_integrity(bio)->bip_flags)
224 return false;
225
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200226 bio->bi_next = NULL;
227 nr_integrity_segs = blk_rq_count_integrity_sg(q, bio);
228 bio->bi_next = next;
229
230 if (req->nr_integrity_segments + nr_integrity_segs >
231 q->limits.max_integrity_segments)
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400232 return false;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200233
234 req->nr_integrity_segments += nr_integrity_segs;
235
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400236 return true;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200237}
238EXPORT_SYMBOL(blk_integrity_merge_bio);
239
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200240struct integrity_sysfs_entry {
241 struct attribute attr;
242 ssize_t (*show)(struct blk_integrity *, char *);
243 ssize_t (*store)(struct blk_integrity *, const char *, size_t);
244};
245
246static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr,
247 char *page)
248{
249 struct blk_integrity *bi =
250 container_of(kobj, struct blk_integrity, kobj);
251 struct integrity_sysfs_entry *entry =
252 container_of(attr, struct integrity_sysfs_entry, attr);
253
254 return entry->show(bi, page);
255}
256
Jens Axboeb9846792008-06-17 19:05:48 +0200257static ssize_t integrity_attr_store(struct kobject *kobj,
258 struct attribute *attr, const char *page,
259 size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200260{
261 struct blk_integrity *bi =
262 container_of(kobj, struct blk_integrity, kobj);
263 struct integrity_sysfs_entry *entry =
264 container_of(attr, struct integrity_sysfs_entry, attr);
265 ssize_t ret = 0;
266
267 if (entry->store)
268 ret = entry->store(bi, page, count);
269
270 return ret;
271}
272
273static ssize_t integrity_format_show(struct blk_integrity *bi, char *page)
274{
275 if (bi != NULL && bi->name != NULL)
276 return sprintf(page, "%s\n", bi->name);
277 else
278 return sprintf(page, "none\n");
279}
280
281static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page)
282{
283 if (bi != NULL)
284 return sprintf(page, "%u\n", bi->tag_size);
285 else
286 return sprintf(page, "0\n");
287}
288
Martin K. Petersen8288f492014-09-26 19:20:02 -0400289static ssize_t integrity_verify_store(struct blk_integrity *bi,
290 const char *page, size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200291{
292 char *p = (char *) page;
293 unsigned long val = simple_strtoul(p, &p, 10);
294
295 if (val)
Martin K. Petersen8288f492014-09-26 19:20:02 -0400296 bi->flags |= BLK_INTEGRITY_VERIFY;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200297 else
Martin K. Petersen8288f492014-09-26 19:20:02 -0400298 bi->flags &= ~BLK_INTEGRITY_VERIFY;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200299
300 return count;
301}
302
Martin K. Petersen8288f492014-09-26 19:20:02 -0400303static ssize_t integrity_verify_show(struct blk_integrity *bi, char *page)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200304{
Martin K. Petersen8288f492014-09-26 19:20:02 -0400305 return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_VERIFY) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200306}
307
Martin K. Petersen8288f492014-09-26 19:20:02 -0400308static ssize_t integrity_generate_store(struct blk_integrity *bi,
309 const char *page, size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200310{
311 char *p = (char *) page;
312 unsigned long val = simple_strtoul(p, &p, 10);
313
314 if (val)
Martin K. Petersen8288f492014-09-26 19:20:02 -0400315 bi->flags |= BLK_INTEGRITY_GENERATE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200316 else
Martin K. Petersen8288f492014-09-26 19:20:02 -0400317 bi->flags &= ~BLK_INTEGRITY_GENERATE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200318
319 return count;
320}
321
Martin K. Petersen8288f492014-09-26 19:20:02 -0400322static ssize_t integrity_generate_show(struct blk_integrity *bi, char *page)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200323{
Martin K. Petersen8288f492014-09-26 19:20:02 -0400324 return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_GENERATE) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200325}
326
Martin K. Petersen3aec2f42014-09-26 19:20:03 -0400327static ssize_t integrity_device_show(struct blk_integrity *bi, char *page)
328{
329 return sprintf(page, "%u\n",
330 (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) != 0);
331}
332
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200333static struct integrity_sysfs_entry integrity_format_entry = {
334 .attr = { .name = "format", .mode = S_IRUGO },
335 .show = integrity_format_show,
336};
337
338static struct integrity_sysfs_entry integrity_tag_size_entry = {
339 .attr = { .name = "tag_size", .mode = S_IRUGO },
340 .show = integrity_tag_size_show,
341};
342
Martin K. Petersen8288f492014-09-26 19:20:02 -0400343static struct integrity_sysfs_entry integrity_verify_entry = {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200344 .attr = { .name = "read_verify", .mode = S_IRUGO | S_IWUSR },
Martin K. Petersen8288f492014-09-26 19:20:02 -0400345 .show = integrity_verify_show,
346 .store = integrity_verify_store,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200347};
348
Martin K. Petersen8288f492014-09-26 19:20:02 -0400349static struct integrity_sysfs_entry integrity_generate_entry = {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200350 .attr = { .name = "write_generate", .mode = S_IRUGO | S_IWUSR },
Martin K. Petersen8288f492014-09-26 19:20:02 -0400351 .show = integrity_generate_show,
352 .store = integrity_generate_store,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200353};
354
Martin K. Petersen3aec2f42014-09-26 19:20:03 -0400355static struct integrity_sysfs_entry integrity_device_entry = {
356 .attr = { .name = "device_is_integrity_capable", .mode = S_IRUGO },
357 .show = integrity_device_show,
358};
359
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200360static struct attribute *integrity_attrs[] = {
361 &integrity_format_entry.attr,
362 &integrity_tag_size_entry.attr,
Martin K. Petersen8288f492014-09-26 19:20:02 -0400363 &integrity_verify_entry.attr,
364 &integrity_generate_entry.attr,
Martin K. Petersen3aec2f42014-09-26 19:20:03 -0400365 &integrity_device_entry.attr,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200366 NULL,
367};
368
Emese Revfy52cf25d2010-01-19 02:58:23 +0100369static const struct sysfs_ops integrity_ops = {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200370 .show = &integrity_attr_show,
371 .store = &integrity_attr_store,
372};
373
374static int __init blk_dev_integrity_init(void)
375{
376 integrity_cachep = kmem_cache_create("blkdev_integrity",
377 sizeof(struct blk_integrity),
378 0, SLAB_PANIC, NULL);
379 return 0;
380}
381subsys_initcall(blk_dev_integrity_init);
382
383static void blk_integrity_release(struct kobject *kobj)
384{
385 struct blk_integrity *bi =
386 container_of(kobj, struct blk_integrity, kobj);
387
388 kmem_cache_free(integrity_cachep, bi);
389}
390
391static struct kobj_type integrity_ktype = {
392 .default_attrs = integrity_attrs,
393 .sysfs_ops = &integrity_ops,
394 .release = blk_integrity_release,
395};
396
Mike Snitzera63a5cf2011-04-01 21:02:31 +0200397bool blk_integrity_is_initialized(struct gendisk *disk)
398{
399 struct blk_integrity *bi = blk_get_integrity(disk);
400
401 return (bi && bi->name && strcmp(bi->name, bi_unsupported_name) != 0);
402}
403EXPORT_SYMBOL(blk_integrity_is_initialized);
404
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200405/**
406 * blk_integrity_register - Register a gendisk as being integrity-capable
407 * @disk: struct gendisk pointer to make integrity-aware
Martin K. Petersen32231632009-01-04 02:43:40 -0500408 * @template: optional integrity profile to register
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200409 *
410 * Description: When a device needs to advertise itself as being able
411 * to send/receive integrity metadata it must use this function to
412 * register the capability with the block layer. The template is a
413 * blk_integrity struct with values appropriate for the underlying
Martin K. Petersen32231632009-01-04 02:43:40 -0500414 * hardware. If template is NULL the new profile is allocated but
415 * not filled out. See Documentation/block/data-integrity.txt.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200416 */
417int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
418{
419 struct blk_integrity *bi;
420
421 BUG_ON(disk == NULL);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200422
423 if (disk->integrity == NULL) {
Jens Axboeb9846792008-06-17 19:05:48 +0200424 bi = kmem_cache_alloc(integrity_cachep,
Martin K. Petersen32231632009-01-04 02:43:40 -0500425 GFP_KERNEL | __GFP_ZERO);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200426 if (!bi)
427 return -1;
428
429 if (kobject_init_and_add(&bi->kobj, &integrity_ktype,
Tejun Heoed9e1982008-08-25 19:56:05 +0900430 &disk_to_dev(disk)->kobj,
431 "%s", "integrity")) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200432 kmem_cache_free(integrity_cachep, bi);
433 return -1;
434 }
435
436 kobject_uevent(&bi->kobj, KOBJ_ADD);
437
Martin K. Petersen8288f492014-09-26 19:20:02 -0400438 bi->flags |= BLK_INTEGRITY_VERIFY | BLK_INTEGRITY_GENERATE;
Martin K. Petersen3be91c42014-09-26 19:19:59 -0400439 bi->interval = queue_logical_block_size(disk->queue);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200440 disk->integrity = bi;
441 } else
442 bi = disk->integrity;
443
444 /* Use the provided profile as template */
Martin K. Petersen32231632009-01-04 02:43:40 -0500445 if (template != NULL) {
446 bi->name = template->name;
447 bi->generate_fn = template->generate_fn;
448 bi->verify_fn = template->verify_fn;
449 bi->tuple_size = template->tuple_size;
Martin K. Petersen32231632009-01-04 02:43:40 -0500450 bi->tag_size = template->tag_size;
Martin K. Petersen8288f492014-09-26 19:20:02 -0400451 bi->flags |= template->flags;
Martin K. Petersen32231632009-01-04 02:43:40 -0500452 } else
Mike Snitzera63a5cf2011-04-01 21:02:31 +0200453 bi->name = bi_unsupported_name;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200454
Darrick J. Wong7d311cd2013-02-21 16:42:48 -0800455 disk->queue->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES;
456
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200457 return 0;
458}
459EXPORT_SYMBOL(blk_integrity_register);
460
461/**
462 * blk_integrity_unregister - Remove block integrity profile
463 * @disk: disk whose integrity profile to deallocate
464 *
465 * Description: This function frees all memory used by the block
466 * integrity profile. To be called at device teardown.
467 */
468void blk_integrity_unregister(struct gendisk *disk)
469{
470 struct blk_integrity *bi;
471
472 if (!disk || !disk->integrity)
473 return;
474
Darrick J. Wong7d311cd2013-02-21 16:42:48 -0800475 disk->queue->backing_dev_info.capabilities &= ~BDI_CAP_STABLE_WRITES;
476
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200477 bi = disk->integrity;
478
479 kobject_uevent(&bi->kobj, KOBJ_REMOVE);
480 kobject_del(&bi->kobj);
Xiaotian Feng3839e4b2009-07-28 09:11:14 +0200481 kobject_put(&bi->kobj);
Martin K. Petersen0c032ab2008-10-01 03:38:38 -0400482 disk->integrity = NULL;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200483}
484EXPORT_SYMBOL(blk_integrity_unregister);