blob: 54bcba6c02a75ed87af935c49d4428f18eb6a615 [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
33/**
34 * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
Martin K. Petersen13f05c82010-09-10 20:50:10 +020035 * @q: request queue
36 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020037 *
38 * Description: Returns the number of elements required in a
Martin K. Petersen13f05c82010-09-10 20:50:10 +020039 * scatterlist corresponding to the integrity metadata in a bio.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020040 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020041int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020042{
Martin K. Petersen13f05c82010-09-10 20:50:10 +020043 struct bio_vec *iv, *ivprv = NULL;
44 unsigned int segments = 0;
45 unsigned int seg_size = 0;
46 unsigned int i = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020047
Martin K. Petersen13f05c82010-09-10 20:50:10 +020048 bio_for_each_integrity_vec(iv, bio, i) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020049
Martin K. Petersen13f05c82010-09-10 20:50:10 +020050 if (ivprv) {
51 if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
52 goto new_segment;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020053
Martin K. Petersen13f05c82010-09-10 20:50:10 +020054 if (!BIOVEC_SEG_BOUNDARY(q, ivprv, iv))
55 goto new_segment;
56
57 if (seg_size + iv->bv_len > queue_max_segment_size(q))
58 goto new_segment;
59
60 seg_size += iv->bv_len;
61 } else {
62new_segment:
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020063 segments++;
Martin K. Petersen13f05c82010-09-10 20:50:10 +020064 seg_size = iv->bv_len;
65 }
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020066
67 ivprv = iv;
68 }
69
70 return segments;
71}
72EXPORT_SYMBOL(blk_rq_count_integrity_sg);
73
74/**
75 * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
Martin K. Petersen13f05c82010-09-10 20:50:10 +020076 * @q: request queue
77 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020078 * @sglist: target scatterlist
79 *
80 * Description: Map the integrity vectors in request into a
81 * scatterlist. The scatterlist must be big enough to hold all
82 * elements. I.e. sized using blk_rq_count_integrity_sg().
83 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020084int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
85 struct scatterlist *sglist)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020086{
Martin K. Petersen13f05c82010-09-10 20:50:10 +020087 struct bio_vec *iv, *ivprv = NULL;
88 struct scatterlist *sg = NULL;
89 unsigned int segments = 0;
90 unsigned int i = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020091
Martin K. Petersen13f05c82010-09-10 20:50:10 +020092 bio_for_each_integrity_vec(iv, bio, i) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020093
94 if (ivprv) {
95 if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
96 goto new_segment;
97
Martin K. Petersen13f05c82010-09-10 20:50:10 +020098 if (!BIOVEC_SEG_BOUNDARY(q, ivprv, iv))
99 goto new_segment;
100
101 if (sg->length + iv->bv_len > queue_max_segment_size(q))
102 goto new_segment;
103
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200104 sg->length += iv->bv_len;
105 } else {
106new_segment:
107 if (!sg)
108 sg = sglist;
109 else {
110 sg->page_link &= ~0x02;
111 sg = sg_next(sg);
112 }
113
114 sg_set_page(sg, iv->bv_page, iv->bv_len, iv->bv_offset);
115 segments++;
116 }
117
118 ivprv = iv;
119 }
120
121 if (sg)
122 sg_mark_end(sg);
123
124 return segments;
125}
126EXPORT_SYMBOL(blk_rq_map_integrity_sg);
127
128/**
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400129 * blk_integrity_compare - Compare integrity profile of two disks
130 * @gd1: Disk to compare
131 * @gd2: Disk to compare
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200132 *
133 * Description: Meta-devices like DM and MD need to verify that all
134 * sub-devices use the same integrity format before advertising to
135 * upper layers that they can send/receive integrity metadata. This
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400136 * function can be used to check whether two gendisk devices have
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200137 * compatible integrity formats.
138 */
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400139int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200140{
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400141 struct blk_integrity *b1 = gd1->integrity;
142 struct blk_integrity *b2 = gd2->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200143
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400144 if (!b1 && !b2)
145 return 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200146
147 if (!b1 || !b2)
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400148 return -1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200149
150 if (b1->sector_size != b2->sector_size) {
151 printk(KERN_ERR "%s: %s/%s sector sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400152 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200153 b1->sector_size, b2->sector_size);
154 return -1;
155 }
156
157 if (b1->tuple_size != b2->tuple_size) {
158 printk(KERN_ERR "%s: %s/%s tuple sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400159 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200160 b1->tuple_size, b2->tuple_size);
161 return -1;
162 }
163
164 if (b1->tag_size && b2->tag_size && (b1->tag_size != b2->tag_size)) {
165 printk(KERN_ERR "%s: %s/%s tag sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400166 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200167 b1->tag_size, b2->tag_size);
168 return -1;
169 }
170
171 if (strcmp(b1->name, b2->name)) {
172 printk(KERN_ERR "%s: %s/%s type %s != %s\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400173 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200174 b1->name, b2->name);
175 return -1;
176 }
177
178 return 0;
179}
180EXPORT_SYMBOL(blk_integrity_compare);
181
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200182int blk_integrity_merge_rq(struct request_queue *q, struct request *req,
183 struct request *next)
184{
185 if (blk_integrity_rq(req) != blk_integrity_rq(next))
186 return -1;
187
188 if (req->nr_integrity_segments + next->nr_integrity_segments >
189 q->limits.max_integrity_segments)
190 return -1;
191
192 return 0;
193}
194EXPORT_SYMBOL(blk_integrity_merge_rq);
195
196int blk_integrity_merge_bio(struct request_queue *q, struct request *req,
197 struct bio *bio)
198{
199 int nr_integrity_segs;
200 struct bio *next = bio->bi_next;
201
202 bio->bi_next = NULL;
203 nr_integrity_segs = blk_rq_count_integrity_sg(q, bio);
204 bio->bi_next = next;
205
206 if (req->nr_integrity_segments + nr_integrity_segs >
207 q->limits.max_integrity_segments)
208 return -1;
209
210 req->nr_integrity_segments += nr_integrity_segs;
211
212 return 0;
213}
214EXPORT_SYMBOL(blk_integrity_merge_bio);
215
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200216struct integrity_sysfs_entry {
217 struct attribute attr;
218 ssize_t (*show)(struct blk_integrity *, char *);
219 ssize_t (*store)(struct blk_integrity *, const char *, size_t);
220};
221
222static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr,
223 char *page)
224{
225 struct blk_integrity *bi =
226 container_of(kobj, struct blk_integrity, kobj);
227 struct integrity_sysfs_entry *entry =
228 container_of(attr, struct integrity_sysfs_entry, attr);
229
230 return entry->show(bi, page);
231}
232
Jens Axboeb9846792008-06-17 19:05:48 +0200233static ssize_t integrity_attr_store(struct kobject *kobj,
234 struct attribute *attr, const char *page,
235 size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200236{
237 struct blk_integrity *bi =
238 container_of(kobj, struct blk_integrity, kobj);
239 struct integrity_sysfs_entry *entry =
240 container_of(attr, struct integrity_sysfs_entry, attr);
241 ssize_t ret = 0;
242
243 if (entry->store)
244 ret = entry->store(bi, page, count);
245
246 return ret;
247}
248
249static ssize_t integrity_format_show(struct blk_integrity *bi, char *page)
250{
251 if (bi != NULL && bi->name != NULL)
252 return sprintf(page, "%s\n", bi->name);
253 else
254 return sprintf(page, "none\n");
255}
256
257static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page)
258{
259 if (bi != NULL)
260 return sprintf(page, "%u\n", bi->tag_size);
261 else
262 return sprintf(page, "0\n");
263}
264
265static ssize_t integrity_read_store(struct blk_integrity *bi,
266 const char *page, size_t count)
267{
268 char *p = (char *) page;
269 unsigned long val = simple_strtoul(p, &p, 10);
270
271 if (val)
Jens Axboeb24498d2008-06-27 09:12:09 +0200272 bi->flags |= INTEGRITY_FLAG_READ;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200273 else
Jens Axboeb24498d2008-06-27 09:12:09 +0200274 bi->flags &= ~INTEGRITY_FLAG_READ;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200275
276 return count;
277}
278
279static ssize_t integrity_read_show(struct blk_integrity *bi, char *page)
280{
Jens Axboeb24498d2008-06-27 09:12:09 +0200281 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_READ) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200282}
283
284static ssize_t integrity_write_store(struct blk_integrity *bi,
285 const char *page, size_t count)
286{
287 char *p = (char *) page;
288 unsigned long val = simple_strtoul(p, &p, 10);
289
290 if (val)
Jens Axboeb24498d2008-06-27 09:12:09 +0200291 bi->flags |= INTEGRITY_FLAG_WRITE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200292 else
Jens Axboeb24498d2008-06-27 09:12:09 +0200293 bi->flags &= ~INTEGRITY_FLAG_WRITE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200294
295 return count;
296}
297
298static ssize_t integrity_write_show(struct blk_integrity *bi, char *page)
299{
Jens Axboeb24498d2008-06-27 09:12:09 +0200300 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_WRITE) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200301}
302
303static struct integrity_sysfs_entry integrity_format_entry = {
304 .attr = { .name = "format", .mode = S_IRUGO },
305 .show = integrity_format_show,
306};
307
308static struct integrity_sysfs_entry integrity_tag_size_entry = {
309 .attr = { .name = "tag_size", .mode = S_IRUGO },
310 .show = integrity_tag_size_show,
311};
312
313static struct integrity_sysfs_entry integrity_read_entry = {
314 .attr = { .name = "read_verify", .mode = S_IRUGO | S_IWUSR },
315 .show = integrity_read_show,
316 .store = integrity_read_store,
317};
318
319static struct integrity_sysfs_entry integrity_write_entry = {
320 .attr = { .name = "write_generate", .mode = S_IRUGO | S_IWUSR },
321 .show = integrity_write_show,
322 .store = integrity_write_store,
323};
324
325static struct attribute *integrity_attrs[] = {
326 &integrity_format_entry.attr,
327 &integrity_tag_size_entry.attr,
328 &integrity_read_entry.attr,
329 &integrity_write_entry.attr,
330 NULL,
331};
332
Emese Revfy52cf25d2010-01-19 02:58:23 +0100333static const struct sysfs_ops integrity_ops = {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200334 .show = &integrity_attr_show,
335 .store = &integrity_attr_store,
336};
337
338static int __init blk_dev_integrity_init(void)
339{
340 integrity_cachep = kmem_cache_create("blkdev_integrity",
341 sizeof(struct blk_integrity),
342 0, SLAB_PANIC, NULL);
343 return 0;
344}
345subsys_initcall(blk_dev_integrity_init);
346
347static void blk_integrity_release(struct kobject *kobj)
348{
349 struct blk_integrity *bi =
350 container_of(kobj, struct blk_integrity, kobj);
351
352 kmem_cache_free(integrity_cachep, bi);
353}
354
355static struct kobj_type integrity_ktype = {
356 .default_attrs = integrity_attrs,
357 .sysfs_ops = &integrity_ops,
358 .release = blk_integrity_release,
359};
360
361/**
362 * blk_integrity_register - Register a gendisk as being integrity-capable
363 * @disk: struct gendisk pointer to make integrity-aware
Martin K. Petersen32231632009-01-04 02:43:40 -0500364 * @template: optional integrity profile to register
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200365 *
366 * Description: When a device needs to advertise itself as being able
367 * to send/receive integrity metadata it must use this function to
368 * register the capability with the block layer. The template is a
369 * blk_integrity struct with values appropriate for the underlying
Martin K. Petersen32231632009-01-04 02:43:40 -0500370 * hardware. If template is NULL the new profile is allocated but
371 * not filled out. See Documentation/block/data-integrity.txt.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200372 */
373int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
374{
375 struct blk_integrity *bi;
376
377 BUG_ON(disk == NULL);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200378
379 if (disk->integrity == NULL) {
Jens Axboeb9846792008-06-17 19:05:48 +0200380 bi = kmem_cache_alloc(integrity_cachep,
Martin K. Petersen32231632009-01-04 02:43:40 -0500381 GFP_KERNEL | __GFP_ZERO);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200382 if (!bi)
383 return -1;
384
385 if (kobject_init_and_add(&bi->kobj, &integrity_ktype,
Tejun Heoed9e1982008-08-25 19:56:05 +0900386 &disk_to_dev(disk)->kobj,
387 "%s", "integrity")) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200388 kmem_cache_free(integrity_cachep, bi);
389 return -1;
390 }
391
392 kobject_uevent(&bi->kobj, KOBJ_ADD);
393
Jens Axboeb24498d2008-06-27 09:12:09 +0200394 bi->flags |= INTEGRITY_FLAG_READ | INTEGRITY_FLAG_WRITE;
Martin K. Petersene1defc42009-05-22 17:17:49 -0400395 bi->sector_size = queue_logical_block_size(disk->queue);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200396 disk->integrity = bi;
397 } else
398 bi = disk->integrity;
399
400 /* Use the provided profile as template */
Martin K. Petersen32231632009-01-04 02:43:40 -0500401 if (template != NULL) {
402 bi->name = template->name;
403 bi->generate_fn = template->generate_fn;
404 bi->verify_fn = template->verify_fn;
405 bi->tuple_size = template->tuple_size;
406 bi->set_tag_fn = template->set_tag_fn;
407 bi->get_tag_fn = template->get_tag_fn;
408 bi->tag_size = template->tag_size;
409 } else
410 bi->name = "unsupported";
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200411
412 return 0;
413}
414EXPORT_SYMBOL(blk_integrity_register);
415
416/**
417 * blk_integrity_unregister - Remove block integrity profile
418 * @disk: disk whose integrity profile to deallocate
419 *
420 * Description: This function frees all memory used by the block
421 * integrity profile. To be called at device teardown.
422 */
423void blk_integrity_unregister(struct gendisk *disk)
424{
425 struct blk_integrity *bi;
426
427 if (!disk || !disk->integrity)
428 return;
429
430 bi = disk->integrity;
431
432 kobject_uevent(&bi->kobj, KOBJ_REMOVE);
433 kobject_del(&bi->kobj);
Xiaotian Feng3839e4b2009-07-28 09:11:14 +0200434 kobject_put(&bi->kobj);
Martin K. Petersen0c032ab2008-10-01 03:38:38 -0400435 disk->integrity = NULL;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200436}
437EXPORT_SYMBOL(blk_integrity_unregister);