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