diff options
author | Giovanni Cabiddu <giovanni.cabiddu@intel.com> | 2016-10-21 08:19:49 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2016-10-24 23:08:31 -0400 |
commit | ac9d2c4b39e022d2c61486bfc33b730cfd02898e (patch) | |
tree | 3b63d27ef2cb3c0af1ddf23d186f9ad5b20924fb | |
parent | 1ab53a77b772bf7369464a0e4fa6fd6499acf8f1 (diff) |
crypto: acomp - add support for lzo via scomp
Add scomp backend for lzo compression algorithm.
Signed-off-by: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r-- | crypto/Kconfig | 1 | ||||
-rw-r--r-- | crypto/lzo.c | 97 |
2 files changed, 83 insertions, 15 deletions
diff --git a/crypto/Kconfig b/crypto/Kconfig index 9950c47c9d27..7ffd418b69f8 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig | |||
@@ -1589,6 +1589,7 @@ config CRYPTO_DEFLATE | |||
1589 | config CRYPTO_LZO | 1589 | config CRYPTO_LZO |
1590 | tristate "LZO compression algorithm" | 1590 | tristate "LZO compression algorithm" |
1591 | select CRYPTO_ALGAPI | 1591 | select CRYPTO_ALGAPI |
1592 | select CRYPTO_ACOMP2 | ||
1592 | select LZO_COMPRESS | 1593 | select LZO_COMPRESS |
1593 | select LZO_DECOMPRESS | 1594 | select LZO_DECOMPRESS |
1594 | help | 1595 | help |
diff --git a/crypto/lzo.c b/crypto/lzo.c index c3f3dd9a28c5..168df784da84 100644 --- a/crypto/lzo.c +++ b/crypto/lzo.c | |||
@@ -22,40 +22,55 @@ | |||
22 | #include <linux/vmalloc.h> | 22 | #include <linux/vmalloc.h> |
23 | #include <linux/mm.h> | 23 | #include <linux/mm.h> |
24 | #include <linux/lzo.h> | 24 | #include <linux/lzo.h> |
25 | #include <crypto/internal/scompress.h> | ||
25 | 26 | ||
26 | struct lzo_ctx { | 27 | struct lzo_ctx { |
27 | void *lzo_comp_mem; | 28 | void *lzo_comp_mem; |
28 | }; | 29 | }; |
29 | 30 | ||
31 | static void *lzo_alloc_ctx(struct crypto_scomp *tfm) | ||
32 | { | ||
33 | void *ctx; | ||
34 | |||
35 | ctx = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL | __GFP_NOWARN); | ||
36 | if (!ctx) | ||
37 | ctx = vmalloc(LZO1X_MEM_COMPRESS); | ||
38 | if (!ctx) | ||
39 | return ERR_PTR(-ENOMEM); | ||
40 | |||
41 | return ctx; | ||
42 | } | ||
43 | |||
30 | static int lzo_init(struct crypto_tfm *tfm) | 44 | static int lzo_init(struct crypto_tfm *tfm) |
31 | { | 45 | { |
32 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); | 46 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); |
33 | 47 | ||
34 | ctx->lzo_comp_mem = kmalloc(LZO1X_MEM_COMPRESS, | 48 | ctx->lzo_comp_mem = lzo_alloc_ctx(NULL); |
35 | GFP_KERNEL | __GFP_NOWARN); | 49 | if (IS_ERR(ctx->lzo_comp_mem)) |
36 | if (!ctx->lzo_comp_mem) | ||
37 | ctx->lzo_comp_mem = vmalloc(LZO1X_MEM_COMPRESS); | ||
38 | if (!ctx->lzo_comp_mem) | ||
39 | return -ENOMEM; | 50 | return -ENOMEM; |
40 | 51 | ||
41 | return 0; | 52 | return 0; |
42 | } | 53 | } |
43 | 54 | ||
55 | static void lzo_free_ctx(struct crypto_scomp *tfm, void *ctx) | ||
56 | { | ||
57 | kvfree(ctx); | ||
58 | } | ||
59 | |||
44 | static void lzo_exit(struct crypto_tfm *tfm) | 60 | static void lzo_exit(struct crypto_tfm *tfm) |
45 | { | 61 | { |
46 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); | 62 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); |
47 | 63 | ||
48 | kvfree(ctx->lzo_comp_mem); | 64 | lzo_free_ctx(NULL, ctx->lzo_comp_mem); |
49 | } | 65 | } |
50 | 66 | ||
51 | static int lzo_compress(struct crypto_tfm *tfm, const u8 *src, | 67 | static int __lzo_compress(const u8 *src, unsigned int slen, |
52 | unsigned int slen, u8 *dst, unsigned int *dlen) | 68 | u8 *dst, unsigned int *dlen, void *ctx) |
53 | { | 69 | { |
54 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); | ||
55 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ | 70 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ |
56 | int err; | 71 | int err; |
57 | 72 | ||
58 | err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx->lzo_comp_mem); | 73 | err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx); |
59 | 74 | ||
60 | if (err != LZO_E_OK) | 75 | if (err != LZO_E_OK) |
61 | return -EINVAL; | 76 | return -EINVAL; |
@@ -64,8 +79,23 @@ static int lzo_compress(struct crypto_tfm *tfm, const u8 *src, | |||
64 | return 0; | 79 | return 0; |
65 | } | 80 | } |
66 | 81 | ||
67 | static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src, | 82 | static int lzo_compress(struct crypto_tfm *tfm, const u8 *src, |
68 | unsigned int slen, u8 *dst, unsigned int *dlen) | 83 | unsigned int slen, u8 *dst, unsigned int *dlen) |
84 | { | ||
85 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); | ||
86 | |||
87 | return __lzo_compress(src, slen, dst, dlen, ctx->lzo_comp_mem); | ||
88 | } | ||
89 | |||
90 | static int lzo_scompress(struct crypto_scomp *tfm, const u8 *src, | ||
91 | unsigned int slen, u8 *dst, unsigned int *dlen, | ||
92 | void *ctx) | ||
93 | { | ||
94 | return __lzo_compress(src, slen, dst, dlen, ctx); | ||
95 | } | ||
96 | |||
97 | static int __lzo_decompress(const u8 *src, unsigned int slen, | ||
98 | u8 *dst, unsigned int *dlen) | ||
69 | { | 99 | { |
70 | int err; | 100 | int err; |
71 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ | 101 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ |
@@ -77,7 +107,19 @@ static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src, | |||
77 | 107 | ||
78 | *dlen = tmp_len; | 108 | *dlen = tmp_len; |
79 | return 0; | 109 | return 0; |
110 | } | ||
80 | 111 | ||
112 | static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src, | ||
113 | unsigned int slen, u8 *dst, unsigned int *dlen) | ||
114 | { | ||
115 | return __lzo_decompress(src, slen, dst, dlen); | ||
116 | } | ||
117 | |||
118 | static int lzo_sdecompress(struct crypto_scomp *tfm, const u8 *src, | ||
119 | unsigned int slen, u8 *dst, unsigned int *dlen, | ||
120 | void *ctx) | ||
121 | { | ||
122 | return __lzo_decompress(src, slen, dst, dlen); | ||
81 | } | 123 | } |
82 | 124 | ||
83 | static struct crypto_alg alg = { | 125 | static struct crypto_alg alg = { |
@@ -88,18 +130,43 @@ static struct crypto_alg alg = { | |||
88 | .cra_init = lzo_init, | 130 | .cra_init = lzo_init, |
89 | .cra_exit = lzo_exit, | 131 | .cra_exit = lzo_exit, |
90 | .cra_u = { .compress = { | 132 | .cra_u = { .compress = { |
91 | .coa_compress = lzo_compress, | 133 | .coa_compress = lzo_compress, |
92 | .coa_decompress = lzo_decompress } } | 134 | .coa_decompress = lzo_decompress } } |
135 | }; | ||
136 | |||
137 | static struct scomp_alg scomp = { | ||
138 | .alloc_ctx = lzo_alloc_ctx, | ||
139 | .free_ctx = lzo_free_ctx, | ||
140 | .compress = lzo_scompress, | ||
141 | .decompress = lzo_sdecompress, | ||
142 | .base = { | ||
143 | .cra_name = "lzo", | ||
144 | .cra_driver_name = "lzo-scomp", | ||
145 | .cra_module = THIS_MODULE, | ||
146 | } | ||
93 | }; | 147 | }; |
94 | 148 | ||
95 | static int __init lzo_mod_init(void) | 149 | static int __init lzo_mod_init(void) |
96 | { | 150 | { |
97 | return crypto_register_alg(&alg); | 151 | int ret; |
152 | |||
153 | ret = crypto_register_alg(&alg); | ||
154 | if (ret) | ||
155 | return ret; | ||
156 | |||
157 | ret = crypto_register_scomp(&scomp); | ||
158 | if (ret) { | ||
159 | crypto_unregister_alg(&alg); | ||
160 | return ret; | ||
161 | } | ||
162 | |||
163 | return ret; | ||
98 | } | 164 | } |
99 | 165 | ||
100 | static void __exit lzo_mod_fini(void) | 166 | static void __exit lzo_mod_fini(void) |
101 | { | 167 | { |
102 | crypto_unregister_alg(&alg); | 168 | crypto_unregister_alg(&alg); |
169 | crypto_unregister_scomp(&scomp); | ||
103 | } | 170 | } |
104 | 171 | ||
105 | module_init(lzo_mod_init); | 172 | module_init(lzo_mod_init); |