]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - block/blk-integrity.c
crypto: aesni-intel - Merge with fpu.ko
[linux-2.6.git] / block / blk-integrity.c
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
23 #include <linux/blkdev.h>
24 #include <linux/mempool.h>
25 #include <linux/bio.h>
26 #include <linux/scatterlist.h>
27 #include <linux/slab.h>
28
29 #include "blk.h"
30
31 static struct kmem_cache *integrity_cachep;
32
33 /**
34  * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
35  * @q:          request queue
36  * @bio:        bio with integrity metadata attached
37  *
38  * Description: Returns the number of elements required in a
39  * scatterlist corresponding to the integrity metadata in a bio.
40  */
41 int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
42 {
43         struct bio_vec *iv, *ivprv = NULL;
44         unsigned int segments = 0;
45         unsigned int seg_size = 0;
46         unsigned int i = 0;
47
48         bio_for_each_integrity_vec(iv, bio, i) {
49
50                 if (ivprv) {
51                         if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
52                                 goto new_segment;
53
54                         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 {
62 new_segment:
63                         segments++;
64                         seg_size = iv->bv_len;
65                 }
66
67                 ivprv = iv;
68         }
69
70         return segments;
71 }
72 EXPORT_SYMBOL(blk_rq_count_integrity_sg);
73
74 /**
75  * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
76  * @q:          request queue
77  * @bio:        bio with integrity metadata attached
78  * @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  */
84 int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
85                             struct scatterlist *sglist)
86 {
87         struct bio_vec *iv, *ivprv = NULL;
88         struct scatterlist *sg = NULL;
89         unsigned int segments = 0;
90         unsigned int i = 0;
91
92         bio_for_each_integrity_vec(iv, bio, i) {
93
94                 if (ivprv) {
95                         if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
96                                 goto new_segment;
97
98                         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
104                         sg->length += iv->bv_len;
105                 } else {
106 new_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 }
126 EXPORT_SYMBOL(blk_rq_map_integrity_sg);
127
128 /**
129  * blk_integrity_compare - Compare integrity profile of two disks
130  * @gd1:        Disk to compare
131  * @gd2:        Disk to compare
132  *
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
136  * function can be used to check whether two gendisk devices have
137  * compatible integrity formats.
138  */
139 int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
140 {
141         struct blk_integrity *b1 = gd1->integrity;
142         struct blk_integrity *b2 = gd2->integrity;
143
144         if (!b1 && !b2)
145                 return 0;
146
147         if (!b1 || !b2)
148                 return -1;
149
150         if (b1->sector_size != b2->sector_size) {
151                 printk(KERN_ERR "%s: %s/%s sector sz %u != %u\n", __func__,
152                        gd1->disk_name, gd2->disk_name,
153                        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__,
159                        gd1->disk_name, gd2->disk_name,
160                        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__,
166                        gd1->disk_name, gd2->disk_name,
167                        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__,
173                        gd1->disk_name, gd2->disk_name,
174                        b1->name, b2->name);
175                 return -1;
176         }
177
178         return 0;
179 }
180 EXPORT_SYMBOL(blk_integrity_compare);
181
182 int 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 }
194 EXPORT_SYMBOL(blk_integrity_merge_rq);
195
196 int 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 }
214 EXPORT_SYMBOL(blk_integrity_merge_bio);
215
216 struct 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
222 static 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
233 static ssize_t integrity_attr_store(struct kobject *kobj,
234                                     struct attribute *attr, const char *page,
235                                     size_t count)
236 {
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
249 static 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
257 static 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
265 static 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)
272                 bi->flags |= INTEGRITY_FLAG_READ;
273         else
274                 bi->flags &= ~INTEGRITY_FLAG_READ;
275
276         return count;
277 }
278
279 static ssize_t integrity_read_show(struct blk_integrity *bi, char *page)
280 {
281         return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_READ) != 0);
282 }
283
284 static 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)
291                 bi->flags |= INTEGRITY_FLAG_WRITE;
292         else
293                 bi->flags &= ~INTEGRITY_FLAG_WRITE;
294
295         return count;
296 }
297
298 static ssize_t integrity_write_show(struct blk_integrity *bi, char *page)
299 {
300         return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_WRITE) != 0);
301 }
302
303 static struct integrity_sysfs_entry integrity_format_entry = {
304         .attr = { .name = "format", .mode = S_IRUGO },
305         .show = integrity_format_show,
306 };
307
308 static struct integrity_sysfs_entry integrity_tag_size_entry = {
309         .attr = { .name = "tag_size", .mode = S_IRUGO },
310         .show = integrity_tag_size_show,
311 };
312
313 static 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
319 static 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
325 static 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
333 static const struct sysfs_ops integrity_ops = {
334         .show   = &integrity_attr_show,
335         .store  = &integrity_attr_store,
336 };
337
338 static 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 }
345 subsys_initcall(blk_dev_integrity_init);
346
347 static 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
355 static 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
364  * @template:   optional integrity profile to register
365  *
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
370  * hardware.  If template is NULL the new profile is allocated but
371  * not filled out. See Documentation/block/data-integrity.txt.
372  */
373 int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
374 {
375         struct blk_integrity *bi;
376
377         BUG_ON(disk == NULL);
378
379         if (disk->integrity == NULL) {
380                 bi = kmem_cache_alloc(integrity_cachep,
381                                       GFP_KERNEL | __GFP_ZERO);
382                 if (!bi)
383                         return -1;
384
385                 if (kobject_init_and_add(&bi->kobj, &integrity_ktype,
386                                          &disk_to_dev(disk)->kobj,
387                                          "%s", "integrity")) {
388                         kmem_cache_free(integrity_cachep, bi);
389                         return -1;
390                 }
391
392                 kobject_uevent(&bi->kobj, KOBJ_ADD);
393
394                 bi->flags |= INTEGRITY_FLAG_READ | INTEGRITY_FLAG_WRITE;
395                 bi->sector_size = queue_logical_block_size(disk->queue);
396                 disk->integrity = bi;
397         } else
398                 bi = disk->integrity;
399
400         /* Use the provided profile as template */
401         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";
411
412         return 0;
413 }
414 EXPORT_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  */
423 void 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);
434         kobject_put(&bi->kobj);
435         disk->integrity = NULL;
436 }
437 EXPORT_SYMBOL(blk_integrity_unregister);