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 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
},
53 zfs_lzjb_compress
, zfs_lzjb_decompress
, NULL
},
54 {"empty", 0, NULL
, NULL
, NULL
},
56 zfs_gzip_compress
, zfs_gzip_decompress
, NULL
},
58 zfs_gzip_compress
, zfs_gzip_decompress
, 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_zle_compress
, zfs_zle_decompress
, NULL
},
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
},
82 zio_complevel_select(spa_t
*spa
, enum zio_compress compress
, uint8_t child
,
88 if (!ZIO_COMPRESS_HASLEVEL(compress
))
92 if (result
== ZIO_COMPLEVEL_INHERIT
)
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
);
109 if (result
== ZIO_COMPRESS_INHERIT
)
112 if (result
== ZIO_COMPRESS_ON
) {
113 if (spa_feature_is_active(spa
, SPA_FEATURE_LZ4_COMPRESS
))
114 result
= ZIO_COMPRESS_LZ4_ON_VALUE
;
116 result
= ZIO_COMPRESS_LEGACY_ON_VALUE
;
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
)
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
)
140 if (level
== ZIO_COMPLEVEL_DEFAULT
)
141 complevel
= ZIO_ZSTD_LEVEL_DEFAULT
;
145 ASSERT3U(complevel
, !=, ZIO_COMPLEVEL_INHERIT
);
149 *dst
= abd_alloc_sametype(src
, s_len
);
151 c_len
= ci
->ci_compress(src
, *dst
, s_len
, d_len
, complevel
);
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
));
168 if (ci
->ci_decompress_level
!= NULL
&& level
!= NULL
)
169 err
= ci
->ci_decompress_level(src
, dst
, s_len
, d_len
, level
);
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
);
186 zio_compress_to_feature(enum zio_compress comp
)
189 case ZIO_COMPRESS_ZSTD
:
190 return (SPA_FEATURE_ZSTD_COMPRESS
);
194 return (SPA_FEATURE_NONE
);