hc
2024-05-16 8d2a02b24d66aa359e83eebc1ed3c0f85367a1cb
kernel/block/t10-pi.c
....@@ -1,29 +1,13 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * t10_pi.c - Functions for generating and verifying T10 Protection
34 * Information.
4
- *
5
- * Copyright (C) 2007, 2008, 2014 Oracle Corporation
6
- * Written by: Martin K. Petersen <martin.petersen@oracle.com>
7
- *
8
- * This program is free software; you can redistribute it and/or
9
- * modify it under the terms of the GNU General Public License version
10
- * 2 as published by the Free Software Foundation.
11
- *
12
- * This program is distributed in the hope that it will be useful, but
13
- * WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
- * General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU General Public License
18
- * along with this program; see the file COPYING. If not, write to
19
- * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
20
- * USA.
21
- *
225 */
236
247 #include <linux/t10-pi.h>
258 #include <linux/blkdev.h>
269 #include <linux/crc-t10dif.h>
10
+#include <linux/module.h>
2711 #include <net/checksum.h>
2812
2913 typedef __be16 (csum_fn) (void *, unsigned int);
....@@ -44,7 +28,7 @@
4428 * tag.
4529 */
4630 static blk_status_t t10_pi_generate(struct blk_integrity_iter *iter,
47
- csum_fn *fn, unsigned int type)
31
+ csum_fn *fn, enum t10_dif_type type)
4832 {
4933 unsigned int i;
5034
....@@ -54,7 +38,7 @@
5438 pi->guard_tag = fn(iter->data_buf, iter->interval);
5539 pi->app_tag = 0;
5640
57
- if (type == 1)
41
+ if (type == T10_PI_TYPE1_PROTECTION)
5842 pi->ref_tag = cpu_to_be32(lower_32_bits(iter->seed));
5943 else
6044 pi->ref_tag = 0;
....@@ -68,17 +52,18 @@
6852 }
6953
7054 static blk_status_t t10_pi_verify(struct blk_integrity_iter *iter,
71
- csum_fn *fn, unsigned int type)
55
+ csum_fn *fn, enum t10_dif_type type)
7256 {
7357 unsigned int i;
58
+
59
+ BUG_ON(type == T10_PI_TYPE0_PROTECTION);
7460
7561 for (i = 0 ; i < iter->data_size ; i += iter->interval) {
7662 struct t10_pi_tuple *pi = iter->prot_buf;
7763 __be16 csum;
7864
79
- switch (type) {
80
- case 1:
81
- case 2:
65
+ if (type == T10_PI_TYPE1_PROTECTION ||
66
+ type == T10_PI_TYPE2_PROTECTION) {
8267 if (pi->app_tag == T10_PI_APP_ESCAPE)
8368 goto next;
8469
....@@ -90,12 +75,10 @@
9075 iter->seed, be32_to_cpu(pi->ref_tag));
9176 return BLK_STS_PROTECTION;
9277 }
93
- break;
94
- case 3:
78
+ } else if (type == T10_PI_TYPE3_PROTECTION) {
9579 if (pi->app_tag == T10_PI_APP_ESCAPE &&
9680 pi->ref_tag == T10_PI_REF_ESCAPE)
9781 goto next;
98
- break;
9982 }
10083
10184 csum = fn(iter->data_buf, iter->interval);
....@@ -119,93 +102,39 @@
119102
120103 static blk_status_t t10_pi_type1_generate_crc(struct blk_integrity_iter *iter)
121104 {
122
- return t10_pi_generate(iter, t10_pi_crc_fn, 1);
105
+ return t10_pi_generate(iter, t10_pi_crc_fn, T10_PI_TYPE1_PROTECTION);
123106 }
124107
125108 static blk_status_t t10_pi_type1_generate_ip(struct blk_integrity_iter *iter)
126109 {
127
- return t10_pi_generate(iter, t10_pi_ip_fn, 1);
110
+ return t10_pi_generate(iter, t10_pi_ip_fn, T10_PI_TYPE1_PROTECTION);
128111 }
129112
130113 static blk_status_t t10_pi_type1_verify_crc(struct blk_integrity_iter *iter)
131114 {
132
- return t10_pi_verify(iter, t10_pi_crc_fn, 1);
115
+ return t10_pi_verify(iter, t10_pi_crc_fn, T10_PI_TYPE1_PROTECTION);
133116 }
134117
135118 static blk_status_t t10_pi_type1_verify_ip(struct blk_integrity_iter *iter)
136119 {
137
- return t10_pi_verify(iter, t10_pi_ip_fn, 1);
120
+ return t10_pi_verify(iter, t10_pi_ip_fn, T10_PI_TYPE1_PROTECTION);
138121 }
139
-
140
-static blk_status_t t10_pi_type3_generate_crc(struct blk_integrity_iter *iter)
141
-{
142
- return t10_pi_generate(iter, t10_pi_crc_fn, 3);
143
-}
144
-
145
-static blk_status_t t10_pi_type3_generate_ip(struct blk_integrity_iter *iter)
146
-{
147
- return t10_pi_generate(iter, t10_pi_ip_fn, 3);
148
-}
149
-
150
-static blk_status_t t10_pi_type3_verify_crc(struct blk_integrity_iter *iter)
151
-{
152
- return t10_pi_verify(iter, t10_pi_crc_fn, 3);
153
-}
154
-
155
-static blk_status_t t10_pi_type3_verify_ip(struct blk_integrity_iter *iter)
156
-{
157
- return t10_pi_verify(iter, t10_pi_ip_fn, 3);
158
-}
159
-
160
-const struct blk_integrity_profile t10_pi_type1_crc = {
161
- .name = "T10-DIF-TYPE1-CRC",
162
- .generate_fn = t10_pi_type1_generate_crc,
163
- .verify_fn = t10_pi_type1_verify_crc,
164
-};
165
-EXPORT_SYMBOL(t10_pi_type1_crc);
166
-
167
-const struct blk_integrity_profile t10_pi_type1_ip = {
168
- .name = "T10-DIF-TYPE1-IP",
169
- .generate_fn = t10_pi_type1_generate_ip,
170
- .verify_fn = t10_pi_type1_verify_ip,
171
-};
172
-EXPORT_SYMBOL(t10_pi_type1_ip);
173
-
174
-const struct blk_integrity_profile t10_pi_type3_crc = {
175
- .name = "T10-DIF-TYPE3-CRC",
176
- .generate_fn = t10_pi_type3_generate_crc,
177
- .verify_fn = t10_pi_type3_verify_crc,
178
-};
179
-EXPORT_SYMBOL(t10_pi_type3_crc);
180
-
181
-const struct blk_integrity_profile t10_pi_type3_ip = {
182
- .name = "T10-DIF-TYPE3-IP",
183
- .generate_fn = t10_pi_type3_generate_ip,
184
- .verify_fn = t10_pi_type3_verify_ip,
185
-};
186
-EXPORT_SYMBOL(t10_pi_type3_ip);
187122
188123 /**
189
- * t10_pi_prepare - prepare PI prior submitting request to device
124
+ * t10_pi_type1_prepare - prepare PI prior submitting request to device
190125 * @rq: request with PI that should be prepared
191
- * @protection_type: PI type (Type 1/Type 2/Type 3)
192126 *
193127 * For Type 1/Type 2, the virtual start sector is the one that was
194128 * originally submitted by the block layer for the ref_tag usage. Due to
195129 * partitioning, MD/DM cloning, etc. the actual physical start sector is
196130 * likely to be different. Remap protection information to match the
197131 * physical LBA.
198
- *
199
- * Type 3 does not have a reference tag so no remapping is required.
200132 */
201
-void t10_pi_prepare(struct request *rq, u8 protection_type)
133
+static void t10_pi_type1_prepare(struct request *rq)
202134 {
203135 const int tuple_sz = rq->q->integrity.tuple_size;
204136 u32 ref_tag = t10_pi_ref_tag(rq);
205137 struct bio *bio;
206
-
207
- if (protection_type == T10_PI_TYPE3_PROTECTION)
208
- return;
209138
210139 __rq_for_each_bio(bio, rq) {
211140 struct bio_integrity_payload *bip = bio_integrity(bio);
....@@ -239,13 +168,11 @@
239168 bip->bip_flags |= BIP_MAPPED_INTEGRITY;
240169 }
241170 }
242
-EXPORT_SYMBOL(t10_pi_prepare);
243171
244172 /**
245
- * t10_pi_complete - prepare PI prior returning request to the block layer
173
+ * t10_pi_type1_complete - prepare PI prior returning request to the blk layer
246174 * @rq: request with PI that should be prepared
247
- * @protection_type: PI type (Type 1/Type 2/Type 3)
248
- * @intervals: total elements to prepare
175
+ * @nr_bytes: total bytes to prepare
249176 *
250177 * For Type 1/Type 2, the virtual start sector is the one that was
251178 * originally submitted by the block layer for the ref_tag usage. Due to
....@@ -253,18 +180,13 @@
253180 * likely to be different. Since the physical start sector was submitted
254181 * to the device, we should remap it back to virtual values expected by the
255182 * block layer.
256
- *
257
- * Type 3 does not have a reference tag so no remapping is required.
258183 */
259
-void t10_pi_complete(struct request *rq, u8 protection_type,
260
- unsigned int intervals)
184
+static void t10_pi_type1_complete(struct request *rq, unsigned int nr_bytes)
261185 {
186
+ unsigned intervals = nr_bytes >> rq->q->integrity.interval_exp;
262187 const int tuple_sz = rq->q->integrity.tuple_size;
263188 u32 ref_tag = t10_pi_ref_tag(rq);
264189 struct bio *bio;
265
-
266
- if (protection_type == T10_PI_TYPE3_PROTECTION)
267
- return;
268190
269191 __rq_for_each_bio(bio, rq) {
270192 struct bio_integrity_payload *bip = bio_integrity(bio);
....@@ -293,4 +215,71 @@
293215 }
294216 }
295217 }
296
-EXPORT_SYMBOL(t10_pi_complete);
218
+
219
+static blk_status_t t10_pi_type3_generate_crc(struct blk_integrity_iter *iter)
220
+{
221
+ return t10_pi_generate(iter, t10_pi_crc_fn, T10_PI_TYPE3_PROTECTION);
222
+}
223
+
224
+static blk_status_t t10_pi_type3_generate_ip(struct blk_integrity_iter *iter)
225
+{
226
+ return t10_pi_generate(iter, t10_pi_ip_fn, T10_PI_TYPE3_PROTECTION);
227
+}
228
+
229
+static blk_status_t t10_pi_type3_verify_crc(struct blk_integrity_iter *iter)
230
+{
231
+ return t10_pi_verify(iter, t10_pi_crc_fn, T10_PI_TYPE3_PROTECTION);
232
+}
233
+
234
+static blk_status_t t10_pi_type3_verify_ip(struct blk_integrity_iter *iter)
235
+{
236
+ return t10_pi_verify(iter, t10_pi_ip_fn, T10_PI_TYPE3_PROTECTION);
237
+}
238
+
239
+/* Type 3 does not have a reference tag so no remapping is required. */
240
+static void t10_pi_type3_prepare(struct request *rq)
241
+{
242
+}
243
+
244
+/* Type 3 does not have a reference tag so no remapping is required. */
245
+static void t10_pi_type3_complete(struct request *rq, unsigned int nr_bytes)
246
+{
247
+}
248
+
249
+const struct blk_integrity_profile t10_pi_type1_crc = {
250
+ .name = "T10-DIF-TYPE1-CRC",
251
+ .generate_fn = t10_pi_type1_generate_crc,
252
+ .verify_fn = t10_pi_type1_verify_crc,
253
+ .prepare_fn = t10_pi_type1_prepare,
254
+ .complete_fn = t10_pi_type1_complete,
255
+};
256
+EXPORT_SYMBOL(t10_pi_type1_crc);
257
+
258
+const struct blk_integrity_profile t10_pi_type1_ip = {
259
+ .name = "T10-DIF-TYPE1-IP",
260
+ .generate_fn = t10_pi_type1_generate_ip,
261
+ .verify_fn = t10_pi_type1_verify_ip,
262
+ .prepare_fn = t10_pi_type1_prepare,
263
+ .complete_fn = t10_pi_type1_complete,
264
+};
265
+EXPORT_SYMBOL(t10_pi_type1_ip);
266
+
267
+const struct blk_integrity_profile t10_pi_type3_crc = {
268
+ .name = "T10-DIF-TYPE3-CRC",
269
+ .generate_fn = t10_pi_type3_generate_crc,
270
+ .verify_fn = t10_pi_type3_verify_crc,
271
+ .prepare_fn = t10_pi_type3_prepare,
272
+ .complete_fn = t10_pi_type3_complete,
273
+};
274
+EXPORT_SYMBOL(t10_pi_type3_crc);
275
+
276
+const struct blk_integrity_profile t10_pi_type3_ip = {
277
+ .name = "T10-DIF-TYPE3-IP",
278
+ .generate_fn = t10_pi_type3_generate_ip,
279
+ .verify_fn = t10_pi_type3_verify_ip,
280
+ .prepare_fn = t10_pi_type3_prepare,
281
+ .complete_fn = t10_pi_type3_complete,
282
+};
283
+EXPORT_SYMBOL(t10_pi_type3_ip);
284
+
285
+MODULE_LICENSE("GPL");