xref: /freebsd/sys/geom/uzip/g_uzip_zstd.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>
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 AUTHOR 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 AUTHOR 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 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/errno.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/sysctl.h>
35 
36 #include <geom/uzip/g_uzip.h>
37 #include <geom/uzip/g_uzip_dapi.h>
38 #include <geom/uzip/g_uzip_zstd.h>
39 
40 /*
41  * We don't actually need any static-link ABI, just want to use "experimental"
42  * custom malloc/free APIs.
43  */
44 #define ZSTD_STATIC_LINKING_ONLY
45 #include <contrib/zstd/lib/zstd.h>
46 
47 FEATURE(geom_uzip_zstd, "g_uzip Zstd support");
48 
49 struct g_uzip_zstd {
50 	struct g_uzip_dapi	guz_pub;
51 	uint32_t		guz_blksz;
52 	ZSTD_DCtx		*guz_dctx;
53 };
54 
55 #ifndef container_of
56 #define container_of(ptr, type, member)				\
57 ({								\
58 	const __typeof(((type *)0)->member) *__p = (ptr);	\
59 	(type *)((uintptr_t)__p - offsetof(type, member));	\
60 })
61 #endif
62 #define	to_zstd_softc(zpp)	container_of(zpp, struct g_uzip_zstd, guz_pub)
63 
64 static int
65 guz_zstd_decompress(struct g_uzip_dapi *zpp, const char *gp_name, void *input,
66     size_t ilen, void *outputbuf)
67 {
68 	struct g_uzip_zstd *sc;
69 	size_t rc;
70 
71 	sc = to_zstd_softc(zpp);
72 	rc = ZSTD_decompressDCtx(sc->guz_dctx, outputbuf, sc->guz_blksz, input,
73 	    ilen);
74 	if (ZSTD_isError(rc)) {
75 		printf("%s: UZIP(zstd) decompress failed: %s\n", gp_name,
76 		    ZSTD_getErrorName(rc));
77 		return (EIO);
78 	}
79 	KASSERT(rc == sc->guz_blksz, ("%s: Expected %u bytes, got %zu",
80 	    __func__, sc->guz_blksz, rc));
81 	return (0);
82 }
83 
84 static void
85 guz_zstd_free(struct g_uzip_dapi *zpp)
86 {
87 	struct g_uzip_zstd *sc;
88 	size_t rc;
89 
90 	sc = to_zstd_softc(zpp);
91 	rc = ZSTD_freeDCtx(sc->guz_dctx);
92 	if (ZSTD_isError(rc))
93 		printf("%s: UZIP(zstd) free failed: %s\n", __func__,
94 		    ZSTD_getErrorName(rc));
95 
96 	free(sc, M_GEOM_UZIP);
97 }
98 
99 static int
100 guz_zstd_rewind(struct g_uzip_dapi *zpp, const char *gp_name)
101 {
102 	struct g_uzip_zstd *sc;
103 	size_t rc;
104 
105 	sc = to_zstd_softc(zpp);
106 	rc = ZSTD_DCtx_reset(sc->guz_dctx, ZSTD_reset_session_and_parameters);
107 	if (ZSTD_isError(rc)) {
108 		printf("%s: UZIP(zstd) rewind failed: %s\n", gp_name,
109 		    ZSTD_getErrorName(rc));
110 		return (EIO);
111 	}
112 	return (0);
113 }
114 
115 static void *
116 zstd_alloc(void *opaque, size_t size)
117 {
118 	return (malloc(size, opaque, M_WAITOK));
119 }
120 
121 static void
122 zstd_free(void *opaque, void *address)
123 {
124 	free(address, opaque);
125 }
126 
127 static const ZSTD_customMem zstd_guz_alloc = {
128 	.customAlloc = zstd_alloc,
129 	.customFree = zstd_free,
130 	.opaque = M_GEOM_UZIP,
131 };
132 
133 struct g_uzip_dapi *
134 g_uzip_zstd_ctor(uint32_t blksz)
135 {
136 	struct g_uzip_zstd *sc;
137 
138 	sc = malloc(sizeof(*sc), M_GEOM_UZIP, M_WAITOK | M_ZERO);
139 
140 	sc->guz_dctx = ZSTD_createDCtx_advanced(zstd_guz_alloc);
141 	if (sc->guz_dctx == NULL) {
142 		printf("%s: ZSTD_createDCtx_advanced failed\n", __func__);
143 		free(sc, M_GEOM_UZIP);
144 		return (NULL);
145 	}
146 
147 	sc->guz_blksz = blksz;
148 	sc->guz_pub.max_blen = ZSTD_compressBound(blksz);
149 	sc->guz_pub.decompress = guz_zstd_decompress;
150 	sc->guz_pub.free = guz_zstd_free;
151 	sc->guz_pub.rewind = guz_zstd_rewind;
152 	sc->guz_pub.pvt = NULL;
153 
154 	return (&sc->guz_pub);
155 }
156