Reduce dirty records memory usage
[zfs.git] / module / zfs / zio_compress.c
blob10c4825738623b97c45579ad92e7fc90b8acf3eb
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 * NOTE: DO NOT CHANGE THE NAMES OF THESE COMPRESSION FUNCTIONS.
49 * THEY ARE USED AS ZAP KEY NAMES BY FAST DEDUP AND THEREFORE
50 * PART OF THE ON-DISK FORMAT.
52 zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
53 {"inherit", 0, NULL, NULL, NULL},
54 {"on", 0, NULL, NULL, NULL},
55 {"uncompressed", 0, NULL, NULL, NULL},
56 {"lzjb", 0,
57 zfs_lzjb_compress, zfs_lzjb_decompress, NULL},
58 {"empty", 0, NULL, NULL, NULL},
59 {"gzip-1", 1,
60 zfs_gzip_compress, zfs_gzip_decompress, NULL},
61 {"gzip-2", 2,
62 zfs_gzip_compress, zfs_gzip_decompress, NULL},
63 {"gzip-3", 3,
64 zfs_gzip_compress, zfs_gzip_decompress, NULL},
65 {"gzip-4", 4,
66 zfs_gzip_compress, zfs_gzip_decompress, NULL},
67 {"gzip-5", 5,
68 zfs_gzip_compress, zfs_gzip_decompress, NULL},
69 {"gzip-6", 6,
70 zfs_gzip_compress, zfs_gzip_decompress, NULL},
71 {"gzip-7", 7,
72 zfs_gzip_compress, zfs_gzip_decompress, NULL},
73 {"gzip-8", 8,
74 zfs_gzip_compress, zfs_gzip_decompress, NULL},
75 {"gzip-9", 9,
76 zfs_gzip_compress, zfs_gzip_decompress, NULL},
77 {"zle", 64,
78 zfs_zle_compress, zfs_zle_decompress, NULL},
79 {"lz4", 0,
80 zfs_lz4_compress, zfs_lz4_decompress, NULL},
81 {"zstd", ZIO_ZSTD_LEVEL_DEFAULT,
82 zfs_zstd_compress, zfs_zstd_decompress, zfs_zstd_decompress_level},
85 uint8_t
86 zio_complevel_select(spa_t *spa, enum zio_compress compress, uint8_t child,
87 uint8_t parent)
89 (void) spa;
90 uint8_t result;
92 if (!ZIO_COMPRESS_HASLEVEL(compress))
93 return (0);
95 result = child;
96 if (result == ZIO_COMPLEVEL_INHERIT)
97 result = parent;
99 return (result);
102 enum zio_compress
103 zio_compress_select(spa_t *spa, enum zio_compress child,
104 enum zio_compress parent)
106 enum zio_compress result;
108 ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
109 ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
110 ASSERT(parent != ZIO_COMPRESS_INHERIT);
112 result = child;
113 if (result == ZIO_COMPRESS_INHERIT)
114 result = parent;
116 if (result == ZIO_COMPRESS_ON) {
117 if (spa_feature_is_active(spa, SPA_FEATURE_LZ4_COMPRESS))
118 result = ZIO_COMPRESS_LZ4_ON_VALUE;
119 else
120 result = ZIO_COMPRESS_LEGACY_ON_VALUE;
123 return (result);
126 size_t
127 zio_compress_data(enum zio_compress c, abd_t *src, abd_t **dst, size_t s_len,
128 size_t d_len, uint8_t level)
130 size_t c_len;
131 uint8_t complevel;
132 zio_compress_info_t *ci = &zio_compress_table[c];
134 ASSERT3U(ci->ci_compress, !=, NULL);
135 ASSERT3U(s_len, >, 0);
137 complevel = ci->ci_level;
139 if (c == ZIO_COMPRESS_ZSTD) {
140 /* If we don't know the level, we can't compress it */
141 if (level == ZIO_COMPLEVEL_INHERIT)
142 return (s_len);
144 if (level == ZIO_COMPLEVEL_DEFAULT)
145 complevel = ZIO_ZSTD_LEVEL_DEFAULT;
146 else
147 complevel = level;
149 ASSERT3U(complevel, !=, ZIO_COMPLEVEL_INHERIT);
152 if (*dst == NULL)
153 *dst = abd_alloc_sametype(src, s_len);
155 c_len = ci->ci_compress(src, *dst, s_len, d_len, complevel);
157 if (c_len > d_len)
158 return (s_len);
160 return (c_len);
164 zio_decompress_data(enum zio_compress c, abd_t *src, abd_t *dst,
165 size_t s_len, size_t d_len, uint8_t *level)
167 zio_compress_info_t *ci = &zio_compress_table[c];
168 if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL)
169 return (SET_ERROR(EINVAL));
171 int err;
172 if (ci->ci_decompress_level != NULL && level != NULL)
173 err = ci->ci_decompress_level(src, dst, s_len, d_len, level);
174 else
175 err = ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level);
178 * Decompression shouldn't fail, because we've already verified
179 * the checksum. However, for extra protection (e.g. against bitflips
180 * in non-ECC RAM), we handle this error (and test it).
182 if (zio_decompress_fail_fraction != 0 &&
183 random_in_range(zio_decompress_fail_fraction) == 0)
184 err = SET_ERROR(EINVAL);
186 return (err);
190 zio_compress_to_feature(enum zio_compress comp)
192 switch (comp) {
193 case ZIO_COMPRESS_ZSTD:
194 return (SPA_FEATURE_ZSTD_COMPRESS);
195 default:
196 break;
198 return (SPA_FEATURE_NONE);