blob: ed7a1f0549ecfe354726b244c42497e464be4ba4 [file] [log] [blame]
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -07001/*
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/lzo.h>
Kyeongdon Kimd9138972016-01-14 15:22:29 -080013#include <linux/vmalloc.h>
14#include <linux/mm.h>
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070015
16#include "zcomp_lzo.h"
17
Minchan Kim75d89472016-01-14 15:22:32 -080018static void *lzo_create(gfp_t flags)
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070019{
Kyeongdon Kimd9138972016-01-14 15:22:29 -080020 void *ret;
21
Sergey Senozhatskye02d2382016-01-14 15:22:35 -080022 ret = kmalloc(LZO1X_MEM_COMPRESS, flags);
Kyeongdon Kimd9138972016-01-14 15:22:29 -080023 if (!ret)
24 ret = __vmalloc(LZO1X_MEM_COMPRESS,
Sergey Senozhatskye02d2382016-01-14 15:22:35 -080025 flags | __GFP_HIGHMEM,
Kyeongdon Kimd9138972016-01-14 15:22:29 -080026 PAGE_KERNEL);
27 return ret;
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070028}
29
30static void lzo_destroy(void *private)
31{
Kyeongdon Kimd9138972016-01-14 15:22:29 -080032 kvfree(private);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070033}
34
35static int lzo_compress(const unsigned char *src, unsigned char *dst,
36 size_t *dst_len, void *private)
37{
38 int ret = lzo1x_1_compress(src, PAGE_SIZE, dst, dst_len, private);
39 return ret == LZO_E_OK ? 0 : ret;
40}
41
42static int lzo_decompress(const unsigned char *src, size_t src_len,
43 unsigned char *dst)
44{
45 size_t dst_len = PAGE_SIZE;
46 int ret = lzo1x_decompress_safe(src, src_len, dst, &dst_len);
47 return ret == LZO_E_OK ? 0 : ret;
48}
49
50struct zcomp_backend zcomp_lzo = {
51 .compress = lzo_compress,
52 .decompress = lzo_decompress,
53 .create = lzo_create,
54 .destroy = lzo_destroy,
55 .name = "lzo",
56};