1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or https://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Copyright (c) 2019, Allan Jude
25  * Copyright (c) 2019, Klara Inc.
26  * Use is subject to license terms.
27  * Copyright (c) 2015, 2016 by Delphix. All rights reserved.
28  */
29 
30 #ifndef _SYS_ZIO_COMPRESS_H
31 #define	_SYS_ZIO_COMPRESS_H
32 
33 #include <sys/abd.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 enum zio_compress {
40 	ZIO_COMPRESS_INHERIT = 0,
41 	ZIO_COMPRESS_ON,
42 	ZIO_COMPRESS_OFF,
43 	ZIO_COMPRESS_LZJB,
44 	ZIO_COMPRESS_EMPTY,
45 	ZIO_COMPRESS_GZIP_1,
46 	ZIO_COMPRESS_GZIP_2,
47 	ZIO_COMPRESS_GZIP_3,
48 	ZIO_COMPRESS_GZIP_4,
49 	ZIO_COMPRESS_GZIP_5,
50 	ZIO_COMPRESS_GZIP_6,
51 	ZIO_COMPRESS_GZIP_7,
52 	ZIO_COMPRESS_GZIP_8,
53 	ZIO_COMPRESS_GZIP_9,
54 	ZIO_COMPRESS_ZLE,
55 	ZIO_COMPRESS_LZ4,
56 	ZIO_COMPRESS_ZSTD,
57 	ZIO_COMPRESS_FUNCTIONS
58 };
59 
60 /* Compression algorithms that have levels */
61 #define	ZIO_COMPRESS_HASLEVEL(compress)	((compress == ZIO_COMPRESS_ZSTD || \
62 					(compress >= ZIO_COMPRESS_GZIP_1 && \
63 					compress <= ZIO_COMPRESS_GZIP_9)))
64 
65 #define	ZIO_COMPLEVEL_INHERIT	0
66 #define	ZIO_COMPLEVEL_DEFAULT	255
67 
68 enum zio_zstd_levels {
69 	ZIO_ZSTD_LEVEL_INHERIT = 0,
70 	ZIO_ZSTD_LEVEL_1,
71 #define	ZIO_ZSTD_LEVEL_MIN	ZIO_ZSTD_LEVEL_1
72 	ZIO_ZSTD_LEVEL_2,
73 	ZIO_ZSTD_LEVEL_3,
74 #define	ZIO_ZSTD_LEVEL_DEFAULT	ZIO_ZSTD_LEVEL_3
75 	ZIO_ZSTD_LEVEL_4,
76 	ZIO_ZSTD_LEVEL_5,
77 	ZIO_ZSTD_LEVEL_6,
78 	ZIO_ZSTD_LEVEL_7,
79 	ZIO_ZSTD_LEVEL_8,
80 	ZIO_ZSTD_LEVEL_9,
81 	ZIO_ZSTD_LEVEL_10,
82 	ZIO_ZSTD_LEVEL_11,
83 	ZIO_ZSTD_LEVEL_12,
84 	ZIO_ZSTD_LEVEL_13,
85 	ZIO_ZSTD_LEVEL_14,
86 	ZIO_ZSTD_LEVEL_15,
87 	ZIO_ZSTD_LEVEL_16,
88 	ZIO_ZSTD_LEVEL_17,
89 	ZIO_ZSTD_LEVEL_18,
90 	ZIO_ZSTD_LEVEL_19,
91 #define	ZIO_ZSTD_LEVEL_MAX	ZIO_ZSTD_LEVEL_19
92 	ZIO_ZSTD_LEVEL_RESERVE = 101, /* Leave room for new positive levels */
93 	ZIO_ZSTD_LEVEL_FAST, /* Fast levels are negative */
94 	ZIO_ZSTD_LEVEL_FAST_1,
95 #define	ZIO_ZSTD_LEVEL_FAST_DEFAULT	ZIO_ZSTD_LEVEL_FAST_1
96 	ZIO_ZSTD_LEVEL_FAST_2,
97 	ZIO_ZSTD_LEVEL_FAST_3,
98 	ZIO_ZSTD_LEVEL_FAST_4,
99 	ZIO_ZSTD_LEVEL_FAST_5,
100 	ZIO_ZSTD_LEVEL_FAST_6,
101 	ZIO_ZSTD_LEVEL_FAST_7,
102 	ZIO_ZSTD_LEVEL_FAST_8,
103 	ZIO_ZSTD_LEVEL_FAST_9,
104 	ZIO_ZSTD_LEVEL_FAST_10,
105 	ZIO_ZSTD_LEVEL_FAST_20,
106 	ZIO_ZSTD_LEVEL_FAST_30,
107 	ZIO_ZSTD_LEVEL_FAST_40,
108 	ZIO_ZSTD_LEVEL_FAST_50,
109 	ZIO_ZSTD_LEVEL_FAST_60,
110 	ZIO_ZSTD_LEVEL_FAST_70,
111 	ZIO_ZSTD_LEVEL_FAST_80,
112 	ZIO_ZSTD_LEVEL_FAST_90,
113 	ZIO_ZSTD_LEVEL_FAST_100,
114 	ZIO_ZSTD_LEVEL_FAST_500,
115 	ZIO_ZSTD_LEVEL_FAST_1000,
116 #define	ZIO_ZSTD_LEVEL_FAST_MAX	ZIO_ZSTD_LEVEL_FAST_1000
117 	ZIO_ZSTD_LEVEL_AUTO = 251, /* Reserved for future use */
118 	ZIO_ZSTD_LEVEL_LEVELS
119 };
120 
121 /* Forward Declaration to avoid visibility problems */
122 struct zio_prop;
123 
124 /* Common signature for all zio compress functions. */
125 typedef size_t zio_compress_func_t(void *src, void *dst,
126     size_t s_len, size_t d_len, int);
127 /* Common signature for all zio decompress functions. */
128 typedef int zio_decompress_func_t(void *src, void *dst,
129     size_t s_len, size_t d_len, int);
130 /* Common signature for all zio decompress and get level functions. */
131 typedef int zio_decompresslevel_func_t(void *src, void *dst,
132     size_t s_len, size_t d_len, uint8_t *level);
133 /* Common signature for all zio get-compression-level functions. */
134 typedef int zio_getlevel_func_t(void *src, size_t s_len, uint8_t *level);
135 
136 
137 /*
138  * Common signature for all zio decompress functions using an ABD as input.
139  * This is helpful if you have both compressed ARC and scatter ABDs enabled,
140  * but is not a requirement for all compression algorithms.
141  */
142 typedef int zio_decompress_abd_func_t(abd_t *src, void *dst,
143     size_t s_len, size_t d_len, int);
144 /*
145  * Information about each compression function.
146  */
147 typedef const struct zio_compress_info {
148 	const char			*ci_name;
149 	int				ci_level;
150 	zio_compress_func_t		*ci_compress;
151 	zio_decompress_func_t		*ci_decompress;
152 	zio_decompresslevel_func_t	*ci_decompress_level;
153 } zio_compress_info_t;
154 
155 extern zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS];
156 
157 /*
158  * lz4 compression init & free
159  */
160 extern void lz4_init(void);
161 extern void lz4_fini(void);
162 
163 /*
164  * Compression routines.
165  */
166 extern size_t lzjb_compress(void *src, void *dst, size_t s_len, size_t d_len,
167     int level);
168 extern int lzjb_decompress(void *src, void *dst, size_t s_len, size_t d_len,
169     int level);
170 extern size_t gzip_compress(void *src, void *dst, size_t s_len, size_t d_len,
171     int level);
172 extern int gzip_decompress(void *src, void *dst, size_t s_len, size_t d_len,
173     int level);
174 extern size_t zle_compress(void *src, void *dst, size_t s_len, size_t d_len,
175     int level);
176 extern int zle_decompress(void *src, void *dst, size_t s_len, size_t d_len,
177     int level);
178 extern size_t lz4_compress_zfs(void *src, void *dst, size_t s_len, size_t d_len,
179     int level);
180 extern int lz4_decompress_zfs(void *src, void *dst, size_t s_len, size_t d_len,
181     int level);
182 
183 /*
184  * Compress and decompress data if necessary.
185  */
186 extern size_t zio_compress_data(enum zio_compress c, abd_t *src, void **dst,
187     size_t s_len, uint8_t level);
188 extern int zio_decompress_data(enum zio_compress c, abd_t *src, void *dst,
189     size_t s_len, size_t d_len, uint8_t *level);
190 extern int zio_decompress_data_buf(enum zio_compress c, void *src, void *dst,
191     size_t s_len, size_t d_len, uint8_t *level);
192 extern int zio_compress_to_feature(enum zio_compress comp);
193 
194 #ifdef	__cplusplus
195 }
196 #endif
197 
198 #endif	/* _SYS_ZIO_COMPRESS_H */
199