blob: edc549920fa069478e24edf9fa4e416fdbb8adf6 [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 Kim73f67362016-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
18static void *lzo_create(void)
19{
Kyeongdon Kim73f67362016-01-14 15:22:29 -080020 void *ret;
21
22 /*
23 * This function can be called in swapout/fs write path
24 * so we can't use GFP_FS|IO. And it assumes we already
25 * have at least one stream in zram initialization so we
26 * don't do best effort to allocate more stream in here.
27 * A default stream will work well without further multiple
28 * streams. That's why we use NORETRY | NOWARN.
29 */
30 ret = kzalloc(LZO1X_MEM_COMPRESS, GFP_NOIO | __GFP_NORETRY |
31 __GFP_NOWARN);
32 if (!ret)
33 ret = __vmalloc(LZO1X_MEM_COMPRESS,
34 GFP_NOIO | __GFP_NORETRY | __GFP_NOWARN |
35 __GFP_ZERO | __GFP_HIGHMEM,
36 PAGE_KERNEL);
37 return ret;
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070038}
39
40static void lzo_destroy(void *private)
41{
Kyeongdon Kim73f67362016-01-14 15:22:29 -080042 kvfree(private);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070043}
44
45static int lzo_compress(const unsigned char *src, unsigned char *dst,
46 size_t *dst_len, void *private)
47{
48 int ret = lzo1x_1_compress(src, PAGE_SIZE, dst, dst_len, private);
49 return ret == LZO_E_OK ? 0 : ret;
50}
51
52static int lzo_decompress(const unsigned char *src, size_t src_len,
53 unsigned char *dst)
54{
55 size_t dst_len = PAGE_SIZE;
56 int ret = lzo1x_decompress_safe(src, src_len, dst, &dst_len);
57 return ret == LZO_E_OK ? 0 : ret;
58}
59
60struct zcomp_backend zcomp_lzo = {
61 .compress = lzo_compress,
62 .decompress = lzo_decompress,
63 .create = lzo_create,
64 .destroy = lzo_destroy,
65 .name = "lzo",
66};