xref: /freebsd/sys/contrib/openzfs/module/os/linux/spl/spl-zlib.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*
2  *  Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3  *  Copyright (C) 2007 The Regents of the University of California.
4  *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5  *  Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6  *  UCRL-CODE-235197
7  *
8  *  This file is part of the SPL, Solaris Porting Layer.
9  *
10  *  The SPL is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License as published by the
12  *  Free Software Foundation; either version 2 of the License, or (at your
13  *  option) any later version.
14  *
15  *  The SPL is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18  *  for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with the SPL.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  *
24  *  z_compress_level/z_uncompress are nearly identical copies of the
25  *  compress2/uncompress functions provided by the official zlib package
26  *  available at http://zlib.net/.  The only changes made we to slightly
27  *  adapt the functions called to match the linux kernel implementation
28  *  of zlib.  The full zlib license follows:
29  *
30  *  zlib.h -- interface of the 'zlib' general purpose compression library
31  *  version 1.2.5, April 19th, 2010
32  *
33  *  Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
34  *
35  *  This software is provided 'as-is', without any express or implied
36  *  warranty.  In no event will the authors be held liable for any damages
37  *  arising from the use of this software.
38  *
39  *  Permission is granted to anyone to use this software for any purpose,
40  *  including commercial applications, and to alter it and redistribute it
41  *  freely, subject to the following restrictions:
42  *
43  *  1. The origin of this software must not be misrepresented; you must not
44  *     claim that you wrote the original software. If you use this software
45  *     in a product, an acknowledgment in the product documentation would be
46  *     appreciated but is not required.
47  *  2. Altered source versions must be plainly marked as such, and must not be
48  *     misrepresented as being the original software.
49  *  3. This notice may not be removed or altered from any source distribution.
50  *
51  *  Jean-loup Gailly
52  *  Mark Adler
53  */
54 
55 
56 #include <linux/percpu_compat.h>
57 #include <sys/kmem.h>
58 #include <sys/kmem_cache.h>
59 #include <sys/zmod.h>
60 
61 static spl_kmem_cache_t *zlib_workspace_cache;
62 
63 /*
64  * A kmem_cache is used for the zlib workspaces to avoid having to vmalloc
65  * and vfree for every call.  Using a kmem_cache also has the advantage
66  * that improves the odds that the memory used will be local to this cpu.
67  * To further improve things it might be wise to create a dedicated per-cpu
68  * workspace for use.  This would take some additional care because we then
69  * must disable preemption around the critical section, and verify that
70  * zlib_deflate* and zlib_inflate* never internally call schedule().
71  */
72 static void *
73 zlib_workspace_alloc(int flags)
74 {
75 	return (kmem_cache_alloc(zlib_workspace_cache, flags & ~(__GFP_FS)));
76 }
77 
78 static void
79 zlib_workspace_free(void *workspace)
80 {
81 	kmem_cache_free(zlib_workspace_cache, workspace);
82 }
83 
84 /*
85  * Compresses the source buffer into the destination buffer. The level
86  * parameter has the same meaning as in deflateInit.  sourceLen is the byte
87  * length of the source buffer. Upon entry, destLen is the total size of the
88  * destination buffer, which must be at least 0.1% larger than sourceLen plus
89  * 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
90  *
91  * compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
92  * memory, Z_BUF_ERROR if there was not enough room in the output buffer,
93  * Z_STREAM_ERROR if the level parameter is invalid.
94  */
95 int
96 z_compress_level(void *dest, size_t *destLen, const void *source,
97     size_t sourceLen, int level)
98 {
99 	z_stream stream;
100 	int err;
101 
102 	stream.next_in = (Byte *)source;
103 	stream.avail_in = (uInt)sourceLen;
104 	stream.next_out = dest;
105 	stream.avail_out = (uInt)*destLen;
106 
107 	if ((size_t)stream.avail_out != *destLen)
108 		return (Z_BUF_ERROR);
109 
110 	stream.workspace = zlib_workspace_alloc(KM_SLEEP);
111 	if (!stream.workspace)
112 		return (Z_MEM_ERROR);
113 
114 	err = zlib_deflateInit(&stream, level);
115 	if (err != Z_OK) {
116 		zlib_workspace_free(stream.workspace);
117 		return (err);
118 	}
119 
120 	err = zlib_deflate(&stream, Z_FINISH);
121 	if (err != Z_STREAM_END) {
122 		zlib_deflateEnd(&stream);
123 		zlib_workspace_free(stream.workspace);
124 		return (err == Z_OK ? Z_BUF_ERROR : err);
125 	}
126 	*destLen = stream.total_out;
127 
128 	err = zlib_deflateEnd(&stream);
129 	zlib_workspace_free(stream.workspace);
130 
131 	return (err);
132 }
133 EXPORT_SYMBOL(z_compress_level);
134 
135 /*
136  * Decompresses the source buffer into the destination buffer.  sourceLen is
137  * the byte length of the source buffer. Upon entry, destLen is the total
138  * size of the destination buffer, which must be large enough to hold the
139  * entire uncompressed data. (The size of the uncompressed data must have
140  * been saved previously by the compressor and transmitted to the decompressor
141  * by some mechanism outside the scope of this compression library.)
142  * Upon exit, destLen is the actual size of the compressed buffer.
143  * This function can be used to decompress a whole file at once if the
144  * input file is mmap'ed.
145  *
146  * uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
147  * enough memory, Z_BUF_ERROR if there was not enough room in the output
148  * buffer, or Z_DATA_ERROR if the input data was corrupted.
149  */
150 int
151 z_uncompress(void *dest, size_t *destLen, const void *source, size_t sourceLen)
152 {
153 	z_stream stream;
154 	int err;
155 
156 	stream.next_in = (Byte *)source;
157 	stream.avail_in = (uInt)sourceLen;
158 	stream.next_out = dest;
159 	stream.avail_out = (uInt)*destLen;
160 
161 	if ((size_t)stream.avail_out != *destLen)
162 		return (Z_BUF_ERROR);
163 
164 	stream.workspace = zlib_workspace_alloc(KM_SLEEP);
165 	if (!stream.workspace)
166 		return (Z_MEM_ERROR);
167 
168 	err = zlib_inflateInit(&stream);
169 	if (err != Z_OK) {
170 		zlib_workspace_free(stream.workspace);
171 		return (err);
172 	}
173 
174 	err = zlib_inflate(&stream, Z_FINISH);
175 	if (err != Z_STREAM_END) {
176 		zlib_inflateEnd(&stream);
177 		zlib_workspace_free(stream.workspace);
178 
179 		if (err == Z_NEED_DICT ||
180 		    (err == Z_BUF_ERROR && stream.avail_in == 0))
181 			return (Z_DATA_ERROR);
182 
183 		return (err);
184 	}
185 	*destLen = stream.total_out;
186 
187 	err = zlib_inflateEnd(&stream);
188 	zlib_workspace_free(stream.workspace);
189 
190 	return (err);
191 }
192 EXPORT_SYMBOL(z_uncompress);
193 
194 int
195 spl_zlib_init(void)
196 {
197 	int size;
198 
199 	size = MAX(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL),
200 	    zlib_inflate_workspacesize());
201 
202 	zlib_workspace_cache = kmem_cache_create(
203 	    "spl_zlib_workspace_cache",
204 	    size, 0, NULL, NULL, NULL, NULL, NULL,
205 	    KMC_KVMEM);
206 	if (!zlib_workspace_cache)
207 		return (1);
208 
209 	return (0);
210 }
211 
212 void
213 spl_zlib_fini(void)
214 {
215 	kmem_cache_destroy(zlib_workspace_cache);
216 	zlib_workspace_cache = NULL;
217 }
218