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 | { |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 46 | struct bio_vec iv, ivprv = { NULL }; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 47 | unsigned int segments = 0; |
| 48 | unsigned int seg_size = 0; |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 49 | struct bvec_iter iter; |
| 50 | int prev = 0; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 51 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 52 | bio_for_each_integrity_vec(iv, bio, iter) { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 53 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 54 | if (prev) { |
| 55 | if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv)) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 56 | goto new_segment; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 57 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 58 | if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv)) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 59 | goto new_segment; |
| 60 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 61 | if (seg_size + iv.bv_len > queue_max_segment_size(q)) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 62 | goto new_segment; |
| 63 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 64 | seg_size += iv.bv_len; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 65 | } else { |
| 66 | new_segment: |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 67 | segments++; |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 68 | seg_size = iv.bv_len; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 69 | } |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 70 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 71 | prev = 1; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 72 | ivprv = iv; |
| 73 | } |
| 74 | |
| 75 | return segments; |
| 76 | } |
| 77 | EXPORT_SYMBOL(blk_rq_count_integrity_sg); |
| 78 | |
| 79 | /** |
| 80 | * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 81 | * @q: request queue |
| 82 | * @bio: bio with integrity metadata attached |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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 | */ |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 89 | int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio, |
| 90 | struct scatterlist *sglist) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 91 | { |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 92 | struct bio_vec iv, ivprv = { NULL }; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 93 | struct scatterlist *sg = NULL; |
| 94 | unsigned int segments = 0; |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 95 | struct bvec_iter iter; |
| 96 | int prev = 0; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 97 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 98 | bio_for_each_integrity_vec(iv, bio, iter) { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 99 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 100 | if (prev) { |
| 101 | if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv)) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 102 | goto new_segment; |
| 103 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 104 | if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv)) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 105 | goto new_segment; |
| 106 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 107 | if (sg->length + iv.bv_len > queue_max_segment_size(q)) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 108 | goto new_segment; |
| 109 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 110 | sg->length += iv.bv_len; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 111 | } else { |
| 112 | new_segment: |
| 113 | if (!sg) |
| 114 | sg = sglist; |
| 115 | else { |
Paolo Bonzini | c8164d8 | 2013-03-20 15:37:08 +1030 | [diff] [blame] | 116 | sg_unmark_end(sg); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 117 | sg = sg_next(sg); |
| 118 | } |
| 119 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 120 | sg_set_page(sg, iv.bv_page, iv.bv_len, iv.bv_offset); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 121 | segments++; |
| 122 | } |
| 123 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 124 | prev = 1; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 125 | ivprv = iv; |
| 126 | } |
| 127 | |
| 128 | if (sg) |
| 129 | sg_mark_end(sg); |
| 130 | |
| 131 | return segments; |
| 132 | } |
| 133 | EXPORT_SYMBOL(blk_rq_map_integrity_sg); |
| 134 | |
| 135 | /** |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 136 | * blk_integrity_compare - Compare integrity profile of two disks |
| 137 | * @gd1: Disk to compare |
| 138 | * @gd2: Disk to compare |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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 |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 143 | * function can be used to check whether two gendisk devices have |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 144 | * compatible integrity formats. |
| 145 | */ |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 146 | int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 147 | { |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 148 | struct blk_integrity *b1 = gd1->integrity; |
| 149 | struct blk_integrity *b2 = gd2->integrity; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 150 | |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 151 | if (!b1 && !b2) |
| 152 | return 0; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 153 | |
| 154 | if (!b1 || !b2) |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 155 | return -1; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 156 | |
Martin K. Petersen | 3be91c4 | 2014-09-26 19:19:59 -0400 | [diff] [blame] | 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); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 166 | gd1->disk_name, gd2->disk_name, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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__, |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 173 | gd1->disk_name, gd2->disk_name, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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__, |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 180 | gd1->disk_name, gd2->disk_name, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 181 | b1->name, b2->name); |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | EXPORT_SYMBOL(blk_integrity_compare); |
| 188 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 189 | bool blk_integrity_merge_rq(struct request_queue *q, struct request *req, |
| 190 | struct request *next) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 191 | { |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 192 | if (blk_integrity_rq(req) == 0 && blk_integrity_rq(next) == 0) |
| 193 | return true; |
| 194 | |
| 195 | if (blk_integrity_rq(req) == 0 || blk_integrity_rq(next) == 0) |
| 196 | return false; |
| 197 | |
| 198 | if (bio_integrity(req->bio)->bip_flags != |
| 199 | bio_integrity(next->bio)->bip_flags) |
| 200 | return false; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 201 | |
| 202 | if (req->nr_integrity_segments + next->nr_integrity_segments > |
| 203 | q->limits.max_integrity_segments) |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 204 | return false; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 205 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 206 | return true; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 207 | } |
| 208 | EXPORT_SYMBOL(blk_integrity_merge_rq); |
| 209 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 210 | bool blk_integrity_merge_bio(struct request_queue *q, struct request *req, |
| 211 | struct bio *bio) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 212 | { |
| 213 | int nr_integrity_segs; |
| 214 | struct bio *next = bio->bi_next; |
| 215 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 216 | if (blk_integrity_rq(req) == 0 && bio_integrity(bio) == NULL) |
| 217 | return true; |
| 218 | |
| 219 | if (blk_integrity_rq(req) == 0 || bio_integrity(bio) == NULL) |
| 220 | return false; |
| 221 | |
| 222 | if (bio_integrity(req->bio)->bip_flags != bio_integrity(bio)->bip_flags) |
| 223 | return false; |
| 224 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 225 | bio->bi_next = NULL; |
| 226 | nr_integrity_segs = blk_rq_count_integrity_sg(q, bio); |
| 227 | bio->bi_next = next; |
| 228 | |
| 229 | if (req->nr_integrity_segments + nr_integrity_segs > |
| 230 | q->limits.max_integrity_segments) |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 231 | return false; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 232 | |
| 233 | req->nr_integrity_segments += nr_integrity_segs; |
| 234 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 235 | return true; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 236 | } |
| 237 | EXPORT_SYMBOL(blk_integrity_merge_bio); |
| 238 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 239 | struct integrity_sysfs_entry { |
| 240 | struct attribute attr; |
| 241 | ssize_t (*show)(struct blk_integrity *, char *); |
| 242 | ssize_t (*store)(struct blk_integrity *, const char *, size_t); |
| 243 | }; |
| 244 | |
| 245 | static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr, |
| 246 | char *page) |
| 247 | { |
| 248 | struct blk_integrity *bi = |
| 249 | container_of(kobj, struct blk_integrity, kobj); |
| 250 | struct integrity_sysfs_entry *entry = |
| 251 | container_of(attr, struct integrity_sysfs_entry, attr); |
| 252 | |
| 253 | return entry->show(bi, page); |
| 254 | } |
| 255 | |
Jens Axboe | b984679 | 2008-06-17 19:05:48 +0200 | [diff] [blame] | 256 | static ssize_t integrity_attr_store(struct kobject *kobj, |
| 257 | struct attribute *attr, const char *page, |
| 258 | size_t count) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 259 | { |
| 260 | struct blk_integrity *bi = |
| 261 | container_of(kobj, struct blk_integrity, kobj); |
| 262 | struct integrity_sysfs_entry *entry = |
| 263 | container_of(attr, struct integrity_sysfs_entry, attr); |
| 264 | ssize_t ret = 0; |
| 265 | |
| 266 | if (entry->store) |
| 267 | ret = entry->store(bi, page, count); |
| 268 | |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | static ssize_t integrity_format_show(struct blk_integrity *bi, char *page) |
| 273 | { |
| 274 | if (bi != NULL && bi->name != NULL) |
| 275 | return sprintf(page, "%s\n", bi->name); |
| 276 | else |
| 277 | return sprintf(page, "none\n"); |
| 278 | } |
| 279 | |
| 280 | static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page) |
| 281 | { |
| 282 | if (bi != NULL) |
| 283 | return sprintf(page, "%u\n", bi->tag_size); |
| 284 | else |
| 285 | return sprintf(page, "0\n"); |
| 286 | } |
| 287 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 288 | static ssize_t integrity_verify_store(struct blk_integrity *bi, |
| 289 | const char *page, size_t count) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 290 | { |
| 291 | char *p = (char *) page; |
| 292 | unsigned long val = simple_strtoul(p, &p, 10); |
| 293 | |
| 294 | if (val) |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 295 | bi->flags |= BLK_INTEGRITY_VERIFY; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 296 | else |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 297 | bi->flags &= ~BLK_INTEGRITY_VERIFY; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 298 | |
| 299 | return count; |
| 300 | } |
| 301 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 302 | static ssize_t integrity_verify_show(struct blk_integrity *bi, char *page) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 303 | { |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 304 | return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_VERIFY) != 0); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 307 | static ssize_t integrity_generate_store(struct blk_integrity *bi, |
| 308 | const char *page, size_t count) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 309 | { |
| 310 | char *p = (char *) page; |
| 311 | unsigned long val = simple_strtoul(p, &p, 10); |
| 312 | |
| 313 | if (val) |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 314 | bi->flags |= BLK_INTEGRITY_GENERATE; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 315 | else |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 316 | bi->flags &= ~BLK_INTEGRITY_GENERATE; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 317 | |
| 318 | return count; |
| 319 | } |
| 320 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 321 | static ssize_t integrity_generate_show(struct blk_integrity *bi, char *page) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 322 | { |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 323 | return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_GENERATE) != 0); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Martin K. Petersen | 3aec2f4 | 2014-09-26 19:20:03 -0400 | [diff] [blame] | 326 | static ssize_t integrity_device_show(struct blk_integrity *bi, char *page) |
| 327 | { |
| 328 | return sprintf(page, "%u\n", |
| 329 | (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) != 0); |
| 330 | } |
| 331 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 332 | static struct integrity_sysfs_entry integrity_format_entry = { |
| 333 | .attr = { .name = "format", .mode = S_IRUGO }, |
| 334 | .show = integrity_format_show, |
| 335 | }; |
| 336 | |
| 337 | static struct integrity_sysfs_entry integrity_tag_size_entry = { |
| 338 | .attr = { .name = "tag_size", .mode = S_IRUGO }, |
| 339 | .show = integrity_tag_size_show, |
| 340 | }; |
| 341 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 342 | static struct integrity_sysfs_entry integrity_verify_entry = { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 343 | .attr = { .name = "read_verify", .mode = S_IRUGO | S_IWUSR }, |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 344 | .show = integrity_verify_show, |
| 345 | .store = integrity_verify_store, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 346 | }; |
| 347 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 348 | static struct integrity_sysfs_entry integrity_generate_entry = { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 349 | .attr = { .name = "write_generate", .mode = S_IRUGO | S_IWUSR }, |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 350 | .show = integrity_generate_show, |
| 351 | .store = integrity_generate_store, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 352 | }; |
| 353 | |
Martin K. Petersen | 3aec2f4 | 2014-09-26 19:20:03 -0400 | [diff] [blame] | 354 | static struct integrity_sysfs_entry integrity_device_entry = { |
| 355 | .attr = { .name = "device_is_integrity_capable", .mode = S_IRUGO }, |
| 356 | .show = integrity_device_show, |
| 357 | }; |
| 358 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 359 | static struct attribute *integrity_attrs[] = { |
| 360 | &integrity_format_entry.attr, |
| 361 | &integrity_tag_size_entry.attr, |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 362 | &integrity_verify_entry.attr, |
| 363 | &integrity_generate_entry.attr, |
Martin K. Petersen | 3aec2f4 | 2014-09-26 19:20:03 -0400 | [diff] [blame] | 364 | &integrity_device_entry.attr, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 365 | NULL, |
| 366 | }; |
| 367 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 368 | static const struct sysfs_ops integrity_ops = { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 369 | .show = &integrity_attr_show, |
| 370 | .store = &integrity_attr_store, |
| 371 | }; |
| 372 | |
| 373 | static int __init blk_dev_integrity_init(void) |
| 374 | { |
| 375 | integrity_cachep = kmem_cache_create("blkdev_integrity", |
| 376 | sizeof(struct blk_integrity), |
| 377 | 0, SLAB_PANIC, NULL); |
| 378 | return 0; |
| 379 | } |
| 380 | subsys_initcall(blk_dev_integrity_init); |
| 381 | |
| 382 | static void blk_integrity_release(struct kobject *kobj) |
| 383 | { |
| 384 | struct blk_integrity *bi = |
| 385 | container_of(kobj, struct blk_integrity, kobj); |
| 386 | |
| 387 | kmem_cache_free(integrity_cachep, bi); |
| 388 | } |
| 389 | |
| 390 | static struct kobj_type integrity_ktype = { |
| 391 | .default_attrs = integrity_attrs, |
| 392 | .sysfs_ops = &integrity_ops, |
| 393 | .release = blk_integrity_release, |
| 394 | }; |
| 395 | |
Mike Snitzer | a63a5cf | 2011-04-01 21:02:31 +0200 | [diff] [blame] | 396 | bool blk_integrity_is_initialized(struct gendisk *disk) |
| 397 | { |
| 398 | struct blk_integrity *bi = blk_get_integrity(disk); |
| 399 | |
| 400 | return (bi && bi->name && strcmp(bi->name, bi_unsupported_name) != 0); |
| 401 | } |
| 402 | EXPORT_SYMBOL(blk_integrity_is_initialized); |
| 403 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 404 | /** |
| 405 | * blk_integrity_register - Register a gendisk as being integrity-capable |
| 406 | * @disk: struct gendisk pointer to make integrity-aware |
Martin K. Petersen | 3223163 | 2009-01-04 02:43:40 -0500 | [diff] [blame] | 407 | * @template: optional integrity profile to register |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 408 | * |
| 409 | * Description: When a device needs to advertise itself as being able |
| 410 | * to send/receive integrity metadata it must use this function to |
| 411 | * register the capability with the block layer. The template is a |
| 412 | * blk_integrity struct with values appropriate for the underlying |
Martin K. Petersen | 3223163 | 2009-01-04 02:43:40 -0500 | [diff] [blame] | 413 | * hardware. If template is NULL the new profile is allocated but |
| 414 | * not filled out. See Documentation/block/data-integrity.txt. |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 415 | */ |
| 416 | int blk_integrity_register(struct gendisk *disk, struct blk_integrity *template) |
| 417 | { |
| 418 | struct blk_integrity *bi; |
| 419 | |
| 420 | BUG_ON(disk == NULL); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 421 | |
| 422 | if (disk->integrity == NULL) { |
Jens Axboe | b984679 | 2008-06-17 19:05:48 +0200 | [diff] [blame] | 423 | bi = kmem_cache_alloc(integrity_cachep, |
Martin K. Petersen | 3223163 | 2009-01-04 02:43:40 -0500 | [diff] [blame] | 424 | GFP_KERNEL | __GFP_ZERO); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 425 | if (!bi) |
| 426 | return -1; |
| 427 | |
| 428 | if (kobject_init_and_add(&bi->kobj, &integrity_ktype, |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 429 | &disk_to_dev(disk)->kobj, |
| 430 | "%s", "integrity")) { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 431 | kmem_cache_free(integrity_cachep, bi); |
| 432 | return -1; |
| 433 | } |
| 434 | |
| 435 | kobject_uevent(&bi->kobj, KOBJ_ADD); |
| 436 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 437 | bi->flags |= BLK_INTEGRITY_VERIFY | BLK_INTEGRITY_GENERATE; |
Martin K. Petersen | 3be91c4 | 2014-09-26 19:19:59 -0400 | [diff] [blame] | 438 | bi->interval = queue_logical_block_size(disk->queue); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 439 | disk->integrity = bi; |
| 440 | } else |
| 441 | bi = disk->integrity; |
| 442 | |
| 443 | /* Use the provided profile as template */ |
Martin K. Petersen | 3223163 | 2009-01-04 02:43:40 -0500 | [diff] [blame] | 444 | if (template != NULL) { |
| 445 | bi->name = template->name; |
| 446 | bi->generate_fn = template->generate_fn; |
| 447 | bi->verify_fn = template->verify_fn; |
| 448 | bi->tuple_size = template->tuple_size; |
Martin K. Petersen | 3223163 | 2009-01-04 02:43:40 -0500 | [diff] [blame] | 449 | bi->tag_size = template->tag_size; |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 450 | bi->flags |= template->flags; |
Martin K. Petersen | 3223163 | 2009-01-04 02:43:40 -0500 | [diff] [blame] | 451 | } else |
Mike Snitzer | a63a5cf | 2011-04-01 21:02:31 +0200 | [diff] [blame] | 452 | bi->name = bi_unsupported_name; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 453 | |
Darrick J. Wong | 7d311cd | 2013-02-21 16:42:48 -0800 | [diff] [blame] | 454 | disk->queue->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES; |
| 455 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | EXPORT_SYMBOL(blk_integrity_register); |
| 459 | |
| 460 | /** |
| 461 | * blk_integrity_unregister - Remove block integrity profile |
| 462 | * @disk: disk whose integrity profile to deallocate |
| 463 | * |
| 464 | * Description: This function frees all memory used by the block |
| 465 | * integrity profile. To be called at device teardown. |
| 466 | */ |
| 467 | void blk_integrity_unregister(struct gendisk *disk) |
| 468 | { |
| 469 | struct blk_integrity *bi; |
| 470 | |
| 471 | if (!disk || !disk->integrity) |
| 472 | return; |
| 473 | |
Darrick J. Wong | 7d311cd | 2013-02-21 16:42:48 -0800 | [diff] [blame] | 474 | disk->queue->backing_dev_info.capabilities &= ~BDI_CAP_STABLE_WRITES; |
| 475 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 476 | bi = disk->integrity; |
| 477 | |
| 478 | kobject_uevent(&bi->kobj, KOBJ_REMOVE); |
| 479 | kobject_del(&bi->kobj); |
Xiaotian Feng | 3839e4b | 2009-07-28 09:11:14 +0200 | [diff] [blame] | 480 | kobject_put(&bi->kobj); |
Martin K. Petersen | 0c032ab | 2008-10-01 03:38:38 -0400 | [diff] [blame] | 481 | disk->integrity = NULL; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 482 | } |
| 483 | EXPORT_SYMBOL(blk_integrity_unregister); |