xref: /linux/drivers/block/zram/backend_lz4.c (revision b8f03cb703a160e14f87a467a4443adc5d940087)
1 #include <linux/kernel.h>
2 #include <linux/lz4.h>
3 #include <linux/slab.h>
4 #include <linux/vmalloc.h>
5 
6 #include "backend_lz4.h"
7 
8 static void lz4_release_params(struct zcomp_params *params)
9 {
10 }
11 
12 static int lz4_setup_params(struct zcomp_params *params)
13 {
14 	if (params->level == ZCOMP_PARAM_NO_LEVEL)
15 		params->level = LZ4_ACCELERATION_DEFAULT;
16 
17 	return 0;
18 }
19 
20 static void lz4_destroy(struct zcomp_ctx *ctx)
21 {
22 	vfree(ctx->context);
23 }
24 
25 static int lz4_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
26 {
27 	ctx->context = vmalloc(LZ4_MEM_COMPRESS);
28 	if (!ctx->context)
29 		return -ENOMEM;
30 
31 	return 0;
32 }
33 
34 static int lz4_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
35 			struct zcomp_req *req)
36 {
37 	int ret;
38 
39 	ret = LZ4_compress_fast(req->src, req->dst, req->src_len,
40 				req->dst_len, params->level, ctx->context);
41 	if (!ret)
42 		return -EINVAL;
43 	req->dst_len = ret;
44 	return 0;
45 }
46 
47 static int lz4_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,
48 			  struct zcomp_req *req)
49 {
50 	int ret;
51 
52 	ret = LZ4_decompress_safe(req->src, req->dst, req->src_len,
53 				  req->dst_len);
54 	if (ret < 0)
55 		return -EINVAL;
56 	return 0;
57 }
58 
59 const struct zcomp_ops backend_lz4 = {
60 	.compress	= lz4_compress,
61 	.decompress	= lz4_decompress,
62 	.create_ctx	= lz4_create,
63 	.destroy_ctx	= lz4_destroy,
64 	.setup_params	= lz4_setup_params,
65 	.release_params	= lz4_release_params,
66 	.name		= "lz4",
67 };
68