xref: /linux/drivers/block/zram/backend_lzo.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #define pr_fmt(fmt) "lzo: " fmt
4 
5 #include <linux/kernel.h>
6 #include <linux/slab.h>
7 #include <linux/lzo.h>
8 
9 #include "backend_lzo.h"
10 
11 static void lzo_release_params(struct zcomp_params *params)
12 {
13 }
14 
15 static int lzo_setup_params(struct zcomp_params *params)
16 {
17 	if (params->dict_sz) {
18 		pr_err("dictionary is not supported\n");
19 		return -EOPNOTSUPP;
20 	}
21 	if (params->level != ZCOMP_PARAM_NOT_SET) {
22 		pr_err("compression level is not supported\n");
23 		return -EOPNOTSUPP;
24 	}
25 	return 0;
26 }
27 
28 static int lzo_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
29 {
30 	ctx->context = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
31 	if (!ctx->context)
32 		return -ENOMEM;
33 	return 0;
34 }
35 
36 static void lzo_destroy(struct zcomp_ctx *ctx)
37 {
38 	kfree(ctx->context);
39 }
40 
41 static int lzo_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
42 			struct zcomp_req *req)
43 {
44 	int ret;
45 
46 	ret = lzo1x_1_compress(req->src, req->src_len, req->dst,
47 			       &req->dst_len, ctx->context);
48 	return ret == LZO_E_OK ? 0 : ret;
49 }
50 
51 static int lzo_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx,
52 			  struct zcomp_req *req)
53 {
54 	int ret;
55 
56 	ret = lzo1x_decompress_safe(req->src, req->src_len,
57 				    req->dst, &req->dst_len);
58 	return ret == LZO_E_OK ? 0 : ret;
59 }
60 
61 const struct zcomp_ops backend_lzo = {
62 	.compress	= lzo_compress,
63 	.decompress	= lzo_decompress,
64 	.create_ctx	= lzo_create,
65 	.destroy_ctx	= lzo_destroy,
66 	.setup_params	= lzo_setup_params,
67 	.release_params	= lzo_release_params,
68 	.name		= "lzo",
69 };
70