]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/blk-integrity.c
block: Export integrity data interval size in sysfs
[mirror_ubuntu-artful-kernel.git] / block / blk-integrity.c
CommitLineData
7ba1ba12
MP
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>
66114cad 24#include <linux/backing-dev.h>
7ba1ba12
MP
25#include <linux/mempool.h>
26#include <linux/bio.h>
27#include <linux/scatterlist.h>
d5decd3b 28#include <linux/export.h>
5a0e3ad6 29#include <linux/slab.h>
7ba1ba12
MP
30
31#include "blk.h"
32
33static struct kmem_cache *integrity_cachep;
34
a63a5cf8
MS
35static const char *bi_unsupported_name = "unsupported";
36
7ba1ba12
MP
37/**
38 * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
13f05c8d
MP
39 * @q: request queue
40 * @bio: bio with integrity metadata attached
7ba1ba12
MP
41 *
42 * Description: Returns the number of elements required in a
13f05c8d 43 * scatterlist corresponding to the integrity metadata in a bio.
7ba1ba12 44 */
13f05c8d 45int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
7ba1ba12 46{
d57a5f7c 47 struct bio_vec iv, ivprv = { NULL };
13f05c8d
MP
48 unsigned int segments = 0;
49 unsigned int seg_size = 0;
d57a5f7c
KO
50 struct bvec_iter iter;
51 int prev = 0;
7ba1ba12 52
d57a5f7c 53 bio_for_each_integrity_vec(iv, bio, iter) {
7ba1ba12 54
d57a5f7c
KO
55 if (prev) {
56 if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv))
13f05c8d
MP
57 goto new_segment;
58
d57a5f7c 59 if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv))
13f05c8d
MP
60 goto new_segment;
61
d57a5f7c 62 if (seg_size + iv.bv_len > queue_max_segment_size(q))
13f05c8d 63 goto new_segment;
7ba1ba12 64
d57a5f7c 65 seg_size += iv.bv_len;
13f05c8d
MP
66 } else {
67new_segment:
7ba1ba12 68 segments++;
d57a5f7c 69 seg_size = iv.bv_len;
13f05c8d 70 }
7ba1ba12 71
d57a5f7c 72 prev = 1;
7ba1ba12
MP
73 ivprv = iv;
74 }
75
76 return segments;
77}
78EXPORT_SYMBOL(blk_rq_count_integrity_sg);
79
80/**
81 * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
13f05c8d
MP
82 * @q: request queue
83 * @bio: bio with integrity metadata attached
7ba1ba12
MP
84 * @sglist: target scatterlist
85 *
86 * Description: Map the integrity vectors in request into a
87 * scatterlist. The scatterlist must be big enough to hold all
88 * elements. I.e. sized using blk_rq_count_integrity_sg().
89 */
13f05c8d
MP
90int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
91 struct scatterlist *sglist)
7ba1ba12 92{
d57a5f7c 93 struct bio_vec iv, ivprv = { NULL };
13f05c8d
MP
94 struct scatterlist *sg = NULL;
95 unsigned int segments = 0;
d57a5f7c
KO
96 struct bvec_iter iter;
97 int prev = 0;
7ba1ba12 98
d57a5f7c 99 bio_for_each_integrity_vec(iv, bio, iter) {
7ba1ba12 100
d57a5f7c
KO
101 if (prev) {
102 if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv))
7ba1ba12
MP
103 goto new_segment;
104
d57a5f7c 105 if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv))
13f05c8d
MP
106 goto new_segment;
107
d57a5f7c 108 if (sg->length + iv.bv_len > queue_max_segment_size(q))
13f05c8d
MP
109 goto new_segment;
110
d57a5f7c 111 sg->length += iv.bv_len;
7ba1ba12
MP
112 } else {
113new_segment:
114 if (!sg)
115 sg = sglist;
116 else {
c8164d89 117 sg_unmark_end(sg);
7ba1ba12
MP
118 sg = sg_next(sg);
119 }
120
d57a5f7c 121 sg_set_page(sg, iv.bv_page, iv.bv_len, iv.bv_offset);
7ba1ba12
MP
122 segments++;
123 }
124
d57a5f7c 125 prev = 1;
7ba1ba12
MP
126 ivprv = iv;
127 }
128
129 if (sg)
130 sg_mark_end(sg);
131
132 return segments;
133}
134EXPORT_SYMBOL(blk_rq_map_integrity_sg);
135
136/**
ad7fce93
MP
137 * blk_integrity_compare - Compare integrity profile of two disks
138 * @gd1: Disk to compare
139 * @gd2: Disk to compare
7ba1ba12
MP
140 *
141 * Description: Meta-devices like DM and MD need to verify that all
142 * sub-devices use the same integrity format before advertising to
143 * upper layers that they can send/receive integrity metadata. This
ad7fce93 144 * function can be used to check whether two gendisk devices have
7ba1ba12
MP
145 * compatible integrity formats.
146 */
ad7fce93 147int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
7ba1ba12 148{
ad7fce93
MP
149 struct blk_integrity *b1 = gd1->integrity;
150 struct blk_integrity *b2 = gd2->integrity;
7ba1ba12 151
ad7fce93
MP
152 if (!b1 && !b2)
153 return 0;
7ba1ba12
MP
154
155 if (!b1 || !b2)
ad7fce93 156 return -1;
7ba1ba12 157
a48f041d 158 if (b1->interval_exp != b2->interval_exp) {
3be91c4a
MP
159 pr_err("%s: %s/%s protection interval %u != %u\n",
160 __func__, gd1->disk_name, gd2->disk_name,
a48f041d 161 1 << b1->interval_exp, 1 << b2->interval_exp);
7ba1ba12
MP
162 return -1;
163 }
164
165 if (b1->tuple_size != b2->tuple_size) {
166 printk(KERN_ERR "%s: %s/%s tuple sz %u != %u\n", __func__,
ad7fce93 167 gd1->disk_name, gd2->disk_name,
7ba1ba12
MP
168 b1->tuple_size, b2->tuple_size);
169 return -1;
170 }
171
172 if (b1->tag_size && b2->tag_size && (b1->tag_size != b2->tag_size)) {
173 printk(KERN_ERR "%s: %s/%s tag sz %u != %u\n", __func__,
ad7fce93 174 gd1->disk_name, gd2->disk_name,
7ba1ba12
MP
175 b1->tag_size, b2->tag_size);
176 return -1;
177 }
178
0f8087ec 179 if (b1->profile != b2->profile) {
7ba1ba12 180 printk(KERN_ERR "%s: %s/%s type %s != %s\n", __func__,
ad7fce93 181 gd1->disk_name, gd2->disk_name,
0f8087ec 182 b1->profile->name, b2->profile->name);
7ba1ba12
MP
183 return -1;
184 }
185
186 return 0;
187}
188EXPORT_SYMBOL(blk_integrity_compare);
189
4eaf99be
MP
190bool blk_integrity_merge_rq(struct request_queue *q, struct request *req,
191 struct request *next)
13f05c8d 192{
4eaf99be
MP
193 if (blk_integrity_rq(req) == 0 && blk_integrity_rq(next) == 0)
194 return true;
195
196 if (blk_integrity_rq(req) == 0 || blk_integrity_rq(next) == 0)
197 return false;
198
199 if (bio_integrity(req->bio)->bip_flags !=
200 bio_integrity(next->bio)->bip_flags)
201 return false;
13f05c8d
MP
202
203 if (req->nr_integrity_segments + next->nr_integrity_segments >
204 q->limits.max_integrity_segments)
4eaf99be 205 return false;
13f05c8d 206
7f39add3
SG
207 if (integrity_req_gap_back_merge(req, next->bio))
208 return false;
209
4eaf99be 210 return true;
13f05c8d
MP
211}
212EXPORT_SYMBOL(blk_integrity_merge_rq);
213
4eaf99be
MP
214bool blk_integrity_merge_bio(struct request_queue *q, struct request *req,
215 struct bio *bio)
13f05c8d
MP
216{
217 int nr_integrity_segs;
218 struct bio *next = bio->bi_next;
219
4eaf99be
MP
220 if (blk_integrity_rq(req) == 0 && bio_integrity(bio) == NULL)
221 return true;
222
223 if (blk_integrity_rq(req) == 0 || bio_integrity(bio) == NULL)
224 return false;
225
226 if (bio_integrity(req->bio)->bip_flags != bio_integrity(bio)->bip_flags)
227 return false;
228
13f05c8d
MP
229 bio->bi_next = NULL;
230 nr_integrity_segs = blk_rq_count_integrity_sg(q, bio);
231 bio->bi_next = next;
232
233 if (req->nr_integrity_segments + nr_integrity_segs >
234 q->limits.max_integrity_segments)
4eaf99be 235 return false;
13f05c8d
MP
236
237 req->nr_integrity_segments += nr_integrity_segs;
238
4eaf99be 239 return true;
13f05c8d
MP
240}
241EXPORT_SYMBOL(blk_integrity_merge_bio);
242
7ba1ba12
MP
243struct integrity_sysfs_entry {
244 struct attribute attr;
245 ssize_t (*show)(struct blk_integrity *, char *);
246 ssize_t (*store)(struct blk_integrity *, const char *, size_t);
247};
248
249static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr,
250 char *page)
251{
aff34e19
MP
252 struct gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj);
253 struct blk_integrity *bi = blk_get_integrity(disk);
7ba1ba12
MP
254 struct integrity_sysfs_entry *entry =
255 container_of(attr, struct integrity_sysfs_entry, attr);
256
257 return entry->show(bi, page);
258}
259
b984679e
JA
260static ssize_t integrity_attr_store(struct kobject *kobj,
261 struct attribute *attr, const char *page,
262 size_t count)
7ba1ba12 263{
aff34e19
MP
264 struct gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj);
265 struct blk_integrity *bi = blk_get_integrity(disk);
7ba1ba12
MP
266 struct integrity_sysfs_entry *entry =
267 container_of(attr, struct integrity_sysfs_entry, attr);
268 ssize_t ret = 0;
269
270 if (entry->store)
271 ret = entry->store(bi, page, count);
272
273 return ret;
274}
275
276static ssize_t integrity_format_show(struct blk_integrity *bi, char *page)
277{
0f8087ec
MP
278 if (bi != NULL && bi->profile->name != NULL)
279 return sprintf(page, "%s\n", bi->profile->name);
7ba1ba12
MP
280 else
281 return sprintf(page, "none\n");
282}
283
284static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page)
285{
286 if (bi != NULL)
287 return sprintf(page, "%u\n", bi->tag_size);
288 else
289 return sprintf(page, "0\n");
290}
291
4c241d08
MP
292static ssize_t integrity_interval_show(struct blk_integrity *bi, char *page)
293{
294 if (bi != NULL)
295 return sprintf(page, "%u\n", 1 << bi->interval_exp);
296 else
297 return sprintf(page, "0\n");
298}
299
8288f496
MP
300static ssize_t integrity_verify_store(struct blk_integrity *bi,
301 const char *page, size_t count)
7ba1ba12
MP
302{
303 char *p = (char *) page;
304 unsigned long val = simple_strtoul(p, &p, 10);
305
306 if (val)
8288f496 307 bi->flags |= BLK_INTEGRITY_VERIFY;
7ba1ba12 308 else
8288f496 309 bi->flags &= ~BLK_INTEGRITY_VERIFY;
7ba1ba12
MP
310
311 return count;
312}
313
8288f496 314static ssize_t integrity_verify_show(struct blk_integrity *bi, char *page)
7ba1ba12 315{
8288f496 316 return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_VERIFY) != 0);
7ba1ba12
MP
317}
318
8288f496
MP
319static ssize_t integrity_generate_store(struct blk_integrity *bi,
320 const char *page, size_t count)
7ba1ba12
MP
321{
322 char *p = (char *) page;
323 unsigned long val = simple_strtoul(p, &p, 10);
324
325 if (val)
8288f496 326 bi->flags |= BLK_INTEGRITY_GENERATE;
7ba1ba12 327 else
8288f496 328 bi->flags &= ~BLK_INTEGRITY_GENERATE;
7ba1ba12
MP
329
330 return count;
331}
332
8288f496 333static ssize_t integrity_generate_show(struct blk_integrity *bi, char *page)
7ba1ba12 334{
8288f496 335 return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_GENERATE) != 0);
7ba1ba12
MP
336}
337
3aec2f41
MP
338static ssize_t integrity_device_show(struct blk_integrity *bi, char *page)
339{
340 return sprintf(page, "%u\n",
341 (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) != 0);
342}
343
7ba1ba12
MP
344static struct integrity_sysfs_entry integrity_format_entry = {
345 .attr = { .name = "format", .mode = S_IRUGO },
346 .show = integrity_format_show,
347};
348
349static struct integrity_sysfs_entry integrity_tag_size_entry = {
350 .attr = { .name = "tag_size", .mode = S_IRUGO },
351 .show = integrity_tag_size_show,
352};
353
4c241d08
MP
354static struct integrity_sysfs_entry integrity_interval_entry = {
355 .attr = { .name = "protection_interval_bytes", .mode = S_IRUGO },
356 .show = integrity_interval_show,
357};
358
8288f496 359static struct integrity_sysfs_entry integrity_verify_entry = {
7ba1ba12 360 .attr = { .name = "read_verify", .mode = S_IRUGO | S_IWUSR },
8288f496
MP
361 .show = integrity_verify_show,
362 .store = integrity_verify_store,
7ba1ba12
MP
363};
364
8288f496 365static struct integrity_sysfs_entry integrity_generate_entry = {
7ba1ba12 366 .attr = { .name = "write_generate", .mode = S_IRUGO | S_IWUSR },
8288f496
MP
367 .show = integrity_generate_show,
368 .store = integrity_generate_store,
7ba1ba12
MP
369};
370
3aec2f41
MP
371static struct integrity_sysfs_entry integrity_device_entry = {
372 .attr = { .name = "device_is_integrity_capable", .mode = S_IRUGO },
373 .show = integrity_device_show,
374};
375
7ba1ba12
MP
376static struct attribute *integrity_attrs[] = {
377 &integrity_format_entry.attr,
378 &integrity_tag_size_entry.attr,
4c241d08 379 &integrity_interval_entry.attr,
8288f496
MP
380 &integrity_verify_entry.attr,
381 &integrity_generate_entry.attr,
3aec2f41 382 &integrity_device_entry.attr,
7ba1ba12
MP
383 NULL,
384};
385
52cf25d0 386static const struct sysfs_ops integrity_ops = {
7ba1ba12
MP
387 .show = &integrity_attr_show,
388 .store = &integrity_attr_store,
389};
390
391static int __init blk_dev_integrity_init(void)
392{
393 integrity_cachep = kmem_cache_create("blkdev_integrity",
394 sizeof(struct blk_integrity),
395 0, SLAB_PANIC, NULL);
396 return 0;
397}
398subsys_initcall(blk_dev_integrity_init);
399
400static void blk_integrity_release(struct kobject *kobj)
401{
aff34e19
MP
402 struct gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj);
403 struct blk_integrity *bi = blk_get_integrity(disk);
7ba1ba12
MP
404
405 kmem_cache_free(integrity_cachep, bi);
406}
407
408static struct kobj_type integrity_ktype = {
409 .default_attrs = integrity_attrs,
410 .sysfs_ops = &integrity_ops,
411 .release = blk_integrity_release,
412};
413
a63a5cf8
MS
414bool blk_integrity_is_initialized(struct gendisk *disk)
415{
416 struct blk_integrity *bi = blk_get_integrity(disk);
417
0f8087ec
MP
418 return (bi && bi->profile->name && strcmp(bi->profile->name,
419 bi_unsupported_name) != 0);
a63a5cf8
MS
420}
421EXPORT_SYMBOL(blk_integrity_is_initialized);
422
7ba1ba12
MP
423/**
424 * blk_integrity_register - Register a gendisk as being integrity-capable
425 * @disk: struct gendisk pointer to make integrity-aware
32231638 426 * @template: optional integrity profile to register
7ba1ba12
MP
427 *
428 * Description: When a device needs to advertise itself as being able
429 * to send/receive integrity metadata it must use this function to
430 * register the capability with the block layer. The template is a
431 * blk_integrity struct with values appropriate for the underlying
32231638
MP
432 * hardware. If template is NULL the new profile is allocated but
433 * not filled out. See Documentation/block/data-integrity.txt.
7ba1ba12
MP
434 */
435int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
436{
437 struct blk_integrity *bi;
438
439 BUG_ON(disk == NULL);
7ba1ba12
MP
440
441 if (disk->integrity == NULL) {
b984679e 442 bi = kmem_cache_alloc(integrity_cachep,
32231638 443 GFP_KERNEL | __GFP_ZERO);
7ba1ba12
MP
444 if (!bi)
445 return -1;
446
aff34e19 447 if (kobject_init_and_add(&disk->integrity_kobj, &integrity_ktype,
ed9e1982
TH
448 &disk_to_dev(disk)->kobj,
449 "%s", "integrity")) {
7ba1ba12
MP
450 kmem_cache_free(integrity_cachep, bi);
451 return -1;
452 }
453
aff34e19 454 kobject_uevent(&disk->integrity_kobj, KOBJ_ADD);
7ba1ba12 455
8288f496 456 bi->flags |= BLK_INTEGRITY_VERIFY | BLK_INTEGRITY_GENERATE;
a48f041d 457 bi->interval_exp = ilog2(queue_logical_block_size(disk->queue));
7ba1ba12
MP
458 disk->integrity = bi;
459 } else
460 bi = disk->integrity;
461
462 /* Use the provided profile as template */
32231638 463 if (template != NULL) {
0f8087ec 464 bi->profile = template->profile;
32231638 465 bi->tuple_size = template->tuple_size;
32231638 466 bi->tag_size = template->tag_size;
8288f496 467 bi->flags |= template->flags;
32231638 468 } else
0f8087ec 469 bi->profile->name = bi_unsupported_name;
7ba1ba12 470
7d311cda
DW
471 disk->queue->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES;
472
7ba1ba12
MP
473 return 0;
474}
475EXPORT_SYMBOL(blk_integrity_register);
476
477/**
478 * blk_integrity_unregister - Remove block integrity profile
479 * @disk: disk whose integrity profile to deallocate
480 *
481 * Description: This function frees all memory used by the block
482 * integrity profile. To be called at device teardown.
483 */
484void blk_integrity_unregister(struct gendisk *disk)
485{
486 struct blk_integrity *bi;
487
488 if (!disk || !disk->integrity)
489 return;
490
7d311cda
DW
491 disk->queue->backing_dev_info.capabilities &= ~BDI_CAP_STABLE_WRITES;
492
7ba1ba12
MP
493 bi = disk->integrity;
494
aff34e19
MP
495 kobject_uevent(&disk->integrity_kobj, KOBJ_REMOVE);
496 kobject_del(&disk->integrity_kobj);
497 kobject_put(&disk->integrity_kobj);
0c032ab8 498 disk->integrity = NULL;
7ba1ba12
MP
499}
500EXPORT_SYMBOL(blk_integrity_unregister);