Linux 6.12 compat: META
[zfs.git] / module / zfs / zio_compress.c
blob1a0178eb28307c4bd97ec1f2514ac076adbfbd2a
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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.
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>
40 * If nonzero, every 1/X decompression attempts will fail, simulating
41 * an undetected memory error.
43 static unsigned long zio_decompress_fail_fraction = 0;
46 * Compression vectors.
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},
81 uint8_t
82 zio_complevel_select(spa_t *spa, enum zio_compress compress, uint8_t child,
83 uint8_t parent)
85 (void) spa;
86 uint8_t result;
88 if (!ZIO_COMPRESS_HASLEVEL(compress))
89 return (0);
91 result = child;
92 if (result == ZIO_COMPLEVEL_INHERIT)
93 result = parent;
95 return (result);
98 enum zio_compress
99 zio_compress_select(spa_t *spa, enum zio_compress child,
100 enum zio_compress parent)
102 enum zio_compress result;
104 ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
105 ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
106 ASSERT(parent != ZIO_COMPRESS_INHERIT);
108 result = child;
109 if (result == ZIO_COMPRESS_INHERIT)
110 result = parent;
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;
119 return (result);
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)
126 size_t c_len;
127 uint8_t complevel;
128 zio_compress_info_t *ci = &zio_compress_table[c];
130 ASSERT3U(ci->ci_compress, !=, NULL);
131 ASSERT3U(s_len, >, 0);
133 complevel = ci->ci_level;
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);
140 if (level == ZIO_COMPLEVEL_DEFAULT)
141 complevel = ZIO_ZSTD_LEVEL_DEFAULT;
142 else
143 complevel = level;
145 ASSERT3U(complevel, !=, ZIO_COMPLEVEL_INHERIT);
148 if (*dst == NULL)
149 *dst = abd_alloc_sametype(src, s_len);
151 c_len = ci->ci_compress(src, *dst, s_len, d_len, complevel);
153 if (c_len > d_len)
154 return (s_len);
156 return (c_len);
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)
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));
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);
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).
178 if (zio_decompress_fail_fraction != 0 &&
179 random_in_range(zio_decompress_fail_fraction) == 0)
180 err = SET_ERROR(EINVAL);
182 return (err);
186 zio_compress_to_feature(enum zio_compress comp)
188 switch (comp) {
189 case ZIO_COMPRESS_ZSTD:
190 return (SPA_FEATURE_ZSTD_COMPRESS);
191 default:
192 break;
194 return (SPA_FEATURE_NONE);