Skip to content

Commit cf30f6a

Browse files
committed
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 <[email protected]> Tested By: Paul Jones <[email protected]> Tested-by: Oleksandr Natalenko <[email protected]> Tested-by: Sedat Dilek <[email protected]> # LLVM/Clang v13.0.0 on x86-64 Tested-by: Jean-Denis Girard <[email protected]>
1 parent d2f38a3 commit cf30f6a

File tree

11 files changed

+1691
-1158
lines changed

11 files changed

+1691
-1158
lines changed

crypto/zstd.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@
1818
#define ZSTD_DEF_LEVEL 3
1919

2020
struct zstd_ctx {
21-
ZSTD_CCtx *cctx;
22-
ZSTD_DCtx *dctx;
21+
zstd_cctx *cctx;
22+
zstd_dctx *dctx;
2323
void *cwksp;
2424
void *dwksp;
2525
};
2626

27-
static ZSTD_parameters zstd_params(void)
27+
static zstd_parameters zstd_params(void)
2828
{
29-
return ZSTD_getParams(ZSTD_DEF_LEVEL, 0, 0);
29+
return zstd_get_params(ZSTD_DEF_LEVEL, 0);
3030
}
3131

3232
static int zstd_comp_init(struct zstd_ctx *ctx)
3333
{
3434
int ret = 0;
35-
const ZSTD_parameters params = zstd_params();
36-
const size_t wksp_size = ZSTD_CCtxWorkspaceBound(params.cParams);
35+
const zstd_parameters params = zstd_params();
36+
const size_t wksp_size = zstd_cctx_workspace_bound(&params.cParams);
3737

3838
ctx->cwksp = vzalloc(wksp_size);
3939
if (!ctx->cwksp) {
4040
ret = -ENOMEM;
4141
goto out;
4242
}
4343

44-
ctx->cctx = ZSTD_initCCtx(ctx->cwksp, wksp_size);
44+
ctx->cctx = zstd_init_cctx(ctx->cwksp, wksp_size);
4545
if (!ctx->cctx) {
4646
ret = -EINVAL;
4747
goto out_free;
@@ -56,15 +56,15 @@ static int zstd_comp_init(struct zstd_ctx *ctx)
5656
static int zstd_decomp_init(struct zstd_ctx *ctx)
5757
{
5858
int ret = 0;
59-
const size_t wksp_size = ZSTD_DCtxWorkspaceBound();
59+
const size_t wksp_size = zstd_dctx_workspace_bound();
6060

6161
ctx->dwksp = vzalloc(wksp_size);
6262
if (!ctx->dwksp) {
6363
ret = -ENOMEM;
6464
goto out;
6565
}
6666

67-
ctx->dctx = ZSTD_initDCtx(ctx->dwksp, wksp_size);
67+
ctx->dctx = zstd_init_dctx(ctx->dwksp, wksp_size);
6868
if (!ctx->dctx) {
6969
ret = -EINVAL;
7070
goto out_free;
@@ -152,10 +152,10 @@ static int __zstd_compress(const u8 *src, unsigned int slen,
152152
{
153153
size_t out_len;
154154
struct zstd_ctx *zctx = ctx;
155-
const ZSTD_parameters params = zstd_params();
155+
const zstd_parameters params = zstd_params();
156156

157-
out_len = ZSTD_compressCCtx(zctx->cctx, dst, *dlen, src, slen, params);
158-
if (ZSTD_isError(out_len))
157+
out_len = zstd_compress_cctx(zctx->cctx, dst, *dlen, src, slen, &params);
158+
if (zstd_is_error(out_len))
159159
return -EINVAL;
160160
*dlen = out_len;
161161
return 0;
@@ -182,8 +182,8 @@ static int __zstd_decompress(const u8 *src, unsigned int slen,
182182
size_t out_len;
183183
struct zstd_ctx *zctx = ctx;
184184

185-
out_len = ZSTD_decompressDCtx(zctx->dctx, dst, *dlen, src, slen);
186-
if (ZSTD_isError(out_len))
185+
out_len = zstd_decompress_dctx(zctx->dctx, dst, *dlen, src, slen);
186+
if (zstd_is_error(out_len))
187187
return -EINVAL;
188188
*dlen = out_len;
189189
return 0;

fs/btrfs/zstd.c

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
/* 307s to avoid pathologically clashing with transaction commit */
2929
#define ZSTD_BTRFS_RECLAIM_JIFFIES (307 * HZ)
3030

31-
static ZSTD_parameters zstd_get_btrfs_parameters(unsigned int level,
31+
static zstd_parameters zstd_get_btrfs_parameters(unsigned int level,
3232
size_t src_len)
3333
{
34-
ZSTD_parameters params = ZSTD_getParams(level, src_len, 0);
34+
zstd_parameters params = zstd_get_params(level, src_len);
3535

3636
if (params.cParams.windowLog > ZSTD_BTRFS_MAX_WINDOWLOG)
3737
params.cParams.windowLog = ZSTD_BTRFS_MAX_WINDOWLOG;
@@ -48,8 +48,8 @@ struct workspace {
4848
unsigned long last_used; /* jiffies */
4949
struct list_head list;
5050
struct list_head lru_list;
51-
ZSTD_inBuffer in_buf;
52-
ZSTD_outBuffer out_buf;
51+
zstd_in_buffer in_buf;
52+
zstd_out_buffer out_buf;
5353
};
5454

5555
/*
@@ -155,12 +155,12 @@ static void zstd_calc_ws_mem_sizes(void)
155155
unsigned int level;
156156

157157
for (level = 1; level <= ZSTD_BTRFS_MAX_LEVEL; level++) {
158-
ZSTD_parameters params =
158+
zstd_parameters params =
159159
zstd_get_btrfs_parameters(level, ZSTD_BTRFS_MAX_INPUT);
160160
size_t level_size =
161161
max_t(size_t,
162-
ZSTD_CStreamWorkspaceBound(params.cParams),
163-
ZSTD_DStreamWorkspaceBound(ZSTD_BTRFS_MAX_INPUT));
162+
zstd_cstream_workspace_bound(&params.cParams),
163+
zstd_dstream_workspace_bound(ZSTD_BTRFS_MAX_INPUT));
164164

165165
max_size = max_t(size_t, max_size, level_size);
166166
zstd_ws_mem_sizes[level - 1] = max_size;
@@ -371,7 +371,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
371371
unsigned long *total_in, unsigned long *total_out)
372372
{
373373
struct workspace *workspace = list_entry(ws, struct workspace, list);
374-
ZSTD_CStream *stream;
374+
zstd_cstream *stream;
375375
int ret = 0;
376376
int nr_pages = 0;
377377
struct page *in_page = NULL; /* The current page to read */
@@ -381,18 +381,18 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
381381
unsigned long len = *total_out;
382382
const unsigned long nr_dest_pages = *out_pages;
383383
unsigned long max_out = nr_dest_pages * PAGE_SIZE;
384-
ZSTD_parameters params = zstd_get_btrfs_parameters(workspace->req_level,
384+
zstd_parameters params = zstd_get_btrfs_parameters(workspace->req_level,
385385
len);
386386

387387
*out_pages = 0;
388388
*total_out = 0;
389389
*total_in = 0;
390390

391391
/* Initialize the stream */
392-
stream = ZSTD_initCStream(params, len, workspace->mem,
392+
stream = zstd_init_cstream(&params, len, workspace->mem,
393393
workspace->size);
394394
if (!stream) {
395-
pr_warn("BTRFS: ZSTD_initCStream failed\n");
395+
pr_warn("BTRFS: zstd_init_cstream failed\n");
396396
ret = -EIO;
397397
goto out;
398398
}
@@ -418,11 +418,11 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
418418
while (1) {
419419
size_t ret2;
420420

421-
ret2 = ZSTD_compressStream(stream, &workspace->out_buf,
421+
ret2 = zstd_compress_stream(stream, &workspace->out_buf,
422422
&workspace->in_buf);
423-
if (ZSTD_isError(ret2)) {
424-
pr_debug("BTRFS: ZSTD_compressStream returned %d\n",
425-
ZSTD_getErrorCode(ret2));
423+
if (zstd_is_error(ret2)) {
424+
pr_debug("BTRFS: zstd_compress_stream returned %d\n",
425+
zstd_get_error_code(ret2));
426426
ret = -EIO;
427427
goto out;
428428
}
@@ -487,10 +487,10 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
487487
while (1) {
488488
size_t ret2;
489489

490-
ret2 = ZSTD_endStream(stream, &workspace->out_buf);
491-
if (ZSTD_isError(ret2)) {
492-
pr_debug("BTRFS: ZSTD_endStream returned %d\n",
493-
ZSTD_getErrorCode(ret2));
490+
ret2 = zstd_end_stream(stream, &workspace->out_buf);
491+
if (zstd_is_error(ret2)) {
492+
pr_debug("BTRFS: zstd_end_stream returned %d\n",
493+
zstd_get_error_code(ret2));
494494
ret = -EIO;
495495
goto out;
496496
}
@@ -548,17 +548,17 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
548548
struct workspace *workspace = list_entry(ws, struct workspace, list);
549549
struct page **pages_in = cb->compressed_pages;
550550
size_t srclen = cb->compressed_len;
551-
ZSTD_DStream *stream;
551+
zstd_dstream *stream;
552552
int ret = 0;
553553
unsigned long page_in_index = 0;
554554
unsigned long total_pages_in = DIV_ROUND_UP(srclen, PAGE_SIZE);
555555
unsigned long buf_start;
556556
unsigned long total_out = 0;
557557

558-
stream = ZSTD_initDStream(
558+
stream = zstd_init_dstream(
559559
ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
560560
if (!stream) {
561-
pr_debug("BTRFS: ZSTD_initDStream failed\n");
561+
pr_debug("BTRFS: zstd_init_dstream failed\n");
562562
ret = -EIO;
563563
goto done;
564564
}
@@ -574,11 +574,11 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
574574
while (1) {
575575
size_t ret2;
576576

577-
ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
577+
ret2 = zstd_decompress_stream(stream, &workspace->out_buf,
578578
&workspace->in_buf);
579-
if (ZSTD_isError(ret2)) {
580-
pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
581-
ZSTD_getErrorCode(ret2));
579+
if (zstd_is_error(ret2)) {
580+
pr_debug("BTRFS: zstd_decompress_stream returned %d\n",
581+
zstd_get_error_code(ret2));
582582
ret = -EIO;
583583
goto done;
584584
}
@@ -624,16 +624,16 @@ int zstd_decompress(struct list_head *ws, unsigned char *data_in,
624624
size_t destlen)
625625
{
626626
struct workspace *workspace = list_entry(ws, struct workspace, list);
627-
ZSTD_DStream *stream;
627+
zstd_dstream *stream;
628628
int ret = 0;
629629
size_t ret2;
630630
unsigned long total_out = 0;
631631
unsigned long pg_offset = 0;
632632

633-
stream = ZSTD_initDStream(
633+
stream = zstd_init_dstream(
634634
ZSTD_BTRFS_MAX_INPUT, workspace->mem, workspace->size);
635635
if (!stream) {
636-
pr_warn("BTRFS: ZSTD_initDStream failed\n");
636+
pr_warn("BTRFS: zstd_init_dstream failed\n");
637637
ret = -EIO;
638638
goto finish;
639639
}
@@ -657,15 +657,15 @@ int zstd_decompress(struct list_head *ws, unsigned char *data_in,
657657

658658
/* Check if the frame is over and we still need more input */
659659
if (ret2 == 0) {
660-
pr_debug("BTRFS: ZSTD_decompressStream ended early\n");
660+
pr_debug("BTRFS: zstd_decompress_stream ended early\n");
661661
ret = -EIO;
662662
goto finish;
663663
}
664-
ret2 = ZSTD_decompressStream(stream, &workspace->out_buf,
664+
ret2 = zstd_decompress_stream(stream, &workspace->out_buf,
665665
&workspace->in_buf);
666-
if (ZSTD_isError(ret2)) {
667-
pr_debug("BTRFS: ZSTD_decompressStream returned %d\n",
668-
ZSTD_getErrorCode(ret2));
666+
if (zstd_is_error(ret2)) {
667+
pr_debug("BTRFS: zstd_decompress_stream returned %d\n",
668+
zstd_get_error_code(ret2));
669669
ret = -EIO;
670670
goto finish;
671671
}

fs/f2fs/compress.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ static const struct f2fs_compress_ops f2fs_lz4_ops = {
336336

337337
static int zstd_init_compress_ctx(struct compress_ctx *cc)
338338
{
339-
ZSTD_parameters params;
340-
ZSTD_CStream *stream;
339+
zstd_parameters params;
340+
zstd_cstream *stream;
341341
void *workspace;
342342
unsigned int workspace_size;
343343
unsigned char level = F2FS_I(cc->inode)->i_compress_flag >>
@@ -346,17 +346,17 @@ static int zstd_init_compress_ctx(struct compress_ctx *cc)
346346
if (!level)
347347
level = F2FS_ZSTD_DEFAULT_CLEVEL;
348348

349-
params = ZSTD_getParams(level, cc->rlen, 0);
350-
workspace_size = ZSTD_CStreamWorkspaceBound(params.cParams);
349+
params = zstd_get_params(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen);
350+
workspace_size = zstd_cstream_workspace_bound(&params.cParams);
351351

352352
workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
353353
workspace_size, GFP_NOFS);
354354
if (!workspace)
355355
return -ENOMEM;
356356

357-
stream = ZSTD_initCStream(params, 0, workspace, workspace_size);
357+
stream = zstd_init_cstream(&params, 0, workspace, workspace_size);
358358
if (!stream) {
359-
printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initCStream failed\n",
359+
printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_cstream failed\n",
360360
KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
361361
__func__);
362362
kvfree(workspace);
@@ -379,9 +379,9 @@ static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
379379

380380
static int zstd_compress_pages(struct compress_ctx *cc)
381381
{
382-
ZSTD_CStream *stream = cc->private2;
383-
ZSTD_inBuffer inbuf;
384-
ZSTD_outBuffer outbuf;
382+
zstd_cstream *stream = cc->private2;
383+
zstd_in_buffer inbuf;
384+
zstd_out_buffer outbuf;
385385
int src_size = cc->rlen;
386386
int dst_size = src_size - PAGE_SIZE - COMPRESS_HEADER_SIZE;
387387
int ret;
@@ -394,19 +394,19 @@ static int zstd_compress_pages(struct compress_ctx *cc)
394394
outbuf.dst = cc->cbuf->cdata;
395395
outbuf.size = dst_size;
396396

397-
ret = ZSTD_compressStream(stream, &outbuf, &inbuf);
398-
if (ZSTD_isError(ret)) {
399-
printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
397+
ret = zstd_compress_stream(stream, &outbuf, &inbuf);
398+
if (zstd_is_error(ret)) {
399+
printk_ratelimited("%sF2FS-fs (%s): %s zstd_compress_stream failed, ret: %d\n",
400400
KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
401-
__func__, ZSTD_getErrorCode(ret));
401+
__func__, zstd_get_error_code(ret));
402402
return -EIO;
403403
}
404404

405-
ret = ZSTD_endStream(stream, &outbuf);
406-
if (ZSTD_isError(ret)) {
407-
printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_endStream returned %d\n",
405+
ret = zstd_end_stream(stream, &outbuf);
406+
if (zstd_is_error(ret)) {
407+
printk_ratelimited("%sF2FS-fs (%s): %s zstd_end_stream returned %d\n",
408408
KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
409-
__func__, ZSTD_getErrorCode(ret));
409+
__func__, zstd_get_error_code(ret));
410410
return -EIO;
411411
}
412412

@@ -423,22 +423,22 @@ static int zstd_compress_pages(struct compress_ctx *cc)
423423

424424
static int zstd_init_decompress_ctx(struct decompress_io_ctx *dic)
425425
{
426-
ZSTD_DStream *stream;
426+
zstd_dstream *stream;
427427
void *workspace;
428428
unsigned int workspace_size;
429429
unsigned int max_window_size =
430430
MAX_COMPRESS_WINDOW_SIZE(dic->log_cluster_size);
431431

432-
workspace_size = ZSTD_DStreamWorkspaceBound(max_window_size);
432+
workspace_size = zstd_dstream_workspace_bound(max_window_size);
433433

434434
workspace = f2fs_kvmalloc(F2FS_I_SB(dic->inode),
435435
workspace_size, GFP_NOFS);
436436
if (!workspace)
437437
return -ENOMEM;
438438

439-
stream = ZSTD_initDStream(max_window_size, workspace, workspace_size);
439+
stream = zstd_init_dstream(max_window_size, workspace, workspace_size);
440440
if (!stream) {
441-
printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_initDStream failed\n",
441+
printk_ratelimited("%sF2FS-fs (%s): %s zstd_init_dstream failed\n",
442442
KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
443443
__func__);
444444
kvfree(workspace);
@@ -460,9 +460,9 @@ static void zstd_destroy_decompress_ctx(struct decompress_io_ctx *dic)
460460

461461
static int zstd_decompress_pages(struct decompress_io_ctx *dic)
462462
{
463-
ZSTD_DStream *stream = dic->private2;
464-
ZSTD_inBuffer inbuf;
465-
ZSTD_outBuffer outbuf;
463+
zstd_dstream *stream = dic->private2;
464+
zstd_in_buffer inbuf;
465+
zstd_out_buffer outbuf;
466466
int ret;
467467

468468
inbuf.pos = 0;
@@ -473,11 +473,11 @@ static int zstd_decompress_pages(struct decompress_io_ctx *dic)
473473
outbuf.dst = dic->rbuf;
474474
outbuf.size = dic->rlen;
475475

476-
ret = ZSTD_decompressStream(stream, &outbuf, &inbuf);
477-
if (ZSTD_isError(ret)) {
478-
printk_ratelimited("%sF2FS-fs (%s): %s ZSTD_compressStream failed, ret: %d\n",
476+
ret = zstd_decompress_stream(stream, &outbuf, &inbuf);
477+
if (zstd_is_error(ret)) {
478+
printk_ratelimited("%sF2FS-fs (%s): %s zstd_decompress_stream failed, ret: %d\n",
479479
KERN_ERR, F2FS_I_SB(dic->inode)->sb->s_id,
480-
__func__, ZSTD_getErrorCode(ret));
480+
__func__, zstd_get_error_code(ret));
481481
return -EIO;
482482
}
483483

fs/f2fs/super.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ static int f2fs_set_zstd_level(struct f2fs_sb_info *sbi, const char *str)
592592
if (kstrtouint(str + 1, 10, &level))
593593
return -EINVAL;
594594

595-
if (!level || level > ZSTD_maxCLevel()) {
595+
if (!level || level > zstd_max_clevel()) {
596596
f2fs_info(sbi, "invalid zstd compress level: %d", level);
597597
return -EINVAL;
598598
}

0 commit comments

Comments
 (0)