blob: d670d54e5f7ac6ae3aa20dfcb6d745753799f9f9 [file] [log] [blame]
Christoph Hellwig8c165672019-04-30 14:42:39 -04001// SPDX-License-Identifier: GPL-2.0
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02002/*
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>
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02007 */
8
Christoph Hellwigfe45e632021-09-20 14:33:27 +02009#include <linux/blk-integrity.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040010#include <linux/backing-dev.h>
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020011#include <linux/mempool.h>
12#include <linux/bio.h>
13#include <linux/scatterlist.h>
Paul Gortmakerd5decd32011-05-26 16:00:52 -040014#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020016
17#include "blk.h"
18
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020019/**
20 * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
Martin K. Petersen13f05c82010-09-10 20:50:10 +020021 * @q: request queue
22 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020023 *
24 * Description: Returns the number of elements required in a
Martin K. Petersen13f05c82010-09-10 20:50:10 +020025 * scatterlist corresponding to the integrity metadata in a bio.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020026 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020027int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020028{
Kent Overstreetd57a5f72013-11-23 17:20:16 -080029 struct bio_vec iv, ivprv = { NULL };
Martin K. Petersen13f05c82010-09-10 20:50:10 +020030 unsigned int segments = 0;
31 unsigned int seg_size = 0;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080032 struct bvec_iter iter;
33 int prev = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020034
Kent Overstreetd57a5f72013-11-23 17:20:16 -080035 bio_for_each_integrity_vec(iv, bio, iter) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020036
Kent Overstreetd57a5f72013-11-23 17:20:16 -080037 if (prev) {
Christoph Hellwig3dccdae2018-09-24 09:43:52 +020038 if (!biovec_phys_mergeable(q, &ivprv, &iv))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020039 goto new_segment;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080040 if (seg_size + iv.bv_len > queue_max_segment_size(q))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020041 goto new_segment;
42
Kent Overstreetd57a5f72013-11-23 17:20:16 -080043 seg_size += iv.bv_len;
Martin K. Petersen13f05c82010-09-10 20:50:10 +020044 } else {
45new_segment:
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020046 segments++;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080047 seg_size = iv.bv_len;
Martin K. Petersen13f05c82010-09-10 20:50:10 +020048 }
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020049
Kent Overstreetd57a5f72013-11-23 17:20:16 -080050 prev = 1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020051 ivprv = iv;
52 }
53
54 return segments;
55}
56EXPORT_SYMBOL(blk_rq_count_integrity_sg);
57
58/**
59 * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
Martin K. Petersen13f05c82010-09-10 20:50:10 +020060 * @q: request queue
61 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020062 * @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 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020068int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
69 struct scatterlist *sglist)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020070{
Kent Overstreetd57a5f72013-11-23 17:20:16 -080071 struct bio_vec iv, ivprv = { NULL };
Martin K. Petersen13f05c82010-09-10 20:50:10 +020072 struct scatterlist *sg = NULL;
73 unsigned int segments = 0;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080074 struct bvec_iter iter;
75 int prev = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020076
Kent Overstreetd57a5f72013-11-23 17:20:16 -080077 bio_for_each_integrity_vec(iv, bio, iter) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020078
Kent Overstreetd57a5f72013-11-23 17:20:16 -080079 if (prev) {
Christoph Hellwig3dccdae2018-09-24 09:43:52 +020080 if (!biovec_phys_mergeable(q, &ivprv, &iv))
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020081 goto new_segment;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080082 if (sg->length + iv.bv_len > queue_max_segment_size(q))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020083 goto new_segment;
84
Kent Overstreetd57a5f72013-11-23 17:20:16 -080085 sg->length += iv.bv_len;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020086 } else {
87new_segment:
88 if (!sg)
89 sg = sglist;
90 else {
Paolo Bonzinic8164d82013-03-20 15:37:08 +103091 sg_unmark_end(sg);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020092 sg = sg_next(sg);
93 }
94
Kent Overstreetd57a5f72013-11-23 17:20:16 -080095 sg_set_page(sg, iv.bv_page, iv.bv_len, iv.bv_offset);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020096 segments++;
97 }
98
Kent Overstreetd57a5f72013-11-23 17:20:16 -080099 prev = 1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200100 ivprv = iv;
101 }
102
103 if (sg)
104 sg_mark_end(sg);
105
106 return segments;
107}
108EXPORT_SYMBOL(blk_rq_map_integrity_sg);
109
110/**
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400111 * blk_integrity_compare - Compare integrity profile of two disks
112 * @gd1: Disk to compare
113 * @gd2: Disk to compare
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200114 *
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
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400118 * function can be used to check whether two gendisk devices have
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200119 * compatible integrity formats.
120 */
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400121int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200122{
Dan Williamsac6fc482015-10-21 13:20:18 -0400123 struct blk_integrity *b1 = &gd1->queue->integrity;
124 struct blk_integrity *b2 = &gd2->queue->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200125
Martin K. Petersen25520d52015-10-21 13:19:49 -0400126 if (!b1->profile && !b2->profile)
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400127 return 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200128
Martin K. Petersen25520d52015-10-21 13:19:49 -0400129 if (!b1->profile || !b2->profile)
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400130 return -1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200131
Martin K. Petersena48f0412015-10-21 13:19:38 -0400132 if (b1->interval_exp != b2->interval_exp) {
Martin K. Petersen3be91c42014-09-26 19:19:59 -0400133 pr_err("%s: %s/%s protection interval %u != %u\n",
134 __func__, gd1->disk_name, gd2->disk_name,
Martin K. Petersena48f0412015-10-21 13:19:38 -0400135 1 << b1->interval_exp, 1 << b2->interval_exp);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200136 return -1;
137 }
138
139 if (b1->tuple_size != b2->tuple_size) {
Martin K. Petersen25520d52015-10-21 13:19:49 -0400140 pr_err("%s: %s/%s tuple sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400141 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200142 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)) {
Martin K. Petersen25520d52015-10-21 13:19:49 -0400147 pr_err("%s: %s/%s tag sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400148 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200149 b1->tag_size, b2->tag_size);
150 return -1;
151 }
152
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400153 if (b1->profile != b2->profile) {
Martin K. Petersen25520d52015-10-21 13:19:49 -0400154 pr_err("%s: %s/%s type %s != %s\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400155 gd1->disk_name, gd2->disk_name,
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400156 b1->profile->name, b2->profile->name);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200157 return -1;
158 }
159
160 return 0;
161}
162EXPORT_SYMBOL(blk_integrity_compare);
163
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400164bool blk_integrity_merge_rq(struct request_queue *q, struct request *req,
165 struct request *next)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200166{
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400167 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;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200176
177 if (req->nr_integrity_segments + next->nr_integrity_segments >
178 q->limits.max_integrity_segments)
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400179 return false;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200180
Sagi Grimberg7f39add2015-09-11 09:03:04 -0600181 if (integrity_req_gap_back_merge(req, next->bio))
182 return false;
183
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400184 return true;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200185}
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200186
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400187bool blk_integrity_merge_bio(struct request_queue *q, struct request *req,
188 struct bio *bio)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200189{
190 int nr_integrity_segs;
191 struct bio *next = bio->bi_next;
192
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400193 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
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200202 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)
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400208 return false;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200209
210 req->nr_integrity_segments += nr_integrity_segs;
211
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400212 return true;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200213}
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200214
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200215struct 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
221static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr,
222 char *page)
223{
Martin K. Petersenaff34e12015-10-21 13:19:27 -0400224 struct gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj);
Dan Williamsac6fc482015-10-21 13:20:18 -0400225 struct blk_integrity *bi = &disk->queue->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200226 struct integrity_sysfs_entry *entry =
227 container_of(attr, struct integrity_sysfs_entry, attr);
228
229 return entry->show(bi, page);
230}
231
Jens Axboeb9846792008-06-17 19:05:48 +0200232static ssize_t integrity_attr_store(struct kobject *kobj,
233 struct attribute *attr, const char *page,
234 size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200235{
Martin K. Petersenaff34e12015-10-21 13:19:27 -0400236 struct gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj);
Dan Williamsac6fc482015-10-21 13:20:18 -0400237 struct blk_integrity *bi = &disk->queue->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200238 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
248static ssize_t integrity_format_show(struct blk_integrity *bi, char *page)
249{
Martin K. Petersen25520d52015-10-21 13:19:49 -0400250 if (bi->profile && bi->profile->name)
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400251 return sprintf(page, "%s\n", bi->profile->name);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200252 else
253 return sprintf(page, "none\n");
254}
255
256static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page)
257{
Martin K. Petersen25520d52015-10-21 13:19:49 -0400258 return sprintf(page, "%u\n", bi->tag_size);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200259}
260
Martin K. Petersen4c241d02015-10-21 13:19:43 -0400261static ssize_t integrity_interval_show(struct blk_integrity *bi, char *page)
262{
Martin K. Petersen25520d52015-10-21 13:19:49 -0400263 return sprintf(page, "%u\n",
264 bi->interval_exp ? 1 << bi->interval_exp : 0);
Martin K. Petersen4c241d02015-10-21 13:19:43 -0400265}
266
Martin K. Petersen8288f492014-09-26 19:20:02 -0400267static ssize_t integrity_verify_store(struct blk_integrity *bi,
268 const char *page, size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200269{
270 char *p = (char *) page;
271 unsigned long val = simple_strtoul(p, &p, 10);
272
273 if (val)
Martin K. Petersen8288f492014-09-26 19:20:02 -0400274 bi->flags |= BLK_INTEGRITY_VERIFY;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200275 else
Martin K. Petersen8288f492014-09-26 19:20:02 -0400276 bi->flags &= ~BLK_INTEGRITY_VERIFY;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200277
278 return count;
279}
280
Martin K. Petersen8288f492014-09-26 19:20:02 -0400281static ssize_t integrity_verify_show(struct blk_integrity *bi, char *page)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200282{
Martin K. Petersen8288f492014-09-26 19:20:02 -0400283 return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_VERIFY) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200284}
285
Martin K. Petersen8288f492014-09-26 19:20:02 -0400286static ssize_t integrity_generate_store(struct blk_integrity *bi,
287 const char *page, size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200288{
289 char *p = (char *) page;
290 unsigned long val = simple_strtoul(p, &p, 10);
291
292 if (val)
Martin K. Petersen8288f492014-09-26 19:20:02 -0400293 bi->flags |= BLK_INTEGRITY_GENERATE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200294 else
Martin K. Petersen8288f492014-09-26 19:20:02 -0400295 bi->flags &= ~BLK_INTEGRITY_GENERATE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200296
297 return count;
298}
299
Martin K. Petersen8288f492014-09-26 19:20:02 -0400300static ssize_t integrity_generate_show(struct blk_integrity *bi, char *page)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200301{
Martin K. Petersen8288f492014-09-26 19:20:02 -0400302 return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_GENERATE) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200303}
304
Martin K. Petersen3aec2f42014-09-26 19:20:03 -0400305static 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
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200311static struct integrity_sysfs_entry integrity_format_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600312 .attr = { .name = "format", .mode = 0444 },
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200313 .show = integrity_format_show,
314};
315
316static struct integrity_sysfs_entry integrity_tag_size_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600317 .attr = { .name = "tag_size", .mode = 0444 },
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200318 .show = integrity_tag_size_show,
319};
320
Martin K. Petersen4c241d02015-10-21 13:19:43 -0400321static struct integrity_sysfs_entry integrity_interval_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600322 .attr = { .name = "protection_interval_bytes", .mode = 0444 },
Martin K. Petersen4c241d02015-10-21 13:19:43 -0400323 .show = integrity_interval_show,
324};
325
Martin K. Petersen8288f492014-09-26 19:20:02 -0400326static struct integrity_sysfs_entry integrity_verify_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600327 .attr = { .name = "read_verify", .mode = 0644 },
Martin K. Petersen8288f492014-09-26 19:20:02 -0400328 .show = integrity_verify_show,
329 .store = integrity_verify_store,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200330};
331
Martin K. Petersen8288f492014-09-26 19:20:02 -0400332static struct integrity_sysfs_entry integrity_generate_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600333 .attr = { .name = "write_generate", .mode = 0644 },
Martin K. Petersen8288f492014-09-26 19:20:02 -0400334 .show = integrity_generate_show,
335 .store = integrity_generate_store,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200336};
337
Martin K. Petersen3aec2f42014-09-26 19:20:03 -0400338static struct integrity_sysfs_entry integrity_device_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600339 .attr = { .name = "device_is_integrity_capable", .mode = 0444 },
Martin K. Petersen3aec2f42014-09-26 19:20:03 -0400340 .show = integrity_device_show,
341};
342
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200343static struct attribute *integrity_attrs[] = {
344 &integrity_format_entry.attr,
345 &integrity_tag_size_entry.attr,
Martin K. Petersen4c241d02015-10-21 13:19:43 -0400346 &integrity_interval_entry.attr,
Martin K. Petersen8288f492014-09-26 19:20:02 -0400347 &integrity_verify_entry.attr,
348 &integrity_generate_entry.attr,
Martin K. Petersen3aec2f42014-09-26 19:20:03 -0400349 &integrity_device_entry.attr,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200350 NULL,
351};
Kimberly Brown800f5aa2019-04-01 22:51:30 -0400352ATTRIBUTE_GROUPS(integrity);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200353
Emese Revfy52cf25d2010-01-19 02:58:23 +0100354static const struct sysfs_ops integrity_ops = {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200355 .show = &integrity_attr_show,
356 .store = &integrity_attr_store,
357};
358
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200359static struct kobj_type integrity_ktype = {
Kimberly Brown800f5aa2019-04-01 22:51:30 -0400360 .default_groups = integrity_groups,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200361 .sysfs_ops = &integrity_ops,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200362};
363
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200364static blk_status_t blk_integrity_nop_fn(struct blk_integrity_iter *iter)
Dan Williams4125a092015-10-21 13:20:29 -0400365{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200366 return BLK_STS_OK;
Dan Williams4125a092015-10-21 13:20:29 -0400367}
368
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +0300369static void blk_integrity_nop_prepare(struct request *rq)
370{
371}
372
373static void blk_integrity_nop_complete(struct request *rq,
374 unsigned int nr_bytes)
375{
376}
377
Eric Biggers869ab902017-03-24 18:03:48 -0700378static const struct blk_integrity_profile nop_profile = {
Dan Williams4125a092015-10-21 13:20:29 -0400379 .name = "nop",
380 .generate_fn = blk_integrity_nop_fn,
381 .verify_fn = blk_integrity_nop_fn,
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +0300382 .prepare_fn = blk_integrity_nop_prepare,
383 .complete_fn = blk_integrity_nop_complete,
Dan Williams4125a092015-10-21 13:20:29 -0400384};
385
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200386/**
387 * blk_integrity_register - Register a gendisk as being integrity-capable
388 * @disk: struct gendisk pointer to make integrity-aware
Martin K. Petersen25520d52015-10-21 13:19:49 -0400389 * @template: block integrity profile to register
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200390 *
Martin K. Petersen25520d52015-10-21 13:19:49 -0400391 * 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
Mauro Carvalho Chehab898bd372019-04-18 19:45:00 -0300395 * Documentation/block/data-integrity.rst.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200396 */
Martin K. Petersen25520d52015-10-21 13:19:49 -0400397void blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200398{
Dan Williamsac6fc482015-10-21 13:20:18 -0400399 struct blk_integrity *bi = &disk->queue->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200400
Martin K. Petersen25520d52015-10-21 13:19:49 -0400401 bi->flags = BLK_INTEGRITY_VERIFY | BLK_INTEGRITY_GENERATE |
402 template->flags;
Mike Snitzer28593232017-04-22 17:22:09 -0400403 bi->interval_exp = template->interval_exp ? :
404 ilog2(queue_logical_block_size(disk->queue));
Dan Williams4125a092015-10-21 13:20:29 -0400405 bi->profile = template->profile ? template->profile : &nop_profile;
Martin K. Petersen25520d52015-10-21 13:19:49 -0400406 bi->tuple_size = template->tuple_size;
407 bi->tag_size = template->tag_size;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200408
Christoph Hellwig1cb039f2020-09-24 08:51:38 +0200409 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, disk->queue);
Satya Tangiralad145dc22020-05-14 00:37:19 +0000410
411#ifdef CONFIG_BLK_INLINE_ENCRYPTION
Eric Biggerscb77cb52021-10-18 11:04:52 -0700412 if (disk->queue->crypto_profile) {
Satya Tangiralad145dc22020-05-14 00:37:19 +0000413 pr_warn("blk-integrity: Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n");
Eric Biggerscb77cb52021-10-18 11:04:52 -0700414 blk_crypto_unregister(disk->queue);
Satya Tangiralad145dc22020-05-14 00:37:19 +0000415 }
416#endif
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200417}
418EXPORT_SYMBOL(blk_integrity_register);
419
420/**
Martin K. Petersen25520d52015-10-21 13:19:49 -0400421 * blk_integrity_unregister - Unregister block integrity profile
422 * @disk: disk whose integrity profile to unregister
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200423 *
Martin K. Petersen25520d52015-10-21 13:19:49 -0400424 * Description: This function unregisters the integrity capability from
425 * a block device.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200426 */
427void blk_integrity_unregister(struct gendisk *disk)
428{
Christoph Hellwig783a40a2021-09-14 09:06:55 +0200429 struct blk_integrity *bi = &disk->queue->integrity;
430
431 if (!bi->profile)
432 return;
Lihong Kou3df49962021-09-14 09:06:56 +0200433
434 /* ensure all bios are off the integrity workqueue */
435 blk_flush_integrity();
Christoph Hellwig1cb039f2020-09-24 08:51:38 +0200436 blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, disk->queue);
Christoph Hellwig783a40a2021-09-14 09:06:55 +0200437 memset(bi, 0, sizeof(*bi));
Martin K. Petersen25520d52015-10-21 13:19:49 -0400438}
439EXPORT_SYMBOL(blk_integrity_unregister);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200440
Luis Chamberlain614310c2021-08-18 16:45:38 +0200441int blk_integrity_add(struct gendisk *disk)
Martin K. Petersen25520d52015-10-21 13:19:49 -0400442{
Luis Chamberlain614310c2021-08-18 16:45:38 +0200443 int ret;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200444
Luis Chamberlain614310c2021-08-18 16:45:38 +0200445 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;
Martin K. Petersen25520d52015-10-21 13:19:49 -0400450}
451
452void blk_integrity_del(struct gendisk *disk)
453{
Martin K. Petersenaff34e12015-10-21 13:19:27 -0400454 kobject_uevent(&disk->integrity_kobj, KOBJ_REMOVE);
455 kobject_del(&disk->integrity_kobj);
456 kobject_put(&disk->integrity_kobj);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200457}