blob: 7fbab84399e6c9c602c52c8157596535a2ce2e05 [file] [log] [blame]
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001/*
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>
Paul Gortmakerd5decd32011-05-26 16:00:52 -040027#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020029
30#include "blk.h"
31
32static struct kmem_cache *integrity_cachep;
33
Mike Snitzera63a5cf2011-04-01 21:02:31 +020034static const char *bi_unsupported_name = "unsupported";
35
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020036/**
37 * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
Martin K. Petersen13f05c82010-09-10 20:50:10 +020038 * @q: request queue
39 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020040 *
41 * Description: Returns the number of elements required in a
Martin K. Petersen13f05c82010-09-10 20:50:10 +020042 * scatterlist corresponding to the integrity metadata in a bio.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020043 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020044int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020045{
Kent Overstreetd57a5f72013-11-23 17:20:16 -080046 struct bio_vec iv, ivprv = { NULL };
Martin K. Petersen13f05c82010-09-10 20:50:10 +020047 unsigned int segments = 0;
48 unsigned int seg_size = 0;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080049 struct bvec_iter iter;
50 int prev = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020051
Kent Overstreetd57a5f72013-11-23 17:20:16 -080052 bio_for_each_integrity_vec(iv, bio, iter) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020053
Kent Overstreetd57a5f72013-11-23 17:20:16 -080054 if (prev) {
55 if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020056 goto new_segment;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020057
Kent Overstreetd57a5f72013-11-23 17:20:16 -080058 if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020059 goto new_segment;
60
Kent Overstreetd57a5f72013-11-23 17:20:16 -080061 if (seg_size + iv.bv_len > queue_max_segment_size(q))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020062 goto new_segment;
63
Kent Overstreetd57a5f72013-11-23 17:20:16 -080064 seg_size += iv.bv_len;
Martin K. Petersen13f05c82010-09-10 20:50:10 +020065 } else {
66new_segment:
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020067 segments++;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080068 seg_size = iv.bv_len;
Martin K. Petersen13f05c82010-09-10 20:50:10 +020069 }
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020070
Kent Overstreetd57a5f72013-11-23 17:20:16 -080071 prev = 1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020072 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
Martin K. Petersen13f05c82010-09-10 20:50:10 +020081 * @q: request queue
82 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020083 * @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 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020089int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
90 struct scatterlist *sglist)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020091{
Kent Overstreetd57a5f72013-11-23 17:20:16 -080092 struct bio_vec iv, ivprv = { NULL };
Martin K. Petersen13f05c82010-09-10 20:50:10 +020093 struct scatterlist *sg = NULL;
94 unsigned int segments = 0;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080095 struct bvec_iter iter;
96 int prev = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020097
Kent Overstreetd57a5f72013-11-23 17:20:16 -080098 bio_for_each_integrity_vec(iv, bio, iter) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020099
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800100 if (prev) {
101 if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv))
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200102 goto new_segment;
103
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800104 if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv))
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200105 goto new_segment;
106
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800107 if (sg->length + iv.bv_len > queue_max_segment_size(q))
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200108 goto new_segment;
109
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800110 sg->length += iv.bv_len;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200111 } else {
112new_segment:
113 if (!sg)
114 sg = sglist;
115 else {
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030116 sg_unmark_end(sg);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200117 sg = sg_next(sg);
118 }
119
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800120 sg_set_page(sg, iv.bv_page, iv.bv_len, iv.bv_offset);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200121 segments++;
122 }
123
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800124 prev = 1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200125 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/**
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400136 * blk_integrity_compare - Compare integrity profile of two disks
137 * @gd1: Disk to compare
138 * @gd2: Disk to compare
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200139 *
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
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400143 * function can be used to check whether two gendisk devices have
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200144 * compatible integrity formats.
145 */
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400146int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200147{
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400148 struct blk_integrity *b1 = gd1->integrity;
149 struct blk_integrity *b2 = gd2->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200150
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400151 if (!b1 && !b2)
152 return 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200153
154 if (!b1 || !b2)
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400155 return -1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200156
157 if (b1->sector_size != b2->sector_size) {
158 printk(KERN_ERR "%s: %s/%s sector sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400159 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200160 b1->sector_size, b2->sector_size);
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__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400166 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200167 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__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400173 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200174 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__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400180 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200181 b1->name, b2->name);
182 return -1;
183 }
184
185 return 0;
186}
187EXPORT_SYMBOL(blk_integrity_compare);
188
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200189int 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
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200223struct 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
Jens Axboeb9846792008-06-17 19:05:48 +0200240static ssize_t integrity_attr_store(struct kobject *kobj,
241 struct attribute *attr, const char *page,
242 size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200243{
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
272static ssize_t integrity_read_store(struct blk_integrity *bi,
273 const char *page, size_t count)
274{
275 char *p = (char *) page;
276 unsigned long val = simple_strtoul(p, &p, 10);
277
278 if (val)
Jens Axboeb24498d2008-06-27 09:12:09 +0200279 bi->flags |= INTEGRITY_FLAG_READ;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200280 else
Jens Axboeb24498d2008-06-27 09:12:09 +0200281 bi->flags &= ~INTEGRITY_FLAG_READ;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200282
283 return count;
284}
285
286static ssize_t integrity_read_show(struct blk_integrity *bi, char *page)
287{
Jens Axboeb24498d2008-06-27 09:12:09 +0200288 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_READ) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200289}
290
291static ssize_t integrity_write_store(struct blk_integrity *bi,
292 const char *page, size_t count)
293{
294 char *p = (char *) page;
295 unsigned long val = simple_strtoul(p, &p, 10);
296
297 if (val)
Jens Axboeb24498d2008-06-27 09:12:09 +0200298 bi->flags |= INTEGRITY_FLAG_WRITE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200299 else
Jens Axboeb24498d2008-06-27 09:12:09 +0200300 bi->flags &= ~INTEGRITY_FLAG_WRITE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200301
302 return count;
303}
304
305static ssize_t integrity_write_show(struct blk_integrity *bi, char *page)
306{
Jens Axboeb24498d2008-06-27 09:12:09 +0200307 return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_WRITE) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200308}
309
310static struct integrity_sysfs_entry integrity_format_entry = {
311 .attr = { .name = "format", .mode = S_IRUGO },
312 .show = integrity_format_show,
313};
314
315static struct integrity_sysfs_entry integrity_tag_size_entry = {
316 .attr = { .name = "tag_size", .mode = S_IRUGO },
317 .show = integrity_tag_size_show,
318};
319
320static struct integrity_sysfs_entry integrity_read_entry = {
321 .attr = { .name = "read_verify", .mode = S_IRUGO | S_IWUSR },
322 .show = integrity_read_show,
323 .store = integrity_read_store,
324};
325
326static struct integrity_sysfs_entry integrity_write_entry = {
327 .attr = { .name = "write_generate", .mode = S_IRUGO | S_IWUSR },
328 .show = integrity_write_show,
329 .store = integrity_write_store,
330};
331
332static struct attribute *integrity_attrs[] = {
333 &integrity_format_entry.attr,
334 &integrity_tag_size_entry.attr,
335 &integrity_read_entry.attr,
336 &integrity_write_entry.attr,
337 NULL,
338};
339
Emese Revfy52cf25d2010-01-19 02:58:23 +0100340static const struct sysfs_ops integrity_ops = {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200341 .show = &integrity_attr_show,
342 .store = &integrity_attr_store,
343};
344
345static int __init blk_dev_integrity_init(void)
346{
347 integrity_cachep = kmem_cache_create("blkdev_integrity",
348 sizeof(struct blk_integrity),
349 0, SLAB_PANIC, NULL);
350 return 0;
351}
352subsys_initcall(blk_dev_integrity_init);
353
354static void blk_integrity_release(struct kobject *kobj)
355{
356 struct blk_integrity *bi =
357 container_of(kobj, struct blk_integrity, kobj);
358
359 kmem_cache_free(integrity_cachep, bi);
360}
361
362static struct kobj_type integrity_ktype = {
363 .default_attrs = integrity_attrs,
364 .sysfs_ops = &integrity_ops,
365 .release = blk_integrity_release,
366};
367
Mike Snitzera63a5cf2011-04-01 21:02:31 +0200368bool blk_integrity_is_initialized(struct gendisk *disk)
369{
370 struct blk_integrity *bi = blk_get_integrity(disk);
371
372 return (bi && bi->name && strcmp(bi->name, bi_unsupported_name) != 0);
373}
374EXPORT_SYMBOL(blk_integrity_is_initialized);
375
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200376/**
377 * blk_integrity_register - Register a gendisk as being integrity-capable
378 * @disk: struct gendisk pointer to make integrity-aware
Martin K. Petersen32231632009-01-04 02:43:40 -0500379 * @template: optional integrity profile to register
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200380 *
381 * Description: When a device needs to advertise itself as being able
382 * to send/receive integrity metadata it must use this function to
383 * register the capability with the block layer. The template is a
384 * blk_integrity struct with values appropriate for the underlying
Martin K. Petersen32231632009-01-04 02:43:40 -0500385 * hardware. If template is NULL the new profile is allocated but
386 * not filled out. See Documentation/block/data-integrity.txt.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200387 */
388int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
389{
390 struct blk_integrity *bi;
391
392 BUG_ON(disk == NULL);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200393
394 if (disk->integrity == NULL) {
Jens Axboeb9846792008-06-17 19:05:48 +0200395 bi = kmem_cache_alloc(integrity_cachep,
Martin K. Petersen32231632009-01-04 02:43:40 -0500396 GFP_KERNEL | __GFP_ZERO);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200397 if (!bi)
398 return -1;
399
400 if (kobject_init_and_add(&bi->kobj, &integrity_ktype,
Tejun Heoed9e1982008-08-25 19:56:05 +0900401 &disk_to_dev(disk)->kobj,
402 "%s", "integrity")) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200403 kmem_cache_free(integrity_cachep, bi);
404 return -1;
405 }
406
407 kobject_uevent(&bi->kobj, KOBJ_ADD);
408
Jens Axboeb24498d2008-06-27 09:12:09 +0200409 bi->flags |= INTEGRITY_FLAG_READ | INTEGRITY_FLAG_WRITE;
Martin K. Petersene1defc42009-05-22 17:17:49 -0400410 bi->sector_size = queue_logical_block_size(disk->queue);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200411 disk->integrity = bi;
412 } else
413 bi = disk->integrity;
414
415 /* Use the provided profile as template */
Martin K. Petersen32231632009-01-04 02:43:40 -0500416 if (template != NULL) {
417 bi->name = template->name;
418 bi->generate_fn = template->generate_fn;
419 bi->verify_fn = template->verify_fn;
420 bi->tuple_size = template->tuple_size;
421 bi->set_tag_fn = template->set_tag_fn;
422 bi->get_tag_fn = template->get_tag_fn;
423 bi->tag_size = template->tag_size;
424 } else
Mike Snitzera63a5cf2011-04-01 21:02:31 +0200425 bi->name = bi_unsupported_name;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200426
Darrick J. Wong7d311cd2013-02-21 16:42:48 -0800427 disk->queue->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES;
428
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200429 return 0;
430}
431EXPORT_SYMBOL(blk_integrity_register);
432
433/**
434 * blk_integrity_unregister - Remove block integrity profile
435 * @disk: disk whose integrity profile to deallocate
436 *
437 * Description: This function frees all memory used by the block
438 * integrity profile. To be called at device teardown.
439 */
440void blk_integrity_unregister(struct gendisk *disk)
441{
442 struct blk_integrity *bi;
443
444 if (!disk || !disk->integrity)
445 return;
446
Darrick J. Wong7d311cd2013-02-21 16:42:48 -0800447 disk->queue->backing_dev_info.capabilities &= ~BDI_CAP_STABLE_WRITES;
448
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200449 bi = disk->integrity;
450
451 kobject_uevent(&bi->kobj, KOBJ_REMOVE);
452 kobject_del(&bi->kobj);
Xiaotian Feng3839e4b2009-07-28 09:11:14 +0200453 kobject_put(&bi->kobj);
Martin K. Petersen0c032ab2008-10-01 03:38:38 -0400454 disk->integrity = NULL;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200455}
456EXPORT_SYMBOL(blk_integrity_unregister);