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