blob: 6121611e1316420372ce510300321532e9c76ef0 [file] [log] [blame]
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +02001/*
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 Heo66114ca2015-05-22 17:13:32 -040024#include <linux/backing-dev.h>
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020025#include <linux/mempool.h>
26#include <linux/bio.h>
27#include <linux/scatterlist.h>
Paul Gortmakerd5decd32011-05-26 16:00:52 -040028#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020030
31#include "blk.h"
32
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020033/**
34 * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements
Martin K. Petersen13f05c82010-09-10 20:50:10 +020035 * @q: request queue
36 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020037 *
38 * Description: Returns the number of elements required in a
Martin K. Petersen13f05c82010-09-10 20:50:10 +020039 * scatterlist corresponding to the integrity metadata in a bio.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020040 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020041int blk_rq_count_integrity_sg(struct request_queue *q, struct bio *bio)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020042{
Kent Overstreetd57a5f72013-11-23 17:20:16 -080043 struct bio_vec iv, ivprv = { NULL };
Martin K. Petersen13f05c82010-09-10 20:50:10 +020044 unsigned int segments = 0;
45 unsigned int seg_size = 0;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080046 struct bvec_iter iter;
47 int prev = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020048
Kent Overstreetd57a5f72013-11-23 17:20:16 -080049 bio_for_each_integrity_vec(iv, bio, iter) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020050
Kent Overstreetd57a5f72013-11-23 17:20:16 -080051 if (prev) {
52 if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020053 goto new_segment;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020054
Kent Overstreetd57a5f72013-11-23 17:20:16 -080055 if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020056 goto new_segment;
57
Kent Overstreetd57a5f72013-11-23 17:20:16 -080058 if (seg_size + iv.bv_len > queue_max_segment_size(q))
Martin K. Petersen13f05c82010-09-10 20:50:10 +020059 goto new_segment;
60
Kent Overstreetd57a5f72013-11-23 17:20:16 -080061 seg_size += iv.bv_len;
Martin K. Petersen13f05c82010-09-10 20:50:10 +020062 } else {
63new_segment:
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020064 segments++;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080065 seg_size = iv.bv_len;
Martin K. Petersen13f05c82010-09-10 20:50:10 +020066 }
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020067
Kent Overstreetd57a5f72013-11-23 17:20:16 -080068 prev = 1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020069 ivprv = iv;
70 }
71
72 return segments;
73}
74EXPORT_SYMBOL(blk_rq_count_integrity_sg);
75
76/**
77 * blk_rq_map_integrity_sg - Map integrity metadata into a scatterlist
Martin K. Petersen13f05c82010-09-10 20:50:10 +020078 * @q: request queue
79 * @bio: bio with integrity metadata attached
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020080 * @sglist: target scatterlist
81 *
82 * Description: Map the integrity vectors in request into a
83 * scatterlist. The scatterlist must be big enough to hold all
84 * elements. I.e. sized using blk_rq_count_integrity_sg().
85 */
Martin K. Petersen13f05c82010-09-10 20:50:10 +020086int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio,
87 struct scatterlist *sglist)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020088{
Kent Overstreetd57a5f72013-11-23 17:20:16 -080089 struct bio_vec iv, ivprv = { NULL };
Martin K. Petersen13f05c82010-09-10 20:50:10 +020090 struct scatterlist *sg = NULL;
91 unsigned int segments = 0;
Kent Overstreetd57a5f72013-11-23 17:20:16 -080092 struct bvec_iter iter;
93 int prev = 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020094
Kent Overstreetd57a5f72013-11-23 17:20:16 -080095 bio_for_each_integrity_vec(iv, bio, iter) {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020096
Kent Overstreetd57a5f72013-11-23 17:20:16 -080097 if (prev) {
98 if (!BIOVEC_PHYS_MERGEABLE(&ivprv, &iv))
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +020099 goto new_segment;
100
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800101 if (!BIOVEC_SEG_BOUNDARY(q, &ivprv, &iv))
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200102 goto new_segment;
103
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800104 if (sg->length + iv.bv_len > queue_max_segment_size(q))
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200105 goto new_segment;
106
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800107 sg->length += iv.bv_len;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200108 } else {
109new_segment:
110 if (!sg)
111 sg = sglist;
112 else {
Paolo Bonzinic8164d82013-03-20 15:37:08 +1030113 sg_unmark_end(sg);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200114 sg = sg_next(sg);
115 }
116
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800117 sg_set_page(sg, iv.bv_page, iv.bv_len, iv.bv_offset);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200118 segments++;
119 }
120
Kent Overstreetd57a5f72013-11-23 17:20:16 -0800121 prev = 1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200122 ivprv = iv;
123 }
124
125 if (sg)
126 sg_mark_end(sg);
127
128 return segments;
129}
130EXPORT_SYMBOL(blk_rq_map_integrity_sg);
131
132/**
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400133 * blk_integrity_compare - Compare integrity profile of two disks
134 * @gd1: Disk to compare
135 * @gd2: Disk to compare
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200136 *
137 * Description: Meta-devices like DM and MD need to verify that all
138 * sub-devices use the same integrity format before advertising to
139 * upper layers that they can send/receive integrity metadata. This
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400140 * function can be used to check whether two gendisk devices have
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200141 * compatible integrity formats.
142 */
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400143int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200144{
Dan Williamsac6fc482015-10-21 13:20:18 -0400145 struct blk_integrity *b1 = &gd1->queue->integrity;
146 struct blk_integrity *b2 = &gd2->queue->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200147
Martin K. Petersen25520d52015-10-21 13:19:49 -0400148 if (!b1->profile && !b2->profile)
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400149 return 0;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200150
Martin K. Petersen25520d52015-10-21 13:19:49 -0400151 if (!b1->profile || !b2->profile)
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400152 return -1;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200153
Martin K. Petersena48f0412015-10-21 13:19:38 -0400154 if (b1->interval_exp != b2->interval_exp) {
Martin K. Petersen3be91c42014-09-26 19:19:59 -0400155 pr_err("%s: %s/%s protection interval %u != %u\n",
156 __func__, gd1->disk_name, gd2->disk_name,
Martin K. Petersena48f0412015-10-21 13:19:38 -0400157 1 << b1->interval_exp, 1 << b2->interval_exp);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200158 return -1;
159 }
160
161 if (b1->tuple_size != b2->tuple_size) {
Martin K. Petersen25520d52015-10-21 13:19:49 -0400162 pr_err("%s: %s/%s tuple sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400163 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200164 b1->tuple_size, b2->tuple_size);
165 return -1;
166 }
167
168 if (b1->tag_size && b2->tag_size && (b1->tag_size != b2->tag_size)) {
Martin K. Petersen25520d52015-10-21 13:19:49 -0400169 pr_err("%s: %s/%s tag sz %u != %u\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400170 gd1->disk_name, gd2->disk_name,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200171 b1->tag_size, b2->tag_size);
172 return -1;
173 }
174
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400175 if (b1->profile != b2->profile) {
Martin K. Petersen25520d52015-10-21 13:19:49 -0400176 pr_err("%s: %s/%s type %s != %s\n", __func__,
Martin K. Petersenad7fce92008-10-01 03:38:39 -0400177 gd1->disk_name, gd2->disk_name,
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400178 b1->profile->name, b2->profile->name);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200179 return -1;
180 }
181
182 return 0;
183}
184EXPORT_SYMBOL(blk_integrity_compare);
185
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400186bool blk_integrity_merge_rq(struct request_queue *q, struct request *req,
187 struct request *next)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200188{
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400189 if (blk_integrity_rq(req) == 0 && blk_integrity_rq(next) == 0)
190 return true;
191
192 if (blk_integrity_rq(req) == 0 || blk_integrity_rq(next) == 0)
193 return false;
194
195 if (bio_integrity(req->bio)->bip_flags !=
196 bio_integrity(next->bio)->bip_flags)
197 return false;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200198
199 if (req->nr_integrity_segments + next->nr_integrity_segments >
200 q->limits.max_integrity_segments)
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400201 return false;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200202
Sagi Grimberg7f39add2015-09-11 09:03:04 -0600203 if (integrity_req_gap_back_merge(req, next->bio))
204 return false;
205
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400206 return true;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200207}
208EXPORT_SYMBOL(blk_integrity_merge_rq);
209
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400210bool blk_integrity_merge_bio(struct request_queue *q, struct request *req,
211 struct bio *bio)
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200212{
213 int nr_integrity_segs;
214 struct bio *next = bio->bi_next;
215
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400216 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. Petersen13f05c82010-09-10 20:50:10 +0200225 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. Petersen4eaf99b2014-09-26 19:20:06 -0400231 return false;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200232
233 req->nr_integrity_segments += nr_integrity_segs;
234
Martin K. Petersen4eaf99b2014-09-26 19:20:06 -0400235 return true;
Martin K. Petersen13f05c82010-09-10 20:50:10 +0200236}
237EXPORT_SYMBOL(blk_integrity_merge_bio);
238
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200239struct 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
245static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr,
246 char *page)
247{
Martin K. Petersenaff34e12015-10-21 13:19:27 -0400248 struct gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj);
Dan Williamsac6fc482015-10-21 13:20:18 -0400249 struct blk_integrity *bi = &disk->queue->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200250 struct integrity_sysfs_entry *entry =
251 container_of(attr, struct integrity_sysfs_entry, attr);
252
253 return entry->show(bi, page);
254}
255
Jens Axboeb9846792008-06-17 19:05:48 +0200256static ssize_t integrity_attr_store(struct kobject *kobj,
257 struct attribute *attr, const char *page,
258 size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200259{
Martin K. Petersenaff34e12015-10-21 13:19:27 -0400260 struct gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj);
Dan Williamsac6fc482015-10-21 13:20:18 -0400261 struct blk_integrity *bi = &disk->queue->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200262 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
272static ssize_t integrity_format_show(struct blk_integrity *bi, char *page)
273{
Martin K. Petersen25520d52015-10-21 13:19:49 -0400274 if (bi->profile && bi->profile->name)
Martin K. Petersen0f8087e2015-10-21 13:19:33 -0400275 return sprintf(page, "%s\n", bi->profile->name);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200276 else
277 return sprintf(page, "none\n");
278}
279
280static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page)
281{
Martin K. Petersen25520d52015-10-21 13:19:49 -0400282 return sprintf(page, "%u\n", bi->tag_size);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200283}
284
Martin K. Petersen4c241d02015-10-21 13:19:43 -0400285static ssize_t integrity_interval_show(struct blk_integrity *bi, char *page)
286{
Martin K. Petersen25520d52015-10-21 13:19:49 -0400287 return sprintf(page, "%u\n",
288 bi->interval_exp ? 1 << bi->interval_exp : 0);
Martin K. Petersen4c241d02015-10-21 13:19:43 -0400289}
290
Martin K. Petersen8288f492014-09-26 19:20:02 -0400291static ssize_t integrity_verify_store(struct blk_integrity *bi,
292 const char *page, size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200293{
294 char *p = (char *) page;
295 unsigned long val = simple_strtoul(p, &p, 10);
296
297 if (val)
Martin K. Petersen8288f492014-09-26 19:20:02 -0400298 bi->flags |= BLK_INTEGRITY_VERIFY;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200299 else
Martin K. Petersen8288f492014-09-26 19:20:02 -0400300 bi->flags &= ~BLK_INTEGRITY_VERIFY;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200301
302 return count;
303}
304
Martin K. Petersen8288f492014-09-26 19:20:02 -0400305static ssize_t integrity_verify_show(struct blk_integrity *bi, char *page)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200306{
Martin K. Petersen8288f492014-09-26 19:20:02 -0400307 return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_VERIFY) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200308}
309
Martin K. Petersen8288f492014-09-26 19:20:02 -0400310static ssize_t integrity_generate_store(struct blk_integrity *bi,
311 const char *page, size_t count)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200312{
313 char *p = (char *) page;
314 unsigned long val = simple_strtoul(p, &p, 10);
315
316 if (val)
Martin K. Petersen8288f492014-09-26 19:20:02 -0400317 bi->flags |= BLK_INTEGRITY_GENERATE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200318 else
Martin K. Petersen8288f492014-09-26 19:20:02 -0400319 bi->flags &= ~BLK_INTEGRITY_GENERATE;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200320
321 return count;
322}
323
Martin K. Petersen8288f492014-09-26 19:20:02 -0400324static ssize_t integrity_generate_show(struct blk_integrity *bi, char *page)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200325{
Martin K. Petersen8288f492014-09-26 19:20:02 -0400326 return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_GENERATE) != 0);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200327}
328
Martin K. Petersen3aec2f42014-09-26 19:20:03 -0400329static ssize_t integrity_device_show(struct blk_integrity *bi, char *page)
330{
331 return sprintf(page, "%u\n",
332 (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) != 0);
333}
334
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200335static struct integrity_sysfs_entry integrity_format_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600336 .attr = { .name = "format", .mode = 0444 },
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200337 .show = integrity_format_show,
338};
339
340static struct integrity_sysfs_entry integrity_tag_size_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600341 .attr = { .name = "tag_size", .mode = 0444 },
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200342 .show = integrity_tag_size_show,
343};
344
Martin K. Petersen4c241d02015-10-21 13:19:43 -0400345static struct integrity_sysfs_entry integrity_interval_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600346 .attr = { .name = "protection_interval_bytes", .mode = 0444 },
Martin K. Petersen4c241d02015-10-21 13:19:43 -0400347 .show = integrity_interval_show,
348};
349
Martin K. Petersen8288f492014-09-26 19:20:02 -0400350static struct integrity_sysfs_entry integrity_verify_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600351 .attr = { .name = "read_verify", .mode = 0644 },
Martin K. Petersen8288f492014-09-26 19:20:02 -0400352 .show = integrity_verify_show,
353 .store = integrity_verify_store,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200354};
355
Martin K. Petersen8288f492014-09-26 19:20:02 -0400356static struct integrity_sysfs_entry integrity_generate_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600357 .attr = { .name = "write_generate", .mode = 0644 },
Martin K. Petersen8288f492014-09-26 19:20:02 -0400358 .show = integrity_generate_show,
359 .store = integrity_generate_store,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200360};
361
Martin K. Petersen3aec2f42014-09-26 19:20:03 -0400362static struct integrity_sysfs_entry integrity_device_entry = {
Joe Perches5657a812018-05-24 13:38:59 -0600363 .attr = { .name = "device_is_integrity_capable", .mode = 0444 },
Martin K. Petersen3aec2f42014-09-26 19:20:03 -0400364 .show = integrity_device_show,
365};
366
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200367static struct attribute *integrity_attrs[] = {
368 &integrity_format_entry.attr,
369 &integrity_tag_size_entry.attr,
Martin K. Petersen4c241d02015-10-21 13:19:43 -0400370 &integrity_interval_entry.attr,
Martin K. Petersen8288f492014-09-26 19:20:02 -0400371 &integrity_verify_entry.attr,
372 &integrity_generate_entry.attr,
Martin K. Petersen3aec2f42014-09-26 19:20:03 -0400373 &integrity_device_entry.attr,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200374 NULL,
375};
376
Emese Revfy52cf25d2010-01-19 02:58:23 +0100377static const struct sysfs_ops integrity_ops = {
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200378 .show = &integrity_attr_show,
379 .store = &integrity_attr_store,
380};
381
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200382static struct kobj_type integrity_ktype = {
383 .default_attrs = integrity_attrs,
384 .sysfs_ops = &integrity_ops,
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200385};
386
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200387static blk_status_t blk_integrity_nop_fn(struct blk_integrity_iter *iter)
Dan Williams4125a092015-10-21 13:20:29 -0400388{
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200389 return BLK_STS_OK;
Dan Williams4125a092015-10-21 13:20:29 -0400390}
391
Eric Biggers869ab902017-03-24 18:03:48 -0700392static const struct blk_integrity_profile nop_profile = {
Dan Williams4125a092015-10-21 13:20:29 -0400393 .name = "nop",
394 .generate_fn = blk_integrity_nop_fn,
395 .verify_fn = blk_integrity_nop_fn,
396};
397
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200398/**
399 * blk_integrity_register - Register a gendisk as being integrity-capable
400 * @disk: struct gendisk pointer to make integrity-aware
Martin K. Petersen25520d52015-10-21 13:19:49 -0400401 * @template: block integrity profile to register
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200402 *
Martin K. Petersen25520d52015-10-21 13:19:49 -0400403 * Description: When a device needs to advertise itself as being able to
404 * send/receive integrity metadata it must use this function to register
405 * the capability with the block layer. The template is a blk_integrity
406 * struct with values appropriate for the underlying hardware. See
407 * Documentation/block/data-integrity.txt.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200408 */
Martin K. Petersen25520d52015-10-21 13:19:49 -0400409void blk_integrity_register(struct gendisk *disk, struct blk_integrity *template)
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200410{
Dan Williamsac6fc482015-10-21 13:20:18 -0400411 struct blk_integrity *bi = &disk->queue->integrity;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200412
Martin K. Petersen25520d52015-10-21 13:19:49 -0400413 bi->flags = BLK_INTEGRITY_VERIFY | BLK_INTEGRITY_GENERATE |
414 template->flags;
Mike Snitzer28593232017-04-22 17:22:09 -0400415 bi->interval_exp = template->interval_exp ? :
416 ilog2(queue_logical_block_size(disk->queue));
Dan Williams4125a092015-10-21 13:20:29 -0400417 bi->profile = template->profile ? template->profile : &nop_profile;
Martin K. Petersen25520d52015-10-21 13:19:49 -0400418 bi->tuple_size = template->tuple_size;
419 bi->tag_size = template->tag_size;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200420
Ilya Dryomov19b7ccf2017-04-18 18:43:20 +0200421 disk->queue->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200422}
423EXPORT_SYMBOL(blk_integrity_register);
424
425/**
Martin K. Petersen25520d52015-10-21 13:19:49 -0400426 * blk_integrity_unregister - Unregister block integrity profile
427 * @disk: disk whose integrity profile to unregister
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200428 *
Martin K. Petersen25520d52015-10-21 13:19:49 -0400429 * Description: This function unregisters the integrity capability from
430 * a block device.
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200431 */
432void blk_integrity_unregister(struct gendisk *disk)
433{
Ilya Dryomov19b7ccf2017-04-18 18:43:20 +0200434 disk->queue->backing_dev_info->capabilities &= ~BDI_CAP_STABLE_WRITES;
Dan Williamsac6fc482015-10-21 13:20:18 -0400435 memset(&disk->queue->integrity, 0, sizeof(struct blk_integrity));
Martin K. Petersen25520d52015-10-21 13:19:49 -0400436}
437EXPORT_SYMBOL(blk_integrity_unregister);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200438
Martin K. Petersen25520d52015-10-21 13:19:49 -0400439void blk_integrity_add(struct gendisk *disk)
440{
441 if (kobject_init_and_add(&disk->integrity_kobj, &integrity_ktype,
442 &disk_to_dev(disk)->kobj, "%s", "integrity"))
443 return;
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200444
Martin K. Petersen25520d52015-10-21 13:19:49 -0400445 kobject_uevent(&disk->integrity_kobj, KOBJ_ADD);
446}
447
448void blk_integrity_del(struct gendisk *disk)
449{
Martin K. Petersenaff34e12015-10-21 13:19:27 -0400450 kobject_uevent(&disk->integrity_kobj, KOBJ_REMOVE);
451 kobject_del(&disk->integrity_kobj);
452 kobject_put(&disk->integrity_kobj);
Martin K. Petersen7ba1ba12008-06-30 20:04:41 +0200453}