Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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> |
Paul Gortmaker | d5decd3 | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 29 | |
| 30 | #include "blk.h" |
| 31 | |
| 32 | static struct kmem_cache *integrity_cachep; |
| 33 | |
Mike Snitzer | a63a5cf | 2011-04-01 21:02:31 +0200 | [diff] [blame] | 34 | static const char *bi_unsupported_name = "unsupported"; |
| 35 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 36 | /** |
| 37 | * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 38 | * @q: request queue |
| 39 | * @bio: bio with integrity metadata attached |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 40 | * |
| 41 | * Description: Returns the number of elements required in a |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 42 | * scatterlist corresponding to the integrity metadata in a bio. |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 43 | */ |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 44 | int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 45 | { |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 46 | struct bio_vec *iv, *ivprv = NULL; |
| 47 | unsigned int segments = 0; |
| 48 | unsigned int seg_size = 0; |
| 49 | unsigned int i = 0; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 50 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 51 | bio_for_each_integrity_vec(iv, bio, i) { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 52 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 53 | if (ivprv) { |
| 54 | if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv)) |
| 55 | goto new_segment; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 56 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 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 { |
| 65 | new_segment: |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 66 | segments++; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 67 | seg_size = iv->bv_len; |
| 68 | } |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 69 | |
| 70 | ivprv = iv; |
| 71 | } |
| 72 | |
| 73 | return segments; |
| 74 | } |
| 75 | EXPORT_SYMBOL(blk_rq_count_integrity_sg); |
| 76 | |
| 77 | /** |
| 78 | * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 79 | * @q: request queue |
| 80 | * @bio: bio with integrity metadata attached |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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 | */ |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 87 | int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio, |
| 88 | struct scatterlist *sglist) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 89 | { |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 90 | struct bio_vec *iv, *ivprv = NULL; |
| 91 | struct scatterlist *sg = NULL; |
| 92 | unsigned int segments = 0; |
| 93 | unsigned int i = 0; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 94 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 95 | bio_for_each_integrity_vec(iv, bio, i) { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 96 | |
| 97 | if (ivprv) { |
| 98 | if (!BIOVEC_PHYS_MERGEABLE(ivprv, iv)) |
| 99 | goto new_segment; |
| 100 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 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 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 107 | sg->length += iv->bv_len; |
| 108 | } else { |
| 109 | new_segment: |
| 110 | if (!sg) |
| 111 | sg = sglist; |
| 112 | else { |
| 113 | sg->page_link &= ~0x02; |
| 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 | } |
| 129 | EXPORT_SYMBOL(blk_rq_map_integrity_sg); |
| 130 | |
| 131 | /** |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 132 | * blk_integrity_compare - Compare integrity profile of two disks |
| 133 | * @gd1: Disk to compare |
| 134 | * @gd2: Disk to compare |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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 |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 139 | * function can be used to check whether two gendisk devices have |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 140 | * compatible integrity formats. |
| 141 | */ |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 142 | int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 143 | { |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 144 | struct blk_integrity *b1 = gd1->integrity; |
| 145 | struct blk_integrity *b2 = gd2->integrity; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 146 | |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 147 | if (!b1 && !b2) |
| 148 | return 0; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 149 | |
| 150 | if (!b1 || !b2) |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 151 | return -1; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 152 | |
| 153 | if (b1->sector_size != b2->sector_size) { |
| 154 | printk(KERN_ERR "%s: %s/%s sector sz %u != %u\n", __func__, |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 155 | gd1->disk_name, gd2->disk_name, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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__, |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 162 | gd1->disk_name, gd2->disk_name, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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__, |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 169 | gd1->disk_name, gd2->disk_name, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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__, |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 176 | gd1->disk_name, gd2->disk_name, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 177 | b1->name, b2->name); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | EXPORT_SYMBOL(blk_integrity_compare); |
| 184 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 185 | int blk_integrity_merge_rq(struct request_queue *q, struct request *req, |
| 186 | struct request *next) |
| 187 | { |
| 188 | if (blk_integrity_rq(req) != blk_integrity_rq(next)) |
| 189 | return -1; |
| 190 | |
| 191 | if (req->nr_integrity_segments + next->nr_integrity_segments > |
| 192 | q->limits.max_integrity_segments) |
| 193 | return -1; |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | EXPORT_SYMBOL(blk_integrity_merge_rq); |
| 198 | |
| 199 | int blk_integrity_merge_bio(struct request_queue *q, struct request *req, |
| 200 | struct bio *bio) |
| 201 | { |
| 202 | int nr_integrity_segs; |
| 203 | struct bio *next = bio->bi_next; |
| 204 | |
| 205 | bio->bi_next = NULL; |
| 206 | nr_integrity_segs = blk_rq_count_integrity_sg(q, bio); |
| 207 | bio->bi_next = next; |
| 208 | |
| 209 | if (req->nr_integrity_segments + nr_integrity_segs > |
| 210 | q->limits.max_integrity_segments) |
| 211 | return -1; |
| 212 | |
| 213 | req->nr_integrity_segments += nr_integrity_segs; |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | EXPORT_SYMBOL(blk_integrity_merge_bio); |
| 218 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 219 | struct integrity_sysfs_entry { |
| 220 | struct attribute attr; |
| 221 | ssize_t (*show)(struct blk_integrity *, char *); |
| 222 | ssize_t (*store)(struct blk_integrity *, const char *, size_t); |
| 223 | }; |
| 224 | |
| 225 | static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr, |
| 226 | char *page) |
| 227 | { |
| 228 | struct blk_integrity *bi = |
| 229 | container_of(kobj, struct blk_integrity, kobj); |
| 230 | struct integrity_sysfs_entry *entry = |
| 231 | container_of(attr, struct integrity_sysfs_entry, attr); |
| 232 | |
| 233 | return entry->show(bi, page); |
| 234 | } |
| 235 | |
Jens Axboe | b984679 | 2008-06-17 19:05:48 +0200 | [diff] [blame] | 236 | static ssize_t integrity_attr_store(struct kobject *kobj, |
| 237 | struct attribute *attr, const char *page, |
| 238 | size_t count) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 239 | { |
| 240 | struct blk_integrity *bi = |
| 241 | container_of(kobj, struct blk_integrity, kobj); |
| 242 | struct integrity_sysfs_entry *entry = |
| 243 | container_of(attr, struct integrity_sysfs_entry, attr); |
| 244 | ssize_t ret = 0; |
| 245 | |
| 246 | if (entry->store) |
| 247 | ret = entry->store(bi, page, count); |
| 248 | |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | static ssize_t integrity_format_show(struct blk_integrity *bi, char *page) |
| 253 | { |
| 254 | if (bi != NULL && bi->name != NULL) |
| 255 | return sprintf(page, "%s\n", bi->name); |
| 256 | else |
| 257 | return sprintf(page, "none\n"); |
| 258 | } |
| 259 | |
| 260 | static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page) |
| 261 | { |
| 262 | if (bi != NULL) |
| 263 | return sprintf(page, "%u\n", bi->tag_size); |
| 264 | else |
| 265 | return sprintf(page, "0\n"); |
| 266 | } |
| 267 | |
| 268 | static ssize_t integrity_read_store(struct blk_integrity *bi, |
| 269 | const char *page, size_t count) |
| 270 | { |
| 271 | char *p = (char *) page; |
| 272 | unsigned long val = simple_strtoul(p, &p, 10); |
| 273 | |
| 274 | if (val) |
Jens Axboe | b24498d | 2008-06-27 09:12:09 +0200 | [diff] [blame] | 275 | bi->flags |= INTEGRITY_FLAG_READ; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 276 | else |
Jens Axboe | b24498d | 2008-06-27 09:12:09 +0200 | [diff] [blame] | 277 | bi->flags &= ~INTEGRITY_FLAG_READ; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 278 | |
| 279 | return count; |
| 280 | } |
| 281 | |
| 282 | static ssize_t integrity_read_show(struct blk_integrity *bi, char *page) |
| 283 | { |
Jens Axboe | b24498d | 2008-06-27 09:12:09 +0200 | [diff] [blame] | 284 | return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_READ) != 0); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static ssize_t integrity_write_store(struct blk_integrity *bi, |
| 288 | const char *page, size_t count) |
| 289 | { |
| 290 | char *p = (char *) page; |
| 291 | unsigned long val = simple_strtoul(p, &p, 10); |
| 292 | |
| 293 | if (val) |
Jens Axboe | b24498d | 2008-06-27 09:12:09 +0200 | [diff] [blame] | 294 | bi->flags |= INTEGRITY_FLAG_WRITE; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 295 | else |
Jens Axboe | b24498d | 2008-06-27 09:12:09 +0200 | [diff] [blame] | 296 | bi->flags &= ~INTEGRITY_FLAG_WRITE; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 297 | |
| 298 | return count; |
| 299 | } |
| 300 | |
| 301 | static ssize_t integrity_write_show(struct blk_integrity *bi, char *page) |
| 302 | { |
Jens Axboe | b24498d | 2008-06-27 09:12:09 +0200 | [diff] [blame] | 303 | return sprintf(page, "%d\n", (bi->flags & INTEGRITY_FLAG_WRITE) != 0); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static struct integrity_sysfs_entry integrity_format_entry = { |
| 307 | .attr = { .name = "format", .mode = S_IRUGO }, |
| 308 | .show = integrity_format_show, |
| 309 | }; |
| 310 | |
| 311 | static struct integrity_sysfs_entry integrity_tag_size_entry = { |
| 312 | .attr = { .name = "tag_size", .mode = S_IRUGO }, |
| 313 | .show = integrity_tag_size_show, |
| 314 | }; |
| 315 | |
| 316 | static struct integrity_sysfs_entry integrity_read_entry = { |
| 317 | .attr = { .name = "read_verify", .mode = S_IRUGO | S_IWUSR }, |
| 318 | .show = integrity_read_show, |
| 319 | .store = integrity_read_store, |
| 320 | }; |
| 321 | |
| 322 | static struct integrity_sysfs_entry integrity_write_entry = { |
| 323 | .attr = { .name = "write_generate", .mode = S_IRUGO | S_IWUSR }, |
| 324 | .show = integrity_write_show, |
| 325 | .store = integrity_write_store, |
| 326 | }; |
| 327 | |
| 328 | static struct attribute *integrity_attrs[] = { |
| 329 | &integrity_format_entry.attr, |
| 330 | &integrity_tag_size_entry.attr, |
| 331 | &integrity_read_entry.attr, |
| 332 | &integrity_write_entry.attr, |
| 333 | NULL, |
| 334 | }; |
| 335 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 336 | static const struct sysfs_ops integrity_ops = { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 337 | .show = &integrity_attr_show, |
| 338 | .store = &integrity_attr_store, |
| 339 | }; |
| 340 | |
| 341 | static int __init blk_dev_integrity_init(void) |
| 342 | { |
| 343 | integrity_cachep = kmem_cache_create("blkdev_integrity", |
| 344 | sizeof(struct blk_integrity), |
| 345 | 0, SLAB_PANIC, NULL); |
| 346 | return 0; |
| 347 | } |
| 348 | subsys_initcall(blk_dev_integrity_init); |
| 349 | |
| 350 | static void blk_integrity_release(struct kobject *kobj) |
| 351 | { |
| 352 | struct blk_integrity *bi = |
| 353 | container_of(kobj, struct blk_integrity, kobj); |
| 354 | |
| 355 | kmem_cache_free(integrity_cachep, bi); |
| 356 | } |
| 357 | |
| 358 | static struct kobj_type integrity_ktype = { |
| 359 | .default_attrs = integrity_attrs, |
| 360 | .sysfs_ops = &integrity_ops, |
| 361 | .release = blk_integrity_release, |
| 362 | }; |
| 363 | |
Mike Snitzer | a63a5cf | 2011-04-01 21:02:31 +0200 | [diff] [blame] | 364 | bool blk_integrity_is_initialized(struct gendisk *disk) |
| 365 | { |
| 366 | struct blk_integrity *bi = blk_get_integrity(disk); |
| 367 | |
| 368 | return (bi && bi->name && strcmp(bi->name, bi_unsupported_name) != 0); |
| 369 | } |
| 370 | EXPORT_SYMBOL(blk_integrity_is_initialized); |
| 371 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 372 | /** |
| 373 | * blk_integrity_register - Register a gendisk as being integrity-capable |
| 374 | * @disk: struct gendisk pointer to make integrity-aware |
Martin K. Petersen | 3223163 | 2009-01-04 02:43:40 -0500 | [diff] [blame] | 375 | * @template: optional integrity profile to register |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 376 | * |
| 377 | * Description: When a device needs to advertise itself as being able |
| 378 | * to send/receive integrity metadata it must use this function to |
| 379 | * register the capability with the block layer. The template is a |
| 380 | * blk_integrity struct with values appropriate for the underlying |
Martin K. Petersen | 3223163 | 2009-01-04 02:43:40 -0500 | [diff] [blame] | 381 | * hardware. If template is NULL the new profile is allocated but |
| 382 | * not filled out. See Documentation/block/data-integrity.txt. |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 383 | */ |
| 384 | int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template) |
| 385 | { |
| 386 | struct blk_integrity *bi; |
| 387 | |
| 388 | BUG_ON(disk == NULL); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 389 | |
| 390 | if (disk->integrity == NULL) { |
Jens Axboe | b984679 | 2008-06-17 19:05:48 +0200 | [diff] [blame] | 391 | bi = kmem_cache_alloc(integrity_cachep, |
Martin K. Petersen | 3223163 | 2009-01-04 02:43:40 -0500 | [diff] [blame] | 392 | GFP_KERNEL | __GFP_ZERO); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 393 | if (!bi) |
| 394 | return -1; |
| 395 | |
| 396 | if (kobject_init_and_add(&bi->kobj, &integrity_ktype, |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 397 | &disk_to_dev(disk)->kobj, |
| 398 | "%s", "integrity")) { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 399 | kmem_cache_free(integrity_cachep, bi); |
| 400 | return -1; |
| 401 | } |
| 402 | |
| 403 | kobject_uevent(&bi->kobj, KOBJ_ADD); |
| 404 | |
Jens Axboe | b24498d | 2008-06-27 09:12:09 +0200 | [diff] [blame] | 405 | bi->flags |= INTEGRITY_FLAG_READ | INTEGRITY_FLAG_WRITE; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 406 | bi->sector_size = queue_logical_block_size(disk->queue); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 407 | disk->integrity = bi; |
| 408 | } else |
| 409 | bi = disk->integrity; |
| 410 | |
| 411 | /* Use the provided profile as template */ |
Martin K. Petersen | 3223163 | 2009-01-04 02:43:40 -0500 | [diff] [blame] | 412 | if (template != NULL) { |
| 413 | bi->name = template->name; |
| 414 | bi->generate_fn = template->generate_fn; |
| 415 | bi->verify_fn = template->verify_fn; |
| 416 | bi->tuple_size = template->tuple_size; |
| 417 | bi->set_tag_fn = template->set_tag_fn; |
| 418 | bi->get_tag_fn = template->get_tag_fn; |
| 419 | bi->tag_size = template->tag_size; |
| 420 | } else |
Mike Snitzer | a63a5cf | 2011-04-01 21:02:31 +0200 | [diff] [blame] | 421 | bi->name = bi_unsupported_name; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 422 | |
Darrick J. Wong | 7d311cd | 2013-02-21 16:42:48 -0800 | [diff] [blame^] | 423 | disk->queue->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES; |
| 424 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | EXPORT_SYMBOL(blk_integrity_register); |
| 428 | |
| 429 | /** |
| 430 | * blk_integrity_unregister - Remove block integrity profile |
| 431 | * @disk: disk whose integrity profile to deallocate |
| 432 | * |
| 433 | * Description: This function frees all memory used by the block |
| 434 | * integrity profile. To be called at device teardown. |
| 435 | */ |
| 436 | void blk_integrity_unregister(struct gendisk *disk) |
| 437 | { |
| 438 | struct blk_integrity *bi; |
| 439 | |
| 440 | if (!disk || !disk->integrity) |
| 441 | return; |
| 442 | |
Darrick J. Wong | 7d311cd | 2013-02-21 16:42:48 -0800 | [diff] [blame^] | 443 | disk->queue->backing_dev_info.capabilities &= ~BDI_CAP_STABLE_WRITES; |
| 444 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 445 | bi = disk->integrity; |
| 446 | |
| 447 | kobject_uevent(&bi->kobj, KOBJ_REMOVE); |
| 448 | kobject_del(&bi->kobj); |
Xiaotian Feng | 3839e4b | 2009-07-28 09:11:14 +0200 | [diff] [blame] | 449 | kobject_put(&bi->kobj); |
Martin K. Petersen | 0c032ab | 2008-10-01 03:38:38 -0400 | [diff] [blame] | 450 | disk->integrity = NULL; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 451 | } |
| 452 | EXPORT_SYMBOL(blk_integrity_unregister); |