Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 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 | #ifndef DM_VERITY_FEC_H |
| 13 | #define DM_VERITY_FEC_H |
| 14 | |
Sami Tolvanen | dc99440 | 2016-03-30 14:10:13 -0700 | [diff] [blame] | 15 | #include "dm.h" |
| 16 | #include "dm-core.h" |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 17 | #include "dm-verity.h" |
| 18 | #include <linux/rslib.h> |
| 19 | |
| 20 | /* Reed-Solomon(M, N) parameters */ |
| 21 | #define DM_VERITY_FEC_RSM 255 |
| 22 | #define DM_VERITY_FEC_MAX_RSN 253 |
| 23 | #define DM_VERITY_FEC_MIN_RSN 231 /* ~10% space overhead */ |
| 24 | |
| 25 | /* buffers for deinterleaving and decoding */ |
| 26 | #define DM_VERITY_FEC_BUF_PREALLOC 1 /* buffers to preallocate */ |
| 27 | #define DM_VERITY_FEC_BUF_RS_BITS 4 /* 1 << RS blocks per buffer */ |
| 28 | /* we need buffers for at most 1 << block size RS blocks */ |
| 29 | #define DM_VERITY_FEC_BUF_MAX \ |
| 30 | (1 << (PAGE_SHIFT - DM_VERITY_FEC_BUF_RS_BITS)) |
| 31 | |
Sami Tolvanen | 4902970 | 2016-06-03 14:06:14 -0700 | [diff] [blame] | 32 | /* maximum recursion level for verity_fec_decode */ |
| 33 | #define DM_VERITY_FEC_MAX_RECURSION 4 |
| 34 | |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 35 | #define DM_VERITY_OPT_FEC_DEV "use_fec_from_device" |
| 36 | #define DM_VERITY_OPT_FEC_BLOCKS "fec_blocks" |
| 37 | #define DM_VERITY_OPT_FEC_START "fec_start" |
| 38 | #define DM_VERITY_OPT_FEC_ROOTS "fec_roots" |
| 39 | |
| 40 | /* configuration */ |
| 41 | struct dm_verity_fec { |
| 42 | struct dm_dev *dev; /* parity data device */ |
| 43 | struct dm_bufio_client *data_bufio; /* for data dev access */ |
| 44 | struct dm_bufio_client *bufio; /* for parity data access */ |
| 45 | sector_t start; /* parity data start in blocks */ |
| 46 | sector_t blocks; /* number of blocks covered */ |
| 47 | sector_t rounds; /* number of interleaving rounds */ |
| 48 | sector_t hash_blocks; /* blocks covered after v->hash_start */ |
| 49 | unsigned char roots; /* number of parity bytes, M-N of RS(M, N) */ |
| 50 | unsigned char rsn; /* N of RS(M, N) */ |
| 51 | mempool_t *rs_pool; /* mempool for fio->rs */ |
| 52 | mempool_t *prealloc_pool; /* mempool for preallocated buffers */ |
| 53 | mempool_t *extra_pool; /* mempool for extra buffers */ |
| 54 | mempool_t *output_pool; /* mempool for output */ |
| 55 | struct kmem_cache *cache; /* cache for buffers */ |
Sami Tolvanen | dc99440 | 2016-03-30 14:10:13 -0700 | [diff] [blame] | 56 | atomic_t corrected; /* corrected errors */ |
| 57 | struct dm_kobject_holder kobj_holder; /* for sysfs attributes */ |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /* per-bio data */ |
| 61 | struct dm_verity_fec_io { |
| 62 | struct rs_control *rs; /* Reed-Solomon state */ |
| 63 | int erasures[DM_VERITY_FEC_MAX_RSN]; /* erasures for decode_rs8 */ |
| 64 | u8 *bufs[DM_VERITY_FEC_BUF_MAX]; /* bufs for deinterleaving */ |
| 65 | unsigned nbufs; /* number of buffers allocated */ |
| 66 | u8 *output; /* buffer for corrected output */ |
| 67 | size_t output_pos; |
Sami Tolvanen | 4902970 | 2016-06-03 14:06:14 -0700 | [diff] [blame] | 68 | unsigned level; /* recursion level */ |
Sami Tolvanen | a739ff3 | 2015-12-03 14:26:30 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | #ifdef CONFIG_DM_VERITY_FEC |
| 72 | |
| 73 | /* each feature parameter requires a value */ |
| 74 | #define DM_VERITY_OPTS_FEC 8 |
| 75 | |
| 76 | extern bool verity_fec_is_enabled(struct dm_verity *v); |
| 77 | |
| 78 | extern int verity_fec_decode(struct dm_verity *v, struct dm_verity_io *io, |
| 79 | enum verity_block_type type, sector_t block, |
| 80 | u8 *dest, struct bvec_iter *iter); |
| 81 | |
| 82 | extern unsigned verity_fec_status_table(struct dm_verity *v, unsigned sz, |
| 83 | char *result, unsigned maxlen); |
| 84 | |
| 85 | extern void verity_fec_finish_io(struct dm_verity_io *io); |
| 86 | extern void verity_fec_init_io(struct dm_verity_io *io); |
| 87 | |
| 88 | extern bool verity_is_fec_opt_arg(const char *arg_name); |
| 89 | extern int verity_fec_parse_opt_args(struct dm_arg_set *as, |
| 90 | struct dm_verity *v, unsigned *argc, |
| 91 | const char *arg_name); |
| 92 | |
| 93 | extern void verity_fec_dtr(struct dm_verity *v); |
| 94 | |
| 95 | extern int verity_fec_ctr_alloc(struct dm_verity *v); |
| 96 | extern int verity_fec_ctr(struct dm_verity *v); |
| 97 | |
| 98 | #else /* !CONFIG_DM_VERITY_FEC */ |
| 99 | |
| 100 | #define DM_VERITY_OPTS_FEC 0 |
| 101 | |
| 102 | static inline bool verity_fec_is_enabled(struct dm_verity *v) |
| 103 | { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | static inline int verity_fec_decode(struct dm_verity *v, |
| 108 | struct dm_verity_io *io, |
| 109 | enum verity_block_type type, |
| 110 | sector_t block, u8 *dest, |
| 111 | struct bvec_iter *iter) |
| 112 | { |
| 113 | return -EOPNOTSUPP; |
| 114 | } |
| 115 | |
| 116 | static inline unsigned verity_fec_status_table(struct dm_verity *v, |
| 117 | unsigned sz, char *result, |
| 118 | unsigned maxlen) |
| 119 | { |
| 120 | return sz; |
| 121 | } |
| 122 | |
| 123 | static inline void verity_fec_finish_io(struct dm_verity_io *io) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | static inline void verity_fec_init_io(struct dm_verity_io *io) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | static inline bool verity_is_fec_opt_arg(const char *arg_name) |
| 132 | { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | static inline int verity_fec_parse_opt_args(struct dm_arg_set *as, |
| 137 | struct dm_verity *v, |
| 138 | unsigned *argc, |
| 139 | const char *arg_name) |
| 140 | { |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | static inline void verity_fec_dtr(struct dm_verity *v) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | static inline int verity_fec_ctr_alloc(struct dm_verity *v) |
| 149 | { |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static inline int verity_fec_ctr(struct dm_verity *v) |
| 154 | { |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | #endif /* CONFIG_DM_VERITY_FEC */ |
| 159 | |
| 160 | #endif /* DM_VERITY_FEC_H */ |