Christoph Hellwig | 8c16567 | 2019-04-30 14:42:39 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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> |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/blkdev.h> |
Tejun Heo | 66114ca | 2015-05-22 17:13:32 -0400 | [diff] [blame] | 10 | #include <linux/backing-dev.h> |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 11 | #include <linux/mempool.h> |
| 12 | #include <linux/bio.h> |
| 13 | #include <linux/scatterlist.h> |
Paul Gortmaker | d5decd3 | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 16 | |
| 17 | #include "blk.h" |
| 18 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 19 | /** |
| 20 | * blk_rq_count_integrity_sg - Count number of integrity scatterlist elements |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 21 | * @q: request queue |
| 22 | * @bio: bio with integrity metadata attached |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 23 | * |
| 24 | * Description: Returns the number of elements required in a |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 25 | * scatterlist corresponding to the integrity metadata in a bio. |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 26 | */ |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 27 | 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] | 28 | { |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 29 | struct bio_vec iv, ivprv = { NULL }; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 30 | unsigned int segments = 0; |
| 31 | unsigned int seg_size = 0; |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 32 | struct bvec_iter iter; |
| 33 | int prev = 0; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 34 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 35 | bio_for_each_integrity_vec(iv, bio, iter) { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 36 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 37 | if (prev) { |
Christoph Hellwig | 3dccdae | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 38 | if (!biovec_phys_mergeable(q, &ivprv, &iv)) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 39 | goto new_segment; |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 40 | if (seg_size + iv.bv_len > queue_max_segment_size(q)) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 41 | goto new_segment; |
| 42 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 43 | seg_size += iv.bv_len; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 44 | } else { |
| 45 | new_segment: |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 46 | segments++; |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 47 | seg_size = iv.bv_len; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 48 | } |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 49 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 50 | prev = 1; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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 |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 60 | * @q: request queue |
| 61 | * @bio: bio with integrity metadata attached |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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 | */ |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 68 | int blk_rq_map_integrity_sg(struct request_queue *q, struct bio *bio, |
| 69 | struct scatterlist *sglist) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 70 | { |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 71 | struct bio_vec iv, ivprv = { NULL }; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 72 | struct scatterlist *sg = NULL; |
| 73 | unsigned int segments = 0; |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 74 | struct bvec_iter iter; |
| 75 | int prev = 0; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 76 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 77 | bio_for_each_integrity_vec(iv, bio, iter) { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 78 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 79 | if (prev) { |
Christoph Hellwig | 3dccdae | 2018-09-24 09:43:52 +0200 | [diff] [blame] | 80 | if (!biovec_phys_mergeable(q, &ivprv, &iv)) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 81 | goto new_segment; |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 82 | if (sg->length + iv.bv_len > queue_max_segment_size(q)) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 83 | goto new_segment; |
| 84 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 85 | sg->length += iv.bv_len; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 86 | } else { |
| 87 | new_segment: |
| 88 | if (!sg) |
| 89 | sg = sglist; |
| 90 | else { |
Paolo Bonzini | c8164d8 | 2013-03-20 15:37:08 +1030 | [diff] [blame] | 91 | sg_unmark_end(sg); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 92 | sg = sg_next(sg); |
| 93 | } |
| 94 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 95 | 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] | 96 | segments++; |
| 97 | } |
| 98 | |
Kent Overstreet | d57a5f7 | 2013-11-23 17:20:16 -0800 | [diff] [blame] | 99 | prev = 1; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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 | /** |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 111 | * blk_integrity_compare - Compare integrity profile of two disks |
| 112 | * @gd1: Disk to compare |
| 113 | * @gd2: Disk to compare |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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 |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 118 | * function can be used to check whether two gendisk devices have |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 119 | * compatible integrity formats. |
| 120 | */ |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 121 | int blk_integrity_compare(struct gendisk *gd1, struct gendisk *gd2) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 122 | { |
Dan Williams | ac6fc48 | 2015-10-21 13:20:18 -0400 | [diff] [blame] | 123 | struct blk_integrity *b1 = &gd1->queue->integrity; |
| 124 | struct blk_integrity *b2 = &gd2->queue->integrity; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 125 | |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 126 | if (!b1->profile && !b2->profile) |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 127 | return 0; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 128 | |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 129 | if (!b1->profile || !b2->profile) |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 130 | return -1; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 131 | |
Martin K. Petersen | a48f041 | 2015-10-21 13:19:38 -0400 | [diff] [blame] | 132 | if (b1->interval_exp != b2->interval_exp) { |
Martin K. Petersen | 3be91c4 | 2014-09-26 19:19:59 -0400 | [diff] [blame] | 133 | pr_err("%s: %s/%s protection interval %u != %u\n", |
| 134 | __func__, gd1->disk_name, gd2->disk_name, |
Martin K. Petersen | a48f041 | 2015-10-21 13:19:38 -0400 | [diff] [blame] | 135 | 1 << b1->interval_exp, 1 << b2->interval_exp); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | if (b1->tuple_size != b2->tuple_size) { |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 140 | pr_err("%s: %s/%s tuple sz %u != %u\n", __func__, |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 141 | gd1->disk_name, gd2->disk_name, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 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)) { |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 147 | pr_err("%s: %s/%s tag sz %u != %u\n", __func__, |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 148 | gd1->disk_name, gd2->disk_name, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 149 | b1->tag_size, b2->tag_size); |
| 150 | return -1; |
| 151 | } |
| 152 | |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 153 | if (b1->profile != b2->profile) { |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 154 | pr_err("%s: %s/%s type %s != %s\n", __func__, |
Martin K. Petersen | ad7fce9 | 2008-10-01 03:38:39 -0400 | [diff] [blame] | 155 | gd1->disk_name, gd2->disk_name, |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 156 | b1->profile->name, b2->profile->name); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | EXPORT_SYMBOL(blk_integrity_compare); |
| 163 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 164 | bool blk_integrity_merge_rq(struct request_queue *q, struct request *req, |
| 165 | struct request *next) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 166 | { |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 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; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 176 | |
| 177 | if (req->nr_integrity_segments + next->nr_integrity_segments > |
| 178 | q->limits.max_integrity_segments) |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 179 | return false; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 180 | |
Sagi Grimberg | 7f39add | 2015-09-11 09:03:04 -0600 | [diff] [blame] | 181 | if (integrity_req_gap_back_merge(req, next->bio)) |
| 182 | return false; |
| 183 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 184 | return true; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 185 | } |
| 186 | EXPORT_SYMBOL(blk_integrity_merge_rq); |
| 187 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 188 | bool blk_integrity_merge_bio(struct request_queue *q, struct request *req, |
| 189 | struct bio *bio) |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 190 | { |
| 191 | int nr_integrity_segs; |
| 192 | struct bio *next = bio->bi_next; |
| 193 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 194 | if (blk_integrity_rq(req) == 0 && bio_integrity(bio) == NULL) |
| 195 | return true; |
| 196 | |
| 197 | if (blk_integrity_rq(req) == 0 || bio_integrity(bio) == NULL) |
| 198 | return false; |
| 199 | |
| 200 | if (bio_integrity(req->bio)->bip_flags != bio_integrity(bio)->bip_flags) |
| 201 | return false; |
| 202 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 203 | bio->bi_next = NULL; |
| 204 | nr_integrity_segs = blk_rq_count_integrity_sg(q, bio); |
| 205 | bio->bi_next = next; |
| 206 | |
| 207 | if (req->nr_integrity_segments + nr_integrity_segs > |
| 208 | q->limits.max_integrity_segments) |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 209 | return false; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 210 | |
| 211 | req->nr_integrity_segments += nr_integrity_segs; |
| 212 | |
Martin K. Petersen | 4eaf99b | 2014-09-26 19:20:06 -0400 | [diff] [blame] | 213 | return true; |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 214 | } |
| 215 | EXPORT_SYMBOL(blk_integrity_merge_bio); |
| 216 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 217 | struct integrity_sysfs_entry { |
| 218 | struct attribute attr; |
| 219 | ssize_t (*show)(struct blk_integrity *, char *); |
| 220 | ssize_t (*store)(struct blk_integrity *, const char *, size_t); |
| 221 | }; |
| 222 | |
| 223 | static ssize_t integrity_attr_show(struct kobject *kobj, struct attribute *attr, |
| 224 | char *page) |
| 225 | { |
Martin K. Petersen | aff34e1 | 2015-10-21 13:19:27 -0400 | [diff] [blame] | 226 | struct gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj); |
Dan Williams | ac6fc48 | 2015-10-21 13:20:18 -0400 | [diff] [blame] | 227 | struct blk_integrity *bi = &disk->queue->integrity; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 228 | struct integrity_sysfs_entry *entry = |
| 229 | container_of(attr, struct integrity_sysfs_entry, attr); |
| 230 | |
| 231 | return entry->show(bi, page); |
| 232 | } |
| 233 | |
Jens Axboe | b984679 | 2008-06-17 19:05:48 +0200 | [diff] [blame] | 234 | static ssize_t integrity_attr_store(struct kobject *kobj, |
| 235 | struct attribute *attr, const char *page, |
| 236 | size_t count) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 237 | { |
Martin K. Petersen | aff34e1 | 2015-10-21 13:19:27 -0400 | [diff] [blame] | 238 | struct gendisk *disk = container_of(kobj, struct gendisk, integrity_kobj); |
Dan Williams | ac6fc48 | 2015-10-21 13:20:18 -0400 | [diff] [blame] | 239 | struct blk_integrity *bi = &disk->queue->integrity; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 240 | struct integrity_sysfs_entry *entry = |
| 241 | container_of(attr, struct integrity_sysfs_entry, attr); |
| 242 | ssize_t ret = 0; |
| 243 | |
| 244 | if (entry->store) |
| 245 | ret = entry->store(bi, page, count); |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static ssize_t integrity_format_show(struct blk_integrity *bi, char *page) |
| 251 | { |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 252 | if (bi->profile && bi->profile->name) |
Martin K. Petersen | 0f8087e | 2015-10-21 13:19:33 -0400 | [diff] [blame] | 253 | return sprintf(page, "%s\n", bi->profile->name); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 254 | else |
| 255 | return sprintf(page, "none\n"); |
| 256 | } |
| 257 | |
| 258 | static ssize_t integrity_tag_size_show(struct blk_integrity *bi, char *page) |
| 259 | { |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 260 | return sprintf(page, "%u\n", bi->tag_size); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Martin K. Petersen | 4c241d0 | 2015-10-21 13:19:43 -0400 | [diff] [blame] | 263 | static ssize_t integrity_interval_show(struct blk_integrity *bi, char *page) |
| 264 | { |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 265 | return sprintf(page, "%u\n", |
| 266 | bi->interval_exp ? 1 << bi->interval_exp : 0); |
Martin K. Petersen | 4c241d0 | 2015-10-21 13:19:43 -0400 | [diff] [blame] | 267 | } |
| 268 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 269 | static ssize_t integrity_verify_store(struct blk_integrity *bi, |
| 270 | const char *page, size_t count) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 271 | { |
| 272 | char *p = (char *) page; |
| 273 | unsigned long val = simple_strtoul(p, &p, 10); |
| 274 | |
| 275 | if (val) |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 276 | bi->flags |= BLK_INTEGRITY_VERIFY; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 277 | else |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 278 | bi->flags &= ~BLK_INTEGRITY_VERIFY; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 279 | |
| 280 | return count; |
| 281 | } |
| 282 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 283 | 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] | 284 | { |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 285 | return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_VERIFY) != 0); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 288 | static ssize_t integrity_generate_store(struct blk_integrity *bi, |
| 289 | const char *page, size_t count) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 290 | { |
| 291 | char *p = (char *) page; |
| 292 | unsigned long val = simple_strtoul(p, &p, 10); |
| 293 | |
| 294 | if (val) |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 295 | bi->flags |= BLK_INTEGRITY_GENERATE; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 296 | else |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 297 | bi->flags &= ~BLK_INTEGRITY_GENERATE; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 298 | |
| 299 | return count; |
| 300 | } |
| 301 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 302 | 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] | 303 | { |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 304 | return sprintf(page, "%d\n", (bi->flags & BLK_INTEGRITY_GENERATE) != 0); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Martin K. Petersen | 3aec2f4 | 2014-09-26 19:20:03 -0400 | [diff] [blame] | 307 | static ssize_t integrity_device_show(struct blk_integrity *bi, char *page) |
| 308 | { |
| 309 | return sprintf(page, "%u\n", |
| 310 | (bi->flags & BLK_INTEGRITY_DEVICE_CAPABLE) != 0); |
| 311 | } |
| 312 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 313 | static struct integrity_sysfs_entry integrity_format_entry = { |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 314 | .attr = { .name = "format", .mode = 0444 }, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 315 | .show = integrity_format_show, |
| 316 | }; |
| 317 | |
| 318 | static struct integrity_sysfs_entry integrity_tag_size_entry = { |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 319 | .attr = { .name = "tag_size", .mode = 0444 }, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 320 | .show = integrity_tag_size_show, |
| 321 | }; |
| 322 | |
Martin K. Petersen | 4c241d0 | 2015-10-21 13:19:43 -0400 | [diff] [blame] | 323 | static struct integrity_sysfs_entry integrity_interval_entry = { |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 324 | .attr = { .name = "protection_interval_bytes", .mode = 0444 }, |
Martin K. Petersen | 4c241d0 | 2015-10-21 13:19:43 -0400 | [diff] [blame] | 325 | .show = integrity_interval_show, |
| 326 | }; |
| 327 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 328 | static struct integrity_sysfs_entry integrity_verify_entry = { |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 329 | .attr = { .name = "read_verify", .mode = 0644 }, |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 330 | .show = integrity_verify_show, |
| 331 | .store = integrity_verify_store, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 332 | }; |
| 333 | |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 334 | static struct integrity_sysfs_entry integrity_generate_entry = { |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 335 | .attr = { .name = "write_generate", .mode = 0644 }, |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 336 | .show = integrity_generate_show, |
| 337 | .store = integrity_generate_store, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 338 | }; |
| 339 | |
Martin K. Petersen | 3aec2f4 | 2014-09-26 19:20:03 -0400 | [diff] [blame] | 340 | static struct integrity_sysfs_entry integrity_device_entry = { |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 341 | .attr = { .name = "device_is_integrity_capable", .mode = 0444 }, |
Martin K. Petersen | 3aec2f4 | 2014-09-26 19:20:03 -0400 | [diff] [blame] | 342 | .show = integrity_device_show, |
| 343 | }; |
| 344 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 345 | static struct attribute *integrity_attrs[] = { |
| 346 | &integrity_format_entry.attr, |
| 347 | &integrity_tag_size_entry.attr, |
Martin K. Petersen | 4c241d0 | 2015-10-21 13:19:43 -0400 | [diff] [blame] | 348 | &integrity_interval_entry.attr, |
Martin K. Petersen | 8288f49 | 2014-09-26 19:20:02 -0400 | [diff] [blame] | 349 | &integrity_verify_entry.attr, |
| 350 | &integrity_generate_entry.attr, |
Martin K. Petersen | 3aec2f4 | 2014-09-26 19:20:03 -0400 | [diff] [blame] | 351 | &integrity_device_entry.attr, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 352 | NULL, |
| 353 | }; |
Kimberly Brown | 800f5aa | 2019-04-01 22:51:30 -0400 | [diff] [blame] | 354 | ATTRIBUTE_GROUPS(integrity); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 355 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 356 | static const struct sysfs_ops integrity_ops = { |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 357 | .show = &integrity_attr_show, |
| 358 | .store = &integrity_attr_store, |
| 359 | }; |
| 360 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 361 | static struct kobj_type integrity_ktype = { |
Kimberly Brown | 800f5aa | 2019-04-01 22:51:30 -0400 | [diff] [blame] | 362 | .default_groups = integrity_groups, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 363 | .sysfs_ops = &integrity_ops, |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 364 | }; |
| 365 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 366 | static blk_status_t blk_integrity_nop_fn(struct blk_integrity_iter *iter) |
Dan Williams | 4125a09 | 2015-10-21 13:20:29 -0400 | [diff] [blame] | 367 | { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 368 | return BLK_STS_OK; |
Dan Williams | 4125a09 | 2015-10-21 13:20:29 -0400 | [diff] [blame] | 369 | } |
| 370 | |
Max Gurtovoy | 54d4e6a | 2019-09-16 18:44:29 +0300 | [diff] [blame] | 371 | static void blk_integrity_nop_prepare(struct request *rq) |
| 372 | { |
| 373 | } |
| 374 | |
| 375 | static void blk_integrity_nop_complete(struct request *rq, |
| 376 | unsigned int nr_bytes) |
| 377 | { |
| 378 | } |
| 379 | |
Eric Biggers | 869ab90 | 2017-03-24 18:03:48 -0700 | [diff] [blame] | 380 | static const struct blk_integrity_profile nop_profile = { |
Dan Williams | 4125a09 | 2015-10-21 13:20:29 -0400 | [diff] [blame] | 381 | .name = "nop", |
| 382 | .generate_fn = blk_integrity_nop_fn, |
| 383 | .verify_fn = blk_integrity_nop_fn, |
Max Gurtovoy | 54d4e6a | 2019-09-16 18:44:29 +0300 | [diff] [blame] | 384 | .prepare_fn = blk_integrity_nop_prepare, |
| 385 | .complete_fn = blk_integrity_nop_complete, |
Dan Williams | 4125a09 | 2015-10-21 13:20:29 -0400 | [diff] [blame] | 386 | }; |
| 387 | |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 388 | /** |
| 389 | * blk_integrity_register - Register a gendisk as being integrity-capable |
| 390 | * @disk: struct gendisk pointer to make integrity-aware |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 391 | * @template: block integrity profile to register |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 392 | * |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 393 | * Description: When a device needs to advertise itself as being able to |
| 394 | * send/receive integrity metadata it must use this function to register |
| 395 | * the capability with the block layer. The template is a blk_integrity |
| 396 | * struct with values appropriate for the underlying hardware. See |
Mauro Carvalho Chehab | 898bd37 | 2019-04-18 19:45:00 -0300 | [diff] [blame] | 397 | * Documentation/block/data-integrity.rst. |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 398 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 399 | void blk_integrity_register(struct gendisk *disk, struct blk_integrity *template) |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 400 | { |
Dan Williams | ac6fc48 | 2015-10-21 13:20:18 -0400 | [diff] [blame] | 401 | struct blk_integrity *bi = &disk->queue->integrity; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 402 | |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 403 | bi->flags = BLK_INTEGRITY_VERIFY | BLK_INTEGRITY_GENERATE | |
| 404 | template->flags; |
Mike Snitzer | 2859323 | 2017-04-22 17:22:09 -0400 | [diff] [blame] | 405 | bi->interval_exp = template->interval_exp ? : |
| 406 | ilog2(queue_logical_block_size(disk->queue)); |
Dan Williams | 4125a09 | 2015-10-21 13:20:29 -0400 | [diff] [blame] | 407 | bi->profile = template->profile ? template->profile : &nop_profile; |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 408 | bi->tuple_size = template->tuple_size; |
| 409 | bi->tag_size = template->tag_size; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 410 | |
Ilya Dryomov | 19b7ccf | 2017-04-18 18:43:20 +0200 | [diff] [blame] | 411 | disk->queue->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 412 | } |
| 413 | EXPORT_SYMBOL(blk_integrity_register); |
| 414 | |
| 415 | /** |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 416 | * blk_integrity_unregister - Unregister block integrity profile |
| 417 | * @disk: disk whose integrity profile to unregister |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 418 | * |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 419 | * Description: This function unregisters the integrity capability from |
| 420 | * a block device. |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 421 | */ |
| 422 | void blk_integrity_unregister(struct gendisk *disk) |
| 423 | { |
Ilya Dryomov | 19b7ccf | 2017-04-18 18:43:20 +0200 | [diff] [blame] | 424 | disk->queue->backing_dev_info->capabilities &= ~BDI_CAP_STABLE_WRITES; |
Dan Williams | ac6fc48 | 2015-10-21 13:20:18 -0400 | [diff] [blame] | 425 | memset(&disk->queue->integrity, 0, sizeof(struct blk_integrity)); |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 426 | } |
| 427 | EXPORT_SYMBOL(blk_integrity_unregister); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 428 | |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 429 | void blk_integrity_add(struct gendisk *disk) |
| 430 | { |
| 431 | if (kobject_init_and_add(&disk->integrity_kobj, &integrity_ktype, |
| 432 | &disk_to_dev(disk)->kobj, "%s", "integrity")) |
| 433 | return; |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 434 | |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 435 | kobject_uevent(&disk->integrity_kobj, KOBJ_ADD); |
| 436 | } |
| 437 | |
| 438 | void blk_integrity_del(struct gendisk *disk) |
| 439 | { |
Martin K. Petersen | aff34e1 | 2015-10-21 13:19:27 -0400 | [diff] [blame] | 440 | kobject_uevent(&disk->integrity_kobj, KOBJ_REMOVE); |
| 441 | kobject_del(&disk->integrity_kobj); |
| 442 | kobject_put(&disk->integrity_kobj); |
Martin K. Petersen | 7ba1ba1 | 2008-06-30 20:04:41 +0200 | [diff] [blame] | 443 | } |