1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/blkdev.h>
24#include <linux/mempool.h>
25#include <linux/bio.h>
26#include <linux/scatterlist.h>
27
28#include "blk.h"
29
30static struct kmem_cache *integrity_cachep;
31
32
33
34
35
36
37
38
39int blk_rq_count_integrity_sg(struct request *rq)
40{
41 struct bio_vec *iv, *ivprv;
42 struct req_iterator iter;
43 unsigned int segments;
44
45 ivprv = NULL;
46 segments = 0;
47
48 rq_for_each_integrity_segment(iv, rq, iter) {
49
50 if (!ivprv || !BIOVEC_PHYS_MERGEABLE(ivprv, iv))
51 segments++;
52
53 ivprv = iv;
54 }
55
56 return segments;
57}
58EXPORT_SYMBOL(blk_rq_count_integrity_sg);
59
60
61
62
63
64
65
66
67
68
69int blk_rq_map_integrity_sg(struct request *rq, struct scatterlist *sglist)
70{
71 struct bio_vec *iv, *ivprv;
72 struct req_iterator iter;
73 struct scatterlist *sg;
74 unsigned int segments;
75
76 ivprv = NULL;
77 sg = NULL;
78 segments = 0;
79
80 rq_for_each_integrity_segment(iv, rq, iter) {
81
82 if (ivprv) {
83 if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
84 goto new_segment;
85
86 sg->length += iv->bv_len;
87 } else {
88new_segment:
89 if (!sg)
90 sg = sglist;
91 else {
92 sg->page_link &= ~0x02;
93 sg = sg_next(sg);
94 }
95
96 sg_set_page(sg, iv->bv_page, iv->bv_len, iv->bv_offset);
97 segments++;
98 }
99
100 ivprv = iv;
101 }
102
103 if (sg)
104 sg_mark_end(sg);
105
106 return segments;
107}
108EXPORT_SYMBOL(blk_rq_map_integrity_sg);
109
110
111
112
113
114
115
116
117
118
119
120
121int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
122{
123 struct blk_integrity *b1 = gd1->integrity;
124 struct blk_integrity *b2 = gd2->integrity;
125
126 if (!b1 && !b2)
127 return 0;
128
129 if (!b1 || !b2)
130 return -1;
131
132 if (b1->sector_size != b2->sector_size) {
133 printk(KERN_ERR "%s: %s/%s sector sz %u != %u\n", __func__,
134 gd1->disk_name, gd2->disk_name,
135 b1->sector_size, b2->sector_size);
136 return -1;
137 }
138
139 if (b1->tuple_size != b2->tuple_size) {
140 printk(KERN_ERR "%s: %s/%s tuple sz %u != %u\n", __func__,
141 gd1->disk_name, gd2->disk_name,
142 b1->tuple_size, b2->tuple_size);
143 return -1;
144 }
145
146 if (b1->tag_size && b2->tag_size && (b1->tag_size != b2->tag_size)) {
147 printk(KERN_ERR "%s: %s/%s tag sz %u != %u\n", __func__,
148 gd1->disk_name, gd2->disk_name,
149 b1->tag_size, b2->tag_size);
150 return -1;
151 }
152
153 if (strcmp(b1->name, b2->name)) {
154 printk(KERN_ERR "%s: %s/%s type %s != %s\n", __func__,
155 gd1->disk_name, gd2->disk_name,
156 b1->name, b2->name);
157 return -1;
158 }
159
160 return 0;
161}
162EXPORT_SYMBOL(blk_integrity_compare);
163
164struct integrity_sysfs_entry {
165 struct attribute attr;
166 ssize_t (*show)(struct blk_integrity *, char *);
167 ssize_t (*store)(struct blk_integrity *, const char *, size_t);
168};
169
170static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr,
171 char *page)
172{
173 struct blk_integrity *bi =
174 container_of(kobj, struct blk_integrity, kobj);
175 struct integrity_sysfs_entry *entry =
176 container_of(attr, struct integrity_sysfs_entry, attr);
177
178 return entry->show(bi, page);
179}
180
181static ssize_t integrity_attr_store(struct kobject *kobj,
182 struct attribute *attr, const char *page,
183 size_t count)
184{
185 struct blk_integrity *bi =
186 container_of(kobj, struct blk_integrity, kobj);
187 struct integrity_sysfs_entry *entry =
188 container_of(attr, struct integrity_sysfs_entry, attr);
189 ssize_t ret = 0;
190
191 if (entry->store)
192 ret = entry->store(bi, page, count);
193
194 return ret;
195}
196
197static ssize_t integrity_format_show(struct blk_integrity *bi, char *page)
198{
199 if (bi != NULL && bi->name != NULL)
200 return sprintf(page, "%s\n", bi->name);
201 else
202 return sprintf(page, "none\n");
203}
204
205static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page)
206{
207 if (bi != NULL)
208 return sprintf(page, "%u\n", bi->tag_size);
209 else
210 return sprintf(page, "0\n");
211}
212
213static ssize_t integrity_read_store(struct blk_integrity *bi,
214 const char *page, size_t count)
215{
216 char *p = (char *) page;
217 unsigned long val = simple_strtoul(p, &p, 10);
218
219 if (val)
220 bi->flags |= INTEGRITY_FLAG_READ;
221 else
222 bi->flags &= ~INTEGRITY_FLAG_READ;
223
224 return count;
225}
226
227static ssize_t integrity_read_show(struct blk_integrity *bi, char *page)
228{
229 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_READ) != 0);
230}
231
232static ssize_t integrity_write_store(struct blk_integrity *bi,
233 const char *page, size_t count)
234{
235 char *p = (char *) page;
236 unsigned long val = simple_strtoul(p, &p, 10);
237
238 if (val)
239 bi->flags |= INTEGRITY_FLAG_WRITE;
240 else
241 bi->flags &= ~INTEGRITY_FLAG_WRITE;
242
243 return count;
244}
245
246static ssize_t integrity_write_show(struct blk_integrity *bi, char *page)
247{
248 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_WRITE) != 0);
249}
250
251static struct integrity_sysfs_entry integrity_format_entry = {
252 .attr = { .name = "format", .mode = S_IRUGO },
253 .show = integrity_format_show,
254};
255
256static struct integrity_sysfs_entry integrity_tag_size_entry = {
257 .attr = { .name = "tag_size", .mode = S_IRUGO },
258 .show = integrity_tag_size_show,
259};
260
261static struct integrity_sysfs_entry integrity_read_entry = {
262 .attr = { .name = "read_verify", .mode = S_IRUGO | S_IWUSR },
263 .show = integrity_read_show,
264 .store = integrity_read_store,
265};
266
267static struct integrity_sysfs_entry integrity_write_entry = {
268 .attr = { .name = "write_generate", .mode = S_IRUGO | S_IWUSR },
269 .show = integrity_write_show,
270 .store = integrity_write_store,
271};
272
273static struct attribute *integrity_attrs[] = {
274 &integrity_format_entry.attr,
275 &integrity_tag_size_entry.attr,
276 &integrity_read_entry.attr,
277 &integrity_write_entry.attr,
278 NULL,
279};
280
281static struct sysfs_ops integrity_ops = {
282 .show = &integrity_attr_show,
283 .store = &integrity_attr_store,
284};
285
286static int __init blk_dev_integrity_init(void)
287{
288 integrity_cachep = kmem_cache_create("blkdev_integrity",
289 sizeof(struct blk_integrity),
290 0, SLAB_PANIC, NULL);
291 return 0;
292}
293subsys_initcall(blk_dev_integrity_init);
294
295static void blk_integrity_release(struct kobject *kobj)
296{
297 struct blk_integrity *bi =
298 container_of(kobj, struct blk_integrity, kobj);
299
300 kmem_cache_free(integrity_cachep, bi);
301}
302
303static struct kobj_type integrity_ktype = {
304 .default_attrs = integrity_attrs,
305 .sysfs_ops = &integrity_ops,
306 .release = blk_integrity_release,
307};
308
309
310
311
312
313
314
315
316
317
318
319
320
321int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
322{
323 struct blk_integrity *bi;
324
325 BUG_ON(disk == NULL);
326
327 if (disk->integrity == NULL) {
328 bi = kmem_cache_alloc(integrity_cachep,
329 GFP_KERNEL | __GFP_ZERO);
330 if (!bi)
331 return -1;
332
333 if (kobject_init_and_add(&bi->kobj, &integrity_ktype,
334 &disk_to_dev(disk)->kobj,
335 "%s", "integrity")) {
336 kmem_cache_free(integrity_cachep, bi);
337 return -1;
338 }
339
340 kobject_uevent(&bi->kobj, KOBJ_ADD);
341
342 bi->flags |= INTEGRITY_FLAG_READ | INTEGRITY_FLAG_WRITE;
343 bi->sector_size = disk->queue->hardsect_size;
344 disk->integrity = bi;
345 } else
346 bi = disk->integrity;
347
348
349 if (template != NULL) {
350 bi->name = template->name;
351 bi->generate_fn = template->generate_fn;
352 bi->verify_fn = template->verify_fn;
353 bi->tuple_size = template->tuple_size;
354 bi->set_tag_fn = template->set_tag_fn;
355 bi->get_tag_fn = template->get_tag_fn;
356 bi->tag_size = template->tag_size;
357 } else
358 bi->name = "unsupported";
359
360 return 0;
361}
362EXPORT_SYMBOL(blk_integrity_register);
363
364
365
366
367
368
369
370
371void blk_integrity_unregister(struct gendisk *disk)
372{
373 struct blk_integrity *bi;
374
375 if (!disk || !disk->integrity)
376 return;
377
378 bi = disk->integrity;
379
380 kobject_uevent(&bi->kobj, KOBJ_REMOVE);
381 kobject_del(&bi->kobj);
382 kmem_cache_free(integrity_cachep, bi);
383 disk->integrity = NULL;
384}
385EXPORT_SYMBOL(blk_integrity_unregister);
386