blob: 129b9e209a3b46dcf06ab36c0629e632e9b75035 [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>
24#include <linux/mempool.h>
25#include <linux/bio.h>
26#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020028
29#include "blk.h"
30
31static struct kmem_cache *integrity_cachep;
32
Mike Snitzera63a5cf2011-04-01 21:02:31 +020033static const char *bi_unsupported_name = "unsupported";
34
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020035/**
36 * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
Martin K. Petersen13f05c82010-09-10 20:50:10 +020037 * @q: request queue
38 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020039 *
40 * Description: Returns the number of elements required in a
Martin K. Petersen13f05c82010-09-10 20:50:10 +020041 * scatterlist corresponding to the integrity metadata in a bio.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020042 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020043int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020044{
Martin K. Petersen13f05c82010-09-10 20:50:10 +020045 struct bio_vec *iv, *ivprv = NULL;
46 unsigned int segments = 0;
47 unsigned int seg_size = 0;
48 unsigned int i = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020049
Martin K. Petersen13f05c82010-09-10 20:50:10 +020050 bio_for_each_integrity_vec(iv, bio, i) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020051
Martin K. Petersen13f05c82010-09-10 20:50:10 +020052 if (ivprv) {
53 if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
54 goto new_segment;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020055
Martin K. Petersen13f05c82010-09-10 20:50:10 +020056 if (!BIOVEC_SEG_BOUNDARY(q, ivprv, iv))
57 goto new_segment;
58
59 if (seg_size + iv->bv_len > queue_max_segment_size(q))
60 goto new_segment;
61
62 seg_size += iv->bv_len;
63 } else {
64new_segment:
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020065 segments++;
Martin K. Petersen13f05c82010-09-10 20:50:10 +020066 seg_size = iv->bv_len;
67 }
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020068
69 ivprv = iv;
70 }
71
72 return segments;
73}
74EXPORT_SYMBOL(blk_rq_count_integrity_sg);
75
76/**
77 * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
Martin K. Petersen13f05c82010-09-10 20:50:10 +020078 * @q: request queue
79 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020080 * @sglist: target scatterlist
81 *
82 * Description: Map the integrity vectors in request into a
83 * scatterlist. The scatterlist must be big enough to hold all
84 * elements. I.e. sized using blk_rq_count_integrity_sg().
85 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020086int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
87 struct scatterlist *sglist)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020088{
Martin K. Petersen13f05c82010-09-10 20:50:10 +020089 struct bio_vec *iv, *ivprv = NULL;
90 struct scatterlist *sg = NULL;
91 unsigned int segments = 0;
92 unsigned int i = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020093
Martin K. Petersen13f05c82010-09-10 20:50:10 +020094 bio_for_each_integrity_vec(iv, bio, i) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020095
96 if (ivprv) {
97 if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
98 goto new_segment;
99
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200100 if (!BIOVEC_SEG_BOUNDARY(q, ivprv, iv))
101 goto new_segment;
102
103 if (sg->length + iv->bv_len > queue_max_segment_size(q))
104 goto new_segment;
105
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200106 sg->length += iv->bv_len;
107 } else {
108new_segment:
109 if (!sg)
110 sg = sglist;
111 else {
112 sg->page_link &= ~0x02;
113 sg = sg_next(sg);
114 }
115
116 sg_set_page(sg, iv->bv_page, iv->bv_len, iv->bv_offset);
117 segments++;
118 }
119
120 ivprv = iv;
121 }
122
123 if (sg)
124 sg_mark_end(sg);
125
126 return segments;
127}
128EXPORT_SYMBOL(blk_rq_map_integrity_sg);
129
130/**
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400131 * blk_integrity_compare - Compare integrity profile of two disks
132 * @gd1: Disk to compare
133 * @gd2: Disk to compare
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200134 *
135 * Description: Meta-devices like DM and MD need to verify that all
136 * sub-devices use the same integrity format before advertising to
137 * upper layers that they can send/receive integrity metadata. This
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400138 * function can be used to check whether two gendisk devices have
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200139 * compatible integrity formats.
140 */
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400141int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200142{
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400143 struct blk_integrity *b1 = gd1->integrity;
144 struct blk_integrity *b2 = gd2->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200145
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400146 if (!b1 && !b2)
147 return 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200148
149 if (!b1 || !b2)
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400150 return -1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200151
152 if (b1->sector_size != b2->sector_size) {
153 printk(KERN_ERR "%s: %s/%s sector sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400154 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200155 b1->sector_size, b2->sector_size);
156 return -1;
157 }
158
159 if (b1->tuple_size != b2->tuple_size) {
160 printk(KERN_ERR "%s: %s/%s tuple sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400161 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200162 b1->tuple_size, b2->tuple_size);
163 return -1;
164 }
165
166 if (b1->tag_size && b2->tag_size && (b1->tag_size != b2->tag_size)) {
167 printk(KERN_ERR "%s: %s/%s tag sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400168 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200169 b1->tag_size, b2->tag_size);
170 return -1;
171 }
172
173 if (strcmp(b1->name, b2->name)) {
174 printk(KERN_ERR "%s: %s/%s type %s != %s\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400175 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200176 b1->name, b2->name);
177 return -1;
178 }
179
180 return 0;
181}
182EXPORT_SYMBOL(blk_integrity_compare);
183
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200184int blk_integrity_merge_rq(struct request_queue *q, struct request *req,
185 struct request *next)
186{
187 if (blk_integrity_rq(req) != blk_integrity_rq(next))
188 return -1;
189
190 if (req->nr_integrity_segments + next->nr_integrity_segments >
191 q->limits.max_integrity_segments)
192 return -1;
193
194 return 0;
195}
196EXPORT_SYMBOL(blk_integrity_merge_rq);
197
198int blk_integrity_merge_bio(struct request_queue *q, struct request *req,
199 struct bio *bio)
200{
201 int nr_integrity_segs;
202 struct bio *next = bio->bi_next;
203
204 bio->bi_next = NULL;
205 nr_integrity_segs = blk_rq_count_integrity_sg(q, bio);
206 bio->bi_next = next;
207
208 if (req->nr_integrity_segments + nr_integrity_segs >
209 q->limits.max_integrity_segments)
210 return -1;
211
212 req->nr_integrity_segments += nr_integrity_segs;
213
214 return 0;
215}
216EXPORT_SYMBOL(blk_integrity_merge_bio);
217
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200218struct integrity_sysfs_entry {
219 struct attribute attr;
220 ssize_t (*show)(struct blk_integrity *, char *);
221 ssize_t (*store)(struct blk_integrity *, const char *, size_t);
222};
223
224static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr,
225 char *page)
226{
227 struct blk_integrity *bi =
228 container_of(kobj, struct blk_integrity, kobj);
229 struct integrity_sysfs_entry *entry =
230 container_of(attr, struct integrity_sysfs_entry, attr);
231
232 return entry->show(bi, page);
233}
234
Jens Axboeb9846792008-06-17 19:05:48 +0200235static ssize_t integrity_attr_store(struct kobject *kobj,
236 struct attribute *attr, const char *page,
237 size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200238{
239 struct blk_integrity *bi =
240 container_of(kobj, struct blk_integrity, kobj);
241 struct integrity_sysfs_entry *entry =
242 container_of(attr, struct integrity_sysfs_entry, attr);
243 ssize_t ret = 0;
244
245 if (entry->store)
246 ret = entry->store(bi, page, count);
247
248 return ret;
249}
250
251static ssize_t integrity_format_show(struct blk_integrity *bi, char *page)
252{
253 if (bi != NULL && bi->name != NULL)
254 return sprintf(page, "%s\n", bi->name);
255 else
256 return sprintf(page, "none\n");
257}
258
259static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page)
260{
261 if (bi != NULL)
262 return sprintf(page, "%u\n", bi->tag_size);
263 else
264 return sprintf(page, "0\n");
265}
266
267static ssize_t integrity_read_store(struct blk_integrity *bi,
268 const char *page, size_t count)
269{
270 char *p = (char *) page;
271 unsigned long val = simple_strtoul(p, &p, 10);
272
273 if (val)
Jens Axboeb24498d2008-06-27 09:12:09 +0200274 bi->flags |= INTEGRITY_FLAG_READ;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200275 else
Jens Axboeb24498d2008-06-27 09:12:09 +0200276 bi->flags &= ~INTEGRITY_FLAG_READ;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200277
278 return count;
279}
280
281static ssize_t integrity_read_show(struct blk_integrity *bi, char *page)
282{
Jens Axboeb24498d2008-06-27 09:12:09 +0200283 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_READ) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200284}
285
286static ssize_t integrity_write_store(struct blk_integrity *bi,
287 const char *page, size_t count)
288{
289 char *p = (char *) page;
290 unsigned long val = simple_strtoul(p, &p, 10);
291
292 if (val)
Jens Axboeb24498d2008-06-27 09:12:09 +0200293 bi->flags |= INTEGRITY_FLAG_WRITE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200294 else
Jens Axboeb24498d2008-06-27 09:12:09 +0200295 bi->flags &= ~INTEGRITY_FLAG_WRITE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200296
297 return count;
298}
299
300static ssize_t integrity_write_show(struct blk_integrity *bi, char *page)
301{
Jens Axboeb24498d2008-06-27 09:12:09 +0200302 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_WRITE) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200303}
304
305static struct integrity_sysfs_entry integrity_format_entry = {
306 .attr = { .name = "format", .mode = S_IRUGO },
307 .show = integrity_format_show,
308};
309
310static struct integrity_sysfs_entry integrity_tag_size_entry = {
311 .attr = { .name = "tag_size", .mode = S_IRUGO },
312 .show = integrity_tag_size_show,
313};
314
315static struct integrity_sysfs_entry integrity_read_entry = {
316 .attr = { .name = "read_verify", .mode = S_IRUGO | S_IWUSR },
317 .show = integrity_read_show,
318 .store = integrity_read_store,
319};
320
321static struct integrity_sysfs_entry integrity_write_entry = {
322 .attr = { .name = "write_generate", .mode = S_IRUGO | S_IWUSR },
323 .show = integrity_write_show,
324 .store = integrity_write_store,
325};
326
327static struct attribute *integrity_attrs[] = {
328 &integrity_format_entry.attr,
329 &integrity_tag_size_entry.attr,
330 &integrity_read_entry.attr,
331 &integrity_write_entry.attr,
332 NULL,
333};
334
Emese Revfy52cf25d2010-01-19 02:58:23 +0100335static const struct sysfs_ops integrity_ops = {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200336 .show = &integrity_attr_show,
337 .store = &integrity_attr_store,
338};
339
340static int __init blk_dev_integrity_init(void)
341{
342 integrity_cachep = kmem_cache_create("blkdev_integrity",
343 sizeof(struct blk_integrity),
344 0, SLAB_PANIC, NULL);
345 return 0;
346}
347subsys_initcall(blk_dev_integrity_init);
348
349static void blk_integrity_release(struct kobject *kobj)
350{
351 struct blk_integrity *bi =
352 container_of(kobj, struct blk_integrity, kobj);
353
354 kmem_cache_free(integrity_cachep, bi);
355}
356
357static struct kobj_type integrity_ktype = {
358 .default_attrs = integrity_attrs,
359 .sysfs_ops = &integrity_ops,
360 .release = blk_integrity_release,
361};
362
Mike Snitzera63a5cf2011-04-01 21:02:31 +0200363bool blk_integrity_is_initialized(struct gendisk *disk)
364{
365 struct blk_integrity *bi = blk_get_integrity(disk);
366
367 return (bi && bi->name && strcmp(bi->name, bi_unsupported_name) != 0);
368}
369EXPORT_SYMBOL(blk_integrity_is_initialized);
370
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200371/**
372 * blk_integrity_register - Register a gendisk as being integrity-capable
373 * @disk: struct gendisk pointer to make integrity-aware
Martin K. Petersen32231632009-01-04 02:43:40 -0500374 * @template: optional integrity profile to register
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200375 *
376 * Description: When a device needs to advertise itself as being able
377 * to send/receive integrity metadata it must use this function to
378 * register the capability with the block layer. The template is a
379 * blk_integrity struct with values appropriate for the underlying
Martin K. Petersen32231632009-01-04 02:43:40 -0500380 * hardware. If template is NULL the new profile is allocated but
381 * not filled out. See Documentation/block/data-integrity.txt.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200382 */
383int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
384{
385 struct blk_integrity *bi;
386
387 BUG_ON(disk == NULL);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200388
389 if (disk->integrity == NULL) {
Jens Axboeb9846792008-06-17 19:05:48 +0200390 bi = kmem_cache_alloc(integrity_cachep,
Martin K. Petersen32231632009-01-04 02:43:40 -0500391 GFP_KERNEL | __GFP_ZERO);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200392 if (!bi)
393 return -1;
394
395 if (kobject_init_and_add(&bi->kobj, &integrity_ktype,
Tejun Heoed9e1982008-08-25 19:56:05 +0900396 &disk_to_dev(disk)->kobj,
397 "%s", "integrity")) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200398 kmem_cache_free(integrity_cachep, bi);
399 return -1;
400 }
401
402 kobject_uevent(&bi->kobj, KOBJ_ADD);
403
Jens Axboeb24498d2008-06-27 09:12:09 +0200404 bi->flags |= INTEGRITY_FLAG_READ | INTEGRITY_FLAG_WRITE;
Martin K. Petersene1defc42009-05-22 17:17:49 -0400405 bi->sector_size = queue_logical_block_size(disk->queue);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200406 disk->integrity = bi;
407 } else
408 bi = disk->integrity;
409
410 /* Use the provided profile as template */
Martin K. Petersen32231632009-01-04 02:43:40 -0500411 if (template != NULL) {
412 bi->name = template->name;
413 bi->generate_fn = template->generate_fn;
414 bi->verify_fn = template->verify_fn;
415 bi->tuple_size = template->tuple_size;
416 bi->set_tag_fn = template->set_tag_fn;
417 bi->get_tag_fn = template->get_tag_fn;
418 bi->tag_size = template->tag_size;
419 } else
Mike Snitzera63a5cf2011-04-01 21:02:31 +0200420 bi->name = bi_unsupported_name;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200421
422 return 0;
423}
424EXPORT_SYMBOL(blk_integrity_register);
425
426/**
427 * blk_integrity_unregister - Remove block integrity profile
428 * @disk: disk whose integrity profile to deallocate
429 *
430 * Description: This function frees all memory used by the block
431 * integrity profile. To be called at device teardown.
432 */
433void blk_integrity_unregister(struct gendisk *disk)
434{
435 struct blk_integrity *bi;
436
437 if (!disk || !disk->integrity)
438 return;
439
440 bi = disk->integrity;
441
442 kobject_uevent(&bi->kobj, KOBJ_REMOVE);
443 kobject_del(&bi->kobj);
Xiaotian Feng3839e4b2009-07-28 09:11:14 +0200444 kobject_put(&bi->kobj);
Martin K. Petersen0c032ab2008-10-01 03:38:38 -0400445 disk->integrity = NULL;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200446}
447EXPORT_SYMBOL(blk_integrity_unregister);