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>
14 #include <linux/sysfs.h>
16 #define DM_MSG_PREFIX "verity-fec"
19 * If error correction has been configured, returns true.
21 bool verity_fec_is_enabled(struct dm_verity *v)
23 return v->fec && v->fec->dev;
27 * Return a pointer to dm_verity_fec_io after dm_verity_io and its variable
30 static inline struct dm_verity_fec_io *fec_io(struct dm_verity_io *io)
32 return (struct dm_verity_fec_io *) verity_io_digest_end(io->v, io);
36 * Return an interleaved offset for a byte in RS block.
38 static inline u64 fec_interleave(struct dm_verity *v, u64 offset)
42 mod = do_div(offset, v->fec->rsn);
43 return offset + mod * (v->fec->rounds << v->data_dev_block_bits);
47 * Decode an RS block using Reed-Solomon.
49 static int fec_decode_rs8(struct dm_verity *v, struct dm_verity_fec_io *fio,
50 u8 *data, u8 *fec, int neras)
53 uint16_t par[DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN];
55 for (i = 0; i < v->fec->roots; i++)
58 return decode_rs8(fio->rs, data, par, v->fec->rsn, NULL, neras,
59 fio->erasures, 0, NULL);
63 * Read error-correcting codes for the requested RS block. Returns a pointer
64 * to the data block. Caller is responsible for releasing buf.
66 static u8 *fec_read_parity(struct dm_verity *v, u64 rsb, int index,
67 unsigned *offset, struct dm_buffer **buf)
72 position = (index + rsb) * v->fec->roots;
73 block = position >> v->data_dev_block_bits;
74 *offset = (unsigned)(position - (block << v->data_dev_block_bits));
76 res = dm_bufio_read(v->fec->bufio, v->fec->start + block, buf);
77 if (unlikely(IS_ERR(res))) {
78 DMERR("%s: FEC %llu: parity read failed (block %llu): %ld",
79 v->data_dev->name, (unsigned long long)rsb,
80 (unsigned long long)(v->fec->start + block),
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++)
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++)
96 /* Loop over each allocated buffer. */
97 #define fec_for_each_buffer(io, __i) \
98 for (__i = 0; __i < (io)->nbufs; __i++)
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++)
106 * Return a pointer to the current RS block when called inside
107 * fec_for_each_buffer_rs_block.
109 static inline u8 *fec_buffer_rs_block(struct dm_verity *v,
110 struct dm_verity_fec_io *fio,
111 unsigned i, unsigned j)
113 return &fio->bufs[i][j * v->fec->rsn];
117 * Return an index to the current RS block when called inside
118 * fec_for_each_buffer_rs_block.
120 static inline unsigned fec_buffer_rs_index(unsigned i, unsigned j)
122 return (i << DM_VERITY_FEC_BUF_RS_BITS) + j;
126 * Decode all RS blocks from buffers and copy corrected bytes into fio->output
127 * starting from block_offset.
129 static int fec_decode_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio,
130 u64 rsb, int byte_index, unsigned block_offset,
133 int r, corrected = 0, res;
134 struct dm_buffer *buf;
135 unsigned n, i, offset;
138 par = fec_read_parity(v, rsb, block_offset, &offset, &buf);
143 * Decode the RS blocks we have in bufs. Each RS block results in
144 * one corrected target byte and consumes fec->roots parity bytes.
146 fec_for_each_buffer_rs_block(fio, n, i) {
147 block = fec_buffer_rs_block(v, fio, n, i);
148 res = fec_decode_rs8(v, fio, block, &par[offset], neras);
150 dm_bufio_release(buf);
157 fio->output[block_offset] = block[byte_index];
160 if (block_offset >= 1 << v->data_dev_block_bits)
163 /* read the next block when we run out of parity bytes */
164 offset += v->fec->roots;
165 if (offset >= 1 << v->data_dev_block_bits) {
166 dm_bufio_release(buf);
168 par = fec_read_parity(v, rsb, block_offset, &offset, &buf);
169 if (unlikely(IS_ERR(par)))
177 DMERR_LIMIT("%s: FEC %llu: failed to correct: %d",
178 v->data_dev->name, (unsigned long long)rsb, r);
180 DMWARN_LIMIT("%s: FEC %llu: corrected %d errors",
181 v->data_dev->name, (unsigned long long)rsb, r);
182 atomic_add_unless(&v->fec->corrected, 1, INT_MAX);
189 * Locate data block erasures using verity hashes.
191 static int fec_is_erasure(struct dm_verity *v, struct dm_verity_io *io,
192 u8 *want_digest, u8 *data)
194 if (unlikely(verity_hash(v, verity_io_hash_desc(v, io),
195 data, 1 << v->data_dev_block_bits,
196 verity_io_real_digest(v, io))))
199 return memcmp(verity_io_real_digest(v, io), want_digest,
200 v->digest_size) != 0;
204 * Read data blocks that are part of the RS block and deinterleave as much as
205 * fits into buffers. Check for erasure locations if @neras is non-NULL.
207 static int fec_read_bufs(struct dm_verity *v, struct dm_verity_io *io,
208 u64 rsb, u64 target, unsigned block_offset,
212 int i, j, target_index = -1;
213 struct dm_buffer *buf;
214 struct dm_bufio_client *bufio;
215 struct dm_verity_fec_io *fio = fec_io(io);
218 u8 want_digest[v->digest_size];
225 * read each of the rsn data blocks that are part of the RS block, and
226 * interleave contents to available bufs
228 for (i = 0; i < v->fec->rsn; i++) {
229 ileaved = fec_interleave(v, rsb * v->fec->rsn + i);
232 * target is the data block we want to correct, target_index is
233 * the index of this block within the rsn RS blocks
235 if (ileaved == target)
238 block = ileaved >> v->data_dev_block_bits;
239 bufio = v->fec->data_bufio;
241 if (block >= v->data_blocks) {
242 block -= v->data_blocks;
245 * blocks outside the area were assumed to contain
246 * zeros when encoding data was generated
248 if (unlikely(block >= v->fec->hash_blocks))
251 block += v->hash_start;
255 bbuf = dm_bufio_read(bufio, block, &buf);
256 if (unlikely(IS_ERR(bbuf))) {
257 DMWARN_LIMIT("%s: FEC %llu: read failed (%llu): %ld",
259 (unsigned long long)rsb,
260 (unsigned long long)block, PTR_ERR(bbuf));
262 /* assume the block is corrupted */
263 if (neras && *neras <= v->fec->roots)
264 fio->erasures[(*neras)++] = i;
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,
273 /* skip known zero blocks entirely */
278 * skip if we have already found the theoretical
279 * maximum number (i.e. fec->roots) of erasures
281 if (neras && *neras <= v->fec->roots &&
282 fec_is_erasure(v, io, want_digest, bbuf))
283 fio->erasures[(*neras)++] = i;
287 * deinterleave and copy the bytes that fit into bufs,
288 * starting from block_offset
290 fec_for_each_buffer_rs_block(fio, n, j) {
291 k = fec_buffer_rs_index(n, j) + block_offset;
293 if (k >= 1 << v->data_dev_block_bits)
296 rs_block = fec_buffer_rs_block(v, fio, n, j);
297 rs_block[i] = bbuf[k];
300 dm_bufio_release(buf);
307 * Allocate RS control structure and FEC buffers from preallocated mempools,
308 * and attempt to allocate as many extra buffers as available.
310 static int fec_alloc_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio)
315 fio->rs = mempool_alloc(v->fec->rs_pool, 0);
316 if (unlikely(!fio->rs)) {
317 DMERR("failed to allocate RS");
322 fec_for_each_prealloc_buffer(n) {
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");
333 /* try to allocate the maximum number of buffers */
334 fec_for_each_extra_buffer(fio, n) {
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]))
346 fio->output = mempool_alloc(v->fec->output_pool, GFP_NOIO);
349 DMERR("failed to allocate FEC page");
358 * Initialize buffers and clear erasures. fec_read_bufs() assumes buffers are
359 * zeroed before deinterleaving.
361 static void fec_init_bufs(struct dm_verity *v, struct dm_verity_fec_io *fio)
365 fec_for_each_buffer(fio, n)
366 memset(fio->bufs[n], 0, v->fec->rsn << DM_VERITY_FEC_BUF_RS_BITS);
368 memset(fio->erasures, 0, sizeof(fio->erasures));
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.
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,
383 r = fec_alloc_bufs(v, fio);
387 for (pos = 0; pos < 1 << v->data_dev_block_bits; ) {
388 fec_init_bufs(v, fio);
390 r = fec_read_bufs(v, io, rsb, offset, pos,
391 use_erasures ? &neras : NULL);
395 r = fec_decode_bufs(v, fio, rsb, r, pos, neras);
399 pos += fio->nbufs << DM_VERITY_FEC_BUF_RS_BITS;
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));
409 if (memcmp(verity_io_real_digest(v, io), verity_io_want_digest(v, io),
411 DMERR_LIMIT("%s: FEC %llu: failed to correct (%d erasures)",
412 v->data_dev->name, (unsigned long long)rsb, neras);
419 static int fec_bv_copy(struct dm_verity *v, struct dm_verity_io *io, u8 *data,
422 struct dm_verity_fec_io *fio = fec_io(io);
424 memcpy(data, &fio->output[fio->output_pos], len);
425 fio->output_pos += len;
431 * Correct errors in a block. Copies corrected block to dest if non-NULL,
432 * otherwise to a bio_vec starting from iter.
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 struct bvec_iter *iter)
439 struct dm_verity_fec_io *fio = fec_io(io);
440 u64 offset, res, rsb;
442 if (!verity_fec_is_enabled(v))
445 if (type == DM_VERITY_BLOCK_TYPE_METADATA)
446 block += v->data_blocks;
449 * For RS(M, N), the continuous FEC data is divided into blocks of N
450 * bytes. Since block size may not be divisible by N, the last block
451 * is zero padded when decoding.
453 * Each byte of the block is covered by a different RS(M, N) code,
454 * and each code is interleaved over N blocks to make it less likely
455 * that bursty corruption will leave us in unrecoverable state.
458 offset = block << v->data_dev_block_bits;
461 div64_u64(res, v->fec->rounds << v->data_dev_block_bits);
464 * The base RS block we can feed to the interleaver to find out all
465 * blocks required for decoding.
467 rsb = offset - res * (v->fec->rounds << v->data_dev_block_bits);
470 * Locating erasures is slow, so attempt to recover the block without
471 * them first. Do a second attempt with erasures if the corruption is
474 r = fec_decode_rsb(v, io, fio, rsb, offset, false);
476 r = fec_decode_rsb(v, io, fio, rsb, offset, true);
482 memcpy(dest, fio->output, 1 << v->data_dev_block_bits);
485 r = verity_for_bv_block(v, io, iter, fec_bv_copy);
492 * Clean up per-bio data.
494 void verity_fec_finish_io(struct dm_verity_io *io)
497 struct dm_verity_fec *f = io->v->fec;
498 struct dm_verity_fec_io *fio = fec_io(io);
500 if (!verity_fec_is_enabled(io->v))
503 mempool_free(fio->rs, f->rs_pool);
505 fec_for_each_prealloc_buffer(n)
506 mempool_free(fio->bufs[n], f->prealloc_pool);
508 fec_for_each_extra_buffer(fio, n)
509 mempool_free(fio->bufs[n], f->extra_pool);
511 mempool_free(fio->output, f->output_pool);
515 * Initialize per-bio data.
517 void verity_fec_init_io(struct dm_verity_io *io)
519 struct dm_verity_fec_io *fio = fec_io(io);
521 if (!verity_fec_is_enabled(io->v))
525 memset(fio->bufs, 0, sizeof(fio->bufs));
531 * Append feature arguments and values to the status table.
533 unsigned verity_fec_status_table(struct dm_verity *v, unsigned sz,
534 char *result, unsigned maxlen)
536 if (!verity_fec_is_enabled(v))
539 DMEMIT(" " DM_VERITY_OPT_FEC_DEV " %s "
540 DM_VERITY_OPT_FEC_BLOCKS " %llu "
541 DM_VERITY_OPT_FEC_START " %llu "
542 DM_VERITY_OPT_FEC_ROOTS " %d",
544 (unsigned long long)v->fec->blocks,
545 (unsigned long long)v->fec->start,
551 void verity_fec_dtr(struct dm_verity *v)
553 struct dm_verity_fec *f = v->fec;
554 struct kobject *kobj = &f->kobj_holder.kobj;
556 if (!verity_fec_is_enabled(v))
559 mempool_destroy(f->rs_pool);
560 mempool_destroy(f->prealloc_pool);
561 mempool_destroy(f->extra_pool);
562 kmem_cache_destroy(f->cache);
565 dm_bufio_client_destroy(f->data_bufio);
567 dm_bufio_client_destroy(f->bufio);
570 dm_put_device(v->ti, f->dev);
572 if (kobj->state_initialized) {
574 wait_for_completion(dm_get_completion_from_kobject(kobj));
582 static void *fec_rs_alloc(gfp_t gfp_mask, void *pool_data)
584 struct dm_verity *v = (struct dm_verity *)pool_data;
586 return init_rs(8, 0x11d, 0, 1, v->fec->roots);
589 static void fec_rs_free(void *element, void *pool_data)
591 struct rs_control *rs = (struct rs_control *)element;
597 bool verity_is_fec_opt_arg(const char *arg_name)
599 return (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_DEV) ||
600 !strcasecmp(arg_name, DM_VERITY_OPT_FEC_BLOCKS) ||
601 !strcasecmp(arg_name, DM_VERITY_OPT_FEC_START) ||
602 !strcasecmp(arg_name, DM_VERITY_OPT_FEC_ROOTS));
605 int verity_fec_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
606 unsigned *argc, const char *arg_name)
609 struct dm_target *ti = v->ti;
610 const char *arg_value;
611 unsigned long long num_ll;
616 ti->error = "FEC feature arguments require a value";
620 arg_value = dm_shift_arg(as);
623 if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_DEV)) {
624 r = dm_get_device(ti, arg_value, FMODE_READ, &v->fec->dev);
626 ti->error = "FEC device lookup failed";
630 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_BLOCKS)) {
631 if (sscanf(arg_value, "%llu%c", &num_ll, &dummy) != 1 ||
632 ((sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
633 >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll)) {
634 ti->error = "Invalid " DM_VERITY_OPT_FEC_BLOCKS;
637 v->fec->blocks = num_ll;
639 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_START)) {
640 if (sscanf(arg_value, "%llu%c", &num_ll, &dummy) != 1 ||
641 ((sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT)) >>
642 (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll)) {
643 ti->error = "Invalid " DM_VERITY_OPT_FEC_START;
646 v->fec->start = num_ll;
648 } else if (!strcasecmp(arg_name, DM_VERITY_OPT_FEC_ROOTS)) {
649 if (sscanf(arg_value, "%hhu%c", &num_c, &dummy) != 1 || !num_c ||
650 num_c < (DM_VERITY_FEC_RSM - DM_VERITY_FEC_MAX_RSN) ||
651 num_c > (DM_VERITY_FEC_RSM - DM_VERITY_FEC_MIN_RSN)) {
652 ti->error = "Invalid " DM_VERITY_OPT_FEC_ROOTS;
655 v->fec->roots = num_c;
658 ti->error = "Unrecognized verity FEC feature request";
665 static ssize_t corrected_show(struct kobject *kobj, struct kobj_attribute *attr,
668 struct dm_verity_fec *f = container_of(kobj, struct dm_verity_fec,
671 return sprintf(buf, "%d\n", atomic_read(&f->corrected));
674 static struct kobj_attribute attr_corrected = __ATTR_RO(corrected);
676 static struct attribute *fec_attrs[] = {
677 &attr_corrected.attr,
681 static struct kobj_type fec_ktype = {
682 .sysfs_ops = &kobj_sysfs_ops,
683 .default_attrs = fec_attrs
687 * Allocate dm_verity_fec for v->fec. Must be called before verity_fec_ctr.
689 int verity_fec_ctr_alloc(struct dm_verity *v)
691 struct dm_verity_fec *f;
693 f = kzalloc(sizeof(struct dm_verity_fec), GFP_KERNEL);
695 v->ti->error = "Cannot allocate FEC structure";
704 * Validate arguments and preallocate memory. Must be called after arguments
705 * have been parsed using verity_fec_parse_opt_args.
707 int verity_fec_ctr(struct dm_verity *v)
710 struct dm_verity_fec *f = v->fec;
711 struct dm_target *ti = v->ti;
712 struct mapped_device *md = dm_table_get_md(ti->table);
715 if (!verity_fec_is_enabled(v)) {
720 /* Create a kobject and sysfs attributes */
721 init_completion(&f->kobj_holder.completion);
723 r = kobject_init_and_add(&f->kobj_holder.kobj, &fec_ktype,
724 &disk_to_dev(dm_disk(md))->kobj, "%s", "fec");
726 ti->error = "Cannot create kobject";
731 * FEC is computed over data blocks, possible metadata, and
732 * hash blocks. In other words, FEC covers total of fec_blocks
733 * blocks consisting of the following:
735 * data blocks | hash blocks | metadata (optional)
737 * We allow metadata after hash blocks to support a use case
738 * where all data is stored on the same device and FEC covers
741 * If metadata is included, we require it to be available on the
742 * hash device after the hash blocks.
745 hash_blocks = v->hash_blocks - v->hash_start;
748 * Require matching block sizes for data and hash devices for
751 if (v->data_dev_block_bits != v->hash_dev_block_bits) {
752 ti->error = "Block sizes must match to use FEC";
757 ti->error = "Missing " DM_VERITY_OPT_FEC_ROOTS;
760 f->rsn = DM_VERITY_FEC_RSM - f->roots;
763 ti->error = "Missing " DM_VERITY_OPT_FEC_BLOCKS;
767 f->rounds = f->blocks;
768 if (sector_div(f->rounds, f->rsn))
772 * Due to optional metadata, f->blocks can be larger than
773 * data_blocks and hash_blocks combined.
775 if (f->blocks < v->data_blocks + hash_blocks || !f->rounds) {
776 ti->error = "Invalid " DM_VERITY_OPT_FEC_BLOCKS;
781 * Metadata is accessed through the hash device, so we require
782 * it to be large enough.
784 f->hash_blocks = f->blocks - v->data_blocks;
785 if (dm_bufio_get_device_size(v->bufio) < f->hash_blocks) {
786 ti->error = "Hash device is too small for "
787 DM_VERITY_OPT_FEC_BLOCKS;
791 f->bufio = dm_bufio_client_create(f->dev->bdev,
792 1 << v->data_dev_block_bits,
794 if (IS_ERR(f->bufio)) {
795 ti->error = "Cannot initialize FEC bufio client";
796 return PTR_ERR(f->bufio);
799 if (dm_bufio_get_device_size(f->bufio) <
800 ((f->start + f->rounds * f->roots) >> v->data_dev_block_bits)) {
801 ti->error = "FEC device is too small";
805 f->data_bufio = dm_bufio_client_create(v->data_dev->bdev,
806 1 << v->data_dev_block_bits,
808 if (IS_ERR(f->data_bufio)) {
809 ti->error = "Cannot initialize FEC data bufio client";
810 return PTR_ERR(f->data_bufio);
813 if (dm_bufio_get_device_size(f->data_bufio) < v->data_blocks) {
814 ti->error = "Data device is too small";
818 /* Preallocate an rs_control structure for each worker thread */
819 f->rs_pool = mempool_create(num_online_cpus(), fec_rs_alloc,
820 fec_rs_free, (void *) v);
822 ti->error = "Cannot allocate RS pool";
826 f->cache = kmem_cache_create("dm_verity_fec_buffers",
827 f->rsn << DM_VERITY_FEC_BUF_RS_BITS,
830 ti->error = "Cannot create FEC buffer cache";
834 /* Preallocate DM_VERITY_FEC_BUF_PREALLOC buffers for each thread */
835 f->prealloc_pool = mempool_create_slab_pool(num_online_cpus() *
836 DM_VERITY_FEC_BUF_PREALLOC,
838 if (!f->prealloc_pool) {
839 ti->error = "Cannot allocate FEC buffer prealloc pool";
843 f->extra_pool = mempool_create_slab_pool(0, f->cache);
844 if (!f->extra_pool) {
845 ti->error = "Cannot allocate FEC buffer extra pool";
849 /* Preallocate an output buffer for each thread */
850 f->output_pool = mempool_create_kmalloc_pool(num_online_cpus(),
851 1 << v->data_dev_block_bits);
852 if (!f->output_pool) {
853 ti->error = "Cannot allocate FEC output pool";
857 /* Reserve space for our per-bio data */
858 ti->per_bio_data_size += sizeof(struct dm_verity_fec_io);