xref: /freebsd-src/sys/contrib/openzfs/module/zfs/zio_compress.c (revision 53b70c86d93c1e4d3c76f1282e94154e88780d7e)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24eda14cbcSMatt Macy  * Use is subject to license terms.
25eda14cbcSMatt Macy  */
26eda14cbcSMatt Macy /*
27eda14cbcSMatt Macy  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
28eda14cbcSMatt Macy  */
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy /*
31eda14cbcSMatt Macy  * Copyright (c) 2013, 2018 by Delphix. All rights reserved.
32eda14cbcSMatt Macy  * Copyright (c) 2019, Klara Inc.
33eda14cbcSMatt Macy  * Copyright (c) 2019, Allan Jude
34eda14cbcSMatt Macy  */
35eda14cbcSMatt Macy 
36eda14cbcSMatt Macy #include <sys/zfs_context.h>
37eda14cbcSMatt Macy #include <sys/spa.h>
38eda14cbcSMatt Macy #include <sys/zfeature.h>
39eda14cbcSMatt Macy #include <sys/zio.h>
40eda14cbcSMatt Macy #include <sys/zio_compress.h>
41eda14cbcSMatt Macy #include <sys/zstd/zstd.h>
42eda14cbcSMatt Macy 
43eda14cbcSMatt Macy /*
44eda14cbcSMatt Macy  * If nonzero, every 1/X decompression attempts will fail, simulating
45eda14cbcSMatt Macy  * an undetected memory error.
46eda14cbcSMatt Macy  */
47eda14cbcSMatt Macy unsigned long zio_decompress_fail_fraction = 0;
48eda14cbcSMatt Macy 
49eda14cbcSMatt Macy /*
50eda14cbcSMatt Macy  * Compression vectors.
51eda14cbcSMatt Macy  */
52eda14cbcSMatt Macy zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
53eda14cbcSMatt Macy 	{"inherit",	0,	NULL,		NULL, NULL},
54eda14cbcSMatt Macy 	{"on",		0,	NULL,		NULL, NULL},
55eda14cbcSMatt Macy 	{"uncompressed", 0,	NULL,		NULL, NULL},
56eda14cbcSMatt Macy 	{"lzjb",	0,	lzjb_compress,	lzjb_decompress, NULL},
57eda14cbcSMatt Macy 	{"empty",	0,	NULL,		NULL, NULL},
58eda14cbcSMatt Macy 	{"gzip-1",	1,	gzip_compress,	gzip_decompress, NULL},
59eda14cbcSMatt Macy 	{"gzip-2",	2,	gzip_compress,	gzip_decompress, NULL},
60eda14cbcSMatt Macy 	{"gzip-3",	3,	gzip_compress,	gzip_decompress, NULL},
61eda14cbcSMatt Macy 	{"gzip-4",	4,	gzip_compress,	gzip_decompress, NULL},
62eda14cbcSMatt Macy 	{"gzip-5",	5,	gzip_compress,	gzip_decompress, NULL},
63eda14cbcSMatt Macy 	{"gzip-6",	6,	gzip_compress,	gzip_decompress, NULL},
64eda14cbcSMatt Macy 	{"gzip-7",	7,	gzip_compress,	gzip_decompress, NULL},
65eda14cbcSMatt Macy 	{"gzip-8",	8,	gzip_compress,	gzip_decompress, NULL},
66eda14cbcSMatt Macy 	{"gzip-9",	9,	gzip_compress,	gzip_decompress, NULL},
67eda14cbcSMatt Macy 	{"zle",		64,	zle_compress,	zle_decompress, NULL},
68eda14cbcSMatt Macy 	{"lz4",		0,	lz4_compress_zfs, lz4_decompress_zfs, NULL},
69eda14cbcSMatt Macy 	{"zstd",	ZIO_ZSTD_LEVEL_DEFAULT,	zfs_zstd_compress,
70eda14cbcSMatt Macy 	    zfs_zstd_decompress, zfs_zstd_decompress_level},
71eda14cbcSMatt Macy };
72eda14cbcSMatt Macy 
73eda14cbcSMatt Macy uint8_t
74eda14cbcSMatt Macy zio_complevel_select(spa_t *spa, enum zio_compress compress, uint8_t child,
75eda14cbcSMatt Macy     uint8_t parent)
76eda14cbcSMatt Macy {
77eda14cbcSMatt Macy 	uint8_t result;
78eda14cbcSMatt Macy 
79eda14cbcSMatt Macy 	if (!ZIO_COMPRESS_HASLEVEL(compress))
80eda14cbcSMatt Macy 		return (0);
81eda14cbcSMatt Macy 
82eda14cbcSMatt Macy 	result = child;
83eda14cbcSMatt Macy 	if (result == ZIO_COMPLEVEL_INHERIT)
84eda14cbcSMatt Macy 		result = parent;
85eda14cbcSMatt Macy 
86eda14cbcSMatt Macy 	return (result);
87eda14cbcSMatt Macy }
88eda14cbcSMatt Macy 
89eda14cbcSMatt Macy enum zio_compress
90eda14cbcSMatt Macy zio_compress_select(spa_t *spa, enum zio_compress child,
91eda14cbcSMatt Macy     enum zio_compress parent)
92eda14cbcSMatt Macy {
93eda14cbcSMatt Macy 	enum zio_compress result;
94eda14cbcSMatt Macy 
95eda14cbcSMatt Macy 	ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
96eda14cbcSMatt Macy 	ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
97eda14cbcSMatt Macy 	ASSERT(parent != ZIO_COMPRESS_INHERIT);
98eda14cbcSMatt Macy 
99eda14cbcSMatt Macy 	result = child;
100eda14cbcSMatt Macy 	if (result == ZIO_COMPRESS_INHERIT)
101eda14cbcSMatt Macy 		result = parent;
102eda14cbcSMatt Macy 
103eda14cbcSMatt Macy 	if (result == ZIO_COMPRESS_ON) {
104eda14cbcSMatt Macy 		if (spa_feature_is_active(spa, SPA_FEATURE_LZ4_COMPRESS))
105eda14cbcSMatt Macy 			result = ZIO_COMPRESS_LZ4_ON_VALUE;
106eda14cbcSMatt Macy 		else
107eda14cbcSMatt Macy 			result = ZIO_COMPRESS_LEGACY_ON_VALUE;
108eda14cbcSMatt Macy 	}
109eda14cbcSMatt Macy 
110eda14cbcSMatt Macy 	return (result);
111eda14cbcSMatt Macy }
112eda14cbcSMatt Macy 
113eda14cbcSMatt Macy /*ARGSUSED*/
114eda14cbcSMatt Macy static int
115eda14cbcSMatt Macy zio_compress_zeroed_cb(void *data, size_t len, void *private)
116eda14cbcSMatt Macy {
117eda14cbcSMatt Macy 	uint64_t *end = (uint64_t *)((char *)data + len);
118eda14cbcSMatt Macy 	for (uint64_t *word = (uint64_t *)data; word < end; word++)
119eda14cbcSMatt Macy 		if (*word != 0)
120eda14cbcSMatt Macy 			return (1);
121eda14cbcSMatt Macy 
122eda14cbcSMatt Macy 	return (0);
123eda14cbcSMatt Macy }
124eda14cbcSMatt Macy 
125eda14cbcSMatt Macy size_t
126eda14cbcSMatt Macy zio_compress_data(enum zio_compress c, abd_t *src, void *dst, size_t s_len,
127eda14cbcSMatt Macy     uint8_t level)
128eda14cbcSMatt Macy {
129eda14cbcSMatt Macy 	size_t c_len, d_len;
130eda14cbcSMatt Macy 	uint8_t complevel;
131eda14cbcSMatt Macy 	zio_compress_info_t *ci = &zio_compress_table[c];
132eda14cbcSMatt Macy 
133eda14cbcSMatt Macy 	ASSERT((uint_t)c < ZIO_COMPRESS_FUNCTIONS);
134eda14cbcSMatt Macy 	ASSERT((uint_t)c == ZIO_COMPRESS_EMPTY || ci->ci_compress != NULL);
135eda14cbcSMatt Macy 
136eda14cbcSMatt Macy 	/*
137eda14cbcSMatt Macy 	 * If the data is all zeroes, we don't even need to allocate
138eda14cbcSMatt Macy 	 * a block for it.  We indicate this by returning zero size.
139eda14cbcSMatt Macy 	 */
140eda14cbcSMatt Macy 	if (abd_iterate_func(src, 0, s_len, zio_compress_zeroed_cb, NULL) == 0)
141eda14cbcSMatt Macy 		return (0);
142eda14cbcSMatt Macy 
143eda14cbcSMatt Macy 	if (c == ZIO_COMPRESS_EMPTY)
144eda14cbcSMatt Macy 		return (s_len);
145eda14cbcSMatt Macy 
146eda14cbcSMatt Macy 	/* Compress at least 12.5% */
147eda14cbcSMatt Macy 	d_len = s_len - (s_len >> 3);
148eda14cbcSMatt Macy 
149eda14cbcSMatt Macy 	complevel = ci->ci_level;
150eda14cbcSMatt Macy 
151eda14cbcSMatt Macy 	if (c == ZIO_COMPRESS_ZSTD) {
152eda14cbcSMatt Macy 		/* If we don't know the level, we can't compress it */
153eda14cbcSMatt Macy 		if (level == ZIO_COMPLEVEL_INHERIT)
154eda14cbcSMatt Macy 			return (s_len);
155eda14cbcSMatt Macy 
156eda14cbcSMatt Macy 		if (level == ZIO_COMPLEVEL_DEFAULT)
157eda14cbcSMatt Macy 			complevel = ZIO_ZSTD_LEVEL_DEFAULT;
158eda14cbcSMatt Macy 		else
159eda14cbcSMatt Macy 			complevel = level;
160eda14cbcSMatt Macy 
161eda14cbcSMatt Macy 		ASSERT3U(complevel, !=, ZIO_COMPLEVEL_INHERIT);
162eda14cbcSMatt Macy 	}
163eda14cbcSMatt Macy 
164eda14cbcSMatt Macy 	/* No compression algorithms can read from ABDs directly */
165eda14cbcSMatt Macy 	void *tmp = abd_borrow_buf_copy(src, s_len);
166eda14cbcSMatt Macy 	c_len = ci->ci_compress(tmp, dst, s_len, d_len, complevel);
167eda14cbcSMatt Macy 	abd_return_buf(src, tmp, s_len);
168eda14cbcSMatt Macy 
169eda14cbcSMatt Macy 	if (c_len > d_len)
170eda14cbcSMatt Macy 		return (s_len);
171eda14cbcSMatt Macy 
172eda14cbcSMatt Macy 	ASSERT3U(c_len, <=, d_len);
173eda14cbcSMatt Macy 	return (c_len);
174eda14cbcSMatt Macy }
175eda14cbcSMatt Macy 
176eda14cbcSMatt Macy int
177eda14cbcSMatt Macy zio_decompress_data_buf(enum zio_compress c, void *src, void *dst,
178eda14cbcSMatt Macy     size_t s_len, size_t d_len, uint8_t *level)
179eda14cbcSMatt Macy {
180eda14cbcSMatt Macy 	zio_compress_info_t *ci = &zio_compress_table[c];
181eda14cbcSMatt Macy 	if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL)
182eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
183eda14cbcSMatt Macy 
184eda14cbcSMatt Macy 	if (ci->ci_decompress_level != NULL && level != NULL)
185eda14cbcSMatt Macy 		return (ci->ci_decompress_level(src, dst, s_len, d_len, level));
186eda14cbcSMatt Macy 
187eda14cbcSMatt Macy 	return (ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level));
188eda14cbcSMatt Macy }
189eda14cbcSMatt Macy 
190eda14cbcSMatt Macy int
191eda14cbcSMatt Macy zio_decompress_data(enum zio_compress c, abd_t *src, void *dst,
192eda14cbcSMatt Macy     size_t s_len, size_t d_len, uint8_t *level)
193eda14cbcSMatt Macy {
194eda14cbcSMatt Macy 	void *tmp = abd_borrow_buf_copy(src, s_len);
195eda14cbcSMatt Macy 	int ret = zio_decompress_data_buf(c, tmp, dst, s_len, d_len, level);
196eda14cbcSMatt Macy 	abd_return_buf(src, tmp, s_len);
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy 	/*
199eda14cbcSMatt Macy 	 * Decompression shouldn't fail, because we've already verified
200eda14cbcSMatt Macy 	 * the checksum.  However, for extra protection (e.g. against bitflips
201eda14cbcSMatt Macy 	 * in non-ECC RAM), we handle this error (and test it).
202eda14cbcSMatt Macy 	 */
203eda14cbcSMatt Macy 	if (zio_decompress_fail_fraction != 0 &&
20433b8c039SMartin Matuska 	    random_in_range(zio_decompress_fail_fraction) == 0)
205eda14cbcSMatt Macy 		ret = SET_ERROR(EINVAL);
206eda14cbcSMatt Macy 
207eda14cbcSMatt Macy 	return (ret);
208eda14cbcSMatt Macy }
209eda14cbcSMatt Macy 
210eda14cbcSMatt Macy int
211eda14cbcSMatt Macy zio_compress_to_feature(enum zio_compress comp)
212eda14cbcSMatt Macy {
213eda14cbcSMatt Macy 	switch (comp) {
214eda14cbcSMatt Macy 	case ZIO_COMPRESS_ZSTD:
215eda14cbcSMatt Macy 		return (SPA_FEATURE_ZSTD_COMPRESS);
216eda14cbcSMatt Macy 	default:
217*53b70c86SMartin Matuska 		break;
218eda14cbcSMatt Macy 	}
219eda14cbcSMatt Macy 	return (SPA_FEATURE_NONE);
220eda14cbcSMatt Macy }
221