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.
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.
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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Copyright (c) 2019, Allan Jude
25 * Copyright (c) 2019, 2024, Klara, Inc.
26 * Use is subject to license terms.
27 * Copyright (c) 2015, 2016 by Delphix. All rights reserved.
28 * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
31 #ifndef _SYS_ZIO_COMPRESS_H
32 #define _SYS_ZIO_COMPRESS_H
41 ZIO_COMPRESS_INHERIT
= 0,
58 ZIO_COMPRESS_FUNCTIONS
61 /* Compression algorithms that have levels */
62 #define ZIO_COMPRESS_HASLEVEL(compress) ((compress == ZIO_COMPRESS_ZSTD || \
63 (compress >= ZIO_COMPRESS_GZIP_1 && \
64 compress <= ZIO_COMPRESS_GZIP_9)))
66 #define ZIO_COMPLEVEL_INHERIT 0
67 #define ZIO_COMPLEVEL_DEFAULT 255
69 enum zio_zstd_levels
{
70 ZIO_ZSTD_LEVEL_INHERIT
= 0,
72 #define ZIO_ZSTD_LEVEL_MIN ZIO_ZSTD_LEVEL_1
75 #define ZIO_ZSTD_LEVEL_DEFAULT ZIO_ZSTD_LEVEL_3
92 #define ZIO_ZSTD_LEVEL_MAX ZIO_ZSTD_LEVEL_19
93 ZIO_ZSTD_LEVEL_RESERVE
= 101, /* Leave room for new positive levels */
94 ZIO_ZSTD_LEVEL_FAST
, /* Fast levels are negative */
95 ZIO_ZSTD_LEVEL_FAST_1
,
96 #define ZIO_ZSTD_LEVEL_FAST_DEFAULT ZIO_ZSTD_LEVEL_FAST_1
97 ZIO_ZSTD_LEVEL_FAST_2
,
98 ZIO_ZSTD_LEVEL_FAST_3
,
99 ZIO_ZSTD_LEVEL_FAST_4
,
100 ZIO_ZSTD_LEVEL_FAST_5
,
101 ZIO_ZSTD_LEVEL_FAST_6
,
102 ZIO_ZSTD_LEVEL_FAST_7
,
103 ZIO_ZSTD_LEVEL_FAST_8
,
104 ZIO_ZSTD_LEVEL_FAST_9
,
105 ZIO_ZSTD_LEVEL_FAST_10
,
106 ZIO_ZSTD_LEVEL_FAST_20
,
107 ZIO_ZSTD_LEVEL_FAST_30
,
108 ZIO_ZSTD_LEVEL_FAST_40
,
109 ZIO_ZSTD_LEVEL_FAST_50
,
110 ZIO_ZSTD_LEVEL_FAST_60
,
111 ZIO_ZSTD_LEVEL_FAST_70
,
112 ZIO_ZSTD_LEVEL_FAST_80
,
113 ZIO_ZSTD_LEVEL_FAST_90
,
114 ZIO_ZSTD_LEVEL_FAST_100
,
115 ZIO_ZSTD_LEVEL_FAST_500
,
116 ZIO_ZSTD_LEVEL_FAST_1000
,
117 #define ZIO_ZSTD_LEVEL_FAST_MAX ZIO_ZSTD_LEVEL_FAST_1000
118 ZIO_ZSTD_LEVEL_AUTO
= 251, /* Reserved for future use */
119 ZIO_ZSTD_LEVEL_LEVELS
122 /* Forward Declaration to avoid visibility problems */
125 /* Common signature for all zio compress functions. */
126 typedef size_t zio_compress_func_t(abd_t
*src
, abd_t
*dst
,
127 size_t s_len
, size_t d_len
, int);
128 /* Common signature for all zio decompress functions. */
129 typedef int zio_decompress_func_t(abd_t
*src
, abd_t
*dst
,
130 size_t s_len
, size_t d_len
, int);
131 /* Common signature for all zio decompress and get level functions. */
132 typedef int zio_decompresslevel_func_t(abd_t
*src
, abd_t
*dst
,
133 size_t s_len
, size_t d_len
, uint8_t *level
);
136 * Information about each compression function.
138 typedef const struct zio_compress_info
{
141 zio_compress_func_t
*ci_compress
;
142 zio_decompress_func_t
*ci_decompress
;
143 zio_decompresslevel_func_t
*ci_decompress_level
;
144 } zio_compress_info_t
;
146 extern zio_compress_info_t zio_compress_table
[ZIO_COMPRESS_FUNCTIONS
];
149 * lz4 compression init & free
151 extern void lz4_init(void);
152 extern void lz4_fini(void);
155 * Compression routines.
157 extern size_t zfs_lzjb_compress(abd_t
*src
, abd_t
*dst
, size_t s_len
,
158 size_t d_len
, int level
);
159 extern int zfs_lzjb_decompress(abd_t
*src
, abd_t
*dst
, size_t s_len
,
160 size_t d_len
, int level
);
161 extern size_t zfs_gzip_compress(abd_t
*src
, abd_t
*dst
, size_t s_len
,
162 size_t d_len
, int level
);
163 extern int zfs_gzip_decompress(abd_t
*src
, abd_t
*dst
, size_t s_len
,
164 size_t d_len
, int level
);
165 extern size_t zfs_zle_compress(abd_t
*src
, abd_t
*dst
, size_t s_len
,
166 size_t d_len
, int level
);
167 extern int zfs_zle_decompress(abd_t
*src
, abd_t
*dst
, size_t s_len
,
168 size_t d_len
, int level
);
169 extern size_t zfs_lz4_compress(abd_t
*src
, abd_t
*dst
, size_t s_len
,
170 size_t d_len
, int level
);
171 extern int zfs_lz4_decompress(abd_t
*src
, abd_t
*dst
, size_t s_len
,
172 size_t d_len
, int level
);
175 * Compress and decompress data if necessary.
177 extern size_t zio_compress_data(enum zio_compress c
, abd_t
*src
, abd_t
**dst
,
178 size_t s_len
, size_t d_len
, uint8_t level
);
179 extern int zio_decompress_data(enum zio_compress c
, abd_t
*src
, abd_t
*abd
,
180 size_t s_len
, size_t d_len
, uint8_t *level
);
181 extern int zio_compress_to_feature(enum zio_compress comp
);
183 #define ZFS_COMPRESS_WRAP_DECL(name) \
185 name(abd_t *src, abd_t *dst, size_t s_len, size_t d_len, int n) \
187 void *s_buf = abd_borrow_buf_copy(src, s_len); \
188 void *d_buf = abd_borrow_buf(dst, d_len); \
189 size_t c_len = name##_buf(s_buf, d_buf, s_len, d_len, n); \
190 abd_return_buf(src, s_buf, s_len); \
191 abd_return_buf_copy(dst, d_buf, d_len); \
194 #define ZFS_DECOMPRESS_WRAP_DECL(name) \
196 name(abd_t *src, abd_t *dst, size_t s_len, size_t d_len, int n) \
198 void *s_buf = abd_borrow_buf_copy(src, s_len); \
199 void *d_buf = abd_borrow_buf(dst, d_len); \
200 int err = name##_buf(s_buf, d_buf, s_len, d_len, n); \
201 abd_return_buf(src, s_buf, s_len); \
202 abd_return_buf_copy(dst, d_buf, d_len); \
205 #define ZFS_DECOMPRESS_LEVEL_WRAP_DECL(name) \
207 name(abd_t *src, abd_t *dst, size_t s_len, size_t d_len, uint8_t *n) \
209 void *s_buf = abd_borrow_buf_copy(src, s_len); \
210 void *d_buf = abd_borrow_buf(dst, d_len); \
211 int err = name##_buf(s_buf, d_buf, s_len, d_len, n); \
212 abd_return_buf(src, s_buf, s_len); \
213 abd_return_buf_copy(dst, d_buf, d_len); \
221 #endif /* _SYS_ZIO_COMPRESS_H */