blob: 565cb56d72257cc5f4015132f3d8749b44d3a941 [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
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 */
34static struct ubifs_compressor none_compr = {
35 .compr_type = UBIFS_COMPR_NONE,
Artem Bityutskiy553dea42008-11-01 14:57:49 +020036 .name = "none",
Artem Bityutskiy1e517642008-07-14 19:08:37 +030037 .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,
Artem Bityutskiy553dea42008-11-01 14:57:49 +020046 .name = "lzo",
Artem Bityutskiy1e517642008-07-14 19:08:37 +030047 .capi_name = "lzo",
48};
49#else
50static struct ubifs_compressor lzo_compr = {
51 .compr_type = UBIFS_COMPR_LZO,
Artem Bityutskiy553dea42008-11-01 14:57:49 +020052 .name = "lzo",
Artem Bityutskiy1e517642008-07-14 19:08:37 +030053};
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/* All UBIFS compressors */
75struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
76
77/**
78 * ubifs_compress - compress data.
79 * @in_buf: data to compress
80 * @in_len: length of the data to compress
81 * @out_buf: output buffer where compressed data should be stored
82 * @out_len: output buffer length is returned here
83 * @compr_type: type of compression to use on enter, actually used compression
84 * type on exit
85 *
86 * This function compresses input buffer @in_buf of length @in_len and stores
87 * the result in the output buffer @out_buf and the resulting length in
88 * @out_len. If the input buffer does not compress, it is just copied to the
89 * @out_buf. The same happens if @compr_type is %UBIFS_COMPR_NONE or if
90 * compression error occurred.
91 *
92 * Note, if the input buffer was not compressed, it is copied to the output
93 * buffer and %UBIFS_COMPR_NONE is returned in @compr_type.
Artem Bityutskiy1e517642008-07-14 19:08:37 +030094 */
Sheng Yong235c3622015-03-20 10:39:42 +000095void ubifs_compress(const struct ubifs_info *c, const void *in_buf,
96 int in_len, void *out_buf, int *out_len, int *compr_type)
Artem Bityutskiy1e517642008-07-14 19:08:37 +030097{
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 /* If the input data is small, do not even try to compress it */
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,
Artem Bityutskiy6a4a9b42008-12-28 11:00:55 +0200111 (unsigned int *)out_len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300112 if (compr->comp_mutex)
113 mutex_unlock(compr->comp_mutex);
114 if (unlikely(err)) {
Sheng Yong235c3622015-03-20 10:39:42 +0000115 ubifs_warn(c, "cannot compress %d bytes, compressor %s, error %d, leave data uncompressed",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300116 in_len, compr->name, err);
Sheng Yong235c3622015-03-20 10:39:42 +0000117 goto no_compr;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300118 }
119
120 /*
Artem Bityutskiy062e4fe2008-10-26 16:58:25 +0200121 * If the data compressed only slightly, it is better to leave it
122 * uncompressed to improve read speed.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300123 */
Artem Bityutskiy062e4fe2008-10-26 16:58:25 +0200124 if (in_len - *out_len < UBIFS_MIN_COMPRESS_DIFF)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300125 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 * ubifs_decompress - decompress data.
137 * @in_buf: data to decompress
138 * @in_len: length of the data to decompress
139 * @out_buf: output buffer where decompressed data should
140 * @out_len: output length is returned here
141 * @compr_type: type of compression
142 *
143 * This function decompresses data from buffer @in_buf into buffer @out_buf.
144 * The length of the uncompressed data is returned in @out_len. This functions
145 * returns %0 on success or a negative error code on failure.
146 */
Sheng Yong235c3622015-03-20 10:39:42 +0000147int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
148 int in_len, void *out_buf, int *out_len, int compr_type)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300149{
150 int err;
151 struct ubifs_compressor *compr;
152
153 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
Sheng Yong235c3622015-03-20 10:39:42 +0000154 ubifs_err(c, "invalid compression type %d", compr_type);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300155 return -EINVAL;
156 }
157
158 compr = ubifs_compressors[compr_type];
159
160 if (unlikely(!compr->capi_name)) {
Sheng Yong235c3622015-03-20 10:39:42 +0000161 ubifs_err(c, "%s compression is not compiled in", compr->name);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300162 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,
Artem Bityutskiy6a4a9b42008-12-28 11:00:55 +0200174 (unsigned int *)out_len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300175 if (compr->decomp_mutex)
176 mutex_unlock(compr->decomp_mutex);
177 if (err)
Sheng Yong235c3622015-03-20 10:39:42 +0000178 ubifs_err(c, "cannot decompress %d bytes, compressor %s, error %d",
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300179 in_len, compr->name, err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300180
181 return err;
182}
183
184/**
185 * compr_init - initialize a compressor.
186 * @compr: compressor description object
187 *
188 * This function initializes the requested compressor and returns zero in case
189 * of success or a negative error code in case of failure.
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)) {
Sheng Yong235c3622015-03-20 10:39:42 +0000196 pr_err("UBIFS error (pid %d): cannot initialize compressor %s, error %ld",
197 current->pid, compr->name, PTR_ERR(compr->cc));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300198 return PTR_ERR(compr->cc);
199 }
200 }
201
202 ubifs_compressors[compr->compr_type] = compr;
203 return 0;
204}
205
206/**
207 * compr_exit - de-initialize a compressor.
208 * @compr: compressor description object
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 * ubifs_compressors_init - initialize UBIFS compressors.
219 *
220 * This function initializes the compressor which were compiled in. Returns
221 * zero in case of success and a negative error code in case of failure.
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 * ubifs_compressors_exit - de-initialize UBIFS compressors.
245 */
Alexey Dobriyan995be042008-12-04 17:04:18 +0300246void ubifs_compressors_exit(void)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300247{
248 compr_exit(&lzo_compr);
249 compr_exit(&zlib_compr);
250}