Sergey Senozhatsky | 6e76668 | 2014-04-07 15:38:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Sergey Senozhatsky. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/lz4.h> |
Kyeongdon Kim | d913897 | 2016-01-14 15:22:29 -0800 | [diff] [blame] | 13 | #include <linux/vmalloc.h> |
| 14 | #include <linux/mm.h> |
Sergey Senozhatsky | 6e76668 | 2014-04-07 15:38:18 -0700 | [diff] [blame] | 15 | |
| 16 | #include "zcomp_lz4.h" |
| 17 | |
Minchan Kim | 75d8947 | 2016-01-14 15:22:32 -0800 | [diff] [blame] | 18 | static void *zcomp_lz4_create(gfp_t flags) |
Sergey Senozhatsky | 6e76668 | 2014-04-07 15:38:18 -0700 | [diff] [blame] | 19 | { |
Kyeongdon Kim | d913897 | 2016-01-14 15:22:29 -0800 | [diff] [blame] | 20 | void *ret; |
| 21 | |
Sergey Senozhatsky | e02d238 | 2016-01-14 15:22:35 -0800 | [diff] [blame] | 22 | ret = kmalloc(LZ4_MEM_COMPRESS, flags); |
Kyeongdon Kim | d913897 | 2016-01-14 15:22:29 -0800 | [diff] [blame] | 23 | if (!ret) |
| 24 | ret = __vmalloc(LZ4_MEM_COMPRESS, |
Sergey Senozhatsky | e02d238 | 2016-01-14 15:22:35 -0800 | [diff] [blame] | 25 | flags | __GFP_HIGHMEM, |
Kyeongdon Kim | d913897 | 2016-01-14 15:22:29 -0800 | [diff] [blame] | 26 | PAGE_KERNEL); |
| 27 | return ret; |
Sergey Senozhatsky | 6e76668 | 2014-04-07 15:38:18 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static void zcomp_lz4_destroy(void *private) |
| 31 | { |
Kyeongdon Kim | d913897 | 2016-01-14 15:22:29 -0800 | [diff] [blame] | 32 | kvfree(private); |
Sergey Senozhatsky | 6e76668 | 2014-04-07 15:38:18 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static int zcomp_lz4_compress(const unsigned char *src, unsigned char *dst, |
| 36 | size_t *dst_len, void *private) |
| 37 | { |
| 38 | /* return : Success if return 0 */ |
| 39 | return lz4_compress(src, PAGE_SIZE, dst, dst_len, private); |
| 40 | } |
| 41 | |
| 42 | static int zcomp_lz4_decompress(const unsigned char *src, size_t src_len, |
| 43 | unsigned char *dst) |
| 44 | { |
| 45 | size_t dst_len = PAGE_SIZE; |
| 46 | /* return : Success if return 0 */ |
| 47 | return lz4_decompress_unknownoutputsize(src, src_len, dst, &dst_len); |
| 48 | } |
| 49 | |
| 50 | struct zcomp_backend zcomp_lz4 = { |
| 51 | .compress = zcomp_lz4_compress, |
| 52 | .decompress = zcomp_lz4_decompress, |
| 53 | .create = zcomp_lz4_create, |
| 54 | .destroy = zcomp_lz4_destroy, |
| 55 | .name = "lz4", |
| 56 | }; |