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