import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ubifs / compress.c
1 /*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 * Copyright (C) 2006, 2007 University of Szeged, Hungary
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors: Adrian Hunter
21 * Artem Bityutskiy (Битюцкий Артём)
22 * Zoltan Sogor
23 */
24
25 /*
26 * This file provides a single place to access to compression and
27 * decompression.
28 */
29
30 #include <linux/crypto.h>
31 #include "ubifs.h"
32
33 /* Fake description object for the "none" compressor */
34 static 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
41 static DEFINE_MUTEX(lzo_mutex);
42
43 static 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
50 static struct ubifs_compressor lzo_compr = {
51 .compr_type = UBIFS_COMPR_LZO,
52 .name = "lzo",
53 };
54 #endif
55
56 #if defined(CONFIG_UBIFS_FS_LZ4K)
57
58 static DEFINE_MUTEX(lz4k_mutex);
59
60
61 static struct ubifs_compressor lz4k_compr = {
62 .compr_type = UBIFS_COMPR_LZ4K,
63 .comp_mutex = &lz4k_mutex,
64 .name = "lz4k",
65 .capi_name = "lz4k",
66 };
67
68 #else
69 static struct ubifs_compressor lz4k_compr = {
70 .compr_type = UBIFS_COMPR_LZ4K,
71 .name = "lz4k",
72 };
73
74 #endif
75
76
77 #ifdef CONFIG_UBIFS_FS_ZLIB
78 static DEFINE_MUTEX(deflate_mutex);
79 static DEFINE_MUTEX(inflate_mutex);
80
81 static struct ubifs_compressor zlib_compr = {
82 .compr_type = UBIFS_COMPR_ZLIB,
83 .comp_mutex = &deflate_mutex,
84 .decomp_mutex = &inflate_mutex,
85 .name = "zlib",
86 .capi_name = "deflate",
87 };
88 #else
89 static struct ubifs_compressor zlib_compr = {
90 .compr_type = UBIFS_COMPR_ZLIB,
91 .name = "zlib",
92 };
93 #endif
94
95 /* All UBIFS compressors */
96 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
97
98 /**
99 * ubifs_compress - compress data.
100 * @in_buf: data to compress
101 * @in_len: length of the data to compress
102 * @out_buf: output buffer where compressed data should be stored
103 * @out_len: output buffer length is returned here
104 * @compr_type: type of compression to use on enter, actually used compression
105 * type on exit
106 *
107 * This function compresses input buffer @in_buf of length @in_len and stores
108 * the result in the output buffer @out_buf and the resulting length in
109 * @out_len. If the input buffer does not compress, it is just copied to the
110 * @out_buf. The same happens if @compr_type is %UBIFS_COMPR_NONE or if
111 * compression error occurred.
112 *
113 * Note, if the input buffer was not compressed, it is copied to the output
114 * buffer and %UBIFS_COMPR_NONE is returned in @compr_type.
115 */
116 void ubifs_compress(const void *in_buf, int in_len, void *out_buf, int *out_len,
117 int *compr_type)
118 {
119 int err;
120 struct ubifs_compressor *compr = ubifs_compressors[*compr_type];
121
122 if (*compr_type == UBIFS_COMPR_NONE)
123 goto no_compr;
124
125 /* If the input data is small, do not even try to compress it */
126 if (in_len < UBIFS_MIN_COMPR_LEN)
127 goto no_compr;
128
129 if (compr->comp_mutex)
130 mutex_lock(compr->comp_mutex);
131 err = crypto_comp_compress(compr->cc, in_buf, in_len, out_buf,
132 (unsigned int *)out_len);
133 if (compr->comp_mutex)
134 mutex_unlock(compr->comp_mutex);
135 if (unlikely(err)) {
136 ubifs_warn("cannot compress %d bytes, compressor %s, error %d, leave data uncompressed",
137 in_len, compr->name, err);
138 goto no_compr;
139 }
140
141 /*
142 * If the data compressed only slightly, it is better to leave it
143 * uncompressed to improve read speed.
144 */
145 if (in_len - *out_len < UBIFS_MIN_COMPRESS_DIFF)
146 goto no_compr;
147
148 return;
149
150 no_compr:
151 memcpy(out_buf, in_buf, in_len);
152 *out_len = in_len;
153 *compr_type = UBIFS_COMPR_NONE;
154 }
155
156 /**
157 * ubifs_decompress - decompress data.
158 * @in_buf: data to decompress
159 * @in_len: length of the data to decompress
160 * @out_buf: output buffer where decompressed data should
161 * @out_len: output length is returned here
162 * @compr_type: type of compression
163 *
164 * This function decompresses data from buffer @in_buf into buffer @out_buf.
165 * The length of the uncompressed data is returned in @out_len. This functions
166 * returns %0 on success or a negative error code on failure.
167 */
168 int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
169 int *out_len, int compr_type)
170 {
171 int err;
172 struct ubifs_compressor *compr;
173
174 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
175 ubifs_err("invalid compression type %d", compr_type);
176 return -EINVAL;
177 }
178
179 compr = ubifs_compressors[compr_type];
180
181 if (unlikely(!compr->capi_name)) {
182 ubifs_err("%s compression is not compiled in", compr->name);
183 return -EINVAL;
184 }
185
186 if (compr_type == UBIFS_COMPR_NONE) {
187 memcpy(out_buf, in_buf, in_len);
188 *out_len = in_len;
189 return 0;
190 }
191
192 if (compr->decomp_mutex)
193 mutex_lock(compr->decomp_mutex);
194 err = crypto_comp_decompress(compr->cc, in_buf, in_len, out_buf,
195 (unsigned int *)out_len);
196 if (compr->decomp_mutex)
197 mutex_unlock(compr->decomp_mutex);
198 if (err)
199 ubifs_err("cannot decompress %d bytes, compressor %s, error %d",
200 in_len, compr->name, err);
201
202 return err;
203 }
204
205 /**
206 * compr_init - initialize a compressor.
207 * @compr: compressor description object
208 *
209 * This function initializes the requested compressor and returns zero in case
210 * of success or a negative error code in case of failure.
211 */
212 static int __init compr_init(struct ubifs_compressor *compr)
213 {
214 if (compr->capi_name) {
215 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
216 if (IS_ERR(compr->cc)) {
217 ubifs_err("cannot initialize compressor %s, error %ld",
218 compr->name, PTR_ERR(compr->cc));
219 return PTR_ERR(compr->cc);
220 }
221 }
222
223 ubifs_compressors[compr->compr_type] = compr;
224 return 0;
225 }
226
227 /**
228 * compr_exit - de-initialize a compressor.
229 * @compr: compressor description object
230 */
231 static void compr_exit(struct ubifs_compressor *compr)
232 {
233 if (compr->capi_name)
234 crypto_free_comp(compr->cc);
235 return;
236 }
237
238 /**
239 * ubifs_compressors_init - initialize UBIFS compressors.
240 *
241 * This function initializes the compressor which were compiled in. Returns
242 * zero in case of success and a negative error code in case of failure.
243 */
244 int __init ubifs_compressors_init(void)
245 {
246 int err;
247
248 err = compr_init(&lzo_compr);
249 if (err)
250 return err;
251
252 err = compr_init(&zlib_compr);
253 if (err)
254 goto out_lzo;
255
256
257 err = compr_init(&lz4k_compr);
258 if (err)
259 goto out_lzo;
260
261 ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr;
262 return 0;
263
264 out_lzo:
265 compr_exit(&lzo_compr);
266
267 compr_exit(&lz4k_compr);
268
269
270 return err;
271 }
272
273 /**
274 * ubifs_compressors_exit - de-initialize UBIFS compressors.
275 */
276 void ubifs_compressors_exit(void)
277 {
278 compr_exit(&lzo_compr);
279 compr_exit(&zlib_compr);
280 }