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