2222#include <linux/vmalloc.h>
2323#include <linux/mm.h>
2424#include <linux/lzo.h>
25+ #include <crypto/internal/scompress.h>
2526
2627struct lzo_ctx {
2728 void * lzo_comp_mem ;
2829};
2930
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+
3044static int lzo_init (struct crypto_tfm * tfm )
3145{
3246 struct lzo_ctx * ctx = crypto_tfm_ctx (tfm );
3347
34- ctx -> lzo_comp_mem = kmalloc (LZO1X_MEM_COMPRESS ,
35- GFP_KERNEL | __GFP_NOWARN );
36- if (!ctx -> lzo_comp_mem )
37- ctx -> lzo_comp_mem = vmalloc (LZO1X_MEM_COMPRESS );
38- if (!ctx -> lzo_comp_mem )
48+ ctx -> lzo_comp_mem = lzo_alloc_ctx (NULL );
49+ if (IS_ERR (ctx -> lzo_comp_mem ))
3950 return - ENOMEM ;
4051
4152 return 0 ;
4253}
4354
55+ static void lzo_free_ctx (struct crypto_scomp * tfm , void * ctx )
56+ {
57+ kvfree (ctx );
58+ }
59+
4460static void lzo_exit (struct crypto_tfm * tfm )
4561{
4662 struct lzo_ctx * ctx = crypto_tfm_ctx (tfm );
4763
48- kvfree ( ctx -> lzo_comp_mem );
64+ lzo_free_ctx ( NULL , ctx -> lzo_comp_mem );
4965}
5066
51- static int lzo_compress ( struct crypto_tfm * tfm , const u8 * src ,
52- unsigned int slen , u8 * dst , unsigned int * dlen )
67+ static int __lzo_compress ( const u8 * src , unsigned int slen ,
68+ u8 * dst , unsigned int * dlen , void * ctx )
5369{
54- struct lzo_ctx * ctx = crypto_tfm_ctx (tfm );
5570 size_t tmp_len = * dlen ; /* size_t(ulong) <-> uint on 64 bit */
5671 int err ;
5772
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 );
5974
6075 if (err != LZO_E_OK )
6176 return - EINVAL ;
@@ -64,8 +79,23 @@ static int lzo_compress(struct crypto_tfm *tfm, const u8 *src,
6479 return 0 ;
6580}
6681
67- static int lzo_decompress (struct crypto_tfm * tfm , const u8 * src ,
68- unsigned int slen , u8 * dst , unsigned int * dlen )
82+ static int lzo_compress (struct crypto_tfm * tfm , const u8 * src ,
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 )
6999{
70100 int err ;
71101 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,
77107
78108 * dlen = tmp_len ;
79109 return 0 ;
110+ }
80111
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 );
81123}
82124
83125static struct crypto_alg alg = {
@@ -88,18 +130,43 @@ static struct crypto_alg alg = {
88130 .cra_init = lzo_init ,
89131 .cra_exit = lzo_exit ,
90132 .cra_u = { .compress = {
91- .coa_compress = lzo_compress ,
92- .coa_decompress = lzo_decompress } }
133+ .coa_compress = lzo_compress ,
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+ }
93147};
94148
95149static int __init lzo_mod_init (void )
96150{
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 ;
98164}
99165
100166static void __exit lzo_mod_fini (void )
101167{
102168 crypto_unregister_alg (& alg );
169+ crypto_unregister_scomp (& scomp );
103170}
104171
105172module_init (lzo_mod_init );
0 commit comments