xref: /freebsd/sys/geom/uzip/g_uzip_zstd.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1eefd8f96SConrad Meyer /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3eefd8f96SConrad Meyer  *
4eefd8f96SConrad Meyer  * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>
5eefd8f96SConrad Meyer  *
6eefd8f96SConrad Meyer  * Redistribution and use in source and binary forms, with or without
7eefd8f96SConrad Meyer  * modification, are permitted provided that the following conditions
8eefd8f96SConrad Meyer  * are met:
9eefd8f96SConrad Meyer  * 1. Redistributions of source code must retain the above copyright
10eefd8f96SConrad Meyer  *    notice, this list of conditions and the following disclaimer.
11eefd8f96SConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
12eefd8f96SConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
13eefd8f96SConrad Meyer  *    documentation and/or other materials provided with the distribution.
14eefd8f96SConrad Meyer  *
15eefd8f96SConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16eefd8f96SConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17eefd8f96SConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18eefd8f96SConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19eefd8f96SConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20eefd8f96SConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21eefd8f96SConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22eefd8f96SConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23eefd8f96SConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24eefd8f96SConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25eefd8f96SConrad Meyer  * SUCH DAMAGE.
26eefd8f96SConrad Meyer  */
27eefd8f96SConrad Meyer 
28eefd8f96SConrad Meyer #include <sys/param.h>
29eefd8f96SConrad Meyer #include <sys/systm.h>
30eefd8f96SConrad Meyer #include <sys/errno.h>
31eefd8f96SConrad Meyer #include <sys/kernel.h>
32eefd8f96SConrad Meyer #include <sys/malloc.h>
33eefd8f96SConrad Meyer #include <sys/sysctl.h>
34eefd8f96SConrad Meyer 
35eefd8f96SConrad Meyer #include <geom/uzip/g_uzip.h>
36eefd8f96SConrad Meyer #include <geom/uzip/g_uzip_dapi.h>
37eefd8f96SConrad Meyer #include <geom/uzip/g_uzip_zstd.h>
38eefd8f96SConrad Meyer 
39eefd8f96SConrad Meyer /*
40eefd8f96SConrad Meyer  * We don't actually need any static-link ABI, just want to use "experimental"
41eefd8f96SConrad Meyer  * custom malloc/free APIs.
42eefd8f96SConrad Meyer  */
43eefd8f96SConrad Meyer #define ZSTD_STATIC_LINKING_ONLY
44eefd8f96SConrad Meyer #include <contrib/zstd/lib/zstd.h>
45eefd8f96SConrad Meyer 
46eefd8f96SConrad Meyer FEATURE(geom_uzip_zstd, "g_uzip Zstd support");
47eefd8f96SConrad Meyer 
48eefd8f96SConrad Meyer struct g_uzip_zstd {
49eefd8f96SConrad Meyer 	struct g_uzip_dapi	guz_pub;
50eefd8f96SConrad Meyer 	uint32_t		guz_blksz;
51eefd8f96SConrad Meyer 	ZSTD_DCtx		*guz_dctx;
52eefd8f96SConrad Meyer };
53eefd8f96SConrad Meyer 
54eefd8f96SConrad Meyer #ifndef container_of
55eefd8f96SConrad Meyer #define container_of(ptr, type, member)				\
56eefd8f96SConrad Meyer ({								\
57eefd8f96SConrad Meyer 	const __typeof(((type *)0)->member) *__p = (ptr);	\
58eefd8f96SConrad Meyer 	(type *)((uintptr_t)__p - offsetof(type, member));	\
59eefd8f96SConrad Meyer })
60eefd8f96SConrad Meyer #endif
61eefd8f96SConrad Meyer #define	to_zstd_softc(zpp)	container_of(zpp, struct g_uzip_zstd, guz_pub)
62eefd8f96SConrad Meyer 
63eefd8f96SConrad Meyer static int
guz_zstd_decompress(struct g_uzip_dapi * zpp,const char * gp_name,void * input,size_t ilen,void * outputbuf)64eefd8f96SConrad Meyer guz_zstd_decompress(struct g_uzip_dapi *zpp, const char *gp_name, void *input,
65eefd8f96SConrad Meyer     size_t ilen, void *outputbuf)
66eefd8f96SConrad Meyer {
67eefd8f96SConrad Meyer 	struct g_uzip_zstd *sc;
68eefd8f96SConrad Meyer 	size_t rc;
69eefd8f96SConrad Meyer 
70eefd8f96SConrad Meyer 	sc = to_zstd_softc(zpp);
71eefd8f96SConrad Meyer 	rc = ZSTD_decompressDCtx(sc->guz_dctx, outputbuf, sc->guz_blksz, input,
72eefd8f96SConrad Meyer 	    ilen);
73eefd8f96SConrad Meyer 	if (ZSTD_isError(rc)) {
74eefd8f96SConrad Meyer 		printf("%s: UZIP(zstd) decompress failed: %s\n", gp_name,
75eefd8f96SConrad Meyer 		    ZSTD_getErrorName(rc));
76eefd8f96SConrad Meyer 		return (EIO);
77eefd8f96SConrad Meyer 	}
78eefd8f96SConrad Meyer 	KASSERT(rc == sc->guz_blksz, ("%s: Expected %u bytes, got %zu",
79eefd8f96SConrad Meyer 	    __func__, sc->guz_blksz, rc));
80eefd8f96SConrad Meyer 	return (0);
81eefd8f96SConrad Meyer }
82eefd8f96SConrad Meyer 
83eefd8f96SConrad Meyer static void
guz_zstd_free(struct g_uzip_dapi * zpp)84eefd8f96SConrad Meyer guz_zstd_free(struct g_uzip_dapi *zpp)
85eefd8f96SConrad Meyer {
86eefd8f96SConrad Meyer 	struct g_uzip_zstd *sc;
87eefd8f96SConrad Meyer 	size_t rc;
88eefd8f96SConrad Meyer 
89eefd8f96SConrad Meyer 	sc = to_zstd_softc(zpp);
90eefd8f96SConrad Meyer 	rc = ZSTD_freeDCtx(sc->guz_dctx);
91eefd8f96SConrad Meyer 	if (ZSTD_isError(rc))
92eefd8f96SConrad Meyer 		printf("%s: UZIP(zstd) free failed: %s\n", __func__,
93eefd8f96SConrad Meyer 		    ZSTD_getErrorName(rc));
94eefd8f96SConrad Meyer 
95eefd8f96SConrad Meyer 	free(sc, M_GEOM_UZIP);
96eefd8f96SConrad Meyer }
97eefd8f96SConrad Meyer 
98eefd8f96SConrad Meyer static int
guz_zstd_rewind(struct g_uzip_dapi * zpp,const char * gp_name)99eefd8f96SConrad Meyer guz_zstd_rewind(struct g_uzip_dapi *zpp, const char *gp_name)
100eefd8f96SConrad Meyer {
101eefd8f96SConrad Meyer 	struct g_uzip_zstd *sc;
102eefd8f96SConrad Meyer 	size_t rc;
103eefd8f96SConrad Meyer 
104eefd8f96SConrad Meyer 	sc = to_zstd_softc(zpp);
105eefd8f96SConrad Meyer 	rc = ZSTD_DCtx_reset(sc->guz_dctx, ZSTD_reset_session_and_parameters);
106eefd8f96SConrad Meyer 	if (ZSTD_isError(rc)) {
107eefd8f96SConrad Meyer 		printf("%s: UZIP(zstd) rewind failed: %s\n", gp_name,
108eefd8f96SConrad Meyer 		    ZSTD_getErrorName(rc));
109eefd8f96SConrad Meyer 		return (EIO);
110eefd8f96SConrad Meyer 	}
111eefd8f96SConrad Meyer 	return (0);
112eefd8f96SConrad Meyer }
113eefd8f96SConrad Meyer 
114eefd8f96SConrad Meyer static void *
zstd_alloc(void * opaque,size_t size)115eefd8f96SConrad Meyer zstd_alloc(void *opaque, size_t size)
116eefd8f96SConrad Meyer {
117eefd8f96SConrad Meyer 	return (malloc(size, opaque, M_WAITOK));
118eefd8f96SConrad Meyer }
119eefd8f96SConrad Meyer 
120eefd8f96SConrad Meyer static void
zstd_free(void * opaque,void * address)121eefd8f96SConrad Meyer zstd_free(void *opaque, void *address)
122eefd8f96SConrad Meyer {
123eefd8f96SConrad Meyer 	free(address, opaque);
124eefd8f96SConrad Meyer }
125eefd8f96SConrad Meyer 
126eefd8f96SConrad Meyer static const ZSTD_customMem zstd_guz_alloc = {
127eefd8f96SConrad Meyer 	.customAlloc = zstd_alloc,
128eefd8f96SConrad Meyer 	.customFree = zstd_free,
129eefd8f96SConrad Meyer 	.opaque = M_GEOM_UZIP,
130eefd8f96SConrad Meyer };
131eefd8f96SConrad Meyer 
132eefd8f96SConrad Meyer struct g_uzip_dapi *
g_uzip_zstd_ctor(uint32_t blksz)133eefd8f96SConrad Meyer g_uzip_zstd_ctor(uint32_t blksz)
134eefd8f96SConrad Meyer {
135eefd8f96SConrad Meyer 	struct g_uzip_zstd *sc;
136eefd8f96SConrad Meyer 
137eefd8f96SConrad Meyer 	sc = malloc(sizeof(*sc), M_GEOM_UZIP, M_WAITOK | M_ZERO);
138eefd8f96SConrad Meyer 
139eefd8f96SConrad Meyer 	sc->guz_dctx = ZSTD_createDCtx_advanced(zstd_guz_alloc);
140eefd8f96SConrad Meyer 	if (sc->guz_dctx == NULL) {
141eefd8f96SConrad Meyer 		printf("%s: ZSTD_createDCtx_advanced failed\n", __func__);
142eefd8f96SConrad Meyer 		free(sc, M_GEOM_UZIP);
143eefd8f96SConrad Meyer 		return (NULL);
144eefd8f96SConrad Meyer 	}
145eefd8f96SConrad Meyer 
146eefd8f96SConrad Meyer 	sc->guz_blksz = blksz;
147eefd8f96SConrad Meyer 	sc->guz_pub.max_blen = ZSTD_compressBound(blksz);
148eefd8f96SConrad Meyer 	sc->guz_pub.decompress = guz_zstd_decompress;
149eefd8f96SConrad Meyer 	sc->guz_pub.free = guz_zstd_free;
150eefd8f96SConrad Meyer 	sc->guz_pub.rewind = guz_zstd_rewind;
151eefd8f96SConrad Meyer 	sc->guz_pub.pvt = NULL;
152eefd8f96SConrad Meyer 
153eefd8f96SConrad Meyer 	return (&sc->guz_pub);
154eefd8f96SConrad Meyer }
155