122d651c3SSergey Senozhatsky #include <linux/kernel.h> 222d651c3SSergey Senozhatsky #include <linux/lz4.h> 322d651c3SSergey Senozhatsky #include <linux/slab.h> 422d651c3SSergey Senozhatsky #include <linux/vmalloc.h> 522d651c3SSergey Senozhatsky 622d651c3SSergey Senozhatsky #include "backend_lz4.h" 722d651c3SSergey Senozhatsky 8*b8f03cb7SSergey Senozhatsky static void lz4_release_params(struct zcomp_params *params) 9*b8f03cb7SSergey Senozhatsky { 10*b8f03cb7SSergey Senozhatsky } 11*b8f03cb7SSergey Senozhatsky 12*b8f03cb7SSergey Senozhatsky static int lz4_setup_params(struct zcomp_params *params) 13*b8f03cb7SSergey Senozhatsky { 14*b8f03cb7SSergey Senozhatsky if (params->level == ZCOMP_PARAM_NO_LEVEL) 15*b8f03cb7SSergey Senozhatsky params->level = LZ4_ACCELERATION_DEFAULT; 16*b8f03cb7SSergey Senozhatsky 17*b8f03cb7SSergey Senozhatsky return 0; 18*b8f03cb7SSergey Senozhatsky } 1922d651c3SSergey Senozhatsky 206a81bdfeSSergey Senozhatsky static void lz4_destroy(struct zcomp_ctx *ctx) 2122d651c3SSergey Senozhatsky { 22*b8f03cb7SSergey Senozhatsky vfree(ctx->context); 2322d651c3SSergey Senozhatsky } 2422d651c3SSergey Senozhatsky 256a81bdfeSSergey Senozhatsky static int lz4_create(struct zcomp_params *params, struct zcomp_ctx *ctx) 2622d651c3SSergey Senozhatsky { 27*b8f03cb7SSergey Senozhatsky ctx->context = vmalloc(LZ4_MEM_COMPRESS); 28*b8f03cb7SSergey Senozhatsky if (!ctx->context) 296a81bdfeSSergey Senozhatsky return -ENOMEM; 3022d651c3SSergey Senozhatsky 316a81bdfeSSergey Senozhatsky return 0; 3222d651c3SSergey Senozhatsky } 3322d651c3SSergey Senozhatsky 34*b8f03cb7SSergey Senozhatsky static int lz4_compress(struct zcomp_params *params, struct zcomp_ctx *ctx, 35*b8f03cb7SSergey Senozhatsky struct zcomp_req *req) 3622d651c3SSergey Senozhatsky { 3722d651c3SSergey Senozhatsky int ret; 3822d651c3SSergey Senozhatsky 3952c7b4e2SSergey Senozhatsky ret = LZ4_compress_fast(req->src, req->dst, req->src_len, 40*b8f03cb7SSergey Senozhatsky req->dst_len, params->level, ctx->context); 4122d651c3SSergey Senozhatsky if (!ret) 4222d651c3SSergey Senozhatsky return -EINVAL; 4352c7b4e2SSergey Senozhatsky req->dst_len = ret; 4422d651c3SSergey Senozhatsky return 0; 4522d651c3SSergey Senozhatsky } 4622d651c3SSergey Senozhatsky 47*b8f03cb7SSergey Senozhatsky static int lz4_decompress(struct zcomp_params *params, struct zcomp_ctx *ctx, 48*b8f03cb7SSergey Senozhatsky struct zcomp_req *req) 4922d651c3SSergey Senozhatsky { 5022d651c3SSergey Senozhatsky int ret; 5122d651c3SSergey Senozhatsky 5252c7b4e2SSergey Senozhatsky ret = LZ4_decompress_safe(req->src, req->dst, req->src_len, 5352c7b4e2SSergey Senozhatsky req->dst_len); 5422d651c3SSergey Senozhatsky if (ret < 0) 5522d651c3SSergey Senozhatsky return -EINVAL; 5622d651c3SSergey Senozhatsky return 0; 5722d651c3SSergey Senozhatsky } 5822d651c3SSergey Senozhatsky 5922d651c3SSergey Senozhatsky const struct zcomp_ops backend_lz4 = { 6022d651c3SSergey Senozhatsky .compress = lz4_compress, 6122d651c3SSergey Senozhatsky .decompress = lz4_decompress, 6222d651c3SSergey Senozhatsky .create_ctx = lz4_create, 6322d651c3SSergey Senozhatsky .destroy_ctx = lz4_destroy, 64*b8f03cb7SSergey Senozhatsky .setup_params = lz4_setup_params, 65*b8f03cb7SSergey Senozhatsky .release_params = lz4_release_params, 6622d651c3SSergey Senozhatsky .name = "lz4", 6722d651c3SSergey Senozhatsky }; 68