1 /* 2 * CDDL HEADER START 3 * 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. 7 * 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. 12 * 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] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Copyright (c) 2019, Allan Jude 25 * Copyright (c) 2019, 2024, Klara, Inc. 26 * Use is subject to license terms. 27 * Copyright (c) 2015, 2016 by Delphix. All rights reserved. 28 * Copyright (c) 2021, 2024 by George Melikov. All rights reserved. 29 */ 30 31 #ifndef _SYS_ZIO_COMPRESS_H 32 #define _SYS_ZIO_COMPRESS_H 33 34 #include <sys/abd.h> 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 enum zio_compress { 41 ZIO_COMPRESS_INHERIT = 0, 42 ZIO_COMPRESS_ON, 43 ZIO_COMPRESS_OFF, 44 ZIO_COMPRESS_LZJB, 45 ZIO_COMPRESS_EMPTY, 46 ZIO_COMPRESS_GZIP_1, 47 ZIO_COMPRESS_GZIP_2, 48 ZIO_COMPRESS_GZIP_3, 49 ZIO_COMPRESS_GZIP_4, 50 ZIO_COMPRESS_GZIP_5, 51 ZIO_COMPRESS_GZIP_6, 52 ZIO_COMPRESS_GZIP_7, 53 ZIO_COMPRESS_GZIP_8, 54 ZIO_COMPRESS_GZIP_9, 55 ZIO_COMPRESS_ZLE, 56 ZIO_COMPRESS_LZ4, 57 ZIO_COMPRESS_ZSTD, 58 ZIO_COMPRESS_FUNCTIONS 59 }; 60 61 /* Compression algorithms that have levels */ 62 #define ZIO_COMPRESS_HASLEVEL(compress) ((compress == ZIO_COMPRESS_ZSTD || \ 63 (compress >= ZIO_COMPRESS_GZIP_1 && \ 64 compress <= ZIO_COMPRESS_GZIP_9))) 65 66 #define ZIO_COMPLEVEL_INHERIT 0 67 #define ZIO_COMPLEVEL_DEFAULT 255 68 69 enum zio_zstd_levels { 70 ZIO_ZSTD_LEVEL_INHERIT = 0, 71 ZIO_ZSTD_LEVEL_1, 72 #define ZIO_ZSTD_LEVEL_MIN ZIO_ZSTD_LEVEL_1 73 ZIO_ZSTD_LEVEL_2, 74 ZIO_ZSTD_LEVEL_3, 75 #define ZIO_ZSTD_LEVEL_DEFAULT ZIO_ZSTD_LEVEL_3 76 ZIO_ZSTD_LEVEL_4, 77 ZIO_ZSTD_LEVEL_5, 78 ZIO_ZSTD_LEVEL_6, 79 ZIO_ZSTD_LEVEL_7, 80 ZIO_ZSTD_LEVEL_8, 81 ZIO_ZSTD_LEVEL_9, 82 ZIO_ZSTD_LEVEL_10, 83 ZIO_ZSTD_LEVEL_11, 84 ZIO_ZSTD_LEVEL_12, 85 ZIO_ZSTD_LEVEL_13, 86 ZIO_ZSTD_LEVEL_14, 87 ZIO_ZSTD_LEVEL_15, 88 ZIO_ZSTD_LEVEL_16, 89 ZIO_ZSTD_LEVEL_17, 90 ZIO_ZSTD_LEVEL_18, 91 ZIO_ZSTD_LEVEL_19, 92 #define ZIO_ZSTD_LEVEL_MAX ZIO_ZSTD_LEVEL_19 93 ZIO_ZSTD_LEVEL_RESERVE = 101, /* Leave room for new positive levels */ 94 ZIO_ZSTD_LEVEL_FAST, /* Fast levels are negative */ 95 ZIO_ZSTD_LEVEL_FAST_1, 96 #define ZIO_ZSTD_LEVEL_FAST_DEFAULT ZIO_ZSTD_LEVEL_FAST_1 97 ZIO_ZSTD_LEVEL_FAST_2, 98 ZIO_ZSTD_LEVEL_FAST_3, 99 ZIO_ZSTD_LEVEL_FAST_4, 100 ZIO_ZSTD_LEVEL_FAST_5, 101 ZIO_ZSTD_LEVEL_FAST_6, 102 ZIO_ZSTD_LEVEL_FAST_7, 103 ZIO_ZSTD_LEVEL_FAST_8, 104 ZIO_ZSTD_LEVEL_FAST_9, 105 ZIO_ZSTD_LEVEL_FAST_10, 106 ZIO_ZSTD_LEVEL_FAST_20, 107 ZIO_ZSTD_LEVEL_FAST_30, 108 ZIO_ZSTD_LEVEL_FAST_40, 109 ZIO_ZSTD_LEVEL_FAST_50, 110 ZIO_ZSTD_LEVEL_FAST_60, 111 ZIO_ZSTD_LEVEL_FAST_70, 112 ZIO_ZSTD_LEVEL_FAST_80, 113 ZIO_ZSTD_LEVEL_FAST_90, 114 ZIO_ZSTD_LEVEL_FAST_100, 115 ZIO_ZSTD_LEVEL_FAST_500, 116 ZIO_ZSTD_LEVEL_FAST_1000, 117 #define ZIO_ZSTD_LEVEL_FAST_MAX ZIO_ZSTD_LEVEL_FAST_1000 118 ZIO_ZSTD_LEVEL_AUTO = 251, /* Reserved for future use */ 119 ZIO_ZSTD_LEVEL_LEVELS 120 }; 121 122 /* Forward Declaration to avoid visibility problems */ 123 struct zio_prop; 124 125 /* Common signature for all zio compress functions. */ 126 typedef size_t zio_compress_func_t(abd_t *src, abd_t *dst, 127 size_t s_len, size_t d_len, int); 128 /* Common signature for all zio decompress functions. */ 129 typedef int zio_decompress_func_t(abd_t *src, abd_t *dst, 130 size_t s_len, size_t d_len, int); 131 /* Common signature for all zio decompress and get level functions. */ 132 typedef int zio_decompresslevel_func_t(abd_t *src, abd_t *dst, 133 size_t s_len, size_t d_len, uint8_t *level); 134 135 /* 136 * Information about each compression function. 137 */ 138 typedef const struct zio_compress_info { 139 const char *ci_name; 140 int ci_level; 141 zio_compress_func_t *ci_compress; 142 zio_decompress_func_t *ci_decompress; 143 zio_decompresslevel_func_t *ci_decompress_level; 144 } zio_compress_info_t; 145 146 extern zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS]; 147 148 /* 149 * lz4 compression init & free 150 */ 151 extern void lz4_init(void); 152 extern void lz4_fini(void); 153 154 /* 155 * Compression routines. 156 */ 157 extern size_t zfs_lzjb_compress(abd_t *src, abd_t *dst, size_t s_len, 158 size_t d_len, int level); 159 extern int zfs_lzjb_decompress(abd_t *src, abd_t *dst, size_t s_len, 160 size_t d_len, int level); 161 extern size_t zfs_gzip_compress(abd_t *src, abd_t *dst, size_t s_len, 162 size_t d_len, int level); 163 extern int zfs_gzip_decompress(abd_t *src, abd_t *dst, size_t s_len, 164 size_t d_len, int level); 165 extern size_t zfs_zle_compress(abd_t *src, abd_t *dst, size_t s_len, 166 size_t d_len, int level); 167 extern int zfs_zle_decompress(abd_t *src, abd_t *dst, size_t s_len, 168 size_t d_len, int level); 169 extern size_t zfs_lz4_compress(abd_t *src, abd_t *dst, size_t s_len, 170 size_t d_len, int level); 171 extern int zfs_lz4_decompress(abd_t *src, abd_t *dst, size_t s_len, 172 size_t d_len, int level); 173 174 /* 175 * Compress and decompress data if necessary. 176 */ 177 extern size_t zio_compress_data(enum zio_compress c, abd_t *src, abd_t **dst, 178 size_t s_len, size_t d_len, uint8_t level); 179 extern int zio_decompress_data(enum zio_compress c, abd_t *src, abd_t *abd, 180 size_t s_len, size_t d_len, uint8_t *level); 181 extern int zio_compress_to_feature(enum zio_compress comp); 182 183 #define ZFS_COMPRESS_WRAP_DECL(name) \ 184 size_t \ 185 name(abd_t *src, abd_t *dst, size_t s_len, size_t d_len, int n) \ 186 { \ 187 void *s_buf = abd_borrow_buf_copy(src, s_len); \ 188 void *d_buf = abd_borrow_buf(dst, d_len); \ 189 size_t c_len = name##_buf(s_buf, d_buf, s_len, d_len, n); \ 190 abd_return_buf(src, s_buf, s_len); \ 191 abd_return_buf_copy(dst, d_buf, d_len); \ 192 return (c_len); \ 193 } 194 #define ZFS_DECOMPRESS_WRAP_DECL(name) \ 195 int \ 196 name(abd_t *src, abd_t *dst, size_t s_len, size_t d_len, int n) \ 197 { \ 198 void *s_buf = abd_borrow_buf_copy(src, s_len); \ 199 void *d_buf = abd_borrow_buf(dst, d_len); \ 200 int err = name##_buf(s_buf, d_buf, s_len, d_len, n); \ 201 abd_return_buf(src, s_buf, s_len); \ 202 abd_return_buf_copy(dst, d_buf, d_len); \ 203 return (err); \ 204 } 205 #define ZFS_DECOMPRESS_LEVEL_WRAP_DECL(name) \ 206 int \ 207 name(abd_t *src, abd_t *dst, size_t s_len, size_t d_len, uint8_t *n) \ 208 { \ 209 void *s_buf = abd_borrow_buf_copy(src, s_len); \ 210 void *d_buf = abd_borrow_buf(dst, d_len); \ 211 int err = name##_buf(s_buf, d_buf, s_len, d_len, n); \ 212 abd_return_buf(src, s_buf, s_len); \ 213 abd_return_buf_copy(dst, d_buf, d_len); \ 214 return (err); \ 215 } 216 217 #ifdef __cplusplus 218 } 219 #endif 220 221 #endif /* _SYS_ZIO_COMPRESS_H */ 222