Files
mirror_zfs/module/zfs/zio_compress.c
T

182 lines
4.7 KiB
C
Raw Normal View History

2025-01-04 11:04:27 +11:00
// SPDX-License-Identifier: CDDL-1.0
2008-11-20 12:01:55 -08:00
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2022-07-11 23:16:13 +02:00
* or https://opensource.org/licenses/CDDL-1.0.
2008-11-20 12:01:55 -08:00
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2008-11-20 12:01:55 -08:00
* Use is subject to license terms.
* Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
* Copyright (c) 2013, 2018 by Delphix. All rights reserved.
* Copyright (c) 2019, 2024, Klara, Inc.
2020-08-18 19:10:17 +02:00
* Copyright (c) 2019, Allan Jude
2019-09-10 23:34:53 +03:00
* Copyright (c) 2021, 2024 by George Melikov. All rights reserved.
2013-03-08 10:41:28 -08:00
*/
2008-11-20 12:01:55 -08:00
#include <sys/zfs_context.h>
#include <sys/spa.h>
#include <sys/zfeature.h>
2008-11-20 12:01:55 -08:00
#include <sys/zio.h>
#include <sys/zio_compress.h>
2020-08-18 19:10:17 +02:00
#include <sys/zstd/zstd.h>
2008-11-20 12:01:55 -08:00
/*
* Compression vectors.
*/
2022-10-01 00:34:39 +02:00
zio_compress_info_t zio_compress_table[ZIO_COMPRESS_FUNCTIONS] = {
{"inherit", 0, NULL, NULL, NULL},
{"on", 0, NULL, NULL, NULL},
{"uncompressed", 0, NULL, NULL, NULL},
{"lzjb", 0,
zfs_lzjb_compress, zfs_lzjb_decompress, NULL},
{"empty", 0, NULL, NULL, NULL},
{"gzip-1", 1,
zfs_gzip_compress, zfs_gzip_decompress, NULL},
{"gzip-2", 2,
zfs_gzip_compress, zfs_gzip_decompress, NULL},
{"gzip-3", 3,
zfs_gzip_compress, zfs_gzip_decompress, NULL},
{"gzip-4", 4,
zfs_gzip_compress, zfs_gzip_decompress, NULL},
{"gzip-5", 5,
zfs_gzip_compress, zfs_gzip_decompress, NULL},
{"gzip-6", 6,
zfs_gzip_compress, zfs_gzip_decompress, NULL},
{"gzip-7", 7,
zfs_gzip_compress, zfs_gzip_decompress, NULL},
{"gzip-8", 8,
zfs_gzip_compress, zfs_gzip_decompress, NULL},
{"gzip-9", 9,
zfs_gzip_compress, zfs_gzip_decompress, NULL},
{"zle", 64,
zfs_zle_compress, zfs_zle_decompress, NULL},
{"lz4", 0,
zfs_lz4_compress, zfs_lz4_decompress, NULL},
{"zstd", ZIO_ZSTD_LEVEL_DEFAULT,
zfs_zstd_compress, zfs_zstd_decompress, zfs_zstd_decompress_level},
2008-11-20 12:01:55 -08:00
};
2020-08-18 19:10:17 +02:00
uint8_t
zio_complevel_select(spa_t *spa, enum zio_compress compress, uint8_t child,
uint8_t parent)
{
2021-12-12 16:06:44 +01:00
(void) spa;
2020-08-18 19:10:17 +02:00
uint8_t result;
if (!ZIO_COMPRESS_HASLEVEL(compress))
return (0);
result = child;
if (result == ZIO_COMPLEVEL_INHERIT)
result = parent;
return (result);
}
enum zio_compress
zio_compress_select(spa_t *spa, enum zio_compress child,
enum zio_compress parent)
2008-11-20 12:01:55 -08:00
{
enum zio_compress result;
2008-11-20 12:01:55 -08:00
ASSERT(child < ZIO_COMPRESS_FUNCTIONS);
ASSERT(parent < ZIO_COMPRESS_FUNCTIONS);
ASSERT(parent != ZIO_COMPRESS_INHERIT);
2008-11-20 12:01:55 -08:00
result = child;
if (result == ZIO_COMPRESS_INHERIT)
result = parent;
2008-11-20 12:01:55 -08:00
if (result == ZIO_COMPRESS_ON) {
if (spa_feature_is_active(spa, SPA_FEATURE_LZ4_COMPRESS))
result = ZIO_COMPRESS_LZ4_ON_VALUE;
else
result = ZIO_COMPRESS_LEGACY_ON_VALUE;
}
2008-11-20 12:01:55 -08:00
return (result);
2008-11-20 12:01:55 -08:00
}
size_t
zio_compress_data(enum zio_compress c, abd_t *src, abd_t **dst, size_t s_len,
2019-09-10 23:34:53 +03:00
size_t d_len, uint8_t level)
2008-11-20 12:01:55 -08:00
{
2019-09-10 23:34:53 +03:00
size_t c_len;
2020-08-18 19:10:17 +02:00
uint8_t complevel;
zio_compress_info_t *ci = &zio_compress_table[c];
2008-11-20 12:01:55 -08:00
2026-02-19 10:15:41 -08:00
ASSERT3P(ci->ci_compress, !=, NULL);
ASSERT3U(s_len, >, 0);
2020-08-18 19:10:17 +02:00
complevel = ci->ci_level;
if (c == ZIO_COMPRESS_ZSTD) {
/* If we don't know the level, we can't compress it */
if (level == ZIO_COMPLEVEL_INHERIT)
return (s_len);
if (level == ZIO_COMPLEVEL_DEFAULT)
complevel = ZIO_ZSTD_LEVEL_DEFAULT;
else
complevel = level;
ASSERT3U(complevel, !=, ZIO_COMPLEVEL_INHERIT);
}
if (*dst == NULL)
*dst = abd_alloc_sametype(src, s_len);
c_len = ci->ci_compress(src, *dst, s_len, d_len, complevel);
2008-11-20 12:01:55 -08:00
if (c_len > d_len)
return (s_len);
2008-11-20 12:01:55 -08:00
return (c_len);
2008-11-20 12:01:55 -08:00
}
int
zio_decompress_data(enum zio_compress c, abd_t *src, abd_t *dst,
2020-08-18 19:10:17 +02:00
size_t s_len, size_t d_len, uint8_t *level)
2008-11-20 12:01:55 -08:00
{
zio_compress_info_t *ci = &zio_compress_table[c];
if ((uint_t)c >= ZIO_COMPRESS_FUNCTIONS || ci->ci_decompress == NULL)
2013-03-08 10:41:28 -08:00
return (SET_ERROR(EINVAL));
2008-11-20 12:01:55 -08:00
2024-06-29 11:47:16 +10:00
int err;
if (ci->ci_decompress_level != NULL && level != NULL)
err = ci->ci_decompress_level(src, dst, s_len, d_len, level);
2024-06-29 11:47:16 +10:00
else
err = ci->ci_decompress(src, dst, s_len, d_len, ci->ci_level);
2024-06-29 11:47:16 +10:00
return (err);
}
2020-08-18 19:10:17 +02:00
int
zio_compress_to_feature(enum zio_compress comp)
{
switch (comp) {
case ZIO_COMPRESS_ZSTD:
return (SPA_FEATURE_ZSTD_COMPRESS);
default:
2021-09-14 09:17:54 -07:00
break;
2020-08-18 19:10:17 +02:00
}
return (SPA_FEATURE_NONE);
}