1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
#include "erofs/internal.h"
#include "erofs/print.h"
#include "erofs/config.h"
#include <zstd.h>
#include <zstd_errors.h>
#include <stdlib.h>
#include "compressor.h"
#include "erofs/atomic.h"
struct erofs_libzstd_context {
ZSTD_CCtx *cctx;
u8 *fitblk_buffer;
unsigned int fitblk_bufsiz;
};
static int libzstd_compress_destsize(const struct erofs_compress *c,
const void *src, unsigned int *srcsize,
void *dst, unsigned int dstsize)
{
struct erofs_libzstd_context *ctx = c->private_data;
size_t l = 0; /* largest input that fits so far */
size_t l_csize = 0;
size_t r = *srcsize + 1; /* smallest input that doesn't fit so far */
size_t m;
if (dstsize + 32 > ctx->fitblk_bufsiz) {
u8 *buf = realloc(ctx->fitblk_buffer, dstsize + 32);
if (!buf)
return -ENOMEM;
ctx->fitblk_bufsiz = dstsize + 32;
ctx->fitblk_buffer = buf;
}
m = dstsize * 4;
for (;;) {
size_t csize;
m = max(m, l + 1);
m = min(m, r - 1);
csize = ZSTD_compress2(ctx->cctx, ctx->fitblk_buffer,
dstsize + 32, src, m);
if (ZSTD_isError(csize)) {
if (ZSTD_getErrorCode(csize) == ZSTD_error_dstSize_tooSmall)
goto doesnt_fit;
return -EFAULT;
}
if (csize > 0 && csize <= dstsize) {
/* Fits */
memcpy(dst, ctx->fitblk_buffer, csize);
l = m;
l_csize = csize;
if (r <= l + 1 || csize + 1 >= dstsize)
break;
/*
* Estimate needed input prefix size based on current
* compression ratio.
*/
m = (dstsize * m) / csize;
} else {
doesnt_fit:
/* Doesn't fit */
r = m;
if (r <= l + 1)
break;
m = (l + r) / 2;
}
}
*srcsize = l;
return l_csize;
}
static int compressor_libzstd_exit(struct erofs_compress *c)
{
struct erofs_libzstd_context *ctx = c->private_data;
if (!ctx)
return -EINVAL;
free(ctx->fitblk_buffer);
ZSTD_freeCCtx(ctx->cctx);
free(ctx);
return 0;
}
static int erofs_compressor_libzstd_setlevel(struct erofs_compress *c,
int compression_level)
{
if (compression_level > erofs_compressor_libzstd.best_level) {
erofs_err("invalid compression level %d", compression_level);
return -EINVAL;
}
c->compression_level = compression_level;
return 0;
}
static int erofs_compressor_libzstd_setdictsize(struct erofs_compress *c,
u32 dict_size)
{
if (!dict_size) {
if (erofs_compressor_libzstd.default_dictsize) {
dict_size = erofs_compressor_libzstd.default_dictsize;
} else {
dict_size = min_t(u32, Z_EROFS_ZSTD_MAX_DICT_SIZE,
cfg.c_mkfs_pclustersize_max << 3);
dict_size = 1 << ilog2(dict_size);
}
}
if (dict_size != 1 << ilog2(dict_size) ||
dict_size > Z_EROFS_ZSTD_MAX_DICT_SIZE) {
erofs_err("invalid dictionary size %u", dict_size);
return -EINVAL;
}
c->dict_size = dict_size;
return 0;
}
static int compressor_libzstd_init(struct erofs_compress *c)
{
struct erofs_libzstd_context *ctx = c->private_data;
static erofs_atomic_bool_t __warnonce;
ZSTD_CCtx *cctx;
size_t errcode;
int err;
if (ctx) {
ZSTD_freeCCtx(ctx->cctx);
ctx->cctx = NULL;
c->private_data = NULL;
} else {
ctx = calloc(1, sizeof(*ctx));
if (!ctx)
return -ENOMEM;
}
cctx = ZSTD_createCCtx();
if (!cctx) {
err = -ENOMEM;
goto out_err;
}
err = -EINVAL;
errcode = ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, c->compression_level);
if (ZSTD_isError(errcode)) {
erofs_err("failed to set compression level: %s",
ZSTD_getErrorName(errcode));
goto out_err;
}
errcode = ZSTD_CCtx_setParameter(cctx, ZSTD_c_windowLog, ilog2(c->dict_size));
if (ZSTD_isError(errcode)) {
erofs_err("failed to set window log: %s", ZSTD_getErrorName(errcode));
goto out_err;
}
ctx->cctx = cctx;
c->private_data = ctx;
if (!erofs_atomic_test_and_set(&__warnonce)) {
erofs_warn("EXPERIMENTAL libzstd compressor in use. Note that `fitblk` isn't supported by upstream zstd for now.");
erofs_warn("Therefore it will takes more time in order to get the optimal result.");
erofs_info("You could clarify further needs in zstd repository <https://github.com/facebook/zstd/issues> for reference too.");
}
return 0;
out_err:
ZSTD_freeCCtx(cctx);
free(ctx);
return err;
}
const struct erofs_compressor erofs_compressor_libzstd = {
.default_level = ZSTD_CLEVEL_DEFAULT,
.best_level = 22,
.max_dictsize = Z_EROFS_ZSTD_MAX_DICT_SIZE,
.init = compressor_libzstd_init,
.exit = compressor_libzstd_exit,
.setlevel = erofs_compressor_libzstd_setlevel,
.setdictsize = erofs_compressor_libzstd_setdictsize,
.compress_destsize = libzstd_compress_destsize,
};
|