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