File: zio_compress.h

package info (click to toggle)
zfs-linux 2.3.4~git20250812.3b64a96-1
  • links: PTS, VCS
  • area: contrib
  • in suites: experimental
  • size: 70,688 kB
  • sloc: ansic: 393,668; sh: 68,068; asm: 47,734; python: 8,160; makefile: 5,125; perl: 859; sed: 41
file content (222 lines) | stat: -rw-r--r-- 6,881 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// SPDX-License-Identifier: CDDL-1.0
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or https://opensource.org/licenses/CDDL-1.0.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Copyright (c) 2019, Allan Jude
 * Copyright (c) 2019, 2024, Klara, Inc.
 * Use is subject to license terms.
 * Copyright (c) 2015, 2016 by Delphix. All rights reserved.
 * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
 */

#ifndef _SYS_ZIO_COMPRESS_H
#define	_SYS_ZIO_COMPRESS_H

#include <sys/abd.h>

#ifdef	__cplusplus
extern "C" {
#endif

enum zio_compress {
	ZIO_COMPRESS_INHERIT = 0,
	ZIO_COMPRESS_ON,
	ZIO_COMPRESS_OFF,
	ZIO_COMPRESS_LZJB,
	ZIO_COMPRESS_EMPTY,
	ZIO_COMPRESS_GZIP_1,
	ZIO_COMPRESS_GZIP_2,
	ZIO_COMPRESS_GZIP_3,
	ZIO_COMPRESS_GZIP_4,
	ZIO_COMPRESS_GZIP_5,
	ZIO_COMPRESS_GZIP_6,
	ZIO_COMPRESS_GZIP_7,
	ZIO_COMPRESS_GZIP_8,
	ZIO_COMPRESS_GZIP_9,
	ZIO_COMPRESS_ZLE,
	ZIO_COMPRESS_LZ4,
	ZIO_COMPRESS_ZSTD,
	ZIO_COMPRESS_FUNCTIONS
};

/* Compression algorithms that have levels */
#define	ZIO_COMPRESS_HASLEVEL(compress)	((compress == ZIO_COMPRESS_ZSTD || \
					(compress >= ZIO_COMPRESS_GZIP_1 && \
					compress <= ZIO_COMPRESS_GZIP_9)))

#define	ZIO_COMPLEVEL_INHERIT	0
#define	ZIO_COMPLEVEL_DEFAULT	255

enum zio_zstd_levels {
	ZIO_ZSTD_LEVEL_INHERIT = 0,
	ZIO_ZSTD_LEVEL_1,
#define	ZIO_ZSTD_LEVEL_MIN	ZIO_ZSTD_LEVEL_1
	ZIO_ZSTD_LEVEL_2,
	ZIO_ZSTD_LEVEL_3,
#define	ZIO_ZSTD_LEVEL_DEFAULT	ZIO_ZSTD_LEVEL_3
	ZIO_ZSTD_LEVEL_4,
	ZIO_ZSTD_LEVEL_5,
	ZIO_ZSTD_LEVEL_6,
	ZIO_ZSTD_LEVEL_7,
	ZIO_Z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served for future use */
	ZIO_ZSTD_LEVEL_LEVELS
};

/* Forward Declaration to avoid visibility problems */
struct zio_prop;

/* Common signature for all zio compress functions. */
typedef size_t zio_compress_func_t(abd_t *src, abd_t *dst,
    size_t s_len, size_t d_len, int);
/* Common signature for all zio decompress functions. */
typedef int zio_decompress_func_t(abd_t *src, abd_t *dst,
    size_t s_len, size_t d_len, int);
/* Common signature for all zio decompress and get level functions. */
typedef int zio_decompresslevel_func_t(abd_t *src, abd_t *dst,
    size_t s_len, size_t d_len, uint8_t *level);

/*
 * Information about each compression function.
 */
typedef const struct zio_compress_info {
	const char			*ci_name;
	int				ci_level;
	zio_compress_func_t		*ci_compress;
	zio_decompress_func_t		*ci_decompress;
	zio_decompresslevel_func_t	*ci_decompress_level;
} zio_compress_info_t;

extern zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS];

/*
 * lz4 compression init & free
 */
extern void lz4_init(void);
extern void lz4_fini(void);

/*
 * Compression routines.
 */
extern size_t zfs_lzjb_compress(abd_t *src, abd_t *dst, size_t s_len,
    size_t d_len, int level);
extern int zfs_lzjb_decompress(abd_t *src, abd_t *dst, size_t s_len,
    size_t d_len, int level);
extern size_t zfs_gzip_compress(abd_t *src, abd_t *dst, size_t s_len,
    size_t d_len, int level);
extern int zfs_gzip_decompress(abd_t *src, abd_t *dst, size_t s_len,
    size_t d_len, int level);
extern size_t zfs_zle_compress(abd_t *src, abd_t *dst, size_t s_len,
    size_t d_len, int level);
extern int zfs_zle_decompress(abd_t *src, abd_t *dst, size_t s_len,
    size_t d_len, int level);
extern size_t zfs_lz4_compress(abd_t *src, abd_t *dst, size_t s_len,
    size_t d_len, int level);
extern int zfs_lz4_decompress(abd_t *src, abd_t *dst, size_t s_len,
    size_t d_len, int level);

/*
 * Compress and decompress data if necessary.
 */
extern size_t zio_compress_data(enum zio_compress c, abd_t *src, abd_t **dst,
    size_t s_len, size_t d_len, uint8_t level);
extern int zio_decompress_data(enum zio_compress c, abd_t *src, abd_t *abd,
    size_t s_len, size_t d_len, uint8_t *level);
extern int zio_compress_to_feature(enum zio_compress comp);

#define	ZFS_COMPRESS_WRAP_DECL(name)					\
size_t									\
name(abd_t *src, abd_t *dst, size_t s_len, size_t d_len, int n)		\
{									\
	void *s_buf = abd_borrow_buf_copy(src, s_len);			\
	void *d_buf = abd_borrow_buf(dst, d_len);			\
	size_t c_len = name##_buf(s_buf, d_buf, s_len, d_len, n);	\
	abd_return_buf(src, s_buf, s_len);				\
	abd_return_buf_copy(dst, d_buf, d_len);				\
	return (c_len);							\
}
#define	ZFS_DECOMPRESS_WRAP_DECL(name)					\
int									\
name(abd_t *src, abd_t *dst, size_t s_len, size_t d_len, int n)		\
{									\
	void *s_buf = abd_borrow_buf_copy(src, s_len);			\
	void *d_buf = abd_borrow_buf(dst, d_len);			\
	int err = name##_buf(s_buf, d_buf, s_len, d_len, n);		\
	abd_return_buf(src, s_buf, s_len);				\
	abd_return_buf_copy(dst, d_buf, d_len);				\
	return (err);							\
}
#define	ZFS_DECOMPRESS_LEVEL_WRAP_DECL(name)				\
int									\
name(abd_t *src, abd_t *dst, size_t s_len, size_t d_len, uint8_t *n)	\
{									\
	void *s_buf = abd_borrow_buf_copy(src, s_len);			\
	void *d_buf = abd_borrow_buf(dst, d_len);			\
	int err = name##_buf(s_buf, d_buf, s_len, d_len, n);		\
	abd_return_buf(src, s_buf, s_len);				\
	abd_return_buf_copy(dst, d_buf, d_len);				\
	return (err);							\
}

#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_ZIO_COMPRESS_H */