acer-wmi: Fix capitalisation of GUID
[zen-stable.git] / block / blk-integrity.c
blob54bcba6c02a75ed87af935c49d4428f18eb6a615
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>
27 #include <linux/slab.h>
29 #include "blk.h"
31 static struct kmem_cache *integrity_cachep;
33 /**
34 * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
35 * @q: request queue
36 * @bio: bio with integrity metadata attached
38 * Description: Returns the number of elements required in a
39 * scatterlist corresponding to the integrity metadata in a bio.
41 int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
43 struct bio_vec *iv, *ivprv = NULL;
44 unsigned int segments = 0;
45 unsigned int seg_size = 0;
46 unsigned int i = 0;
48 bio_for_each_integrity_vec(iv, bio, i) {
50 if (ivprv) {
51 if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
52 goto new_segment;
54 if (!BIOVEC_SEG_BOUNDARY(q, ivprv, iv))
55 goto new_segment;
57 if (seg_size + iv->bv_len > queue_max_segment_size(q))
58 goto new_segment;
60 seg_size += iv->bv_len;
61 } else {
62 new_segment:
63 segments++;
64 seg_size = iv->bv_len;
67 ivprv = iv;
70 return segments;
72 EXPORT_SYMBOL(blk_rq_count_integrity_sg);
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
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().
84 int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
85 struct scatterlist *sglist)
87 struct bio_vec *iv, *ivprv = NULL;
88 struct scatterlist *sg = NULL;
89 unsigned int segments = 0;
90 unsigned int i = 0;
92 bio_for_each_integrity_vec(iv, bio, i) {
94 if (ivprv) {
95 if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv))
96 goto new_segment;
98 if (!BIOVEC_SEG_BOUNDARY(q, ivprv, iv))
99 goto new_segment;
101 if (sg->length + iv->bv_len > queue_max_segment_size(q))
102 goto new_segment;
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);
114 sg_set_page(sg, iv->bv_page, iv->bv_len, iv->bv_offset);
115 segments++;
118 ivprv = iv;
121 if (sg)
122 sg_mark_end(sg);
124 return segments;
126 EXPORT_SYMBOL(blk_rq_map_integrity_sg);
129 * blk_integrity_compare - Compare integrity profile of two disks
130 * @gd1: Disk to compare
131 * @gd2: Disk to compare
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.
139 int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
141 struct blk_integrity *b1 = gd1->integrity;
142 struct blk_integrity *b2 = gd2->integrity;
144 if (!b1 && !b2)
145 return 0;
147 if (!b1 || !b2)
148 return -1;
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;
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;
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;
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;
178 return 0;
180 EXPORT_SYMBOL(blk_integrity_compare);
182 int blk_integrity_merge_rq(struct request_queue *q, struct request *req,
183 struct request *next)
185 if (blk_integrity_rq(req) != blk_integrity_rq(next))
186 return -1;
188 if (req->nr_integrity_segments + next->nr_integrity_segments >
189 q->limits.max_integrity_segments)
190 return -1;
192 return 0;
194 EXPORT_SYMBOL(blk_integrity_merge_rq);
196 int blk_integrity_merge_bio(struct request_queue *q, struct request *req,
197 struct bio *bio)
199 int nr_integrity_segs;
200 struct bio *next = bio->bi_next;
202 bio->bi_next = NULL;
203 nr_integrity_segs = blk_rq_count_integrity_sg(q, bio);
204 bio->bi_next = next;
206 if (req->nr_integrity_segments + nr_integrity_segs >
207 q->limits.max_integrity_segments)
208 return -1;
210 req->nr_integrity_segments += nr_integrity_segs;
212 return 0;
214 EXPORT_SYMBOL(blk_integrity_merge_bio);
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);
222 static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr,
223 char *page)
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);
230 return entry->show(bi, page);
233 static ssize_t integrity_attr_store(struct kobject *kobj,
234 struct attribute *attr, const char *page,
235 size_t count)
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;
243 if (entry->store)
244 ret = entry->store(bi, page, count);
246 return ret;
249 static ssize_t integrity_format_show(struct blk_integrity *bi, char *page)
251 if (bi != NULL && bi->name != NULL)
252 return sprintf(page, "%s\n", bi->name);
253 else
254 return sprintf(page, "none\n");
257 static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page)
259 if (bi != NULL)
260 return sprintf(page, "%u\n", bi->tag_size);
261 else
262 return sprintf(page, "0\n");
265 static ssize_t integrity_read_store(struct blk_integrity *bi,
266 const char *page, size_t count)
268 char *p = (char *) page;
269 unsigned long val = simple_strtoul(p, &p, 10);
271 if (val)
272 bi->flags |= INTEGRITY_FLAG_READ;
273 else
274 bi->flags &= ~INTEGRITY_FLAG_READ;
276 return count;
279 static ssize_t integrity_read_show(struct blk_integrity *bi, char *page)
281 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_READ) != 0);
284 static ssize_t integrity_write_store(struct blk_integrity *bi,
285 const char *page, size_t count)
287 char *p = (char *) page;
288 unsigned long val = simple_strtoul(p, &p, 10);
290 if (val)
291 bi->flags |= INTEGRITY_FLAG_WRITE;
292 else
293 bi->flags &= ~INTEGRITY_FLAG_WRITE;
295 return count;
298 static ssize_t integrity_write_show(struct blk_integrity *bi, char *page)
300 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_WRITE) != 0);
303 static struct integrity_sysfs_entry integrity_format_entry = {
304 .attr = { .name = "format", .mode = S_IRUGO },
305 .show = integrity_format_show,
308 static struct integrity_sysfs_entry integrity_tag_size_entry = {
309 .attr = { .name = "tag_size", .mode = S_IRUGO },
310 .show = integrity_tag_size_show,
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,
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,
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,
333 static const struct sysfs_ops integrity_ops = {
334 .show = &integrity_attr_show,
335 .store = &integrity_attr_store,
338 static int __init blk_dev_integrity_init(void)
340 integrity_cachep = kmem_cache_create("blkdev_integrity",
341 sizeof(struct blk_integrity),
342 0, SLAB_PANIC, NULL);
343 return 0;
345 subsys_initcall(blk_dev_integrity_init);
347 static void blk_integrity_release(struct kobject *kobj)
349 struct blk_integrity *bi =
350 container_of(kobj, struct blk_integrity, kobj);
352 kmem_cache_free(integrity_cachep, bi);
355 static struct kobj_type integrity_ktype = {
356 .default_attrs = integrity_attrs,
357 .sysfs_ops = &integrity_ops,
358 .release = blk_integrity_release,
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
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.
373 int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
375 struct blk_integrity *bi;
377 BUG_ON(disk == NULL);
379 if (disk->integrity == NULL) {
380 bi = kmem_cache_alloc(integrity_cachep,
381 GFP_KERNEL | __GFP_ZERO);
382 if (!bi)
383 return -1;
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;
392 kobject_uevent(&bi->kobj, KOBJ_ADD);
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;
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";
412 return 0;
414 EXPORT_SYMBOL(blk_integrity_register);
417 * blk_integrity_unregister - Remove block integrity profile
418 * @disk: disk whose integrity profile to deallocate
420 * Description: This function frees all memory used by the block
421 * integrity profile. To be called at device teardown.
423 void blk_integrity_unregister(struct gendisk *disk)
425 struct blk_integrity *bi;
427 if (!disk || !disk->integrity)
428 return;
430 bi = disk->integrity;
432 kobject_uevent(&bi->kobj, KOBJ_REMOVE);
433 kobject_del(&bi->kobj);
434 kobject_put(&bi->kobj);
435 disk->integrity = NULL;
437 EXPORT_SYMBOL(blk_integrity_unregister);