2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014, 2017 Mark Johnston <markj@FreeBSD.org>
5 * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Subroutines used for writing compressed user process and kernel core dumps.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_zstdio.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/compressor.h>
44 #include <sys/kernel.h>
45 #include <sys/linker_set.h>
46 #include <sys/malloc.h>
48 MALLOC_DEFINE(M_COMPRESS, "compressor", "kernel compression subroutines");
50 struct compressor_methods {
52 void *(* const init)(size_t, int);
53 void (* const reset)(void *);
54 int (* const write)(void *, void *, size_t, compressor_cb_t, void *);
55 void (* const fini)(void *);
59 const struct compressor_methods *methods;
65 SET_DECLARE(compressors, struct compressor_methods);
69 #include <contrib/zlib/zutil.h>
72 uint8_t *gz_buffer; /* output buffer */
73 size_t gz_bufsz; /* output buffer size */
74 off_t gz_off; /* offset into the output stream */
75 uint32_t gz_crc; /* stream CRC32 */
76 z_stream gz_stream; /* zlib state */
79 static void *gz_init(size_t maxiosize, int level);
80 static void gz_reset(void *stream);
81 static int gz_write(void *stream, void *data, size_t len, compressor_cb_t,
83 static void gz_fini(void *stream);
86 gz_alloc(void *arg __unused, u_int n, u_int sz)
90 * Memory for zlib state is allocated using M_NODUMP since it may be
91 * used to compress a kernel dump, and we don't want zlib to attempt to
92 * compress its own state.
94 return (malloc(n * sz, M_COMPRESS, M_WAITOK | M_ZERO | M_NODUMP));
98 gz_free(void *arg __unused, void *ptr)
101 free(ptr, M_COMPRESS);
105 gz_init(size_t maxiosize, int level)
110 s = gz_alloc(NULL, 1, roundup2(sizeof(*s), PAGE_SIZE));
111 s->gz_buffer = gz_alloc(NULL, 1, maxiosize);
112 s->gz_bufsz = maxiosize;
114 s->gz_stream.zalloc = gz_alloc;
115 s->gz_stream.zfree = gz_free;
116 s->gz_stream.opaque = NULL;
117 s->gz_stream.next_in = Z_NULL;
118 s->gz_stream.avail_in = 0;
120 if (level != Z_DEFAULT_COMPRESSION) {
121 if (level < Z_BEST_SPEED)
122 level = Z_BEST_SPEED;
123 else if (level > Z_BEST_COMPRESSION)
124 level = Z_BEST_COMPRESSION;
127 error = deflateInit2(&s->gz_stream, level, Z_DEFLATED, -MAX_WBITS,
128 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
142 gz_reset(void *stream)
146 const size_t hdrlen = 10;
150 s->gz_crc = crc32(0L, Z_NULL, 0);
152 (void)deflateReset(&s->gz_stream);
153 s->gz_stream.avail_out = s->gz_bufsz;
154 s->gz_stream.next_out = s->gz_buffer;
156 /* Write the gzip header to the output buffer. */
158 memset(hdr, 0, hdrlen);
163 s->gz_stream.next_out += hdrlen;
164 s->gz_stream.avail_out -= hdrlen;
168 gz_write(void *stream, void *data, size_t len, compressor_cb_t cb,
174 int error, zerror, zflag;
177 zflag = data == NULL ? Z_FINISH : Z_NO_FLUSH;
180 s->gz_stream.avail_in = len;
181 s->gz_stream.next_in = data;
182 s->gz_crc = crc32(s->gz_crc, data, len);
187 zerror = deflate(&s->gz_stream, zflag);
188 if (zerror != Z_OK && zerror != Z_STREAM_END) {
193 if (s->gz_stream.avail_out == 0 || zerror == Z_STREAM_END) {
195 * Our output buffer is full or there's nothing left
196 * to produce, so we're flushing the buffer.
198 len = s->gz_bufsz - s->gz_stream.avail_out;
199 if (zerror == Z_STREAM_END) {
201 * Try to pack as much of the trailer into the
202 * output buffer as we can.
204 ((uint32_t *)trailer)[0] = s->gz_crc;
205 ((uint32_t *)trailer)[1] =
206 s->gz_stream.total_in;
207 room = MIN(sizeof(trailer),
209 memcpy(s->gz_buffer + len, trailer, room);
213 error = cb(s->gz_buffer, len, s->gz_off, arg);
218 s->gz_stream.next_out = s->gz_buffer;
219 s->gz_stream.avail_out = s->gz_bufsz;
222 * If we couldn't pack the trailer into the output
223 * buffer, write it out now.
225 if (zerror == Z_STREAM_END && room < sizeof(trailer))
226 error = cb(trailer + room,
227 sizeof(trailer) - room, s->gz_off, arg);
229 } while (zerror != Z_STREAM_END &&
230 (zflag == Z_FINISH || s->gz_stream.avail_in > 0));
236 gz_fini(void *stream)
241 (void)deflateEnd(&s->gz_stream);
242 gz_free(NULL, s->gz_buffer);
246 struct compressor_methods gzip_methods = {
247 .format = COMPRESS_GZIP,
253 DATA_SET(compressors, gzip_methods);
259 #define ZSTD_STATIC_LINKING_ONLY
260 #include <contrib/zstd/lib/zstd.h>
262 struct zstdio_stream {
263 ZSTD_CCtx *zst_stream;
264 ZSTD_inBuffer zst_inbuffer;
265 ZSTD_outBuffer zst_outbuffer;
266 uint8_t * zst_buffer; /* output buffer */
267 size_t zst_maxiosz; /* Max output IO size */
268 off_t zst_off; /* offset into the output stream */
269 void * zst_static_wkspc;
272 static void *zstdio_init(size_t maxiosize, int level);
273 static void zstdio_reset(void *stream);
274 static int zstdio_write(void *stream, void *data, size_t len,
275 compressor_cb_t, void *);
276 static void zstdio_fini(void *stream);
279 zstdio_init(size_t maxiosize, int level)
281 ZSTD_CCtx *dump_compressor;
282 struct zstdio_stream *s;
283 void *wkspc, *owkspc, *buffer;
284 size_t wkspc_size, buf_size, rc;
287 wkspc_size = ZSTD_estimateCStreamSize(level);
288 owkspc = wkspc = malloc(wkspc_size + 8, M_COMPRESS,
289 M_WAITOK | M_NODUMP);
290 /* Zstd API requires 8-byte alignment. */
291 if ((uintptr_t)wkspc % 8 != 0)
292 wkspc = (void *)roundup2((uintptr_t)wkspc, 8);
294 dump_compressor = ZSTD_initStaticCCtx(wkspc, wkspc_size);
295 if (dump_compressor == NULL) {
296 printf("%s: workspace too small.\n", __func__);
300 rc = ZSTD_CCtx_setParameter(dump_compressor, ZSTD_c_checksumFlag, 1);
301 if (ZSTD_isError(rc)) {
302 printf("%s: error setting checksumFlag: %s\n", __func__,
303 ZSTD_getErrorName(rc));
306 rc = ZSTD_CCtx_setParameter(dump_compressor, ZSTD_c_compressionLevel,
308 if (ZSTD_isError(rc)) {
309 printf("%s: error setting compressLevel: %s\n", __func__,
310 ZSTD_getErrorName(rc));
314 buf_size = ZSTD_CStreamOutSize() * 2;
315 buffer = malloc(buf_size, M_COMPRESS, M_WAITOK | M_NODUMP);
317 s = malloc(sizeof(*s), M_COMPRESS, M_NODUMP | M_WAITOK);
318 s->zst_buffer = buffer;
319 s->zst_outbuffer.dst = buffer;
320 s->zst_outbuffer.size = buf_size;
321 s->zst_maxiosz = maxiosize;
322 s->zst_stream = dump_compressor;
323 s->zst_static_wkspc = owkspc;
329 free(owkspc, M_COMPRESS);
334 zstdio_reset(void *stream)
336 struct zstdio_stream *s;
340 res = ZSTD_resetCStream(s->zst_stream, 0);
341 if (ZSTD_isError(res))
342 panic("%s: could not reset stream %p: %s\n", __func__, s,
343 ZSTD_getErrorName(res));
346 s->zst_inbuffer.src = NULL;
347 s->zst_inbuffer.size = 0;
348 s->zst_inbuffer.pos = 0;
349 s->zst_outbuffer.pos = 0;
353 zst_flush_intermediate(struct zstdio_stream *s, compressor_cb_t cb, void *arg)
355 size_t bytes_to_dump;
358 /* Flush as many full output blocks as possible. */
359 /* XXX: 4096 is arbitrary safe HDD block size for kernel dumps */
360 while (s->zst_outbuffer.pos >= 4096) {
361 bytes_to_dump = rounddown(s->zst_outbuffer.pos, 4096);
363 if (bytes_to_dump > s->zst_maxiosz)
364 bytes_to_dump = s->zst_maxiosz;
366 error = cb(s->zst_buffer, bytes_to_dump, s->zst_off, arg);
371 * Shift any non-full blocks up to the front of the output
374 s->zst_outbuffer.pos -= bytes_to_dump;
375 memmove(s->zst_outbuffer.dst,
376 (char *)s->zst_outbuffer.dst + bytes_to_dump,
377 s->zst_outbuffer.pos);
378 s->zst_off += bytes_to_dump;
384 zstdio_flush(struct zstdio_stream *s, compressor_cb_t cb, void *arg)
390 * Positive return indicates unflushed data remaining; need to call
391 * endStream again after clearing out room in output buffer.
394 lastpos = s->zst_outbuffer.pos;
396 rc = ZSTD_endStream(s->zst_stream, &s->zst_outbuffer);
397 if (ZSTD_isError(rc)) {
398 printf("%s: ZSTD_endStream failed (%s)\n", __func__,
399 ZSTD_getErrorName(rc));
402 if (lastpos == s->zst_outbuffer.pos) {
403 printf("%s: did not make forward progress endStream %zu\n",
408 error = zst_flush_intermediate(s, cb, arg);
412 lastpos = s->zst_outbuffer.pos;
416 * We've already done an intermediate flush, so all full blocks have
417 * been written. Only a partial block remains. Padding happens in a
420 if (s->zst_outbuffer.pos != 0) {
421 error = cb(s->zst_buffer, s->zst_outbuffer.pos, s->zst_off,
431 zstdio_write(void *stream, void *data, size_t len, compressor_cb_t cb,
434 struct zstdio_stream *s;
440 return (zstdio_flush(s, cb, arg));
442 s->zst_inbuffer.src = data;
443 s->zst_inbuffer.size = len;
444 s->zst_inbuffer.pos = 0;
447 while (s->zst_inbuffer.pos < s->zst_inbuffer.size) {
448 rc = ZSTD_compressStream(s->zst_stream, &s->zst_outbuffer,
450 if (ZSTD_isError(rc)) {
451 printf("%s: Compress failed on %p! (%s)\n",
452 __func__, data, ZSTD_getErrorName(rc));
456 if (lastpos == s->zst_inbuffer.pos) {
458 * XXX: May need flushStream to make forward progress
460 printf("ZSTD: did not make forward progress @pos %zu\n",
464 lastpos = s->zst_inbuffer.pos;
466 error = zst_flush_intermediate(s, cb, arg);
474 zstdio_fini(void *stream)
476 struct zstdio_stream *s;
479 if (s->zst_static_wkspc != NULL)
480 free(s->zst_static_wkspc, M_COMPRESS);
482 ZSTD_freeCCtx(s->zst_stream);
483 free(s->zst_buffer, M_COMPRESS);
487 static struct compressor_methods zstd_methods = {
488 .format = COMPRESS_ZSTD,
490 .reset = zstdio_reset,
491 .write = zstdio_write,
494 DATA_SET(compressors, zstd_methods);
499 compressor_avail(int format)
501 struct compressor_methods **iter;
503 SET_FOREACH(iter, compressors) {
504 if ((*iter)->format == format)
511 compressor_init(compressor_cb_t cb, int format, size_t maxiosize, int level,
514 struct compressor_methods **iter;
515 struct compressor *s;
518 SET_FOREACH(iter, compressors) {
519 if ((*iter)->format == format)
522 if (iter == SET_LIMIT(compressors))
525 priv = (*iter)->init(maxiosize, level);
529 s = malloc(sizeof(*s), M_COMPRESS, M_WAITOK | M_ZERO);
530 s->methods = (*iter);
538 compressor_reset(struct compressor *stream)
541 stream->methods->reset(stream->priv);
545 compressor_write(struct compressor *stream, void *data, size_t len)
548 return (stream->methods->write(stream->priv, data, len, stream->cb,
553 compressor_flush(struct compressor *stream)
556 return (stream->methods->write(stream->priv, NULL, 0, stream->cb,
561 compressor_fini(struct compressor *stream)
564 stream->methods->fini(stream->priv);