xref: /freebsd/sys/contrib/openzfs/module/os/freebsd/spl/spl_zlib.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2020 iXsystems, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/types.h>
30 #include <sys/kmem.h>
31 #include <sys/kmem_cache.h>
32 #include <sys/zmod.h>
33 #include <contrib/zlib/zlib.h>
34 #include <sys/kobj.h>
35 
36 
37 static void *
zcalloc(void * opaque,uint_t items,uint_t size)38 zcalloc(void *opaque, uint_t items, uint_t size)
39 {
40 	(void) opaque;
41 	return (malloc((size_t)items*size, M_SOLARIS, M_NOWAIT));
42 }
43 
44 static void
zcfree(void * opaque,void * ptr)45 zcfree(void *opaque, void *ptr)
46 {
47 	(void) opaque;
48 	free(ptr, M_SOLARIS);
49 }
50 
51 static int
zlib_deflateInit(z_stream * stream,int level)52 zlib_deflateInit(z_stream *stream, int level)
53 {
54 
55 	stream->zalloc = zcalloc;
56 	stream->opaque = NULL;
57 	stream->zfree = zcfree;
58 
59 	return (deflateInit(stream, level));
60 }
61 
62 static int
zlib_deflate(z_stream * stream,int flush)63 zlib_deflate(z_stream *stream, int flush)
64 {
65 	return (deflate(stream, flush));
66 }
67 
68 static int
zlib_deflateEnd(z_stream * stream)69 zlib_deflateEnd(z_stream *stream)
70 {
71 	return (deflateEnd(stream));
72 }
73 
74 static int
zlib_inflateInit(z_stream * stream)75 zlib_inflateInit(z_stream *stream)
76 {
77 	stream->zalloc = zcalloc;
78 	stream->opaque = NULL;
79 	stream->zfree = zcfree;
80 
81 	return (inflateInit(stream));
82 }
83 
84 static int
zlib_inflate(z_stream * stream,int finish)85 zlib_inflate(z_stream *stream, int finish)
86 {
87 	return (inflate(stream, finish));
88 }
89 
90 
91 static int
zlib_inflateEnd(z_stream * stream)92 zlib_inflateEnd(z_stream *stream)
93 {
94 	return (inflateEnd(stream));
95 }
96 
97 /*
98  * A kmem_cache is used for the zlib workspaces to avoid having to vmalloc
99  * and vfree for every call.  Using a kmem_cache also has the advantage
100  * that improves the odds that the memory used will be local to this cpu.
101  * To further improve things it might be wise to create a dedicated per-cpu
102  * workspace for use.  This would take some additional care because we then
103  * must disable preemption around the critical section, and verify that
104  * zlib_deflate* and zlib_inflate* never internally call schedule().
105  */
106 static void *
zlib_workspace_alloc(int flags)107 zlib_workspace_alloc(int flags)
108 {
109 	// return (kmem_cache_alloc(zlib_workspace_cache, flags));
110 	return (NULL);
111 }
112 
113 static void
zlib_workspace_free(void * workspace)114 zlib_workspace_free(void *workspace)
115 {
116 	// kmem_cache_free(zlib_workspace_cache, workspace);
117 }
118 
119 /*
120  * Compresses the source buffer into the destination buffer. The level
121  * parameter has the same meaning as in deflateInit.  sourceLen is the byte
122  * length of the source buffer. Upon entry, destLen is the total size of the
123  * destination buffer, which must be at least 0.1% larger than sourceLen plus
124  * 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
125  *
126  * compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
127  * memory, Z_BUF_ERROR if there was not enough room in the output buffer,
128  * Z_STREAM_ERROR if the level parameter is invalid.
129  */
130 int
z_compress_level(void * dest,size_t * destLen,const void * source,size_t sourceLen,int level)131 z_compress_level(void *dest, size_t *destLen, const void *source,
132     size_t sourceLen, int level)
133 {
134 	z_stream stream = {0};
135 	int err;
136 
137 	stream.next_in = (Byte *)source;
138 	stream.avail_in = (uInt)sourceLen;
139 	stream.next_out = dest;
140 	stream.avail_out = (uInt)*destLen;
141 	stream.opaque = NULL;
142 
143 	if ((size_t)stream.avail_out != *destLen)
144 		return (Z_BUF_ERROR);
145 
146 	stream.opaque = zlib_workspace_alloc(KM_SLEEP);
147 #if 0
148 	if (!stream.opaque)
149 		return (Z_MEM_ERROR);
150 #endif
151 	err = zlib_deflateInit(&stream, level);
152 	if (err != Z_OK) {
153 		zlib_workspace_free(stream.opaque);
154 		return (err);
155 	}
156 
157 	err = zlib_deflate(&stream, Z_FINISH);
158 	if (err != Z_STREAM_END) {
159 		zlib_deflateEnd(&stream);
160 		zlib_workspace_free(stream.opaque);
161 		return (err == Z_OK ? Z_BUF_ERROR : err);
162 	}
163 	*destLen = stream.total_out;
164 
165 	err = zlib_deflateEnd(&stream);
166 	zlib_workspace_free(stream.opaque);
167 	return (err);
168 }
169 
170 /*
171  * Decompresses the source buffer into the destination buffer.  sourceLen is
172  * the byte length of the source buffer. Upon entry, destLen is the total
173  * size of the destination buffer, which must be large enough to hold the
174  * entire uncompressed data. (The size of the uncompressed data must have
175  * been saved previously by the compressor and transmitted to the decompressor
176  * by some mechanism outside the scope of this compression library.)
177  * Upon exit, destLen is the actual size of the compressed buffer.
178  * This function can be used to decompress a whole file at once if the
179  * input file is mmap'ed.
180  *
181  * uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
182  * enough memory, Z_BUF_ERROR if there was not enough room in the output
183  * buffer, or Z_DATA_ERROR if the input data was corrupted.
184  */
185 int
z_uncompress(void * dest,size_t * destLen,const void * source,size_t sourceLen)186 z_uncompress(void *dest, size_t *destLen, const void *source, size_t sourceLen)
187 {
188 	z_stream stream = {0};
189 	int err;
190 
191 	stream.next_in = (Byte *)source;
192 	stream.avail_in = (uInt)sourceLen;
193 	stream.next_out = dest;
194 	stream.avail_out = (uInt)*destLen;
195 
196 	if ((size_t)stream.avail_out != *destLen)
197 		return (Z_BUF_ERROR);
198 
199 	stream.opaque = zlib_workspace_alloc(KM_SLEEP);
200 #if 0
201 	if (!stream.opaque)
202 		return (Z_MEM_ERROR);
203 #endif
204 	err = zlib_inflateInit(&stream);
205 	if (err != Z_OK) {
206 		zlib_workspace_free(stream.opaque);
207 		return (err);
208 	}
209 
210 	err = zlib_inflate(&stream, Z_FINISH);
211 	if (err != Z_STREAM_END) {
212 		zlib_inflateEnd(&stream);
213 		zlib_workspace_free(stream.opaque);
214 
215 		if (err == Z_NEED_DICT ||
216 		    (err == Z_BUF_ERROR && stream.avail_in == 0))
217 			return (Z_DATA_ERROR);
218 
219 		return (err);
220 	}
221 	*destLen = stream.total_out;
222 
223 	err = zlib_inflateEnd(&stream);
224 	zlib_workspace_free(stream.opaque);
225 
226 	return (err);
227 }
228