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]
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>
34 #include <sys/zfeature.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
},
57 zfs_lzjb_compress
, zfs_lzjb_decompress
, NULL
},
58 {"empty", 0, NULL
, NULL
, NULL
},
60 zfs_gzip_compress
, zfs_gzip_decompress
, NULL
},
62 zfs_gzip_compress
, zfs_gzip_decompress
, NULL
},
64 zfs_gzip_compress
, zfs_gzip_decompress
, NULL
},
66 zfs_gzip_compress
, zfs_gzip_decompress
, NULL
},
68 zfs_gzip_compress
, zfs_gzip_decompress
, NULL
},
70 zfs_gzip_compress
, zfs_gzip_decompress
, NULL
},
72 zfs_gzip_compress
, zfs_gzip_decompress
, NULL
},
74 zfs_gzip_compress
, zfs_gzip_decompress
, NULL
},
76 zfs_gzip_compress
, zfs_gzip_decompress
, NULL
},
78 zfs_zle_compress
, zfs_zle_decompress
, NULL
},
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
},
86 zio_complevel_select(spa_t
*spa
, enum zio_compress compress
, uint8_t child
,
92 if (!ZIO_COMPRESS_HASLEVEL(compress
))
96 if (result
== ZIO_COMPLEVEL_INHERIT
)
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
);
113 if (result
== ZIO_COMPRESS_INHERIT
)
116 if (result
== ZIO_COMPRESS_ON
) {
117 if (spa_feature_is_active(spa
, SPA_FEATURE_LZ4_COMPRESS
))
118 result
= ZIO_COMPRESS_LZ4_ON_VALUE
;
120 result
= ZIO_COMPRESS_LEGACY_ON_VALUE
;
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
)
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
)
144 if (level
== ZIO_COMPLEVEL_DEFAULT
)
145 complevel
= ZIO_ZSTD_LEVEL_DEFAULT
;
149 ASSERT3U(complevel
, !=, ZIO_COMPLEVEL_INHERIT
);
153 *dst
= abd_alloc_sametype(src
, s_len
);
155 c_len
= ci
->ci_compress(src
, *dst
, s_len
, d_len
, complevel
);
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
));
172 if (ci
->ci_decompress_level
!= NULL
&& level
!= NULL
)
173 err
= ci
->ci_decompress_level(src
, dst
, s_len
, d_len
, level
);
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
);
190 zio_compress_to_feature(enum zio_compress comp
)
193 case ZIO_COMPRESS_ZSTD
:
194 return (SPA_FEATURE_ZSTD_COMPRESS
);
198 return (SPA_FEATURE_NONE
);