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