1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2015 Google, Inc.
4 *
5 * Author: Sami Tolvanen <samitolvanen@google.com>
6 */
7
8 #include "dm-verity-fec.h"
9 #include <linux/math64.h>
10
11 #define DM_MSG_PREFIX "verity-fec"
12
13 /*
14 * If error correction has been configured, returns true.
15 */
verity_fec_is_enabled(struct dm_verity * v)16 bool verity_fec_is_enabled(struct dm_verity *v)
17 {
18 return v->fec && v->fec->dev;
19 }
20
21 /*
22 * Return a pointer to dm_verity_fec_io after dm_verity_io and its variable
23 * length fields.
24 */
fec_io(struct dm_verity_io * io)25 static inline struct dm_verity_fec_io *fec_io(struct dm_verity_io *io)
26 {
27 return (struct dm_verity_fec_io *)
28 ((char *)io + io->v->ti->per_io_data_size - sizeof(struct dm_verity_fec_io));
29 }
30
31 /*
32 * Return an interleaved offset for a byte in RS block.
33 */
fec_interleave(struct dm_verity * v,u64 offset)34 static inline u64 fec_interleave(struct dm_verity *v, u64 offset)
35 {
36 u32 mod;
37
38 mod = do_div(offset, v->fec->rsn);
39 return offset + mod * (v->fec->rounds << v->data_dev_block_bits);
40 }
41
42 /*
43 * Decode an RS block using Reed-Solomon.
44 */
fec_decode_rs8(struct dm_verity * v,struct dm_verity_fec_io * fio,u8 * data,u8 * fec,int neras)45 static int fec_decode_rs8(struct dm_verity *v, struct dm_verity_fec_io *fio,
46 u8 *data, u8 *fec, int neras)
47 {
48 int i;
49 uint16_t par[DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN];
50
51 for (i = 0; i < v->fec->roots; i++)
52 par[i] = fec[i];
53
54 return decode_rs8(fio->rs, data, par, v->fec->rsn, NULL, neras,
55 fio->erasures, 0, NULL);
56 }
57
58 /*
59 * Read error-correcting codes for the requested RS block. Returns a pointer
60 * to the data block. Caller is responsible for releasing buf.
61 */
fec_read_parity(struct dm_verity * v,u64 rsb,int index,unsigned int * offset,unsigned int par_buf_offset,struct dm_buffer ** buf)62 static u8 *fec_read_parity(struct dm_verity *v, u64 rsb, int index,
63 unsigned int *offset, unsigned int par_buf_offset,
64 struct dm_buffer **buf)
65 {
66 u64 position, block, rem;
67 u8 *res;
68
69 /* We have already part of parity bytes read, skip to the next block */
70 if (par_buf_offset)
71 index++;
72
73 position = (index + rsb) * v->fec->roots;
74 block = div64_u64_rem(position, v->fec->io_size, &rem);
75 *offset = par_buf_offset ? 0 : (unsigned int)rem;
76
77 res = dm_bufio_read(v->fec->bufio, block, buf);
78 if (IS_ERR(res)) {
79 DMERR("%s: FEC %llu: parity read failed (block %llu): %ld",
80 v->data_dev->name, (unsigned long long)rsb,
81 (unsigned long long)block, PTR_ERR(res));
82 *buf = NULL;
83 }
84
85 return res;
86 }
87
88 /* Loop over each preallocated buffer slot. */
89 #define fec_for_each_prealloc_buffer(__i) \
90 for (__i = 0; __i < DM_VERITY_FEC_BUF_PREALLOC; __i++)
91
92 /* Loop over each extra buffer slot. */
93 #define fec_for_each_extra_buffer(io, __i) \
94 for (__i = DM_VERITY_FEC_BUF_PREALLOC; __i < DM_VERITY_FEC_BUF_MAX; __i++)
95
96 /* Loop over each allocated buffer. */
97 #define fec_for_each_buffer(io, __i) \
98 for (__i = 0; __i < (io)->nbufs; __i++)
99
100 /* Loop over each RS block in each allocated buffer. */
101 #define fec_for_each_buffer_rs_block(io, __i, __j) \
102 fec_for_each_buffer(io, __i) \
103 for (__j = 0; __j < 1 << DM_VERITY_FEC_BUF_RS_BITS; __j++)
104
105 /*
106 * Return a pointer to the current RS block when called inside
107 * fec_for_each_buffer_rs_block.
108 */
fec_buffer_rs_block(struct dm_verity * v,struct dm_verity_fec_io * fio,unsigned int i,unsigned int j)109 static inline u8 *fec_buffer_rs_block(struct dm_verity *v,
110 struct dm_verity_fec_io *fio,
111 unsigned int i, unsigned int j)
112 {
113 return &fio->bufs[i][j * v->fec->rsn];
114 }
115
116 /*
117 * Return an index to the current RS block when called inside
118 * fec_for_each_buffer_rs_block.
119 */
fec_buffer_rs_index(unsigned int i,unsigned int j)120 static inline unsigned int fec_buffer_rs_index(unsigned int i, unsigned int j)
121 {
122 return (i << DM_VERITY_FEC_BUF_RS_BITS) + j;
123 }
124
125 /*
126 * Decode all RS blocks from buffers and copy corrected bytes into fio->output
127 * starting from block_offset.
128 */
fec_decode_bufs(struct dm_verity * v,struct dm_verity_fec_io * fio,u64 rsb,int byte_index,unsigned int block_offset,int neras)129 static int fec_decode_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio,
130 u64 rsb, int byte_index, unsigned int block_offset,
131 int neras)
132 {
133 int r, corrected = 0, res;
134 struct dm_buffer *buf;
135 unsigned int n, i, offset, par_buf_offset = 0;
136 u8 *par, *block, par_buf[DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN];
137
138 par = fec_read_parity(v, rsb, block_offset, &offset,
139 par_buf_offset, &buf);
140 if (IS_ERR(par))
141 return PTR_ERR(par);
142
143 /*
144 * Decode the RS blocks we have in bufs. Each RS block results in
145 * one corrected target byte and consumes fec->roots parity bytes.
146 */
147 fec_for_each_buffer_rs_block(fio, n, i) {
148 block = fec_buffer_rs_block(v, fio, n, i);
149 memcpy(&par_buf[par_buf_offset], &par[offset], v->fec->roots - par_buf_offset);
150 res = fec_decode_rs8(v, fio, block, par_buf, neras);
151 if (res < 0) {
152 r = res;
153 goto error;
154 }
155
156 corrected += res;
157 fio->output[block_offset] = block[byte_index];
158
159 block_offset++;
160 if (block_offset >= 1 << v->data_dev_block_bits)
161 goto done;
162
163 /* Read the next block when we run out of parity bytes */
164 offset += (v->fec->roots - par_buf_offset);
165 /* Check if parity bytes are split between blocks */
166 if (offset < v->fec->io_size && (offset + v->fec->roots) > v->fec->io_size) {
167 par_buf_offset = v->fec->io_size - offset;
168 memcpy(par_buf, &par[offset], par_buf_offset);
169 offset += par_buf_offset;
170 } else
171 par_buf_offset = 0;
172
173 if (offset >= v->fec->io_size) {
174 dm_bufio_release(buf);
175
176 par = fec_read_parity(v, rsb, block_offset, &offset,
177 par_buf_offset, &buf);
178 if (IS_ERR(par))
179 return PTR_ERR(par);
180 }
181 }
182 done:
183 r = corrected;
184 error:
185 dm_bufio_release(buf);
186
187 if (r < 0 && neras)
188 DMERR_LIMIT("%s: FEC %llu: failed to correct: %d",
189 v->data_dev->name, (unsigned long long)rsb, r);
190 else if (r > 0)
191 DMWARN_LIMIT("%s: FEC %llu: corrected %d errors",
192 v->data_dev->name, (unsigned long long)rsb, r);
193
194 return r;
195 }
196
197 /*
198 * Locate data block erasures using verity hashes.
199 */
fec_is_erasure(struct dm_verity * v,struct dm_verity_io * io,u8 * want_digest,u8 * data)200 static int fec_is_erasure(struct dm_verity *v, struct dm_verity_io *io,
201 u8 *want_digest, u8 *data)
202 {
203 if (unlikely(verity_hash(v, verity_io_hash_req(v, io),
204 data, 1 << v->data_dev_block_bits,
205 verity_io_real_digest(v, io), true)))
206 return 0;
207
208 return memcmp(verity_io_real_digest(v, io), want_digest,
209 v->digest_size) != 0;
210 }
211
212 /*
213 * Read data blocks that are part of the RS block and deinterleave as much as
214 * fits into buffers. Check for erasure locations if @neras is non-NULL.
215 */
fec_read_bufs(struct dm_verity * v,struct dm_verity_io * io,u64 rsb,u64 target,unsigned int block_offset,int * neras)216 static int fec_read_bufs(struct dm_verity *v, struct dm_verity_io *io,
217 u64 rsb, u64 target, unsigned int block_offset,
218 int *neras)
219 {
220 bool is_zero;
221 int i, j, target_index = -1;
222 struct dm_buffer *buf;
223 struct dm_bufio_client *bufio;
224 struct dm_verity_fec_io *fio = fec_io(io);
225 u64 block, ileaved;
226 u8 *bbuf, *rs_block;
227 u8 want_digest[HASH_MAX_DIGESTSIZE];
228 unsigned int n, k;
229
230 if (neras)
231 *neras = 0;
232
233 if (WARN_ON(v->digest_size > sizeof(want_digest)))
234 return -EINVAL;
235
236 /*
237 * read each of the rsn data blocks that are part of the RS block, and
238 * interleave contents to available bufs
239 */
240 for (i = 0; i < v->fec->rsn; i++) {
241 ileaved = fec_interleave(v, rsb * v->fec->rsn + i);
242
243 /*
244 * target is the data block we want to correct, target_index is
245 * the index of this block within the rsn RS blocks
246 */
247 if (ileaved == target)
248 target_index = i;
249
250 block = ileaved >> v->data_dev_block_bits;
251 bufio = v->fec->data_bufio;
252
253 if (block >= v->data_blocks) {
254 block -= v->data_blocks;
255
256 /*
257 * blocks outside the area were assumed to contain
258 * zeros when encoding data was generated
259 */
260 if (unlikely(block >= v->fec->hash_blocks))
261 continue;
262
263 block += v->hash_start;
264 bufio = v->bufio;
265 }
266
267 bbuf = dm_bufio_read(bufio, block, &buf);
268 if (IS_ERR(bbuf)) {
269 DMWARN_LIMIT("%s: FEC %llu: read failed (%llu): %ld",
270 v->data_dev->name,
271 (unsigned long long)rsb,
272 (unsigned long long)block, PTR_ERR(bbuf));
273
274 /* assume the block is corrupted */
275 if (neras && *neras <= v->fec->roots)
276 fio->erasures[(*neras)++] = i;
277
278 continue;
279 }
280
281 /* locate erasures if the block is on the data device */
282 if (bufio == v->fec->data_bufio &&
283 verity_hash_for_block(v, io, block, want_digest,
284 &is_zero) == 0) {
285 /* skip known zero blocks entirely */
286 if (is_zero)
287 goto done;
288
289 /*
290 * skip if we have already found the theoretical
291 * maximum number (i.e. fec->roots) of erasures
292 */
293 if (neras && *neras <= v->fec->roots &&
294 fec_is_erasure(v, io, want_digest, bbuf))
295 fio->erasures[(*neras)++] = i;
296 }
297
298 /*
299 * deinterleave and copy the bytes that fit into bufs,
300 * starting from block_offset
301 */
302 fec_for_each_buffer_rs_block(fio, n, j) {
303 k = fec_buffer_rs_index(n, j) + block_offset;
304
305 if (k >= 1 << v->data_dev_block_bits)
306 goto done;
307
308 rs_block = fec_buffer_rs_block(v, fio, n, j);
309 rs_block[i] = bbuf[k];
310 }
311 done:
312 dm_bufio_release(buf);
313 }
314
315 return target_index;
316 }
317
318 /*
319 * Allocate RS control structure and FEC buffers from preallocated mempools,
320 * and attempt to allocate as many extra buffers as available.
321 */
fec_alloc_bufs(struct dm_verity * v,struct dm_verity_fec_io * fio)322 static int fec_alloc_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio)
323 {
324 unsigned int n;
325
326 if (!fio->rs)
327 fio->rs = mempool_alloc(&v->fec->rs_pool, GFP_NOIO);
328
329 fec_for_each_prealloc_buffer(n) {
330 if (fio->bufs[n])
331 continue;
332
333 fio->bufs[n] = mempool_alloc(&v->fec->prealloc_pool, GFP_NOWAIT);
334 if (unlikely(!fio->bufs[n])) {
335 DMERR("failed to allocate FEC buffer");
336 return -ENOMEM;
337 }
338 }
339
340 /* try to allocate the maximum number of buffers */
341 fec_for_each_extra_buffer(fio, n) {
342 if (fio->bufs[n])
343 continue;
344
345 fio->bufs[n] = mempool_alloc(&v->fec->extra_pool, GFP_NOWAIT);
346 /* we can manage with even one buffer if necessary */
347 if (unlikely(!fio->bufs[n]))
348 break;
349 }
350 fio->nbufs = n;
351
352 if (!fio->output)
353 fio->output = mempool_alloc(&v->fec->output_pool, GFP_NOIO);
354
355 return 0;
356 }
357
358 /*
359 * Initialize buffers and clear erasures. fec_read_bufs() assumes buffers are
360 * zeroed before deinterleaving.
361 */
fec_init_bufs(struct dm_verity * v,struct dm_verity_fec_io * fio)362 static void fec_init_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio)
363 {
364 unsigned int n;
365
366 fec_for_each_buffer(fio, n)
367 memset(fio->bufs[n], 0, v->fec->rsn << DM_VERITY_FEC_BUF_RS_BITS);
368
369 memset(fio->erasures, 0, sizeof(fio->erasures));
370 }
371
372 /*
373 * Decode all RS blocks in a single data block and return the target block
374 * (indicated by @offset) in fio->output. If @use_erasures is non-zero, uses
375 * hashes to locate erasures.
376 */
fec_decode_rsb(struct dm_verity * v,struct dm_verity_io * io,struct dm_verity_fec_io * fio,u64 rsb,u64 offset,bool use_erasures)377 static int fec_decode_rsb(struct dm_verity *v, struct dm_verity_io *io,
378 struct dm_verity_fec_io *fio, u64 rsb, u64 offset,
379 bool use_erasures)
380 {
381 int r, neras = 0;
382 unsigned int pos;
383
384 r = fec_alloc_bufs(v, fio);
385 if (unlikely(r < 0))
386 return r;
387
388 for (pos = 0; pos < 1 << v->data_dev_block_bits; ) {
389 fec_init_bufs(v, fio);
390
391 r = fec_read_bufs(v, io, rsb, offset, pos,
392 use_erasures ? &neras : NULL);
393 if (unlikely(r < 0))
394 return r;
395
396 r = fec_decode_bufs(v, fio, rsb, r, pos, neras);
397 if (r < 0)
398 return r;
399
400 pos += fio->nbufs << DM_VERITY_FEC_BUF_RS_BITS;
401 }
402
403 /* Always re-validate the corrected block against the expected hash */
404 r = verity_hash(v, verity_io_hash_req(v, io), fio->output,
405 1 << v->data_dev_block_bits,
406 verity_io_real_digest(v, io), true);
407 if (unlikely(r < 0))
408 return r;
409
410 if (memcmp(verity_io_real_digest(v, io), verity_io_want_digest(v, io),
411 v->digest_size)) {
412 DMERR_LIMIT("%s: FEC %llu: failed to correct (%d erasures)",
413 v->data_dev->name, (unsigned long long)rsb, neras);
414 return -EILSEQ;
415 }
416
417 return 0;
418 }
419
fec_bv_copy(struct dm_verity * v,struct dm_verity_io * io,u8 * data,size_t len)420 static int fec_bv_copy(struct dm_verity *v, struct dm_verity_io *io, u8 *data,
421 size_t len)
422 {
423 struct dm_verity_fec_io *fio = fec_io(io);
424
425 memcpy(data, &fio->output[fio->output_pos], len);
426 fio->output_pos += len;
427
428 return 0;
429 }
430
431 /*
432 * Correct errors in a block. Copies corrected block to dest if non-NULL,
433 * otherwise to a bio_vec starting from iter.
434 */
verity_fec_decode(struct dm_verity * v,struct dm_verity_io * io,enum verity_block_type type,sector_t block,u8 * dest,struct bvec_iter * iter)435 int verity_fec_decode(struct dm_verity *v, struct dm_verity_io *io,
436 enum verity_block_type type, sector_t block, u8 *dest,
437 struct bvec_iter *iter)
438 {
439 int r;
440 struct dm_verity_fec_io *fio = fec_io(io);
441 u64 offset, res, rsb;
442
443 if (!verity_fec_is_enabled(v))
444 return -EOPNOTSUPP;
445
446 if (fio->level >= DM_VERITY_FEC_MAX_RECURSION) {
447 DMWARN_LIMIT("%s: FEC: recursion too deep", v->data_dev->name);
448 return -EIO;
449 }
450
451 fio->level++;
452
453 if (type == DM_VERITY_BLOCK_TYPE_METADATA)
454 block = block - v->hash_start + v->data_blocks;
455
456 /*
457 * For RS(M, N), the continuous FEC data is divided into blocks of N
458 * bytes. Since block size may not be divisible by N, the last block
459 * is zero padded when decoding.
460 *
461 * Each byte of the block is covered by a different RS(M, N) code,
462 * and each code is interleaved over N blocks to make it less likely
463 * that bursty corruption will leave us in unrecoverable state.
464 */
465
466 offset = block << v->data_dev_block_bits;
467 res = div64_u64(offset, v->fec->rounds << v->data_dev_block_bits);
468
469 /*
470 * The base RS block we can feed to the interleaver to find out all
471 * blocks required for decoding.
472 */
473 rsb = offset - res * (v->fec->rounds << v->data_dev_block_bits);
474
475 /*
476 * Locating erasures is slow, so attempt to recover the block without
477 * them first. Do a second attempt with erasures if the corruption is
478 * bad enough.
479 */
480 r = fec_decode_rsb(v, io, fio, rsb, offset, false);
481 if (r < 0) {
482 r = fec_decode_rsb(v, io, fio, rsb, offset, true);
483 if (r < 0)
484 goto done;
485 }
486
487 if (dest)
488 memcpy(dest, fio->output, 1 << v->data_dev_block_bits);
489 else if (iter) {
490 fio->output_pos = 0;
491 r = verity_for_bv_block(v, io, iter, fec_bv_copy);
492 }
493
494 done:
495 fio->level--;
496 return r;
497 }
498
499 /*
500 * Clean up per-bio data.
501 */
verity_fec_finish_io(struct dm_verity_io * io)502 void verity_fec_finish_io(struct dm_verity_io *io)
503 {
504 unsigned int n;
505 struct dm_verity_fec *f = io->v->fec;
506 struct dm_verity_fec_io *fio = fec_io(io);
507
508 if (!verity_fec_is_enabled(io->v))
509 return;
510
511 mempool_free(fio->rs, &f->rs_pool);
512
513 fec_for_each_prealloc_buffer(n)
514 mempool_free(fio->bufs[n], &f->prealloc_pool);
515
516 fec_for_each_extra_buffer(fio, n)
517 mempool_free(fio->bufs[n], &f->extra_pool);
518
519 mempool_free(fio->output, &f->output_pool);
520 }
521
522 /*
523 * Initialize per-bio data.
524 */
verity_fec_init_io(struct dm_verity_io * io)525 void verity_fec_init_io(struct dm_verity_io *io)
526 {
527 struct dm_verity_fec_io *fio = fec_io(io);
528
529 if (!verity_fec_is_enabled(io->v))
530 return;
531
532 fio->rs = NULL;
533 memset(fio->bufs, 0, sizeof(fio->bufs));
534 fio->nbufs = 0;
535 fio->output = NULL;
536 fio->level = 0;
537 }
538
539 /*
540 * Append feature arguments and values to the status table.
541 */
verity_fec_status_table(struct dm_verity * v,unsigned int sz,char * result,unsigned int maxlen)542 unsigned int verity_fec_status_table(struct dm_verity *v, unsigned int sz,
543 char *result, unsigned int maxlen)
544 {
545 if (!verity_fec_is_enabled(v))
546 return sz;
547
548 DMEMIT(" " DM_VERITY_OPT_FEC_DEV " %s "
549 DM_VERITY_OPT_FEC_BLOCKS " %llu "
550 DM_VERITY_OPT_FEC_START " %llu "
551 DM_VERITY_OPT_FEC_ROOTS " %d",
552 v->fec->dev->name,
553 (unsigned long long)v->fec->blocks,
554 (unsigned long long)v->fec->start,
555 v->fec->roots);
556
557 return sz;
558 }
559
verity_fec_dtr(struct dm_verity * v)560 void verity_fec_dtr(struct dm_verity *v)
561 {
562 struct dm_verity_fec *f = v->fec;
563
564 if (!verity_fec_is_enabled(v))
565 goto out;
566
567 mempool_exit(&f->rs_pool);
568 mempool_exit(&f->prealloc_pool);
569 mempool_exit(&f->extra_pool);
570 mempool_exit(&f->output_pool);
571 kmem_cache_destroy(f->cache);
572
573 if (f->data_bufio)
574 dm_bufio_client_destroy(f->data_bufio);
575 if (f->bufio)
576 dm_bufio_client_destroy(f->bufio);
577
578 if (f->dev)
579 dm_put_device(v->ti, f->dev);
580 out:
581 kfree(f);
582 v->fec = NULL;
583 }
584
fec_rs_alloc(gfp_t gfp_mask,void * pool_data)585 static void *fec_rs_alloc(gfp_t gfp_mask, void *pool_data)
586 {
587 struct dm_verity *v = pool_data;
588
589 return init_rs_gfp(8, 0x11d, 0, 1, v->fec->roots, gfp_mask);
590 }
591
fec_rs_free(void * element,void * pool_data)592 static void fec_rs_free(void *element, void *pool_data)
593 {
594 struct rs_control *rs = element;
595
596 if (rs)
597 free_rs(rs);
598 }
599
verity_is_fec_opt_arg(const char * arg_name)600 bool verity_is_fec_opt_arg(const char *arg_name)
601 {
602 return (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_DEV) ||
603 !strcasecmp(arg_name, DM_VERITY_OPT_FEC_BLOCKS) ||
604 !strcasecmp(arg_name, DM_VERITY_OPT_FEC_START) ||
605 !strcasecmp(arg_name, DM_VERITY_OPT_FEC_ROOTS));
606 }
607
verity_fec_parse_opt_args(struct dm_arg_set * as,struct dm_verity * v,unsigned int * argc,const char * arg_name)608 int verity_fec_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
609 unsigned int *argc, const char *arg_name)
610 {
611 int r;
612 struct dm_target *ti = v->ti;
613 const char *arg_value;
614 unsigned long long num_ll;
615 unsigned char num_c;
616 char dummy;
617
618 if (!*argc) {
619 ti->error = "FEC feature arguments require a value";
620 return -EINVAL;
621 }
622
623 arg_value = dm_shift_arg(as);
624 (*argc)--;
625
626 if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_DEV)) {
627 r = dm_get_device(ti, arg_value, BLK_OPEN_READ, &v->fec->dev);
628 if (r) {
629 ti->error = "FEC device lookup failed";
630 return r;
631 }
632
633 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_BLOCKS)) {
634 if (sscanf(arg_value, "%llu%c", &num_ll, &dummy) != 1 ||
635 ((sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
636 >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll)) {
637 ti->error = "Invalid " DM_VERITY_OPT_FEC_BLOCKS;
638 return -EINVAL;
639 }
640 v->fec->blocks = num_ll;
641
642 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_START)) {
643 if (sscanf(arg_value, "%llu%c", &num_ll, &dummy) != 1 ||
644 ((sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT)) >>
645 (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll)) {
646 ti->error = "Invalid " DM_VERITY_OPT_FEC_START;
647 return -EINVAL;
648 }
649 v->fec->start = num_ll;
650
651 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_ROOTS)) {
652 if (sscanf(arg_value, "%hhu%c", &num_c, &dummy) != 1 || !num_c ||
653 num_c < (DM_VERITY_FEC_RSM - DM_VERITY_FEC_MAX_RSN) ||
654 num_c > (DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN)) {
655 ti->error = "Invalid " DM_VERITY_OPT_FEC_ROOTS;
656 return -EINVAL;
657 }
658 v->fec->roots = num_c;
659
660 } else {
661 ti->error = "Unrecognized verity FEC feature request";
662 return -EINVAL;
663 }
664
665 return 0;
666 }
667
668 /*
669 * Allocate dm_verity_fec for v->fec. Must be called before verity_fec_ctr.
670 */
verity_fec_ctr_alloc(struct dm_verity * v)671 int verity_fec_ctr_alloc(struct dm_verity *v)
672 {
673 struct dm_verity_fec *f;
674
675 f = kzalloc(sizeof(struct dm_verity_fec), GFP_KERNEL);
676 if (!f) {
677 v->ti->error = "Cannot allocate FEC structure";
678 return -ENOMEM;
679 }
680 v->fec = f;
681
682 return 0;
683 }
684
685 /*
686 * Validate arguments and preallocate memory. Must be called after arguments
687 * have been parsed using verity_fec_parse_opt_args.
688 */
verity_fec_ctr(struct dm_verity * v)689 int verity_fec_ctr(struct dm_verity *v)
690 {
691 struct dm_verity_fec *f = v->fec;
692 struct dm_target *ti = v->ti;
693 u64 hash_blocks, fec_blocks;
694 int ret;
695
696 if (!verity_fec_is_enabled(v)) {
697 verity_fec_dtr(v);
698 return 0;
699 }
700
701 /*
702 * FEC is computed over data blocks, possible metadata, and
703 * hash blocks. In other words, FEC covers total of fec_blocks
704 * blocks consisting of the following:
705 *
706 * data blocks | hash blocks | metadata (optional)
707 *
708 * We allow metadata after hash blocks to support a use case
709 * where all data is stored on the same device and FEC covers
710 * the entire area.
711 *
712 * If metadata is included, we require it to be available on the
713 * hash device after the hash blocks.
714 */
715
716 hash_blocks = v->hash_blocks - v->hash_start;
717
718 /*
719 * Require matching block sizes for data and hash devices for
720 * simplicity.
721 */
722 if (v->data_dev_block_bits != v->hash_dev_block_bits) {
723 ti->error = "Block sizes must match to use FEC";
724 return -EINVAL;
725 }
726
727 if (!f->roots) {
728 ti->error = "Missing " DM_VERITY_OPT_FEC_ROOTS;
729 return -EINVAL;
730 }
731 f->rsn = DM_VERITY_FEC_RSM - f->roots;
732
733 if (!f->blocks) {
734 ti->error = "Missing " DM_VERITY_OPT_FEC_BLOCKS;
735 return -EINVAL;
736 }
737
738 f->rounds = f->blocks;
739 if (sector_div(f->rounds, f->rsn))
740 f->rounds++;
741
742 /*
743 * Due to optional metadata, f->blocks can be larger than
744 * data_blocks and hash_blocks combined.
745 */
746 if (f->blocks < v->data_blocks + hash_blocks || !f->rounds) {
747 ti->error = "Invalid " DM_VERITY_OPT_FEC_BLOCKS;
748 return -EINVAL;
749 }
750
751 /*
752 * Metadata is accessed through the hash device, so we require
753 * it to be large enough.
754 */
755 f->hash_blocks = f->blocks - v->data_blocks;
756 if (dm_bufio_get_device_size(v->bufio) < f->hash_blocks) {
757 ti->error = "Hash device is too small for "
758 DM_VERITY_OPT_FEC_BLOCKS;
759 return -E2BIG;
760 }
761
762 f->io_size = 1 << v->data_dev_block_bits;
763
764 f->bufio = dm_bufio_client_create(f->dev->bdev,
765 f->io_size,
766 1, 0, NULL, NULL, 0);
767 if (IS_ERR(f->bufio)) {
768 ti->error = "Cannot initialize FEC bufio client";
769 return PTR_ERR(f->bufio);
770 }
771
772 dm_bufio_set_sector_offset(f->bufio, f->start << (v->data_dev_block_bits - SECTOR_SHIFT));
773
774 fec_blocks = div64_u64(f->rounds * f->roots, v->fec->roots << SECTOR_SHIFT);
775 if (dm_bufio_get_device_size(f->bufio) < fec_blocks) {
776 ti->error = "FEC device is too small";
777 return -E2BIG;
778 }
779
780 f->data_bufio = dm_bufio_client_create(v->data_dev->bdev,
781 1 << v->data_dev_block_bits,
782 1, 0, NULL, NULL, 0);
783 if (IS_ERR(f->data_bufio)) {
784 ti->error = "Cannot initialize FEC data bufio client";
785 return PTR_ERR(f->data_bufio);
786 }
787
788 if (dm_bufio_get_device_size(f->data_bufio) < v->data_blocks) {
789 ti->error = "Data device is too small";
790 return -E2BIG;
791 }
792
793 /* Preallocate an rs_control structure for each worker thread */
794 ret = mempool_init(&f->rs_pool, num_online_cpus(), fec_rs_alloc,
795 fec_rs_free, (void *) v);
796 if (ret) {
797 ti->error = "Cannot allocate RS pool";
798 return ret;
799 }
800
801 f->cache = kmem_cache_create("dm_verity_fec_buffers",
802 f->rsn << DM_VERITY_FEC_BUF_RS_BITS,
803 0, 0, NULL);
804 if (!f->cache) {
805 ti->error = "Cannot create FEC buffer cache";
806 return -ENOMEM;
807 }
808
809 /* Preallocate DM_VERITY_FEC_BUF_PREALLOC buffers for each thread */
810 ret = mempool_init_slab_pool(&f->prealloc_pool, num_online_cpus() *
811 DM_VERITY_FEC_BUF_PREALLOC,
812 f->cache);
813 if (ret) {
814 ti->error = "Cannot allocate FEC buffer prealloc pool";
815 return ret;
816 }
817
818 ret = mempool_init_slab_pool(&f->extra_pool, 0, f->cache);
819 if (ret) {
820 ti->error = "Cannot allocate FEC buffer extra pool";
821 return ret;
822 }
823
824 /* Preallocate an output buffer for each thread */
825 ret = mempool_init_kmalloc_pool(&f->output_pool, num_online_cpus(),
826 1 << v->data_dev_block_bits);
827 if (ret) {
828 ti->error = "Cannot allocate FEC output pool";
829 return ret;
830 }
831
832 /* Reserve space for our per-bio data */
833 ti->per_io_data_size += sizeof(struct dm_verity_fec_io);
834
835 return 0;
836 }
837