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