1 #include <linux/kernel.h>
3 #include <linux/slab.h>
4 #include <linux/vmalloc.h>
6 #include "backend_lz4.h"
11 LZ4_streamDecode_t
*dstrm
;
15 static void lz4_release_params(struct zcomp_params
*params
)
19 static int lz4_setup_params(struct zcomp_params
*params
)
21 if (params
->level
== ZCOMP_PARAM_NO_LEVEL
)
22 params
->level
= LZ4_ACCELERATION_DEFAULT
;
27 static void lz4_destroy(struct zcomp_ctx
*ctx
)
29 struct lz4_ctx
*zctx
= ctx
->context
;
40 static int lz4_create(struct zcomp_params
*params
, struct zcomp_ctx
*ctx
)
44 zctx
= kzalloc(sizeof(*zctx
), GFP_KERNEL
);
49 if (params
->dict_sz
== 0) {
50 zctx
->mem
= vmalloc(LZ4_MEM_COMPRESS
);
54 zctx
->dstrm
= kzalloc(sizeof(*zctx
->dstrm
), GFP_KERNEL
);
58 zctx
->cstrm
= kzalloc(sizeof(*zctx
->cstrm
), GFP_KERNEL
);
70 static int lz4_compress(struct zcomp_params
*params
, struct zcomp_ctx
*ctx
,
71 struct zcomp_req
*req
)
73 struct lz4_ctx
*zctx
= ctx
->context
;
77 ret
= LZ4_compress_fast(req
->src
, req
->dst
, req
->src_len
,
78 req
->dst_len
, params
->level
,
81 /* Cstrm needs to be reset */
82 ret
= LZ4_loadDict(zctx
->cstrm
, params
->dict
, params
->dict_sz
);
83 if (ret
!= params
->dict_sz
)
85 ret
= LZ4_compress_fast_continue(zctx
->cstrm
, req
->src
,
86 req
->dst
, req
->src_len
,
87 req
->dst_len
, params
->level
);
95 static int lz4_decompress(struct zcomp_params
*params
, struct zcomp_ctx
*ctx
,
96 struct zcomp_req
*req
)
98 struct lz4_ctx
*zctx
= ctx
->context
;
102 ret
= LZ4_decompress_safe(req
->src
, req
->dst
, req
->src_len
,
105 /* Dstrm needs to be reset */
106 ret
= LZ4_setStreamDecode(zctx
->dstrm
, params
->dict
,
110 ret
= LZ4_decompress_safe_continue(zctx
->dstrm
, req
->src
,
111 req
->dst
, req
->src_len
,
119 const struct zcomp_ops backend_lz4
= {
120 .compress
= lz4_compress
,
121 .decompress
= lz4_decompress
,
122 .create_ctx
= lz4_create
,
123 .destroy_ctx
= lz4_destroy
,
124 .setup_params
= lz4_setup_params
,
125 .release_params
= lz4_release_params
,