Linux 6.13-rc4
[linux.git] / drivers / block / zram / backend_deflate.c
blob0f7f252c12f44806db9c556bae71e030d6b6d4b5
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/kernel.h>
4 #include <linux/slab.h>
5 #include <linux/vmalloc.h>
6 #include <linux/zlib.h>
8 #include "backend_deflate.h"
10 /* Use the same value as crypto API */
11 #define DEFLATE_DEF_WINBITS 11
12 #define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL
14 struct deflate_ctx {
15 struct z_stream_s cctx;
16 struct z_stream_s dctx;
19 static void deflate_release_params(struct zcomp_params *params)
23 static int deflate_setup_params(struct zcomp_params *params)
25 if (params->level == ZCOMP_PARAM_NO_LEVEL)
26 params->level = Z_DEFAULT_COMPRESSION;
28 return 0;
31 static void deflate_destroy(struct zcomp_ctx *ctx)
33 struct deflate_ctx *zctx = ctx->context;
35 if (!zctx)
36 return;
38 if (zctx->cctx.workspace) {
39 zlib_deflateEnd(&zctx->cctx);
40 vfree(zctx->cctx.workspace);
42 if (zctx->dctx.workspace) {
43 zlib_inflateEnd(&zctx->dctx);
44 vfree(zctx->dctx.workspace);
46 kfree(zctx);
49 static int deflate_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
51 struct deflate_ctx *zctx;
52 size_t sz;
53 int ret;
55 zctx = kzalloc(sizeof(*zctx), GFP_KERNEL);
56 if (!zctx)
57 return -ENOMEM;
59 ctx->context = zctx;
60 sz = zlib_deflate_workspacesize(-DEFLATE_DEF_WINBITS, MAX_MEM_LEVEL);
61 zctx->cctx.workspace = vzalloc(sz);
62 if (!zctx->cctx.workspace)
63 goto error;
65 ret = zlib_deflateInit2(&zctx->cctx, params->level, Z_DEFLATED,
66 -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
67 Z_DEFAULT_STRATEGY);
68 if (ret != Z_OK)
69 goto error;
71 sz = zlib_inflate_workspacesize();
72 zctx->dctx.workspace = vzalloc(sz);
73 if (!zctx->dctx.workspace)
74 goto error;
76 ret = zlib_inflateInit2(&zctx->dctx, -DEFLATE_DEF_WINBITS);
77 if (ret != Z_OK)
78 goto error;
80 return 0;
82 error:
83 deflate_destroy(ctx);
84 return -EINVAL;
87 static int deflate_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
88 struct zcomp_req *req)
90 struct deflate_ctx *zctx = ctx->context;
91 struct z_stream_s *deflate;
92 int ret;
94 deflate = &zctx->cctx;
95 ret = zlib_deflateReset(deflate);
96 if (ret != Z_OK)
97 return -EINVAL;
99 deflate->next_in = (u8 *)req->src;
100 deflate->avail_in = req->src_len;
101 deflate->next_out = (u8 *)req->dst;
102 deflate->avail_out = req->dst_len;
104 ret = zlib_deflate(deflate, Z_FINISH);
105 if (ret != Z_STREAM_END)
106 return -EINVAL;
108 req->dst_len = deflate->total_out;
109 return 0;
112 static int deflate_decompress(struct zcomp_params *params,
113 struct zcomp_ctx *ctx,
114 struct zcomp_req *req)
116 struct deflate_ctx *zctx = ctx->context;
117 struct z_stream_s *inflate;
118 int ret;
120 inflate = &zctx->dctx;
122 ret = zlib_inflateReset(inflate);
123 if (ret != Z_OK)
124 return -EINVAL;
126 inflate->next_in = (u8 *)req->src;
127 inflate->avail_in = req->src_len;
128 inflate->next_out = (u8 *)req->dst;
129 inflate->avail_out = req->dst_len;
131 ret = zlib_inflate(inflate, Z_SYNC_FLUSH);
132 if (ret != Z_STREAM_END)
133 return -EINVAL;
135 return 0;
138 const struct zcomp_ops backend_deflate = {
139 .compress = deflate_compress,
140 .decompress = deflate_decompress,
141 .create_ctx = deflate_create,
142 .destroy_ctx = deflate_destroy,
143 .setup_params = deflate_setup_params,
144 .release_params = deflate_release_params,
145 .name = "deflate",