lib: zstd: Add kernel-specific API

This patch:
- Moves `include/linux/zstd.h` -> `include/linux/zstd_lib.h`
- Updates modified zstd headers to yearless copyright
- Adds a new API in `include/linux/zstd.h` that is functionally
  equivalent to the in-use subset of the current API. Functions are
  renamed to avoid symbol collisions with zstd, to make it clear it is
  not the upstream zstd API, and to follow the kernel style guide.
- Updates all callers to use the new API.

There are no functional changes in this patch. Since there are no
functional change, I felt it was okay to update all the callers in a
single patch. Once the API is approved, the callers are mechanically
changed.

This patch is preparing for the 3rd patch in this series, which updates
zstd to version 1.4.10. Since the upstream zstd API is no longer exposed
to callers, the update can happen transparently.

Signed-off-by: Nick Terrell <terrelln@fb.com>
Tested By: Paul Jones <paul@pauljones.id.au>
Tested-by: Oleksandr Natalenko <oleksandr@natalenko.name>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com> # LLVM/Clang v13.0.0 on x86-64
Tested-by: Jean-Denis Girard <jd.girard@sysnux.pf>
This commit is contained in:
Nick Terrell 2020-09-11 16:49:00 -07:00
parent d2f38a3c65
commit cf30f6a5f0
11 changed files with 1701 additions and 1168 deletions

View File

@ -18,22 +18,22 @@
#define ZSTD_DEF_LEVEL 3 #define ZSTD_DEF_LEVEL 3
struct zstd_ctx { struct zstd_ctx {
ZSTD_CCtx *cctx; zstd_cctx *cctx;
ZSTD_DCtx *dctx; zstd_dctx *dctx;
void *cwksp; void *cwksp;
void *dwksp; void *dwksp;
}; };
static ZSTD_parameters zstd_params(void) static zstd_parameters zstd_params(void)
{ {
return ZSTD_getParams(ZSTD_DEF_LEVEL, 0, 0); return zstd_get_params(ZSTD_DEF_LEVEL, 0);
} }
static int zstd_comp_init(struct zstd_ctx *ctx) static int zstd_comp_init(struct zstd_ctx *ctx)
{ {
int ret = 0; int ret = 0;
const ZSTD_parameters params = zstd_params(); const zstd_parameters params = zstd_params();
const size_t wksp_size = ZSTD_CCtxWorkspaceBound(params.cParams); const size_t wksp_size = zstd_cctx_workspace_bound(&params.cParams);
ctx->cwksp = vzalloc(wksp_size); ctx->cwksp = vzalloc(wksp_size);
if (!ctx->cwksp) { if (!ctx->cwksp) {
@ -41,7 +41,7 @@ static int zstd_comp_init(struct zstd_ctx *ctx)
goto out; goto out;
} }
ctx->cctx = ZSTD_initCCtx(ctx->cwksp, wksp_size); ctx->cctx = zstd_init_cctx(ctx->cwksp, wksp_size);
if (!ctx->cctx) { if (!ctx->cctx) {
ret = -EINVAL; ret = -EINVAL;
goto out_free; goto out_free;
@ -56,7 +56,7 @@ out_free:
static int zstd_decomp_init(struct zstd_ctx *ctx) static int zstd_decomp_init(struct zstd_ctx *ctx)
{ {
int ret = 0; int ret = 0;
const size_t wksp_size = ZSTD_DCtxWorkspaceBound(); const size_t wksp_size = zstd_dctx_workspace_bound();
ctx->dwksp = vzalloc(wksp_size); ctx->dwksp = vzalloc(wksp_size);
if (!ctx->dwksp) { if (!ctx->dwksp) {
@ -64,7 +64,7 @@ static int zstd_decomp_init(struct zstd_ctx *ctx)
goto out; goto out;
} }
ctx->dctx = ZSTD_initDCtx(ctx->dwksp, wksp_size); ctx->dctx = zstd_init_dctx(ctx->dwksp, wksp_size);
if (!ctx->dctx) { if (!ctx->dctx) {
ret = -EINVAL; ret = -EINVAL;
goto out_free; goto out_free;
@ -152,10 +152,10 @@ static int __zstd_compress(const u8 *src, unsigned int slen,
{ {
size_t out_len; size_t out_len;
struct zstd_ctx *zctx = ctx; struct zstd_ctx *zctx = ctx;
const ZSTD_parameters params = zstd_params(); const zstd_parameters params = zstd_params();
out_len = ZSTD_compressCCtx(zctx->cctx, dst, *dlen, src, slen, params); out_len = zstd_compress_cctx(zctx->cctx, dst, *dlen, src, slen, &params);
if (ZSTD_isError(out_len)) if (zstd_is_error(out_len))
return -EINVAL; return -EINVAL;
*dlen = out_len; *dlen = out_len;
return 0; return 0;
@ -182,8 +182,8 @@ static int __zstd_decompress(const u8 *src, unsigned int slen,
size_t out_len; size_t out_len;
struct zstd_ctx *zctx = ctx; struct zstd_ctx *zctx = ctx;
out_len = ZSTD_decompressDCtx(zctx->dctx, dst, *dlen, src, slen); out_len = zstd_decompress_dctx(zctx->dctx, dst, *dlen, src, slen);
if (ZSTD_isError(out_len)) if (zstd_is_error(out_len))
return -EINVAL; return -EINVAL;
*dlen = out_len; *dlen = out_len;
return 0; return 0;

View File

@ -28,10 +28,10 @@
/* 307s to avoid pathologically clashing with transaction commit */ /* 307s to avoid pathologically clashing with transaction commit */
#define ZSTD_BTRFS_RECLAIM_JIFFIES (307 * HZ) #define ZSTD_BTRFS_RECLAIM_JIFFIES (307 * HZ)
static ZSTD_parameters zstd_get_btrfs_parameters(unsigned int level, static zstd_parameters zstd_get_btrfs_parameters(unsigned int level,
size_t src_len) size_t src_len)
{ {
ZSTD_parameters params = ZSTD_getParams(level, src_len, 0); zstd_parameters params = zstd_get_params(level, src_len);
if (params.cParams.windowLog > ZSTD_BTRFS_MAX_WINDOWLOG) if (params.cParams.windowLog > ZSTD_BTRFS_MAX_WINDOWLOG)
params.cParams.windowLog = ZSTD_BTRFS_MAX_WINDOWLOG; params.cParams.windowLog = ZSTD_BTRFS_MAX_WINDOWLOG;
@ -48,8 +48,8 @@ struct workspace {
unsigned long last_used; /* jiffies */ unsigned long last_used; /* jiffies */
struct list_head list; struct list_head list;
struct list_head lru_list; struct list_head lru_list;
ZSTD_inBuffer in_buf; zstd_in_buffer in_buf;
ZSTD_outBuffer out_buf; zstd_out_buffer out_buf;
}; };
/* /*
@ -155,12 +155,12 @@ static void zstd_calc_ws_mem_sizes(void)
unsigned int level; unsigned int level;
for (level = 1; level <= ZSTD_BTRFS_MAX_LEVEL; level++) { for (level = 1; level <= ZSTD_BTRFS_MAX_LEVEL; level++) {
ZSTD_parameters params = zstd_parameters params =
zstd_get_btrfs_parameters(level, ZSTD_BTRFS_MAX_INPUT); zstd_get_btrfs_parameters(level, ZSTD_BTRFS_MAX_INPUT);
size_t level_size = size_t level_size =
max_t(size_t, max_t(size_t,
ZSTD_CStreamWorkspaceBound(params.cParams), zstd_cstream_workspace_bound(&params.cParams),
ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT)); zstd_dstream_workspace_bound(ZSTD_BTRFS_MAX_INPUT));
max_size = max_t(size_t, max_size, level_size); max_size = max_t(size_t, max_size, level_size);
zstd_ws_mem_sizes[level - 1] = max_size; zstd_ws_mem_sizes[level - 1] = max_size;
@ -371,7 +371,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
unsigned long *total_in, unsigned long *total_out) unsigned long *total_in, unsigned long *total_out)
{ {
struct workspace *workspace = list_entry(ws, struct workspace, list); struct workspace *workspace = list_entry(ws, struct workspace, list);
ZSTD_CStream *stream; zstd_cstream *stream;
int ret = 0; int ret = 0;
int nr_pages = 0; int nr_pages = 0;
struct page *in_page = NULL; /* The current page to read */ struct page *in_page = NULL; /* The current page to read */
@ -381,7 +381,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
unsigned long len = *total_out; unsigned long len = *total_out;
const unsigned long nr_dest_pages = *out_pages; const unsigned long nr_dest_pages = *out_pages;
unsigned long max_out = nr_dest_pages * PAGE_SIZE; unsigned long max_out = nr_dest_pages * PAGE_SIZE;
ZSTD_parameters params = zstd_get_btrfs_parameters(workspace->req_level, zstd_parameters params = zstd_get_btrfs_parameters(workspace->req_level,
len); len);
*out_pages = 0; *out_pages = 0;
@ -389,10 +389,10 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
*total_in = 0; *total_in = 0;
/* Initialize the stream */ /* Initialize the stream */
stream = ZSTD_initCStream(params, len, workspace->mem, stream = zstd_init_cstream(&params, len, workspace->mem,
workspace->size); workspace->size);
if (!stream) { if (!stream) {
pr_warn("BTRFS: ZSTD_initCStream failed\n"); pr_warn("BTRFS: zstd_init_cstream failed\n");
ret = -EIO; ret = -EIO;
goto out; goto out;
} }
@ -418,11 +418,11 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
while (1) { while (1) {
size_t ret2; size_t ret2;
ret2 = ZSTD_compressStream(stream, &workspace->out_buf, ret2 = zstd_compress_stream(stream, &workspace->out_buf,
&workspace->in_buf); &workspace->in_buf);
if (ZSTD_isError(ret2)) { if (zstd_is_error(ret2)) {
pr_debug("BTRFS: ZSTD_compressStream returned %d\n", pr_debug("BTRFS: zstd_compress_stream returned %d\n",
ZSTD_getErrorCode(ret2)); zstd_get_error_code(ret2));
ret = -EIO; ret = -EIO;
goto out; goto out;
} }
@ -487,10 +487,10 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
while (1) { while (1) {
size_t ret2; size_t ret2;
ret2 = ZSTD_endStream(stream, &workspace->out_buf); ret2 = zstd_end_stream(stream, &workspace->out_buf);
if (ZSTD_isError(ret2)) { if (zstd_is_error(ret2)) {
pr_debug("BTRFS: ZSTD_endStream returned %d\n", pr_debug("BTRFS: zstd_end_stream returned %d\n",
ZSTD_getErrorCode(ret2)); zstd_get_error_code(ret2));
ret = -EIO; ret = -EIO;
goto out; goto out;
} }
@ -548,17 +548,17 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
struct workspace *workspace = list_entry(ws, struct workspace, list); struct workspace *workspace = list_entry(ws, struct workspace, list);
struct page **pages_in = cb->compressed_pages; struct page **pages_in = cb->compressed_pages;
size_t srclen = cb->compressed_len; size_t srclen = cb->compressed_len;
ZSTD_DStream *stream; zstd_dstream *stream;
int ret = 0; int ret = 0;
unsigned long page_in_index = 0; unsigned long page_in_index = 0;
unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE); unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
unsigned long buf_start; unsigned long buf_start;
unsigned long total_out = 0; unsigned long total_out = 0;
stream = ZSTD_initDStream( stream = zstd_init_dstream(
ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size); ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
if (!stream) { if (!stream) {
pr_debug("BTRFS: ZSTD_initDStream failed\n"); pr_debug("BTRFS: zstd_init_dstream failed\n");
ret = -EIO; ret = -EIO;
goto done; goto done;
} }
@ -574,11 +574,11 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
while (1) { while (1) {
size_t ret2; size_t ret2;
ret2 = ZSTD_decompressStream(stream, &workspace->out_buf, ret2 = zstd_decompress_stream(stream, &workspace->out_buf,
&workspace->in_buf); &workspace->in_buf);
if (ZSTD_isError(ret2)) { if (zstd_is_error(ret2)) {
pr_debug("BTRFS: ZSTD_decompressStream returned %d\n", pr_debug("BTRFS: zstd_decompress_stream returned %d\n",
ZSTD_getErrorCode(ret2)); zstd_get_error_code(ret2));
ret = -EIO; ret = -EIO;
goto done; goto done;
} }
@ -624,16 +624,16 @@ int zstd_decompress(struct list_head *ws, unsigned char *data_in,
size_t destlen) size_t destlen)
{ {
struct workspace *workspace = list_entry(ws, struct workspace, list); struct workspace *workspace = list_entry(ws, struct workspace, list);
ZSTD_DStream *stream; zstd_dstream *stream;
int ret = 0; int ret = 0;
size_t ret2; size_t ret2;
unsigned long total_out = 0; unsigned long total_out = 0;
unsigned long pg_offset = 0; unsigned long pg_offset = 0;
stream = ZSTD_initDStream( stream = zstd_init_dstream(
ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size); ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
if (!stream) { if (!stream) {
pr_warn("BTRFS: ZSTD_initDStream failed\n"); pr_warn("BTRFS: zstd_init_dstream failed\n");
ret = -EIO; ret = -EIO;
goto finish; goto finish;
} }
@ -657,15 +657,15 @@ int zstd_decompress(struct list_head *ws, unsigned char *data_in,
/* Check if the frame is over and we still need more input */ /* Check if the frame is over and we still need more input */
if (ret2 == 0) { if (ret2 == 0) {
pr_debug("BTRFS: ZSTD_decompressStream ended early\n"); pr_debug("BTRFS: zstd_decompress_stream ended early\n");
ret = -EIO; ret = -EIO;
goto finish; goto finish;
} }
ret2 = ZSTD_decompressStream(stream, &workspace->out_buf, ret2 = zstd_decompress_stream(stream, &workspace->out_buf,
&workspace->in_buf); &workspace->in_buf);
if (ZSTD_isError(ret2)) { if (zstd_is_error(ret2)) {
pr_debug("BTRFS: ZSTD_decompressStream returned %d\n", pr_debug("BTRFS: zstd_decompress_stream returned %d\n",
ZSTD_getErrorCode(ret2)); zstd_get_error_code(ret2));
ret = -EIO; ret = -EIO;
goto finish; goto finish;
} }

View File

@ -336,8 +336,8 @@ static const struct f2fs_compress_ops f2fs_lz4_ops = {
static int zstd_init_compress_ctx(struct compress_ctx *cc) static int zstd_init_compress_ctx(struct compress_ctx *cc)
{ {
ZSTD_parameters params; zstd_parameters params;
ZSTD_CStream *stream; zstd_cstream *stream;
void *workspace; void *workspace;
unsigned int workspace_size; unsigned int workspace_size;
unsigned char level = F2FS_I(cc->inode)->i_compress_flag >> unsigned char level = F2FS_I(cc->inode)->i_compress_flag >>
@ -346,17 +346,17 @@ static int zstd_init_compress_ctx(struct compress_ctx *cc)
if (!level) if (!level)
level = F2FS_ZSTD_DEFAULT_CLEVEL; level = F2FS_ZSTD_DEFAULT_CLEVEL;
params = ZSTD_getParams(level, cc->rlen, 0); params = zstd_get_params(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen);
workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams); workspace_size = zstd_cstream_workspace_bound(&params.cParams);
workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode), workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
workspace_size, GFP_NOFS); workspace_size, GFP_NOFS);
if (!workspace) if (!workspace)
return -ENOMEM; return -ENOMEM;
stream = ZSTD_initCStream(params, 0, workspace, workspace_size); stream = zstd_init_cstream(&params, 0, workspace, workspace_size);
if (!stream) { if (!stream) {
printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n", printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_cstream failed\n",
KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
__func__); __func__);
kvfree(workspace); kvfree(workspace);
@ -379,9 +379,9 @@ static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
static int zstd_compress_pages(struct compress_ctx *cc) static int zstd_compress_pages(struct compress_ctx *cc)
{ {
ZSTD_CStream *stream = cc->private2; zstd_cstream *stream = cc->private2;
ZSTD_inBuffer inbuf; zstd_in_buffer inbuf;
ZSTD_outBuffer outbuf; zstd_out_buffer outbuf;
int src_size = cc->rlen; int src_size = cc->rlen;
int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE; int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
int ret; int ret;
@ -394,19 +394,19 @@ static int zstd_compress_pages(struct compress_ctx *cc)
outbuf.dst = cc->cbuf->cdata; outbuf.dst = cc->cbuf->cdata;
outbuf.size = dst_size; outbuf.size = dst_size;
ret = ZSTD_compressStream(stream, &outbuf, &inbuf); ret = zstd_compress_stream(stream, &outbuf, &inbuf);
if (ZSTD_isError(ret)) { if (zstd_is_error(ret)) {
printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n", printk_ratelimited("%sF2FS-fs (%s): %s zstd_compress_stream failed, ret: %d\n",
KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
__func__, ZSTD_getErrorCode(ret)); __func__, zstd_get_error_code(ret));
return -EIO; return -EIO;
} }
ret = ZSTD_endStream(stream, &outbuf); ret = zstd_end_stream(stream, &outbuf);
if (ZSTD_isError(ret)) { if (zstd_is_error(ret)) {
printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n", printk_ratelimited("%sF2FS-fs (%s): %s zstd_end_stream returned %d\n",
KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
__func__, ZSTD_getErrorCode(ret)); __func__, zstd_get_error_code(ret));
return -EIO; return -EIO;
} }
@ -423,22 +423,22 @@ static int zstd_compress_pages(struct compress_ctx *cc)
static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic) static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
{ {
ZSTD_DStream *stream; zstd_dstream *stream;
void *workspace; void *workspace;
unsigned int workspace_size; unsigned int workspace_size;
unsigned int max_window_size = unsigned int max_window_size =
MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size); MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size); workspace_size = zstd_dstream_workspace_bound(max_window_size);
workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode), workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
workspace_size, GFP_NOFS); workspace_size, GFP_NOFS);
if (!workspace) if (!workspace)
return -ENOMEM; return -ENOMEM;
stream = ZSTD_initDStream(max_window_size, workspace, workspace_size); stream = zstd_init_dstream(max_window_size, workspace, workspace_size);
if (!stream) { if (!stream) {
printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n", printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_dstream failed\n",
KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
__func__); __func__);
kvfree(workspace); kvfree(workspace);
@ -460,9 +460,9 @@ static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
static int zstd_decompress_pages(struct decompress_io_ctx *dic) static int zstd_decompress_pages(struct decompress_io_ctx *dic)
{ {
ZSTD_DStream *stream = dic->private2; zstd_dstream *stream = dic->private2;
ZSTD_inBuffer inbuf; zstd_in_buffer inbuf;
ZSTD_outBuffer outbuf; zstd_out_buffer outbuf;
int ret; int ret;
inbuf.pos = 0; inbuf.pos = 0;
@ -473,11 +473,11 @@ static int zstd_decompress_pages(struct decompress_io_ctx *dic)
outbuf.dst = dic->rbuf; outbuf.dst = dic->rbuf;
outbuf.size = dic->rlen; outbuf.size = dic->rlen;
ret = ZSTD_decompressStream(stream, &outbuf, &inbuf); ret = zstd_decompress_stream(stream, &outbuf, &inbuf);
if (ZSTD_isError(ret)) { if (zstd_is_error(ret)) {
printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n", printk_ratelimited("%sF2FS-fs (%s): %s zstd_decompress_stream failed, ret: %d\n",
KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id, KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
__func__, ZSTD_getErrorCode(ret)); __func__, zstd_get_error_code(ret));
return -EIO; return -EIO;
} }

View File

@ -592,7 +592,7 @@ static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
if (kstrtouint(str + 1, 10, &level)) if (kstrtouint(str + 1, 10, &level))
return -EINVAL; return -EINVAL;
if (!level || level > ZSTD_maxCLevel()) { if (!level || level > zstd_max_clevel()) {
f2fs_info(sbi, "invalid zstd compress level: %d", level); f2fs_info(sbi, "invalid zstd compress level: %d", level);
return -EINVAL; return -EINVAL;
} }

View File

@ -218,7 +218,7 @@ static int zbufsize_842(size_t size)
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS) #if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
static int zbufsize_zstd(size_t size) static int zbufsize_zstd(size_t size)
{ {
return ZSTD_compressBound(size); return zstd_compress_bound(size);
} }
#endif #endif

View File

@ -34,7 +34,7 @@ static void *zstd_init(struct squashfs_sb_info *msblk, void *buff)
goto failed; goto failed;
wksp->window_size = max_t(size_t, wksp->window_size = max_t(size_t,
msblk->block_size, SQUASHFS_METADATA_SIZE); msblk->block_size, SQUASHFS_METADATA_SIZE);
wksp->mem_size = ZSTD_DStreamWorkspaceBound(wksp->window_size); wksp->mem_size = zstd_dstream_workspace_bound(wksp->window_size);
wksp->mem = vmalloc(wksp->mem_size); wksp->mem = vmalloc(wksp->mem_size);
if (wksp->mem == NULL) if (wksp->mem == NULL)
goto failed; goto failed;
@ -63,15 +63,15 @@ static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm,
struct squashfs_page_actor *output) struct squashfs_page_actor *output)
{ {
struct workspace *wksp = strm; struct workspace *wksp = strm;
ZSTD_DStream *stream; zstd_dstream *stream;
size_t total_out = 0; size_t total_out = 0;
int error = 0; int error = 0;
ZSTD_inBuffer in_buf = { NULL, 0, 0 }; zstd_in_buffer in_buf = { NULL, 0, 0 };
ZSTD_outBuffer out_buf = { NULL, 0, 0 }; zstd_out_buffer out_buf = { NULL, 0, 0 };
struct bvec_iter_all iter_all = {}; struct bvec_iter_all iter_all = {};
struct bio_vec *bvec = bvec_init_iter_all(&iter_all); struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
stream = ZSTD_initDStream(wksp->window_size, wksp->mem, wksp->mem_size); stream = zstd_init_dstream(wksp->window_size, wksp->mem, wksp->mem_size);
if (!stream) { if (!stream) {
ERROR("Failed to initialize zstd decompressor\n"); ERROR("Failed to initialize zstd decompressor\n");
@ -116,14 +116,14 @@ static int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm,
} }
total_out -= out_buf.pos; total_out -= out_buf.pos;
zstd_err = ZSTD_decompressStream(stream, &out_buf, &in_buf); zstd_err = zstd_decompress_stream(stream, &out_buf, &in_buf);
total_out += out_buf.pos; /* add the additional data produced */ total_out += out_buf.pos; /* add the additional data produced */
if (zstd_err == 0) if (zstd_err == 0)
break; break;
if (ZSTD_isError(zstd_err)) { if (zstd_is_error(zstd_err)) {
ERROR("zstd decompression error: %d\n", ERROR("zstd decompression error: %d\n",
(int)ZSTD_getErrorCode(zstd_err)); (int)zstd_get_error_code(zstd_err));
error = -EIO; error = -EIO;
break; break;
} }

File diff suppressed because it is too large Load Diff

1157
include/linux/zstd_lib.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -91,11 +91,15 @@
static int INIT handle_zstd_error(size_t ret, void (*error)(char *x)) static int INIT handle_zstd_error(size_t ret, void (*error)(char *x))
{ {
const int err = ZSTD_getErrorCode(ret); const zstd_error_code err = zstd_get_error_code(ret);
if (!ZSTD_isError(ret)) if (!zstd_is_error(ret))
return 0; return 0;
/*
* zstd_get_error_name() cannot be used because error takes a char *
* not a const char *
*/
switch (err) { switch (err) {
case ZSTD_error_memory_allocation: case ZSTD_error_memory_allocation:
error("ZSTD decompressor ran out of memory"); error("ZSTD decompressor ran out of memory");
@ -124,28 +128,28 @@ static int INIT decompress_single(const u8 *in_buf, long in_len, u8 *out_buf,
long out_len, long *in_pos, long out_len, long *in_pos,
void (*error)(char *x)) void (*error)(char *x))
{ {
const size_t wksp_size = ZSTD_DCtxWorkspaceBound(); const size_t wksp_size = zstd_dctx_workspace_bound();
void *wksp = large_malloc(wksp_size); void *wksp = large_malloc(wksp_size);
ZSTD_DCtx *dctx = ZSTD_initDCtx(wksp, wksp_size); zstd_dctx *dctx = zstd_init_dctx(wksp, wksp_size);
int err; int err;
size_t ret; size_t ret;
if (dctx == NULL) { if (dctx == NULL) {
error("Out of memory while allocating ZSTD_DCtx"); error("Out of memory while allocating zstd_dctx");
err = -1; err = -1;
goto out; goto out;
} }
/* /*
* Find out how large the frame actually is, there may be junk at * Find out how large the frame actually is, there may be junk at
* the end of the frame that ZSTD_decompressDCtx() can't handle. * the end of the frame that zstd_decompress_dctx() can't handle.
*/ */
ret = ZSTD_findFrameCompressedSize(in_buf, in_len); ret = zstd_find_frame_compressed_size(in_buf, in_len);
err = handle_zstd_error(ret, error); err = handle_zstd_error(ret, error);
if (err) if (err)
goto out; goto out;
in_len = (long)ret; in_len = (long)ret;
ret = ZSTD_decompressDCtx(dctx, out_buf, out_len, in_buf, in_len); ret = zstd_decompress_dctx(dctx, out_buf, out_len, in_buf, in_len);
err = handle_zstd_error(ret, error); err = handle_zstd_error(ret, error);
if (err) if (err)
goto out; goto out;
@ -167,14 +171,14 @@ static int INIT __unzstd(unsigned char *in_buf, long in_len,
long *in_pos, long *in_pos,
void (*error)(char *x)) void (*error)(char *x))
{ {
ZSTD_inBuffer in; zstd_in_buffer in;
ZSTD_outBuffer out; zstd_out_buffer out;
ZSTD_frameParams params; zstd_frame_header header;
void *in_allocated = NULL; void *in_allocated = NULL;
void *out_allocated = NULL; void *out_allocated = NULL;
void *wksp = NULL; void *wksp = NULL;
size_t wksp_size; size_t wksp_size;
ZSTD_DStream *dstream; zstd_dstream *dstream;
int err; int err;
size_t ret; size_t ret;
@ -238,13 +242,13 @@ static int INIT __unzstd(unsigned char *in_buf, long in_len,
out.size = out_len; out.size = out_len;
/* /*
* We need to know the window size to allocate the ZSTD_DStream. * We need to know the window size to allocate the zstd_dstream.
* Since we are streaming, we need to allocate a buffer for the sliding * Since we are streaming, we need to allocate a buffer for the sliding
* window. The window size varies from 1 KB to ZSTD_WINDOWSIZE_MAX * window. The window size varies from 1 KB to ZSTD_WINDOWSIZE_MAX
* (8 MB), so it is important to use the actual value so as not to * (8 MB), so it is important to use the actual value so as not to
* waste memory when it is smaller. * waste memory when it is smaller.
*/ */
ret = ZSTD_getFrameParams(&params, in.src, in.size); ret = zstd_get_frame_header(&header, in.src, in.size);
err = handle_zstd_error(ret, error); err = handle_zstd_error(ret, error);
if (err) if (err)
goto out; goto out;
@ -253,19 +257,19 @@ static int INIT __unzstd(unsigned char *in_buf, long in_len,
err = -1; err = -1;
goto out; goto out;
} }
if (params.windowSize > ZSTD_WINDOWSIZE_MAX) { if (header.windowSize > ZSTD_WINDOWSIZE_MAX) {
error("ZSTD-compressed data has too large a window size"); error("ZSTD-compressed data has too large a window size");
err = -1; err = -1;
goto out; goto out;
} }
/* /*
* Allocate the ZSTD_DStream now that we know how much memory is * Allocate the zstd_dstream now that we know how much memory is
* required. * required.
*/ */
wksp_size = ZSTD_DStreamWorkspaceBound(params.windowSize); wksp_size = zstd_dstream_workspace_bound(header.windowSize);
wksp = large_malloc(wksp_size); wksp = large_malloc(wksp_size);
dstream = ZSTD_initDStream(params.windowSize, wksp, wksp_size); dstream = zstd_init_dstream(header.windowSize, wksp, wksp_size);
if (dstream == NULL) { if (dstream == NULL) {
error("Out of memory while allocating ZSTD_DStream"); error("Out of memory while allocating ZSTD_DStream");
err = -1; err = -1;
@ -298,7 +302,7 @@ static int INIT __unzstd(unsigned char *in_buf, long in_len,
in.size = in_len; in.size = in_len;
} }
/* Returns zero when the frame is complete. */ /* Returns zero when the frame is complete. */
ret = ZSTD_decompressStream(dstream, &out, &in); ret = zstd_decompress_stream(dstream, &out, &in);
err = handle_zstd_error(ret, error); err = handle_zstd_error(ret, error);
if (err) if (err)
goto out; goto out;

View File

@ -3443,43 +3443,92 @@ ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long srcSize,
return params; return params;
} }
EXPORT_SYMBOL(ZSTD_maxCLevel); size_t zstd_compress_bound(size_t src_size)
EXPORT_SYMBOL(ZSTD_compressBound); {
return ZSTD_compressBound(src_size);
}
EXPORT_SYMBOL(zstd_compress_bound);
EXPORT_SYMBOL(ZSTD_CCtxWorkspaceBound); int zstd_min_clevel(void)
EXPORT_SYMBOL(ZSTD_initCCtx); {
EXPORT_SYMBOL(ZSTD_compressCCtx); /*
EXPORT_SYMBOL(ZSTD_compress_usingDict); * zstd-1.3.1 doesn't implement ZSTD_minCLevel().
* Return 0 (default level).
*/
return 0;
}
EXPORT_SYMBOL(zstd_min_clevel);
EXPORT_SYMBOL(ZSTD_CDictWorkspaceBound); int zstd_max_clevel(void)
EXPORT_SYMBOL(ZSTD_initCDict); {
EXPORT_SYMBOL(ZSTD_compress_usingCDict); return ZSTD_maxCLevel();
}
EXPORT_SYMBOL(zstd_max_clevel);
EXPORT_SYMBOL(ZSTD_CStreamWorkspaceBound); zstd_parameters zstd_get_params(int level,
EXPORT_SYMBOL(ZSTD_initCStream); unsigned long long estimated_src_size)
EXPORT_SYMBOL(ZSTD_initCStream_usingCDict); {
EXPORT_SYMBOL(ZSTD_resetCStream); return ZSTD_getParams(level, estimated_src_size, 0);
EXPORT_SYMBOL(ZSTD_compressStream); }
EXPORT_SYMBOL(ZSTD_flushStream); EXPORT_SYMBOL(zstd_get_params);
EXPORT_SYMBOL(ZSTD_endStream);
EXPORT_SYMBOL(ZSTD_CStreamInSize);
EXPORT_SYMBOL(ZSTD_CStreamOutSize);
EXPORT_SYMBOL(ZSTD_getCParams); size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *cparams)
EXPORT_SYMBOL(ZSTD_getParams); {
EXPORT_SYMBOL(ZSTD_checkCParams); return ZSTD_CCtxWorkspaceBound(*cparams);
EXPORT_SYMBOL(ZSTD_adjustCParams); }
EXPORT_SYMBOL(zstd_cctx_workspace_bound);
EXPORT_SYMBOL(ZSTD_compressBegin); zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size)
EXPORT_SYMBOL(ZSTD_compressBegin_usingDict); {
EXPORT_SYMBOL(ZSTD_compressBegin_advanced); return ZSTD_initCCtx(workspace, workspace_size);
EXPORT_SYMBOL(ZSTD_copyCCtx); }
EXPORT_SYMBOL(ZSTD_compressBegin_usingCDict); EXPORT_SYMBOL(zstd_init_cctx);
EXPORT_SYMBOL(ZSTD_compressContinue);
EXPORT_SYMBOL(ZSTD_compressEnd);
EXPORT_SYMBOL(ZSTD_getBlockSizeMax); size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity,
EXPORT_SYMBOL(ZSTD_compressBlock); const void *src, size_t src_size, const zstd_parameters *parameters)
{
return ZSTD_compressCCtx(cctx, dst, dst_capacity, src, src_size, *parameters);
}
EXPORT_SYMBOL(zstd_compress_cctx);
size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams)
{
return ZSTD_CStreamWorkspaceBound(*cparams);
}
EXPORT_SYMBOL(zstd_cstream_workspace_bound);
zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters,
unsigned long long pledged_src_size, void *workspace, size_t workspace_size)
{
return ZSTD_initCStream(*parameters, pledged_src_size, workspace, workspace_size);
}
EXPORT_SYMBOL(zstd_init_cstream);
size_t zstd_reset_cstream(zstd_cstream *cstream,
unsigned long long pledged_src_size)
{
return ZSTD_resetCStream(cstream, pledged_src_size);
}
EXPORT_SYMBOL(zstd_reset_cstream);
size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output,
zstd_in_buffer *input)
{
return ZSTD_compressStream(cstream, output, input);
}
EXPORT_SYMBOL(zstd_compress_stream);
size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output)
{
return ZSTD_flushStream(cstream, output);
}
EXPORT_SYMBOL(zstd_flush_stream);
size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output)
{
return ZSTD_endStream(cstream, output);
}
EXPORT_SYMBOL(zstd_end_stream);
MODULE_LICENSE("Dual BSD/GPL"); MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("Zstd Compressor"); MODULE_DESCRIPTION("Zstd Compressor");

View File

@ -2490,42 +2490,82 @@ size_t ZSTD_decompressStream(ZSTD_DStream *zds, ZSTD_outBuffer *output, ZSTD_inB
} }
} }
EXPORT_SYMBOL(ZSTD_DCtxWorkspaceBound); unsigned int zstd_is_error(size_t code)
EXPORT_SYMBOL(ZSTD_initDCtx); {
EXPORT_SYMBOL(ZSTD_decompressDCtx); return ZSTD_isError(code);
EXPORT_SYMBOL(ZSTD_decompress_usingDict); }
EXPORT_SYMBOL(zstd_is_error);
EXPORT_SYMBOL(ZSTD_DDictWorkspaceBound); zstd_error_code zstd_get_error_code(size_t code)
EXPORT_SYMBOL(ZSTD_initDDict); {
EXPORT_SYMBOL(ZSTD_decompress_usingDDict); return ZSTD_getErrorCode(code);
}
EXPORT_SYMBOL(zstd_get_error_code);
EXPORT_SYMBOL(ZSTD_DStreamWorkspaceBound); const char *zstd_get_error_name(size_t code)
EXPORT_SYMBOL(ZSTD_initDStream); {
EXPORT_SYMBOL(ZSTD_initDStream_usingDDict); /* Real implementation in zstd-1.4.6. */
EXPORT_SYMBOL(ZSTD_resetDStream); return "GENERIC";
EXPORT_SYMBOL(ZSTD_decompressStream); }
EXPORT_SYMBOL(ZSTD_DStreamInSize); EXPORT_SYMBOL(zstd_get_error_name);
EXPORT_SYMBOL(ZSTD_DStreamOutSize);
EXPORT_SYMBOL(ZSTD_findFrameCompressedSize); size_t zstd_dctx_workspace_bound(void)
EXPORT_SYMBOL(ZSTD_getFrameContentSize); {
EXPORT_SYMBOL(ZSTD_findDecompressedSize); return ZSTD_DCtxWorkspaceBound();
}
EXPORT_SYMBOL(zstd_dctx_workspace_bound);
EXPORT_SYMBOL(ZSTD_isFrame); zstd_dctx *zstd_init_dctx(void *workspace, size_t workspace_size)
EXPORT_SYMBOL(ZSTD_getDictID_fromDict); {
EXPORT_SYMBOL(ZSTD_getDictID_fromDDict); return ZSTD_initDCtx(workspace, workspace_size);
EXPORT_SYMBOL(ZSTD_getDictID_fromFrame); }
EXPORT_SYMBOL(zstd_init_dctx);
EXPORT_SYMBOL(ZSTD_getFrameParams); size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity,
EXPORT_SYMBOL(ZSTD_decompressBegin); const void *src, size_t src_size)
EXPORT_SYMBOL(ZSTD_decompressBegin_usingDict); {
EXPORT_SYMBOL(ZSTD_copyDCtx); return ZSTD_decompressDCtx(dctx, dst, dst_capacity, src, src_size);
EXPORT_SYMBOL(ZSTD_nextSrcSizeToDecompress); }
EXPORT_SYMBOL(ZSTD_decompressContinue); EXPORT_SYMBOL(zstd_decompress_dctx);
EXPORT_SYMBOL(ZSTD_nextInputType);
EXPORT_SYMBOL(ZSTD_decompressBlock); size_t zstd_dstream_workspace_bound(size_t max_window_size)
EXPORT_SYMBOL(ZSTD_insertBlock); {
return ZSTD_DStreamWorkspaceBound(max_window_size);
}
EXPORT_SYMBOL(zstd_dstream_workspace_bound);
zstd_dstream *zstd_init_dstream(size_t max_window_size, void *workspace,
size_t workspace_size)
{
return ZSTD_initDStream(max_window_size, workspace, workspace_size);
}
EXPORT_SYMBOL(zstd_init_dstream);
size_t zstd_reset_dstream(zstd_dstream *dstream)
{
return ZSTD_resetDStream(dstream);
}
EXPORT_SYMBOL(zstd_reset_dstream);
size_t zstd_decompress_stream(zstd_dstream *dstream, zstd_out_buffer *output,
zstd_in_buffer *input)
{
return ZSTD_decompressStream(dstream, output, input);
}
EXPORT_SYMBOL(zstd_decompress_stream);
size_t zstd_find_frame_compressed_size(const void *src, size_t src_size)
{
return ZSTD_findFrameCompressedSize(src, src_size);
}
EXPORT_SYMBOL(zstd_find_frame_compressed_size);
size_t zstd_get_frame_header(zstd_frame_header *header, const void *src,
size_t src_size)
{
return ZSTD_getFrameParams(header, src, src_size);
}
EXPORT_SYMBOL(zstd_get_frame_header);
MODULE_LICENSE("Dual BSD/GPL"); MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("Zstd Decompressor"); MODULE_DESCRIPTION("Zstd Decompressor");