1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * blk-integrity.c - Block layer data integrity extensions 4 * 5 * Copyright (C) 2007, 2008 Oracle Corporation 6 * Written by: Martin K. Petersen <martin.petersen@oracle.com> 7 */ 8 9 #include <linux/blk-integrity.h> 10 #include <linux/backing-dev.h> 11 #include <linux/mempool.h> 12 #include <linux/bio.h> 13 #include <linux/scatterlist.h> 14 #include <linux/export.h> 15 #include <linux/slab.h> 16 17 #include "blk.h" 18 19 /** 20 * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements 21 * @q: request queue 22 * @bio: bio with integrity metadata attached 23 * 24 * Description: Returns the number of elements required in a 25 * scatterlist corresponding to the integrity metadata in a bio. 26 */ 27 int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio) 28 { 29 struct bio_vec iv, ivprv = { NULL }; 30 unsigned int segments = 0; 31 unsigned int seg_size = 0; 32 struct bvec_iter iter; 33 int prev = 0; 34 35 bio_for_each_integrity_vec(iv, bio, iter) { 36 37 if (prev) { 38 if (!biovec_phys_mergeable(q, &ivprv, &iv)) 39 goto new_segment; 40 if (seg_size + iv.bv_len > queue_max_segment_size(q)) 41 goto new_segment; 42 43 seg_size += iv.bv_len; 44 } else { 45 new_segment: 46 segments++; 47 seg_size = iv.bv_len; 48 } 49 50 prev = 1; 51 ivprv = iv; 52 } 53 54 return segments; 55 } 56 EXPORT_SYMBOL(blk_rq_count_integrity_sg); 57 58 /** 59 * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist 60 * @q: request queue 61 * @bio: bio with integrity metadata attached 62 * @sglist: target scatterlist 63 * 64 * Description: Map the integrity vectors in request into a 65 * scatterlist. The scatterlist must be big enough to hold all 66 * elements. I.e. sized using blk_rq_count_integrity_sg(). 67 */ 68 int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio, 69 struct scatterlist *sglist) 70 { 71 struct bio_vec iv, ivprv = { NULL }; 72 struct scatterlist *sg = NULL; 73 unsigned int segments = 0; 74 struct bvec_iter iter; 75 int prev = 0; 76 77 bio_for_each_integrity_vec(iv, bio, iter) { 78 79 if (prev) { 80 if (!biovec_phys_mergeable(q, &ivprv, &iv)) 81 goto new_segment; 82 if (sg->length + iv.bv_len > queue_max_segment_size(q)) 83 goto new_segment; 84 85 sg->length += iv.bv_len; 86 } else { 87 new_segment: 88 if (!sg) 89 sg = sglist; 90 else { 91 sg_unmark_end(sg); 92 sg = sg_next(sg); 93 } 94 95 sg_set_page(sg, iv.bv_page, iv.bv_len, iv.bv_offset); 96 segments++; 97 } 98 99 prev = 1; 100 ivprv = iv; 101 } 102 103 if (sg) 104 sg_mark_end(sg); 105 106 return segments; 107 } 108 EXPORT_SYMBOL(blk_rq_map_integrity_sg); 109 110 /** 111 * blk_integrity_compare - Compare integrity profile of two disks 112 * @gd1: Disk to compare 113 * @gd2: Disk to compare 114 * 115 * Description: Meta-devices like DM and MD need to verify that all 116 * sub-devices use the same integrity format before advertising to 117 * upper layers that they can send/receive integrity metadata. This 118 * function can be used to check whether two gendisk devices have 119 * compatible integrity formats. 120 */ 121 int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2) 122 { 123 struct blk_integrity *b1 = &gd1->queue->integrity; 124 struct blk_integrity *b2 = &gd2->queue->integrity; 125 126 if (!b1->profile && !b2->profile) 127 return 0; 128 129 if (!b1->profile || !b2->profile) 130 return -1; 131 132 if (b1->interval_exp != b2->interval_exp) { 133 pr_err("%s: %s/%s protection interval %u != %u\n", 134 __func__, gd1->disk_name, gd2->disk_name, 135 1 << b1->interval_exp, 1 << b2->interval_exp); 136 return -1; 137 } 138 139 if (b1->tuple_size != b2->tuple_size) { 140 pr_err("%s: %s/%s tuple sz %u != %u\n", __func__, 141 gd1->disk_name, gd2->disk_name, 142 b1->tuple_size, b2->tuple_size); 143 return -1; 144 } 145 146 if (b1->tag_size && b2->tag_size && (b1->tag_size != b2->tag_size)) { 147 pr_err("%s: %s/%s tag sz %u != %u\n", __func__, 148 gd1->disk_name, gd2->disk_name, 149 b1->tag_size, b2->tag_size); 150 return -1; 151 } 152 153 if (b1->profile != b2->profile) { 154 pr_err("%s: %s/%s type %s != %s\n", __func__, 155 gd1->disk_name, gd2->disk_name, 156 b1->profile->name, b2->profile->name); 157 return -1; 158 } 159 160 return 0; 161 } 162 EXPORT_SYMBOL(blk_integrity_compare); 163 164 bool blk_integrity_merge_rq(struct request_queue *q, struct request *req, 165 struct request *next) 166 { 167 if (blk_integrity_rq(req) == 0 && blk_integrity_rq(next) == 0) 168 return true; 169 170 if (blk_integrity_rq(req) == 0 || blk_integrity_rq(next) == 0) 171 return false; 172 173 if (bio_integrity(req->bio)->bip_flags != 174 bio_integrity(next->bio)->bip_flags) 175 return false; 176 177 if (req->nr_integrity_segments + next->nr_integrity_segments > 178 q->limits.max_integrity_segments) 179 return false; 180 181 if (integrity_req_gap_back_merge(req, next->bio)) 182 return false; 183 184 return true; 185 } 186 187 bool blk_integrity_merge_bio(struct request_queue *q, struct request *req, 188 struct bio *bio) 189 { 190 int nr_integrity_segs; 191 struct bio *next = bio->bi_next; 192 193 if (blk_integrity_rq(req) == 0 && bio_integrity(bio) == NULL) 194 return true; 195 196 if (blk_integrity_rq(req) == 0 || bio_integrity(bio) == NULL) 197 return false; 198 199 if (bio_integrity(req->bio)->bip_flags != bio_integrity(bio)->bip_flags) 200 return false; 201 202 bio->bi_next = NULL; 203 nr_integrity_segs = blk_rq_count_integrity_sg(q, bio); 204 bio->bi_next = next; 205 206 if (req->nr_integrity_segments + nr_integrity_segs > 207 q->limits.max_integrity_segments) 208 return false; 209 210 req->nr_integrity_segments += nr_integrity_segs; 211 212 return true; 213 } 214 215 struct integrity_sysfs_entry { 216 struct attribute attr; 217 ssize_t (*show)(struct blk_integrity *, char *); 218 ssize_t (*store)(struct blk_integrity *, const char *, size_t); 219 }; 220 221 static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr, 222 char *page) 223 { 224 struct gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj); 225 struct blk_integrity *bi = &disk->queue->integrity; 226 struct integrity_sysfs_entry *entry = 227 container_of(attr, struct integrity_sysfs_entry, attr); 228 229 return entry->show(bi, page); 230 } 231 232 static ssize_t integrity_attr_store(struct kobject *kobj, 233 struct attribute *attr, const char *page, 234 size_t count) 235 { 236 struct gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj); 237 struct blk_integrity *bi = &disk->queue->integrity; 238 struct integrity_sysfs_entry *entry = 239 container_of(attr, struct integrity_sysfs_entry, attr); 240 ssize_t ret = 0; 241 242 if (entry->store) 243 ret = entry->store(bi, page, count); 244 245 return ret; 246 } 247 248 static ssize_t integrity_format_show(struct blk_integrity *bi, char *page) 249 { 250 if (bi->profile && bi->profile->name) 251 return sprintf(page, "%s\n", bi->profile->name); 252 else 253 return sprintf(page, "none\n"); 254 } 255 256 static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page) 257 { 258 return sprintf(page, "%u\n", bi->tag_size); 259 } 260 261 static ssize_t integrity_interval_show(struct blk_integrity *bi, char *page) 262 { 263 return sprintf(page, "%u\n", 264 bi->interval_exp ? 1 << bi->interval_exp : 0); 265 } 266 267 static ssize_t integrity_verify_store(struct blk_integrity *bi, 268 const char *page, size_t count) 269 { 270 char *p = (char *) page; 271 unsigned long val = simple_strtoul(p, &p, 10); 272 273 if (val) 274 bi->flags |= BLK_INTEGRITY_VERIFY; 275 else 276 bi->flags &= ~BLK_INTEGRITY_VERIFY; 277 278 return count; 279 } 280 281 static ssize_t integrity_verify_show(struct blk_integrity *bi, char *page) 282 { 283 return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_VERIFY) != 0); 284 } 285 286 static ssize_t integrity_generate_store(struct blk_integrity *bi, 287 const char *page, size_t count) 288 { 289 char *p = (char *) page; 290 unsigned long val = simple_strtoul(p, &p, 10); 291 292 if (val) 293 bi->flags |= BLK_INTEGRITY_GENERATE; 294 else 295 bi->flags &= ~BLK_INTEGRITY_GENERATE; 296 297 return count; 298 } 299 300 static ssize_t integrity_generate_show(struct blk_integrity *bi, char *page) 301 { 302 return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_GENERATE) != 0); 303 } 304 305 static ssize_t integrity_device_show(struct blk_integrity *bi, char *page) 306 { 307 return sprintf(page, "%u\n", 308 (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) != 0); 309 } 310 311 static struct integrity_sysfs_entry integrity_format_entry = { 312 .attr = { .name = "format", .mode = 0444 }, 313 .show = integrity_format_show, 314 }; 315 316 static struct integrity_sysfs_entry integrity_tag_size_entry = { 317 .attr = { .name = "tag_size", .mode = 0444 }, 318 .show = integrity_tag_size_show, 319 }; 320 321 static struct integrity_sysfs_entry integrity_interval_entry = { 322 .attr = { .name = "protection_interval_bytes", .mode = 0444 }, 323 .show = integrity_interval_show, 324 }; 325 326 static struct integrity_sysfs_entry integrity_verify_entry = { 327 .attr = { .name = "read_verify", .mode = 0644 }, 328 .show = integrity_verify_show, 329 .store = integrity_verify_store, 330 }; 331 332 static struct integrity_sysfs_entry integrity_generate_entry = { 333 .attr = { .name = "write_generate", .mode = 0644 }, 334 .show = integrity_generate_show, 335 .store = integrity_generate_store, 336 }; 337 338 static struct integrity_sysfs_entry integrity_device_entry = { 339 .attr = { .name = "device_is_integrity_capable", .mode = 0444 }, 340 .show = integrity_device_show, 341 }; 342 343 static struct attribute *integrity_attrs[] = { 344 &integrity_format_entry.attr, 345 &integrity_tag_size_entry.attr, 346 &integrity_interval_entry.attr, 347 &integrity_verify_entry.attr, 348 &integrity_generate_entry.attr, 349 &integrity_device_entry.attr, 350 NULL, 351 }; 352 ATTRIBUTE_GROUPS(integrity); 353 354 static const struct sysfs_ops integrity_ops = { 355 .show = &integrity_attr_show, 356 .store = &integrity_attr_store, 357 }; 358 359 static struct kobj_type integrity_ktype = { 360 .default_groups = integrity_groups, 361 .sysfs_ops = &integrity_ops, 362 }; 363 364 static blk_status_t blk_integrity_nop_fn(struct blk_integrity_iter *iter) 365 { 366 return BLK_STS_OK; 367 } 368 369 static void blk_integrity_nop_prepare(struct request *rq) 370 { 371 } 372 373 static void blk_integrity_nop_complete(struct request *rq, 374 unsigned int nr_bytes) 375 { 376 } 377 378 static const struct blk_integrity_profile nop_profile = { 379 .name = "nop", 380 .generate_fn = blk_integrity_nop_fn, 381 .verify_fn = blk_integrity_nop_fn, 382 .prepare_fn = blk_integrity_nop_prepare, 383 .complete_fn = blk_integrity_nop_complete, 384 }; 385 386 /** 387 * blk_integrity_register - Register a gendisk as being integrity-capable 388 * @disk: struct gendisk pointer to make integrity-aware 389 * @template: block integrity profile to register 390 * 391 * Description: When a device needs to advertise itself as being able to 392 * send/receive integrity metadata it must use this function to register 393 * the capability with the block layer. The template is a blk_integrity 394 * struct with values appropriate for the underlying hardware. See 395 * Documentation/block/data-integrity.rst. 396 */ 397 void blk_integrity_register(struct gendisk *disk, struct blk_integrity *template) 398 { 399 struct blk_integrity *bi = &disk->queue->integrity; 400 401 bi->flags = BLK_INTEGRITY_VERIFY | BLK_INTEGRITY_GENERATE | 402 template->flags; 403 bi->interval_exp = template->interval_exp ? : 404 ilog2(queue_logical_block_size(disk->queue)); 405 bi->profile = template->profile ? template->profile : &nop_profile; 406 bi->tuple_size = template->tuple_size; 407 bi->tag_size = template->tag_size; 408 409 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, disk->queue); 410 411 #ifdef CONFIG_BLK_INLINE_ENCRYPTION 412 if (disk->queue->crypto_profile) { 413 pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n"); 414 blk_crypto_unregister(disk->queue); 415 } 416 #endif 417 } 418 EXPORT_SYMBOL(blk_integrity_register); 419 420 /** 421 * blk_integrity_unregister - Unregister block integrity profile 422 * @disk: disk whose integrity profile to unregister 423 * 424 * Description: This function unregisters the integrity capability from 425 * a block device. 426 */ 427 void blk_integrity_unregister(struct gendisk *disk) 428 { 429 struct blk_integrity *bi = &disk->queue->integrity; 430 431 if (!bi->profile) 432 return; 433 434 /* ensure all bios are off the integrity workqueue */ 435 blk_flush_integrity(); 436 blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, disk->queue); 437 memset(bi, 0, sizeof(*bi)); 438 } 439 EXPORT_SYMBOL(blk_integrity_unregister); 440 441 int blk_integrity_add(struct gendisk *disk) 442 { 443 int ret; 444 445 ret = kobject_init_and_add(&disk->integrity_kobj, &integrity_ktype, 446 &disk_to_dev(disk)->kobj, "%s", "integrity"); 447 if (!ret) 448 kobject_uevent(&disk->integrity_kobj, KOBJ_ADD); 449 return ret; 450 } 451 452 void blk_integrity_del(struct gendisk *disk) 453 { 454 kobject_uevent(&disk->integrity_kobj, KOBJ_REMOVE); 455 kobject_del(&disk->integrity_kobj); 456 kobject_put(&disk->integrity_kobj); 457 } 458