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 struct lz4_ctx { 9 void *mem; 10 s32 level; 11 }; 12 13 static void lz4_destroy(void *ctx) 14 { 15 struct lz4_ctx *zctx = ctx; 16 17 vfree(zctx->mem); 18 kfree(zctx); 19 } 20 21 static void *lz4_create(void) 22 { 23 struct lz4_ctx *ctx; 24 25 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 26 if (!ctx) 27 return NULL; 28 29 /* @FIXME: using a hardcoded LZ4_ACCELERATION_DEFAULT for now */ 30 ctx->level = LZ4_ACCELERATION_DEFAULT; 31 ctx->mem = vmalloc(LZ4_MEM_COMPRESS); 32 if (!ctx->mem) 33 goto error; 34 35 return ctx; 36 error: 37 lz4_destroy(ctx); 38 return NULL; 39 } 40 41 static int lz4_compress(void *ctx, const unsigned char *src, size_t src_len, 42 unsigned char *dst, size_t *dst_len) 43 { 44 struct lz4_ctx *zctx = ctx; 45 int ret; 46 47 ret = LZ4_compress_fast(src, dst, src_len, *dst_len, 48 zctx->level, zctx->mem); 49 if (!ret) 50 return -EINVAL; 51 *dst_len = ret; 52 return 0; 53 } 54 55 static int lz4_decompress(void *ctx, const unsigned char *src, 56 size_t src_len, unsigned char *dst, size_t dst_len) 57 { 58 int ret; 59 60 ret = LZ4_decompress_safe(src, dst, src_len, dst_len); 61 if (ret < 0) 62 return -EINVAL; 63 return 0; 64 } 65 66 const struct zcomp_ops backend_lz4 = { 67 .compress = lz4_compress, 68 .decompress = lz4_decompress, 69 .create_ctx = lz4_create, 70 .destroy_ctx = lz4_destroy, 71 .name = "lz4", 72 }; 73