2 * Copyright (C) 2015 Google, Inc.
4 * Author: Sami Tolvanen <samitolvanen@google.com>
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)
12 #include "dm-verity-fec.h"
13 #include <linux/math64.h>
15 #define DM_MSG_PREFIX "verity-fec"
18 * If error correction has been configured, returns true.
20 bool verity_fec_is_enabled(struct dm_verity *v)
22 return v->fec && v->fec->dev;
26 * Return a pointer to dm_verity_fec_io after dm_verity_io and its variable
29 static inline struct dm_verity_fec_io *fec_io(struct dm_verity_io *io)
31 return (struct dm_verity_fec_io *) verity_io_digest_end(io->v, io);
35 * Return an interleaved offset for a byte in RS block.
37 static inline u64 fec_interleave(struct dm_verity *v, u64 offset)
41 mod = do_div(offset, v->fec->rsn);
42 return offset + mod * (v->fec->rounds << v->data_dev_block_bits);
46 * Decode an RS block using Reed-Solomon.
48 static int fec_decode_rs8(struct dm_verity *v, struct dm_verity_fec_io *fio,
49 u8 *data, u8 *fec, int neras)
52 uint16_t par[DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN];
54 for (i = 0; i < v->fec->roots; i++)
57 return decode_rs8(fio->rs, data, par, v->fec->rsn, NULL, neras,
58 fio->erasures, 0, NULL);
62 * Read error-correcting codes for the requested RS block. Returns a pointer
63 * to the data block. Caller is responsible for releasing buf.
65 static u8 *fec_read_parity(struct dm_verity *v, u64 rsb, int index,
66 unsigned *offset, struct dm_buffer **buf)
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));
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),
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++)
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++)
95 /* Loop over each allocated buffer. */
96 #define fec_for_each_buffer(io, __i) \
97 for (__i = 0; __i < (io)->nbufs; __i++)
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++)
105 * Return a pointer to the current RS block when called inside
106 * fec_for_each_buffer_rs_block.
108 static inline u8 *fec_buffer_rs_block(struct dm_verity *v,
109 struct dm_verity_fec_io *fio,
110 unsigned i, unsigned j)
112 return &fio->bufs[i][j * v->fec->rsn];
116 * Return an index to the current RS block when called inside
117 * fec_for_each_buffer_rs_block.
119 static inline unsigned fec_buffer_rs_index(unsigned i, unsigned j)
121 return (i << DM_VERITY_FEC_BUF_RS_BITS) + j;
125 * Decode all RS blocks from buffers and copy corrected bytes into fio->output
126 * starting from block_offset.
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,
132 int r, corrected = 0, res;
133 struct dm_buffer *buf;
134 unsigned n, i, offset;
137 par = fec_read_parity(v, rsb, block_offset, &offset, &buf);
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.
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);
149 dm_bufio_release(buf);
156 fio->output[block_offset] = block[byte_index];
159 if (block_offset >= 1 << v->data_dev_block_bits)
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);
167 par = fec_read_parity(v, rsb, block_offset, &offset, &buf);
168 if (unlikely(IS_ERR(par)))
176 DMERR_LIMIT("%s: FEC %llu: failed to correct: %d",
177 v->data_dev->name, (unsigned long long)rsb, r);
179 DMWARN_LIMIT("%s: FEC %llu: corrected %d errors",
180 v->data_dev->name, (unsigned long long)rsb, r);
186 * Locate data block erasures using verity hashes.
188 static int fec_is_erasure(struct dm_verity *v, struct dm_verity_io *io,
189 u8 *want_digest, u8 *data)
191 if (unlikely(verity_hash(v, verity_io_hash_desc(v, io),
192 data, 1 << v->data_dev_block_bits,
193 verity_io_real_digest(v, io))))
196 return memcmp(verity_io_real_digest(v, io), want_digest,
197 v->digest_size) != 0;
201 * Read data blocks that are part of the RS block and deinterleave as much as
202 * fits into buffers. Check for erasure locations if @neras is non-NULL.
204 static int fec_read_bufs(struct dm_verity *v, struct dm_verity_io *io,
205 u64 rsb, u64 target, unsigned block_offset,
208 int i, j, target_index = -1;
209 struct dm_buffer *buf;
210 struct dm_bufio_client *bufio;
211 struct dm_verity_fec_io *fio = fec_io(io);
214 u8 want_digest[v->digest_size];
221 * read each of the rsn data blocks that are part of the RS block, and
222 * interleave contents to available bufs
224 for (i = 0; i < v->fec->rsn; i++) {
225 ileaved = fec_interleave(v, rsb * v->fec->rsn + i);
228 * target is the data block we want to correct, target_index is
229 * the index of this block within the rsn RS blocks
231 if (ileaved == target)
234 block = ileaved >> v->data_dev_block_bits;
235 bufio = v->fec->data_bufio;
237 if (block >= v->data_blocks) {
238 block -= v->data_blocks;
241 * blocks outside the area were assumed to contain
242 * zeros when encoding data was generated
244 if (unlikely(block >= v->fec->hash_blocks))
247 block += v->hash_start;
251 bbuf = dm_bufio_read(bufio, block, &buf);
252 if (unlikely(IS_ERR(bbuf))) {
253 DMWARN_LIMIT("%s: FEC %llu: read failed (%llu): %ld",
255 (unsigned long long)rsb,
256 (unsigned long long)block, PTR_ERR(bbuf));
258 /* assume the block is corrupted */
259 if (neras && *neras <= v->fec->roots)
260 fio->erasures[(*neras)++] = i;
265 /* locate erasures if the block is on the data device */
266 if (bufio == v->fec->data_bufio &&
267 verity_hash_for_block(v, io, block, want_digest) == 0) {
269 * skip if we have already found the theoretical
270 * maximum number (i.e. fec->roots) of erasures
272 if (neras && *neras <= v->fec->roots &&
273 fec_is_erasure(v, io, want_digest, bbuf))
274 fio->erasures[(*neras)++] = i;
278 * deinterleave and copy the bytes that fit into bufs,
279 * starting from block_offset
281 fec_for_each_buffer_rs_block(fio, n, j) {
282 k = fec_buffer_rs_index(n, j) + block_offset;
284 if (k >= 1 << v->data_dev_block_bits)
287 rs_block = fec_buffer_rs_block(v, fio, n, j);
288 rs_block[i] = bbuf[k];
291 dm_bufio_release(buf);
298 * Allocate RS control structure and FEC buffers from preallocated mempools,
299 * and attempt to allocate as many extra buffers as available.
301 static int fec_alloc_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio)
306 fio->rs = mempool_alloc(v->fec->rs_pool, 0);
307 if (unlikely(!fio->rs)) {
308 DMERR("failed to allocate RS");
313 fec_for_each_prealloc_buffer(n) {
317 fio->bufs[n] = mempool_alloc(v->fec->prealloc_pool, GFP_NOIO);
318 if (unlikely(!fio->bufs[n])) {
319 DMERR("failed to allocate FEC buffer");
324 /* try to allocate the maximum number of buffers */
325 fec_for_each_extra_buffer(fio, n) {
329 fio->bufs[n] = mempool_alloc(v->fec->extra_pool, GFP_NOIO);
330 /* we can manage with even one buffer if necessary */
331 if (unlikely(!fio->bufs[n]))
337 fio->output = mempool_alloc(v->fec->output_pool, GFP_NOIO);
340 DMERR("failed to allocate FEC page");
349 * Initialize buffers and clear erasures. fec_read_bufs() assumes buffers are
350 * zeroed before deinterleaving.
352 static void fec_init_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio)
356 fec_for_each_buffer(fio, n)
357 memset(fio->bufs[n], 0, v->fec->rsn << DM_VERITY_FEC_BUF_RS_BITS);
359 memset(fio->erasures, 0, sizeof(fio->erasures));
363 * Decode all RS blocks in a single data block and return the target block
364 * (indicated by @offset) in fio->output. If @use_erasures is non-zero, uses
365 * hashes to locate erasures.
367 static int fec_decode_rsb(struct dm_verity *v, struct dm_verity_io *io,
368 struct dm_verity_fec_io *fio, u64 rsb, u64 offset,
374 r = fec_alloc_bufs(v, fio);
378 for (pos = 0; pos < 1 << v->data_dev_block_bits; ) {
379 fec_init_bufs(v, fio);
381 r = fec_read_bufs(v, io, rsb, offset, pos,
382 use_erasures ? &neras : NULL);
386 r = fec_decode_bufs(v, fio, rsb, r, pos, neras);
390 pos += fio->nbufs << DM_VERITY_FEC_BUF_RS_BITS;
393 /* Always re-validate the corrected block against the expected hash */
394 r = verity_hash(v, verity_io_hash_desc(v, io), fio->output,
395 1 << v->data_dev_block_bits,
396 verity_io_real_digest(v, io));
400 if (memcmp(verity_io_real_digest(v, io), verity_io_want_digest(v, io),
402 DMERR_LIMIT("%s: FEC %llu: failed to correct (%d erasures)",
403 v->data_dev->name, (unsigned long long)rsb, neras);
410 static int fec_bv_copy(struct dm_verity *v, struct dm_verity_io *io, u8 *data,
413 struct dm_verity_fec_io *fio = fec_io(io);
415 memcpy(data, &fio->output[fio->output_pos], len);
416 fio->output_pos += len;
422 * Correct errors in a block. Copies corrected block to dest if non-NULL,
423 * otherwise to a bio_vec starting from iter.
425 int verity_fec_decode(struct dm_verity *v, struct dm_verity_io *io,
426 enum verity_block_type type, sector_t block, u8 *dest,
427 struct bvec_iter *iter)
430 struct dm_verity_fec_io *fio = fec_io(io);
431 u64 offset, res, rsb;
433 if (!verity_fec_is_enabled(v))
436 if (type == DM_VERITY_BLOCK_TYPE_METADATA)
437 block += v->data_blocks;
440 * For RS(M, N), the continuous FEC data is divided into blocks of N
441 * bytes. Since block size may not be divisible by N, the last block
442 * is zero padded when decoding.
444 * Each byte of the block is covered by a different RS(M, N) code,
445 * and each code is interleaved over N blocks to make it less likely
446 * that bursty corruption will leave us in unrecoverable state.
449 offset = block << v->data_dev_block_bits;
452 div64_u64(res, v->fec->rounds << v->data_dev_block_bits);
455 * The base RS block we can feed to the interleaver to find out all
456 * blocks required for decoding.
458 rsb = offset - res * (v->fec->rounds << v->data_dev_block_bits);
461 * Locating erasures is slow, so attempt to recover the block without
462 * them first. Do a second attempt with erasures if the corruption is
465 r = fec_decode_rsb(v, io, fio, rsb, offset, false);
467 r = fec_decode_rsb(v, io, fio, rsb, offset, true);
473 memcpy(dest, fio->output, 1 << v->data_dev_block_bits);
476 r = verity_for_bv_block(v, io, iter, fec_bv_copy);
483 * Clean up per-bio data.
485 void verity_fec_finish_io(struct dm_verity_io *io)
488 struct dm_verity_fec *f = io->v->fec;
489 struct dm_verity_fec_io *fio = fec_io(io);
491 if (!verity_fec_is_enabled(io->v))
494 mempool_free(fio->rs, f->rs_pool);
496 fec_for_each_prealloc_buffer(n)
497 mempool_free(fio->bufs[n], f->prealloc_pool);
499 fec_for_each_extra_buffer(fio, n)
500 mempool_free(fio->bufs[n], f->extra_pool);
502 mempool_free(fio->output, f->output_pool);
506 * Initialize per-bio data.
508 void verity_fec_init_io(struct dm_verity_io *io)
510 struct dm_verity_fec_io *fio = fec_io(io);
512 if (!verity_fec_is_enabled(io->v))
516 memset(fio->bufs, 0, sizeof(fio->bufs));
522 * Append feature arguments and values to the status table.
524 unsigned verity_fec_status_table(struct dm_verity *v, unsigned sz,
525 char *result, unsigned maxlen)
527 if (!verity_fec_is_enabled(v))
530 DMEMIT(" " DM_VERITY_OPT_FEC_DEV " %s "
531 DM_VERITY_OPT_FEC_BLOCKS " %llu "
532 DM_VERITY_OPT_FEC_START " %llu "
533 DM_VERITY_OPT_FEC_ROOTS " %d",
535 (unsigned long long)v->fec->blocks,
536 (unsigned long long)v->fec->start,
542 void verity_fec_dtr(struct dm_verity *v)
544 struct dm_verity_fec *f = v->fec;
546 if (!verity_fec_is_enabled(v))
549 mempool_destroy(f->rs_pool);
550 mempool_destroy(f->prealloc_pool);
551 mempool_destroy(f->extra_pool);
552 kmem_cache_destroy(f->cache);
555 dm_bufio_client_destroy(f->data_bufio);
557 dm_bufio_client_destroy(f->bufio);
560 dm_put_device(v->ti, f->dev);
566 static void *fec_rs_alloc(gfp_t gfp_mask, void *pool_data)
568 struct dm_verity *v = (struct dm_verity *)pool_data;
570 return init_rs(8, 0x11d, 0, 1, v->fec->roots);
573 static void fec_rs_free(void *element, void *pool_data)
575 struct rs_control *rs = (struct rs_control *)element;
581 bool verity_is_fec_opt_arg(const char *arg_name)
583 return (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_DEV) ||
584 !strcasecmp(arg_name, DM_VERITY_OPT_FEC_BLOCKS) ||
585 !strcasecmp(arg_name, DM_VERITY_OPT_FEC_START) ||
586 !strcasecmp(arg_name, DM_VERITY_OPT_FEC_ROOTS));
589 int verity_fec_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
590 unsigned *argc, const char *arg_name)
593 struct dm_target *ti = v->ti;
594 const char *arg_value;
595 unsigned long long num_ll;
600 ti->error = "FEC feature arguments require a value";
604 arg_value = dm_shift_arg(as);
607 if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_DEV)) {
608 r = dm_get_device(ti, arg_value, FMODE_READ, &v->fec->dev);
610 ti->error = "FEC device lookup failed";
614 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_BLOCKS)) {
615 if (sscanf(arg_value, "%llu%c", &num_ll, &dummy) != 1 ||
616 ((sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
617 >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll)) {
618 ti->error = "Invalid " DM_VERITY_OPT_FEC_BLOCKS;
621 v->fec->blocks = num_ll;
623 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_START)) {
624 if (sscanf(arg_value, "%llu%c", &num_ll, &dummy) != 1 ||
625 ((sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT)) >>
626 (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll)) {
627 ti->error = "Invalid " DM_VERITY_OPT_FEC_START;
630 v->fec->start = num_ll;
632 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_ROOTS)) {
633 if (sscanf(arg_value, "%hhu%c", &num_c, &dummy) != 1 || !num_c ||
634 num_c < (DM_VERITY_FEC_RSM - DM_VERITY_FEC_MAX_RSN) ||
635 num_c > (DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN)) {
636 ti->error = "Invalid " DM_VERITY_OPT_FEC_ROOTS;
639 v->fec->roots = num_c;
642 ti->error = "Unrecognized verity FEC feature request";
650 * Allocate dm_verity_fec for v->fec. Must be called before verity_fec_ctr.
652 int verity_fec_ctr_alloc(struct dm_verity *v)
654 struct dm_verity_fec *f;
656 f = kzalloc(sizeof(struct dm_verity_fec), GFP_KERNEL);
658 v->ti->error = "Cannot allocate FEC structure";
667 * Validate arguments and preallocate memory. Must be called after arguments
668 * have been parsed using verity_fec_parse_opt_args.
670 int verity_fec_ctr(struct dm_verity *v)
672 struct dm_verity_fec *f = v->fec;
673 struct dm_target *ti = v->ti;
676 if (!verity_fec_is_enabled(v)) {
682 * FEC is computed over data blocks, possible metadata, and
683 * hash blocks. In other words, FEC covers total of fec_blocks
684 * blocks consisting of the following:
686 * data blocks | hash blocks | metadata (optional)
688 * We allow metadata after hash blocks to support a use case
689 * where all data is stored on the same device and FEC covers
692 * If metadata is included, we require it to be available on the
693 * hash device after the hash blocks.
696 hash_blocks = v->hash_blocks - v->hash_start;
699 * Require matching block sizes for data and hash devices for
702 if (v->data_dev_block_bits != v->hash_dev_block_bits) {
703 ti->error = "Block sizes must match to use FEC";
708 ti->error = "Missing " DM_VERITY_OPT_FEC_ROOTS;
711 f->rsn = DM_VERITY_FEC_RSM - f->roots;
714 ti->error = "Missing " DM_VERITY_OPT_FEC_BLOCKS;
718 f->rounds = f->blocks;
719 if (sector_div(f->rounds, f->rsn))
723 * Due to optional metadata, f->blocks can be larger than
724 * data_blocks and hash_blocks combined.
726 if (f->blocks < v->data_blocks + hash_blocks || !f->rounds) {
727 ti->error = "Invalid " DM_VERITY_OPT_FEC_BLOCKS;
732 * Metadata is accessed through the hash device, so we require
733 * it to be large enough.
735 f->hash_blocks = f->blocks - v->data_blocks;
736 if (dm_bufio_get_device_size(v->bufio) < f->hash_blocks) {
737 ti->error = "Hash device is too small for "
738 DM_VERITY_OPT_FEC_BLOCKS;
742 f->bufio = dm_bufio_client_create(f->dev->bdev,
743 1 << v->data_dev_block_bits,
745 if (IS_ERR(f->bufio)) {
746 ti->error = "Cannot initialize FEC bufio client";
747 return PTR_ERR(f->bufio);
750 if (dm_bufio_get_device_size(f->bufio) <
751 ((f->start + f->rounds * f->roots) >> v->data_dev_block_bits)) {
752 ti->error = "FEC device is too small";
756 f->data_bufio = dm_bufio_client_create(v->data_dev->bdev,
757 1 << v->data_dev_block_bits,
759 if (IS_ERR(f->data_bufio)) {
760 ti->error = "Cannot initialize FEC data bufio client";
761 return PTR_ERR(f->data_bufio);
764 if (dm_bufio_get_device_size(f->data_bufio) < v->data_blocks) {
765 ti->error = "Data device is too small";
769 /* Preallocate an rs_control structure for each worker thread */
770 f->rs_pool = mempool_create(num_online_cpus(), fec_rs_alloc,
771 fec_rs_free, (void *) v);
773 ti->error = "Cannot allocate RS pool";
777 f->cache = kmem_cache_create("dm_verity_fec_buffers",
778 f->rsn << DM_VERITY_FEC_BUF_RS_BITS,
781 ti->error = "Cannot create FEC buffer cache";
785 /* Preallocate DM_VERITY_FEC_BUF_PREALLOC buffers for each thread */
786 f->prealloc_pool = mempool_create_slab_pool(num_online_cpus() *
787 DM_VERITY_FEC_BUF_PREALLOC,
789 if (!f->prealloc_pool) {
790 ti->error = "Cannot allocate FEC buffer prealloc pool";
794 f->extra_pool = mempool_create_slab_pool(0, f->cache);
795 if (!f->extra_pool) {
796 ti->error = "Cannot allocate FEC buffer extra pool";
800 /* Preallocate an output buffer for each thread */
801 f->output_pool = mempool_create_kmalloc_pool(num_online_cpus(),
802 1 << v->data_dev_block_bits);
803 if (!f->output_pool) {
804 ti->error = "Cannot allocate FEC output pool";
808 /* Reserve space for our per-bio data */
809 ti->per_bio_data_size += sizeof(struct dm_verity_fec_io);