xref: /freebsd/sys/contrib/openzfs/module/zfs/zio_compress.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
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  * Use is subject to license terms.
25  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
26  * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
27  * Copyright (c) 2019, 2024, Klara, Inc.
28  * Copyright (c) 2019, Allan Jude
29  * Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
30  */
31 
32 #include <sys/zfs_context.h>
33 #include <sys/spa.h>
34 #include <sys/zfeature.h>
35 #include <sys/zio.h>
36 #include <sys/zio_compress.h>
37 #include <sys/zstd/zstd.h>
38 
39 /*
40  * If nonzero, every 1/X decompression attempts will fail, simulating
41  * an undetected memory error.
42  */
43 static unsigned long zio_decompress_fail_fraction = 0;
44 
45 /*
46  * Compression vectors.
47  */
48 zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
49 	{"inherit",	0,	NULL,	NULL, NULL},
50 	{"on",		0,	NULL,	NULL, NULL},
51 	{"uncompressed", 0,	NULL,	NULL, NULL},
52 	{"lzjb",	0,
53 	    zfs_lzjb_compress,	zfs_lzjb_decompress, NULL},
54 	{"empty",	0,	NULL,	NULL, NULL},
55 	{"gzip-1",	1,
56 	    zfs_gzip_compress,	zfs_gzip_decompress, NULL},
57 	{"gzip-2",	2,
58 	    zfs_gzip_compress,	zfs_gzip_decompress, NULL},
59 	{"gzip-3",	3,
60 	    zfs_gzip_compress,	zfs_gzip_decompress, NULL},
61 	{"gzip-4",	4,
62 	    zfs_gzip_compress,	zfs_gzip_decompress, NULL},
63 	{"gzip-5",	5,
64 	    zfs_gzip_compress,	zfs_gzip_decompress, NULL},
65 	{"gzip-6",	6,
66 	    zfs_gzip_compress,	zfs_gzip_decompress, NULL},
67 	{"gzip-7",	7,
68 	    zfs_gzip_compress,	zfs_gzip_decompress, NULL},
69 	{"gzip-8",	8,
70 	    zfs_gzip_compress,	zfs_gzip_decompress, NULL},
71 	{"gzip-9",	9,
72 	    zfs_gzip_compress,	zfs_gzip_decompress, NULL},
73 	{"zle",		64,
74 	    zfs_zle_compress,	zfs_zle_decompress, NULL},
75 	{"lz4",		0,
76 	    zfs_lz4_compress,	zfs_lz4_decompress, NULL},
77 	{"zstd",	ZIO_ZSTD_LEVEL_DEFAULT,
78 	    zfs_zstd_compress,	zfs_zstd_decompress, zfs_zstd_decompress_level},
79 };
80 
81 uint8_t
82 zio_complevel_select(spa_t *spa, enum zio_compress compress, uint8_t child,
83     uint8_t parent)
84 {
85 	(void) spa;
86 	uint8_t result;
87 
88 	if (!ZIO_COMPRESS_HASLEVEL(compress))
89 		return (0);
90 
91 	result = child;
92 	if (result == ZIO_COMPLEVEL_INHERIT)
93 		result = parent;
94 
95 	return (result);
96 }
97 
98 enum zio_compress
99 zio_compress_select(spa_t *spa, enum zio_compress child,
100     enum zio_compress parent)
101 {
102 	enum zio_compress result;
103 
104 	ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
105 	ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
106 	ASSERT(parent != ZIO_COMPRESS_INHERIT);
107 
108 	result = child;
109 	if (result == ZIO_COMPRESS_INHERIT)
110 		result = parent;
111 
112 	if (result == ZIO_COMPRESS_ON) {
113 		if (spa_feature_is_active(spa, SPA_FEATURE_LZ4_COMPRESS))
114 			result = ZIO_COMPRESS_LZ4_ON_VALUE;
115 		else
116 			result = ZIO_COMPRESS_LEGACY_ON_VALUE;
117 	}
118 
119 	return (result);
120 }
121 
122 size_t
123 zio_compress_data(enum zio_compress c, abd_t *src, abd_t **dst, size_t s_len,
124     size_t d_len, uint8_t level)
125 {
126 	size_t c_len;
127 	uint8_t complevel;
128 	zio_compress_info_t *ci = &zio_compress_table[c];
129 
130 	ASSERT3U(ci->ci_compress, !=, NULL);
131 	ASSERT3U(s_len, >, 0);
132 
133 	complevel = ci->ci_level;
134 
135 	if (c == ZIO_COMPRESS_ZSTD) {
136 		/* If we don't know the level, we can't compress it */
137 		if (level == ZIO_COMPLEVEL_INHERIT)
138 			return (s_len);
139 
140 		if (level == ZIO_COMPLEVEL_DEFAULT)
141 			complevel = ZIO_ZSTD_LEVEL_DEFAULT;
142 		else
143 			complevel = level;
144 
145 		ASSERT3U(complevel, !=, ZIO_COMPLEVEL_INHERIT);
146 	}
147 
148 	if (*dst == NULL)
149 		*dst = abd_alloc_sametype(src, s_len);
150 
151 	c_len = ci->ci_compress(src, *dst, s_len, d_len, complevel);
152 
153 	if (c_len > d_len)
154 		return (s_len);
155 
156 	return (c_len);
157 }
158 
159 int
160 zio_decompress_data(enum zio_compress c, abd_t *src, abd_t *dst,
161     size_t s_len, size_t d_len, uint8_t *level)
162 {
163 	zio_compress_info_t *ci = &zio_compress_table[c];
164 	if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL)
165 		return (SET_ERROR(EINVAL));
166 
167 	int err;
168 	if (ci->ci_decompress_level != NULL && level != NULL)
169 		err = ci->ci_decompress_level(src, dst, s_len, d_len, level);
170 	else
171 		err = ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level);
172 
173 	/*
174 	 * Decompression shouldn't fail, because we've already verified
175 	 * the checksum.  However, for extra protection (e.g. against bitflips
176 	 * in non-ECC RAM), we handle this error (and test it).
177 	 */
178 	if (zio_decompress_fail_fraction != 0 &&
179 	    random_in_range(zio_decompress_fail_fraction) == 0)
180 		err = SET_ERROR(EINVAL);
181 
182 	return (err);
183 }
184 
185 int
186 zio_compress_to_feature(enum zio_compress comp)
187 {
188 	switch (comp) {
189 	case ZIO_COMPRESS_ZSTD:
190 		return (SPA_FEATURE_ZSTD_COMPRESS);
191 	default:
192 		break;
193 	}
194 	return (SPA_FEATURE_NONE);
195 }
196