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