backend_lz4hc.c (b8f03cb703a160e14f87a467a4443adc5d940087) backend_lz4hc.c (1e673c8cf7f9c1156f615b7c00f224a8110070da)
1#include <linux/kernel.h>
2#include <linux/lz4.h>
3#include <linux/slab.h>
4#include <linux/vmalloc.h>
5
6#include "backend_lz4hc.h"
7
1#include <linux/kernel.h>
2#include <linux/lz4.h>
3#include <linux/slab.h>
4#include <linux/vmalloc.h>
5
6#include "backend_lz4hc.h"
7
8struct lz4hc_ctx {
9 void *mem;
10
11 LZ4_streamDecode_t *dstrm;
12 LZ4_streamHC_t *cstrm;
13};
14
8static void lz4hc_release_params(struct zcomp_params *params)
9{
10}
11
12static int lz4hc_setup_params(struct zcomp_params *params)
13{
14 if (params->level == ZCOMP_PARAM_NO_LEVEL)
15 params->level = LZ4HC_DEFAULT_CLEVEL;
16
17 return 0;
18}
19
20static void lz4hc_destroy(struct zcomp_ctx *ctx)
21{
15static void lz4hc_release_params(struct zcomp_params *params)
16{
17}
18
19static int lz4hc_setup_params(struct zcomp_params *params)
20{
21 if (params->level == ZCOMP_PARAM_NO_LEVEL)
22 params->level = LZ4HC_DEFAULT_CLEVEL;
23
24 return 0;
25}
26
27static void lz4hc_destroy(struct zcomp_ctx *ctx)
28{
22 vfree(ctx->context);
29 struct lz4hc_ctx *zctx = ctx->context;
30
31 if (!zctx)
32 return;
33
34 kfree(zctx->dstrm);
35 kfree(zctx->cstrm);
36 vfree(zctx->mem);
37 kfree(zctx);
23}
24
25static int lz4hc_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
26{
38}
39
40static int lz4hc_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
41{
27 ctx->context = vmalloc(LZ4HC_MEM_COMPRESS);
28 if (!ctx->context)
42 struct lz4hc_ctx *zctx;
43
44 zctx = kzalloc(sizeof(*zctx), GFP_KERNEL);
45 if (!zctx)
29 return -ENOMEM;
30
46 return -ENOMEM;
47
48 ctx->context = zctx;
49 if (params->dict_sz == 0) {
50 zctx->mem = vmalloc(LZ4HC_MEM_COMPRESS);
51 if (!zctx->mem)
52 goto error;
53 } else {
54 zctx->dstrm = kzalloc(sizeof(*zctx->dstrm), GFP_KERNEL);
55 if (!zctx->dstrm)
56 goto error;
57
58 zctx->cstrm = kzalloc(sizeof(*zctx->cstrm), GFP_KERNEL);
59 if (!zctx->cstrm)
60 goto error;
61 }
62
31 return 0;
63 return 0;
64
65error:
66 lz4hc_destroy(ctx);
67 return -EINVAL;
32}
33
34static int lz4hc_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
35 struct zcomp_req *req)
36{
68}
69
70static int lz4hc_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
71 struct zcomp_req *req)
72{
73 struct lz4hc_ctx *zctx = ctx->context;
37 int ret;
38
74 int ret;
75
39 ret = LZ4_compress_HC(req->src, req->dst, req->src_len, req->dst_len,
40 params->level, ctx->context);
76 if (!zctx->cstrm) {
77 ret = LZ4_compress_HC(req->src, req->dst, req->src_len,
78 req->dst_len, params->level,
79 zctx->mem);
80 } else {
81 /* Cstrm needs to be reset */
82 LZ4_resetStreamHC(zctx->cstrm, params->level);
83 ret = LZ4_loadDictHC(zctx->cstrm, params->dict,
84 params->dict_sz);
85 if (ret != params->dict_sz)
86 return -EINVAL;
87 ret = LZ4_compress_HC_continue(zctx->cstrm, req->src, req->dst,
88 req->src_len, req->dst_len);
89 }
41 if (!ret)
42 return -EINVAL;
43 req->dst_len = ret;
44 return 0;
45}
46
47static int lz4hc_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,
48 struct zcomp_req *req)
49{
90 if (!ret)
91 return -EINVAL;
92 req->dst_len = ret;
93 return 0;
94}
95
96static int lz4hc_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,
97 struct zcomp_req *req)
98{
99 struct lz4hc_ctx *zctx = ctx->context;
50 int ret;
51
100 int ret;
101
52 ret = LZ4_decompress_safe(req->src, req->dst, req->src_len,
53 req->dst_len);
102 if (!zctx->dstrm) {
103 ret = LZ4_decompress_safe(req->src, req->dst, req->src_len,
104 req->dst_len);
105 } else {
106 /* Dstrm needs to be reset */
107 ret = LZ4_setStreamDecode(zctx->dstrm, params->dict,
108 params->dict_sz);
109 if (!ret)
110 return -EINVAL;
111 ret = LZ4_decompress_safe_continue(zctx->dstrm, req->src,
112 req->dst, req->src_len,
113 req->dst_len);
114 }
54 if (ret < 0)
55 return -EINVAL;
56 return 0;
57}
58
59const struct zcomp_ops backend_lz4hc = {
60 .compress = lz4hc_compress,
61 .decompress = lz4hc_decompress,
62 .create_ctx = lz4hc_create,
63 .destroy_ctx = lz4hc_destroy,
64 .setup_params = lz4hc_setup_params,
65 .release_params = lz4hc_release_params,
66 .name = "lz4hc",
67};
115 if (ret < 0)
116 return -EINVAL;
117 return 0;
118}
119
120const struct zcomp_ops backend_lz4hc = {
121 .compress = lz4hc_compress,
122 .decompress = lz4hc_decompress,
123 .create_ctx = lz4hc_create,
124 .destroy_ctx = lz4hc_destroy,
125 .setup_params = lz4hc_setup_params,
126 .release_params = lz4hc_release_params,
127 .name = "lz4hc",
128};