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 = "none",
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 struct ubifs_info *c, const void *in_buf,
96 int in_len, void *out_buf, int *out_len, 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 (unsigned int *)out_len);
112 if (compr->comp_mutex)
113 mutex_unlock(compr->comp_mutex);
114 if (unlikely(err)) {
115 ubifs_warn(c, "cannot compress %d bytes, compressor %s, error %d, leave data uncompressed",
116 in_len, compr->name, err);
117 goto no_compr;
118 }
119
120
121
122
123
124 if (in_len - *out_len < UBIFS_MIN_COMPRESS_DIFF)
125 goto no_compr;
126
127 return;
128
129no_compr:
130 memcpy(out_buf, in_buf, in_len);
131 *out_len = in_len;
132 *compr_type = UBIFS_COMPR_NONE;
133}
134
135
136
137
138
139
140
141
142
143
144
145
146
147int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
148 int in_len, void *out_buf, int *out_len, int compr_type)
149{
150 int err;
151 struct ubifs_compressor *compr;
152
153 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
154 ubifs_err(c, "invalid compression type %d", compr_type);
155 return -EINVAL;
156 }
157
158 compr = ubifs_compressors[compr_type];
159
160 if (unlikely(!compr->capi_name)) {
161 ubifs_err(c, "%s compression is not compiled in", compr->name);
162 return -EINVAL;
163 }
164
165 if (compr_type == UBIFS_COMPR_NONE) {
166 memcpy(out_buf, in_buf, in_len);
167 *out_len = in_len;
168 return 0;
169 }
170
171 if (compr->decomp_mutex)
172 mutex_lock(compr->decomp_mutex);
173 err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
174 (unsigned int *)out_len);
175 if (compr->decomp_mutex)
176 mutex_unlock(compr->decomp_mutex);
177 if (err)
178 ubifs_err(c, "cannot decompress %d bytes, compressor %s, error %d",
179 in_len, compr->name, err);
180
181 return err;
182}
183
184
185
186
187
188
189
190
191static int __init compr_init(struct ubifs_compressor *compr)
192{
193 if (compr->capi_name) {
194 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
195 if (IS_ERR(compr->cc)) {
196 pr_err("UBIFS error (pid %d): cannot initialize compressor %s, error %ld",
197 current->pid, compr->name, PTR_ERR(compr->cc));
198 return PTR_ERR(compr->cc);
199 }
200 }
201
202 ubifs_compressors[compr->compr_type] = compr;
203 return 0;
204}
205
206
207
208
209
210static void compr_exit(struct ubifs_compressor *compr)
211{
212 if (compr->capi_name)
213 crypto_free_comp(compr->cc);
214 return;
215}
216
217
218
219
220
221
222
223int __init ubifs_compressors_init(void)
224{
225 int err;
226
227 err = compr_init(&lzo_compr);
228 if (err)
229 return err;
230
231 err = compr_init(&zlib_compr);
232 if (err)
233 goto out_lzo;
234
235 ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr;
236 return 0;
237
238out_lzo:
239 compr_exit(&lzo_compr);
240 return err;
241}
242
243
244
245
246void ubifs_compressors_exit(void)
247{
248 compr_exit(&lzo_compr);
249 compr_exit(&zlib_compr);
250}
251