diff options
author | Giovanni Cabiddu <giovanni.cabiddu@intel.com> | 2016-10-21 08:19:50 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2016-10-24 23:08:32 -0400 |
commit | 8cd9330e0a615c931037d4def98b5ce0d540f08d (patch) | |
tree | ea617e3c32da90177f9075ff3c717cfbe1a4a7a9 /crypto/lz4.c | |
parent | ac9d2c4b39e022d2c61486bfc33b730cfd02898e (diff) |
crypto: acomp - add support for lz4 via scomp
Add scomp backend for lz4 compression algorithm.
Signed-off-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/lz4.c')
-rw-r--r-- | crypto/lz4.c | 91 |
1 files changed, 81 insertions, 10 deletions
diff --git a/crypto/lz4.c b/crypto/lz4.c index aefbceaf3104..99c1b2cc2976 100644 --- a/crypto/lz4.c +++ b/crypto/lz4.c | |||
@@ -23,36 +23,53 @@ | |||
23 | #include <linux/crypto.h> | 23 | #include <linux/crypto.h> |
24 | #include <linux/vmalloc.h> | 24 | #include <linux/vmalloc.h> |
25 | #include <linux/lz4.h> | 25 | #include <linux/lz4.h> |
26 | #include <crypto/internal/scompress.h> | ||
26 | 27 | ||
27 | struct lz4_ctx { | 28 | struct lz4_ctx { |
28 | void *lz4_comp_mem; | 29 | void *lz4_comp_mem; |
29 | }; | 30 | }; |
30 | 31 | ||
32 | static void *lz4_alloc_ctx(struct crypto_scomp *tfm) | ||
33 | { | ||
34 | void *ctx; | ||
35 | |||
36 | ctx = vmalloc(LZ4_MEM_COMPRESS); | ||
37 | if (!ctx) | ||
38 | return ERR_PTR(-ENOMEM); | ||
39 | |||
40 | return ctx; | ||
41 | } | ||
42 | |||
31 | static int lz4_init(struct crypto_tfm *tfm) | 43 | static int lz4_init(struct crypto_tfm *tfm) |
32 | { | 44 | { |
33 | struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); | 45 | struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); |
34 | 46 | ||
35 | ctx->lz4_comp_mem = vmalloc(LZ4_MEM_COMPRESS); | 47 | ctx->lz4_comp_mem = lz4_alloc_ctx(NULL); |
36 | if (!ctx->lz4_comp_mem) | 48 | if (IS_ERR(ctx->lz4_comp_mem)) |
37 | return -ENOMEM; | 49 | return -ENOMEM; |
38 | 50 | ||
39 | return 0; | 51 | return 0; |
40 | } | 52 | } |
41 | 53 | ||
54 | static void lz4_free_ctx(struct crypto_scomp *tfm, void *ctx) | ||
55 | { | ||
56 | vfree(ctx); | ||
57 | } | ||
58 | |||
42 | static void lz4_exit(struct crypto_tfm *tfm) | 59 | static void lz4_exit(struct crypto_tfm *tfm) |
43 | { | 60 | { |
44 | struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); | 61 | struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); |
45 | vfree(ctx->lz4_comp_mem); | 62 | |
63 | lz4_free_ctx(NULL, ctx->lz4_comp_mem); | ||
46 | } | 64 | } |
47 | 65 | ||
48 | static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src, | 66 | static int __lz4_compress_crypto(const u8 *src, unsigned int slen, |
49 | unsigned int slen, u8 *dst, unsigned int *dlen) | 67 | u8 *dst, unsigned int *dlen, void *ctx) |
50 | { | 68 | { |
51 | struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); | ||
52 | size_t tmp_len = *dlen; | 69 | size_t tmp_len = *dlen; |
53 | int err; | 70 | int err; |
54 | 71 | ||
55 | err = lz4_compress(src, slen, dst, &tmp_len, ctx->lz4_comp_mem); | 72 | err = lz4_compress(src, slen, dst, &tmp_len, ctx); |
56 | 73 | ||
57 | if (err < 0) | 74 | if (err < 0) |
58 | return -EINVAL; | 75 | return -EINVAL; |
@@ -61,8 +78,23 @@ static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src, | |||
61 | return 0; | 78 | return 0; |
62 | } | 79 | } |
63 | 80 | ||
64 | static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, | 81 | static int lz4_scompress(struct crypto_scomp *tfm, const u8 *src, |
65 | unsigned int slen, u8 *dst, unsigned int *dlen) | 82 | unsigned int slen, u8 *dst, unsigned int *dlen, |
83 | void *ctx) | ||
84 | { | ||
85 | return __lz4_compress_crypto(src, slen, dst, dlen, ctx); | ||
86 | } | ||
87 | |||
88 | static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src, | ||
89 | unsigned int slen, u8 *dst, unsigned int *dlen) | ||
90 | { | ||
91 | struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); | ||
92 | |||
93 | return __lz4_compress_crypto(src, slen, dst, dlen, ctx->lz4_comp_mem); | ||
94 | } | ||
95 | |||
96 | static int __lz4_decompress_crypto(const u8 *src, unsigned int slen, | ||
97 | u8 *dst, unsigned int *dlen, void *ctx) | ||
66 | { | 98 | { |
67 | int err; | 99 | int err; |
68 | size_t tmp_len = *dlen; | 100 | size_t tmp_len = *dlen; |
@@ -76,6 +108,20 @@ static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, | |||
76 | return err; | 108 | return err; |
77 | } | 109 | } |
78 | 110 | ||
111 | static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src, | ||
112 | unsigned int slen, u8 *dst, unsigned int *dlen, | ||
113 | void *ctx) | ||
114 | { | ||
115 | return __lz4_decompress_crypto(src, slen, dst, dlen, NULL); | ||
116 | } | ||
117 | |||
118 | static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, | ||
119 | unsigned int slen, u8 *dst, | ||
120 | unsigned int *dlen) | ||
121 | { | ||
122 | return __lz4_decompress_crypto(src, slen, dst, dlen, NULL); | ||
123 | } | ||
124 | |||
79 | static struct crypto_alg alg_lz4 = { | 125 | static struct crypto_alg alg_lz4 = { |
80 | .cra_name = "lz4", | 126 | .cra_name = "lz4", |
81 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, | 127 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, |
@@ -89,14 +135,39 @@ static struct crypto_alg alg_lz4 = { | |||
89 | .coa_decompress = lz4_decompress_crypto } } | 135 | .coa_decompress = lz4_decompress_crypto } } |
90 | }; | 136 | }; |
91 | 137 | ||
138 | static struct scomp_alg scomp = { | ||
139 | .alloc_ctx = lz4_alloc_ctx, | ||
140 | .free_ctx = lz4_free_ctx, | ||
141 | .compress = lz4_scompress, | ||
142 | .decompress = lz4_sdecompress, | ||
143 | .base = { | ||
144 | .cra_name = "lz4", | ||
145 | .cra_driver_name = "lz4-scomp", | ||
146 | .cra_module = THIS_MODULE, | ||
147 | } | ||
148 | }; | ||
149 | |||
92 | static int __init lz4_mod_init(void) | 150 | static int __init lz4_mod_init(void) |
93 | { | 151 | { |
94 | return crypto_register_alg(&alg_lz4); | 152 | int ret; |
153 | |||
154 | ret = crypto_register_alg(&alg_lz4); | ||
155 | if (ret) | ||
156 | return ret; | ||
157 | |||
158 | ret = crypto_register_scomp(&scomp); | ||
159 | if (ret) { | ||
160 | crypto_unregister_alg(&alg_lz4); | ||
161 | return ret; | ||
162 | } | ||
163 | |||
164 | return ret; | ||
95 | } | 165 | } |
96 | 166 | ||
97 | static void __exit lz4_mod_fini(void) | 167 | static void __exit lz4_mod_fini(void) |
98 | { | 168 | { |
99 | crypto_unregister_alg(&alg_lz4); | 169 | crypto_unregister_alg(&alg_lz4); |
170 | crypto_unregister_scomp(&scomp); | ||
100 | } | 171 | } |
101 | 172 | ||
102 | module_init(lz4_mod_init); | 173 | module_init(lz4_mod_init); |