1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/crypto.h>
31#include "ubifs.h"
32
33
34static struct ubifs_compressor none_compr = {
35 .compr_type = UBIFS_COMPR_NONE,
36 .name = "no compression",
37 .capi_name = "",
38};
39
40#ifdef CONFIG_UBIFS_FS_LZO
41static DEFINE_MUTEX(lzo_mutex);
42
43static struct ubifs_compressor lzo_compr = {
44 .compr_type = UBIFS_COMPR_LZO,
45 .comp_mutex = &lzo_mutex,
46 .name = "LZO",
47 .capi_name = "lzo",
48};
49#else
50static struct ubifs_compressor lzo_compr = {
51 .compr_type = UBIFS_COMPR_LZO,
52 .name = "LZO",
53};
54#endif
55
56#ifdef CONFIG_UBIFS_FS_ZLIB
57static DEFINE_MUTEX(deflate_mutex);
58static DEFINE_MUTEX(inflate_mutex);
59
60static struct ubifs_compressor zlib_compr = {
61 .compr_type = UBIFS_COMPR_ZLIB,
62 .comp_mutex = &deflate_mutex,
63 .decomp_mutex = &inflate_mutex,
64 .name = "zlib",
65 .capi_name = "deflate",
66};
67#else
68static struct ubifs_compressor zlib_compr = {
69 .compr_type = UBIFS_COMPR_ZLIB,
70 .name = "zlib",
71};
72#endif
73
74
75struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len,
96 int *compr_type)
97{
98 int err;
99 struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
100
101 if (*compr_type == UBIFS_COMPR_NONE)
102 goto no_compr;
103
104
105 if (in_len < UBIFS_MIN_COMPR_LEN)
106 goto no_compr;
107
108 if (compr->comp_mutex)
109 mutex_lock(compr->comp_mutex);
110 err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf,
111 out_len);
112 if (compr->comp_mutex)
113 mutex_unlock(compr->comp_mutex);
114 if (unlikely(err)) {
115 ubifs_warn("cannot compress %d bytes, compressor %s, "
116 "error %d, leave data uncompressed",
117 in_len, compr->name, err);
118 goto no_compr;
119 }
120
121
122
123
124
125 if (ALIGN(*out_len, 8) >= ALIGN(in_len, 8))
126 goto no_compr;
127
128 return;
129
130no_compr:
131 memcpy(out_buf, in_buf, in_len);
132 *out_len = in_len;
133 *compr_type = UBIFS_COMPR_NONE;
134}
135
136
137
138
139
140
141
142
143
144
145
146
147
148int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
149 int *out_len, int compr_type)
150{
151 int err;
152 struct ubifs_compressor *compr;
153
154 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
155 ubifs_err("invalid compression type %d", compr_type);
156 return -EINVAL;
157 }
158
159 compr = ubifs_compressors[compr_type];
160
161 if (unlikely(!compr->capi_name)) {
162 ubifs_err("%s compression is not compiled in", compr->name);
163 return -EINVAL;
164 }
165
166 if (compr_type == UBIFS_COMPR_NONE) {
167 memcpy(out_buf, in_buf, in_len);
168 *out_len = in_len;
169 return 0;
170 }
171
172 if (compr->decomp_mutex)
173 mutex_lock(compr->decomp_mutex);
174 err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
175 out_len);
176 if (compr->decomp_mutex)
177 mutex_unlock(compr->decomp_mutex);
178 if (err)
179 ubifs_err("cannot decompress %d bytes, compressor %s, "
180 "error %d", in_len, compr->name, err);
181
182 return err;
183}
184
185
186
187
188
189
190
191
192static int __init compr_init(struct ubifs_compressor *compr)
193{
194 if (compr->capi_name) {
195 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
196 if (IS_ERR(compr->cc)) {
197 ubifs_err("cannot initialize compressor %s, error %ld",
198 compr->name, PTR_ERR(compr->cc));
199 return PTR_ERR(compr->cc);
200 }
201 }
202
203 ubifs_compressors[compr->compr_type] = compr;
204 return 0;
205}
206
207
208
209
210
211static void compr_exit(struct ubifs_compressor *compr)
212{
213 if (compr->capi_name)
214 crypto_free_comp(compr->cc);
215 return;
216}
217
218
219
220
221
222
223
224int __init ubifs_compressors_init(void)
225{
226 int err;
227
228 err = compr_init(&lzo_compr);
229 if (err)
230 return err;
231
232 err = compr_init(&zlib_compr);
233 if (err)
234 goto out_lzo;
235
236 ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr;
237 return 0;
238
239out_lzo:
240 compr_exit(&lzo_compr);
241 return err;
242}
243
244
245
246
247void __exit ubifs_compressors_exit(void)
248{
249 compr_exit(&lzo_compr);
250 compr_exit(&zlib_compr);
251}