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