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