fbdev: c2p - Extract common c2p core to c2p_core.h
[linux/fpc-iii.git] / block / blk-integrity.c
blob61a8e2f8fdd0bbb384fe7eab86860df6f03f4949
1 /*
2 * blk-integrity.c - Block layer data integrity extensions
4 * Copyright (C) 2007, 2008 Oracle Corporation
5 * Written by: Martin K. Petersen <martin.petersen@oracle.com>
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.
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.
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.
23 #include <linux/blkdev.h>
24 #include <linux/mempool.h>
25 #include <linux/bio.h>
26 #include <linux/scatterlist.h>
28 #include "blk.h"
30 static struct kmem_cache *integrity_cachep;
32 /**
33 * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
34 * @rq: request with integrity metadata attached
36 * Description: Returns the number of elements required in a
37 * scatterlist corresponding to the integrity metadata in a request.
39 int blk_rq_count_integrity_sg(struct request *rq)
41 struct bio_vec *iv, *ivprv;
42 struct req_iterator iter;
43 unsigned int segments;
45 ivprv = NULL;
46 segments = 0;
48 rq_for_each_integrity_segment(iv, rq, iter) {
50 if (!ivprv || !BIOVEC_PHYS_MERGEABLE(ivprv, iv))
51 segments++;
53 ivprv = iv;
56 return segments;
58 EXPORT_SYMBOL(blk_rq_count_integrity_sg);
60 /**
61 * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
62 * @rq: request with integrity metadata attached
63 * @sglist: target scatterlist
65 * Description: Map the integrity vectors in request into a
66 * scatterlist. The scatterlist must be big enough to hold all
67 * elements. I.e. sized using blk_rq_count_integrity_sg().
69 int blk_rq_map_integrity_sg(struct request *rq, struct scatterlist *sglist)
71 struct bio_vec *iv, *ivprv;
72 struct req_iterator iter;
73 struct scatterlist *sg;
74 unsigned int segments;
76 ivprv = NULL;
77 sg = NULL;
78 segments = 0;
80 rq_for_each_integrity_segment(iv, rq, iter) {
82 if (ivprv) {
83 if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
84 goto new_segment;
86 sg->length += iv->bv_len;
87 } else {
88 new_segment:
89 if (!sg)
90 sg = sglist;
91 else {
92 sg->page_link &= ~0x02;
93 sg = sg_next(sg);
96 sg_set_page(sg, iv->bv_page, iv->bv_len, iv->bv_offset);
97 segments++;
100 ivprv = iv;
103 if (sg)
104 sg_mark_end(sg);
106 return segments;
108 EXPORT_SYMBOL(blk_rq_map_integrity_sg);
111 * blk_integrity_compare - Compare integrity profile of two disks
112 * @gd1: Disk to compare
113 * @gd2: Disk to compare
115 * Description: Meta-devices like DM and MD need to verify that all
116 * sub-devices use the same integrity format before advertising to
117 * upper layers that they can send/receive integrity metadata. This
118 * function can be used to check whether two gendisk devices have
119 * compatible integrity formats.
121 int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
123 struct blk_integrity *b1 = gd1->integrity;
124 struct blk_integrity *b2 = gd2->integrity;
126 if (!b1 && !b2)
127 return 0;
129 if (!b1 || !b2)
130 return -1;
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;
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;
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;
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;
160 return 0;
162 EXPORT_SYMBOL(blk_integrity_compare);
164 struct 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);
170 static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr,
171 char *page)
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);
178 return entry->show(bi, page);
181 static ssize_t integrity_attr_store(struct kobject *kobj,
182 struct attribute *attr, const char *page,
183 size_t count)
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;
191 if (entry->store)
192 ret = entry->store(bi, page, count);
194 return ret;
197 static ssize_t integrity_format_show(struct blk_integrity *bi, char *page)
199 if (bi != NULL && bi->name != NULL)
200 return sprintf(page, "%s\n", bi->name);
201 else
202 return sprintf(page, "none\n");
205 static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page)
207 if (bi != NULL)
208 return sprintf(page, "%u\n", bi->tag_size);
209 else
210 return sprintf(page, "0\n");
213 static ssize_t integrity_read_store(struct blk_integrity *bi,
214 const char *page, size_t count)
216 char *p = (char *) page;
217 unsigned long val = simple_strtoul(p, &p, 10);
219 if (val)
220 bi->flags |= INTEGRITY_FLAG_READ;
221 else
222 bi->flags &= ~INTEGRITY_FLAG_READ;
224 return count;
227 static ssize_t integrity_read_show(struct blk_integrity *bi, char *page)
229 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_READ) != 0);
232 static ssize_t integrity_write_store(struct blk_integrity *bi,
233 const char *page, size_t count)
235 char *p = (char *) page;
236 unsigned long val = simple_strtoul(p, &p, 10);
238 if (val)
239 bi->flags |= INTEGRITY_FLAG_WRITE;
240 else
241 bi->flags &= ~INTEGRITY_FLAG_WRITE;
243 return count;
246 static ssize_t integrity_write_show(struct blk_integrity *bi, char *page)
248 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_WRITE) != 0);
251 static struct integrity_sysfs_entry integrity_format_entry = {
252 .attr = { .name = "format", .mode = S_IRUGO },
253 .show = integrity_format_show,
256 static struct integrity_sysfs_entry integrity_tag_size_entry = {
257 .attr = { .name = "tag_size", .mode = S_IRUGO },
258 .show = integrity_tag_size_show,
261 static 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,
267 static 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,
273 static 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,
281 static struct sysfs_ops integrity_ops = {
282 .show = &integrity_attr_show,
283 .store = &integrity_attr_store,
286 static int __init blk_dev_integrity_init(void)
288 integrity_cachep = kmem_cache_create("blkdev_integrity",
289 sizeof(struct blk_integrity),
290 0, SLAB_PANIC, NULL);
291 return 0;
293 subsys_initcall(blk_dev_integrity_init);
295 static void blk_integrity_release(struct kobject *kobj)
297 struct blk_integrity *bi =
298 container_of(kobj, struct blk_integrity, kobj);
300 kmem_cache_free(integrity_cachep, bi);
303 static struct kobj_type integrity_ktype = {
304 .default_attrs = integrity_attrs,
305 .sysfs_ops = &integrity_ops,
306 .release = blk_integrity_release,
310 * blk_integrity_register - Register a gendisk as being integrity-capable
311 * @disk: struct gendisk pointer to make integrity-aware
312 * @template: integrity profile
314 * Description: When a device needs to advertise itself as being able
315 * to send/receive integrity metadata it must use this function to
316 * register the capability with the block layer. The template is a
317 * blk_integrity struct with values appropriate for the underlying
318 * hardware. See Documentation/block/data-integrity.txt.
320 int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
322 struct blk_integrity *bi;
324 BUG_ON(disk == NULL);
325 BUG_ON(template == NULL);
327 if (disk->integrity == NULL) {
328 bi = kmem_cache_alloc(integrity_cachep,
329 GFP_KERNEL | __GFP_ZERO);
330 if (!bi)
331 return -1;
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;
340 kobject_uevent(&bi->kobj, KOBJ_ADD);
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;
348 /* Use the provided profile as template */
349 bi->name = template->name;
350 bi->generate_fn = template->generate_fn;
351 bi->verify_fn = template->verify_fn;
352 bi->tuple_size = template->tuple_size;
353 bi->set_tag_fn = template->set_tag_fn;
354 bi->get_tag_fn = template->get_tag_fn;
355 bi->tag_size = template->tag_size;
357 return 0;
359 EXPORT_SYMBOL(blk_integrity_register);
362 * blk_integrity_unregister - Remove block integrity profile
363 * @disk: disk whose integrity profile to deallocate
365 * Description: This function frees all memory used by the block
366 * integrity profile. To be called at device teardown.
368 void blk_integrity_unregister(struct gendisk *disk)
370 struct blk_integrity *bi;
372 if (!disk || !disk->integrity)
373 return;
375 bi = disk->integrity;
377 kobject_uevent(&bi->kobj, KOBJ_REMOVE);
378 kobject_del(&bi->kobj);
379 kmem_cache_free(integrity_cachep, bi);
380 disk->integrity = NULL;
382 EXPORT_SYMBOL(blk_integrity_unregister);